-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.go
257 lines (239 loc) · 5.85 KB
/
item.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package rich
import (
"errors"
"net/http"
"os"
"sort"
"strings"
"unsafe"
"github.com/labstack/echo"
"github.com/xuender/go-utils"
)
// Item 商品
type Item struct {
Obj
Price int64 `json:"price"` // 价格,单位分
Cost int64 `json:"cost"` // 成本,单位分
Batchs []Batch `json:"batchs"` // 批次
Extend map[string]string `json:"extend"` // 扩展属性
Tags Tags `json:"tags"` // 标签
}
// Batch 批次
type Batch struct {
Cost int64 `json:"cost"` // 成本,单位分
Total int `json:"total"` // 采购总量
Inventory int `json:"inventory"` // 库存
}
// BeforePost 创建前设置拼音标签
func (i *Item) BeforePost(key byte) utils.ID {
i.Obj.BeforePost(key)
for _, f := range Initial(i.Name) {
i.Tags.Add(f)
}
return i.ID
}
// BeforePut 修改前设置拼音标签
func (i *Item) BeforePut(id utils.ID) {
i.Obj.BeforePut(id)
i.Tags.DelPy() // 删除原拼音标签
for _, f := range Initial(i.Name) {
i.Tags.Add(f)
}
}
// Includes 包含
func (i Item) Includes(tags []string) bool {
return i.Tags.Includes(tags)
}
// 商品路由
func (w *Web) itemRoute(c *echo.Group) {
c.GET("", w.itemsGet) // 商品列表
c.POST("", w.itemPost) // 商品创建
c.PUT("/:id", w.itemPut) // 商品修改
c.DELETE("/:id", w.itemDelete) // 商品删除
c.GET("/:id", func(c echo.Context) error { return w.ObjGet(c, &Item{}) }) // 商品列表
c.POST("/file", w.itemsFile) // 上传商品文件
}
// 商品列表
func (w *Web) itemsGet(c echo.Context) error {
items := w.items(true)
w.ObjSearch(c, &items)
w.ObjSelect(c, &items)
return w.ObjPaging(c, items)
}
// 商品列表
func (w *Web) items(cache bool) []Item {
if cache {
if cs, ok := w.cache[ItemIDPrefix]; ok {
return cs.([]Item)
}
}
cs := []Item{}
w.Iterator([]byte{ItemIDPrefix, '-'}, func(key, value []byte) {
c := Item{}
if utils.Decode(value, &c) == nil && !c.IsDelete() {
cs = append(cs, c)
}
})
sort.Slice(cs, func(i int, j int) bool {
return cs[i].Name < cs[j].Name
})
w.cache[ItemIDPrefix] = cs
return cs
}
// 商品创建
func (w *Web) itemPost(c echo.Context) error {
delete(w.cache, ItemIDPrefix)
i := Item{}
return w.ObjPost(c, &i, ItemIDPrefix, func() error { return w.Bind(c, &i) }, func() error { return w.addTags(TagKeys[1], i.Tags) })
}
// 商品修改
func (w *Web) itemPut(c echo.Context) error {
delete(w.cache, ItemIDPrefix)
i := Item{}
return w.ObjPut(c, &i, ItemIDPrefix, func() error { return w.Bind(c, &i) }, func() error { return w.addTags(TagKeys[1], i.Tags) })
}
// 商品删除
func (w *Web) itemDelete(c echo.Context) error {
return w.ObjDeleter(c, ItemIDPrefix, &Item{}, func() error {
w.tagsReset()
delete(w.cache, ItemIDPrefix)
return nil
})
}
// 商品信息上传
func (w *Web) itemsFile(c echo.Context) error {
delete(w.cache, ItemIDPrefix)
// 读取Excel定义
xid := new(utils.ID)
if err := xid.Parse(c.Request().Header.Get("xlsx")); err != nil {
return err
}
xlsx := Xlsx{}
w.Get(xid[:], &xlsx)
// 读取Excel文件
file, err := w.saveTemp(c)
if err != nil {
return err
}
items := []Item{}
err = ReadXlsx(file, func(row []string) {
if is, e := newItems(row, xlsx.Map); e == nil {
items = append(items, is...)
}
})
// 生成商品信息
if err == nil {
for _, i := range items {
i.BeforePost(ItemIDPrefix)
w.addTags(TagKeys[1], i.Tags)
w.Put(i.ID[:], i)
}
os.Remove(file)
w.itemsMerge()
return c.String(http.StatusOK, "ok")
}
return c.String(http.StatusInternalServerError, err.Error())
}
// 新建商品
func newItems(row []string, m map[int]string) (items []Item, err error) {
i := Item{}
p, err := utils.Parse(row, m, &i)
if err != nil {
return
}
i.Cost = i.Cost * 100
i.Price = i.Price * 100
i.Extend = p
for _, name := range []string{"", "商品", "货品", "商品名称"} {
if i.Name == name {
err = errors.New("商品名称错误")
return
}
}
if strings.Contains(i.Name, "+") {
names := strings.Split(i.Name, "+")
if len(names) == 1 {
items = append(items, i)
return
}
for _, name := range names {
name = strings.Trim(name, " ")
if name != "" {
var b = *(*Item)(unsafe.Pointer(&i))
b.Name = name
items = append(items, b)
}
}
}
return
}
// itemsMerge 商品信息合并
func (w *Web) itemsMerge() {
m := map[string]Item{}
for _, c := range w.items(true) {
k := c.Name
if v, ok := m[k]; ok {
// 重复
for _, t := range c.Tags {
v.Tags.Add(t)
}
w.Put(v.ID[:], v)
w.Delete(c.ID[:])
} else {
m[k] = c
}
}
delete(w.cache, ItemIDPrefix)
}
// 商品扣减库存
func (w *Web) deducting(order Order) error {
delete(w.cache, ItemIDPrefix)
i := Item{}
if err := w.Get(order.ID[:], &i); err != nil {
return err
}
i.deducting(order)
return w.Put(i.ID[:], i)
}
func (i *Item) deducting(order Order) {
num := order.Num
for f, b := range i.Batchs {
if b.Inventory >= num {
i.Batchs[f].Inventory -= num
break
} else {
num -= b.Inventory
i.Batchs[f].Inventory = 0
}
}
}
// 商品撤销扣减
func (w *Web) unDeducting(order Order) error {
delete(w.cache, ItemIDPrefix)
i := Item{}
if err := w.Get(order.ID[:], &i); err != nil {
return err
}
i.unDeducting(order)
return w.Put(i.ID[:], i)
}
func (i *Item) unDeducting(order Order) {
num := order.Num
reverse(i.Batchs)
for f, b := range i.Batchs {
if b.Inventory+num <= b.Total {
i.Batchs[f].Inventory += num
break
} else {
num -= b.Total - b.Inventory
i.Batchs[f].Inventory = b.Total
}
}
reverse(i.Batchs)
}
func reverse(s []Batch) []Batch {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}