@@ -2,11 +2,7 @@ package product
2
2
3
3
import (
4
4
"fmt"
5
- "strings"
6
- "strconv"
7
- "unicode/utf8"
8
5
"wemall/model"
9
- "wemall/config"
10
6
"gopkg.in/kataras/iris.v6"
11
7
)
12
8
@@ -40,13 +36,12 @@ func combinationPropValue(productID uint, properties []model.Property) []model.I
40
36
return inventories
41
37
}
42
38
43
- // test 添加商品属性值
44
- func test (ctx * iris.Context ) {
45
- var productID uint
46
- var propertyValue model.PropertyValue
39
+ // ResetInventory 复位库存
40
+ func ResetInventory (ctx * iris.Context ) {
41
+ var product model.Product
47
42
48
- if err := ctx .ReadJSON (& propertyValue ); err != nil {
49
- fmt .Println (err .Error ());
43
+ if err := ctx .ReadJSON (& product ); err != nil {
44
+ fmt .Println (err .Error ())
50
45
ctx .JSON (iris .StatusOK , iris.Map {
51
46
"errNo" : model .ErrorCode .ERROR ,
52
47
"msg" : "参数无效" ,
@@ -55,36 +50,7 @@ func test(ctx *iris.Context) {
55
50
return
56
51
}
57
52
58
- productID = propertyValue .ProductID
59
- propertyValue .Name = strings .TrimSpace (propertyValue .Name )
60
-
61
- var isErr bool
62
- var errMsg = ""
63
-
64
- if productID <= 0 {
65
- isErr = true
66
- errMsg = "无效的商品ID"
67
- } else if utf8 .RuneCountInString (propertyValue .Name ) > config .ServerConfig .MaxNameLen {
68
- isErr = true
69
- errMsg = "名称不能超过" + strconv .Itoa (config .ServerConfig .MaxNameLen ) + "个字符"
70
- } else if utf8 .RuneCountInString (propertyValue .Name ) <= 0 {
71
- isErr = true
72
- errMsg = "名称不能为空"
73
- }
74
-
75
- if isErr {
76
- ctx .JSON (iris .StatusOK , iris.Map {
77
- "errNo" : model .ErrorCode .ERROR ,
78
- "msg" : errMsg ,
79
- "data" : iris.Map {},
80
- })
81
- return
82
- }
83
-
84
- var product model.Product
85
-
86
- if err := model .DB .First (& product , productID ).Error ; err != nil {
87
- fmt .Println (err .Error ())
53
+ if err := model .DB .First (& product , product .ID ).Error ; err != nil {
88
54
ctx .JSON (iris .StatusOK , iris.Map {
89
55
"errNo" : model .ErrorCode .NotFound ,
90
56
"msg" : "错误的商品id" ,
@@ -97,33 +63,14 @@ func test(ctx *iris.Context) {
97
63
fmt .Println (err .Error ())
98
64
ctx .JSON (iris .StatusOK , iris.Map {
99
65
"errNo" : model .ErrorCode .ERROR ,
100
- "msg" : "error" ,
66
+ "msg" : "error. " ,
101
67
"data" : iris.Map {},
102
68
})
103
69
return
104
70
}
105
71
106
- var properties = product .Properties
107
- var index = - 1 //属性(新添加的属性值属于的属性)在属性数组中的索引
108
- for i := 0 ; i < len (properties ); i ++ {
109
- fmt .Println (123 , properties [i ].ID , propertyValue .PropertyID )
110
- if properties [i ].ID == propertyValue .PropertyID {
111
- index = i
112
- break ;
113
- }
114
- }
115
-
116
- if index < 0 {
117
- ctx .JSON (iris .StatusOK , iris.Map {
118
- "errNo" : model .ErrorCode .ERROR ,
119
- "msg" : "错误的propertyID" ,
120
- "data" : iris.Map {},
121
- })
122
- return
123
- }
124
-
125
- for i := 0 ; i < len (properties ); i ++ {
126
- property := properties [i ]
72
+ for i := 0 ; i < len (product .Properties ); i ++ {
73
+ property := product .Properties [i ]
127
74
if err := model .DB .Model (& property ).Related (& property .PropertyValues ).Error ; err != nil {
128
75
fmt .Println (err .Error ())
129
76
ctx .JSON (iris .StatusOK , iris.Map {
@@ -133,34 +80,12 @@ func test(ctx *iris.Context) {
133
80
})
134
81
return
135
82
}
136
- properties [i ] = property
137
- }
138
-
139
- if err := model .DB .Create (& propertyValue ).Error ; err != nil {
140
- fmt .Println (err .Error ())
141
- ctx .JSON (iris .StatusOK , iris.Map {
142
- "errNo" : model .ErrorCode .ERROR ,
143
- "msg" : "error" ,
144
- "data" : iris.Map {},
145
- })
146
- return
147
- }
148
-
149
- var inventories []model.Inventory
150
- if len (properties ) == 1 {
151
- var inventory = model.Inventory {
152
- ProductID : productID ,
153
- PropertyValues : append ([]model.PropertyValue {}, propertyValue ),
154
- }
155
- inventories = append (inventories , inventory )
156
- } else if len (properties ) >= 2 {
157
- properties = append (properties [:index ], properties [index + 1 :]... )
158
- inventories = combinationPropValue (productID , properties )
159
- for i := 0 ; i < len (inventories ); i ++ {
160
- inventories [i ].PropertyValues = append (inventories [i ].PropertyValues , propertyValue )
161
- }
83
+ product .Properties [i ] = property
162
84
}
163
85
86
+ properties := product .Properties
87
+ inventories := combinationPropValue (product .ID , properties )
88
+ fmt .Println (inventories )
164
89
for i := 0 ; i < len (inventories ); i ++ {
165
90
if err := model .DB .Create (& inventories [i ]).Error ; err != nil {
166
91
fmt .Println (err .Error ())
@@ -172,4 +97,12 @@ func test(ctx *iris.Context) {
172
97
return
173
98
}
174
99
}
100
+
101
+ ctx .JSON (iris .StatusOK , iris.Map {
102
+ "errNo" : model .ErrorCode .SUCCESS ,
103
+ "msg" : "success" ,
104
+ "data" : iris.Map {
105
+ "inventories" : inventories ,
106
+ },
107
+ })
175
108
}
0 commit comments