Skip to content

Commit 67e77b3

Browse files
committed
增加商品属性,增加商品属性值接口完成
1 parent 503eff5 commit 67e77b3

File tree

13 files changed

+571
-43
lines changed

13 files changed

+571
-43
lines changed

controller/product/inventory.go

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
package product
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"strconv"
7+
"unicode/utf8"
8+
"wemall/model"
9+
"wemall/config"
10+
"gopkg.in/kataras/iris.v6"
11+
)
12+
13+
func combinationPropValue(productID uint, properties []model.Property) []model.Inventory {
14+
var inventories []model.Inventory
15+
if len(properties) == 1 {
16+
for i := 0; i < len(properties[0].PropertyValues); i++ {
17+
var inventory = model.Inventory{
18+
ProductID : productID,
19+
PropertyValues : []model.PropertyValue{
20+
properties[0].PropertyValues[i],
21+
},
22+
}
23+
inventories = append(inventories, inventory)
24+
}
25+
} else {
26+
theInventories := combinationPropValue(productID, properties[1:])
27+
property := properties[0]
28+
for i := len(theInventories) - 1; i >= 0; i-- {
29+
for j := 0; j < len(property.PropertyValues); j++ {
30+
var inventory = model.Inventory{
31+
ProductID : productID,
32+
PropertyValues : theInventories[i].PropertyValues,
33+
}
34+
inventory.PropertyValues = append(inventory.PropertyValues, property.PropertyValues[j])
35+
inventories = append(inventories, inventory)
36+
}
37+
theInventories = append(theInventories[:i], theInventories[i + 1:]...)
38+
}
39+
}
40+
return inventories
41+
}
42+
43+
// test 添加商品属性值
44+
func test(ctx *iris.Context) {
45+
var productID uint
46+
var propertyValue model.PropertyValue
47+
48+
if err := ctx.ReadJSON(&propertyValue); err != nil {
49+
fmt.Println(err.Error());
50+
ctx.JSON(iris.StatusOK, iris.Map{
51+
"errNo" : model.ErrorCode.ERROR,
52+
"msg" : "参数无效",
53+
"data" : iris.Map{},
54+
})
55+
return
56+
}
57+
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())
88+
ctx.JSON(iris.StatusOK, iris.Map{
89+
"errNo" : model.ErrorCode.NotFound,
90+
"msg" : "错误的商品id",
91+
"data" : iris.Map{},
92+
})
93+
return
94+
}
95+
96+
if err := model.DB.Model(&product).Related(&product.Properties).Error; err != nil {
97+
fmt.Println(err.Error())
98+
ctx.JSON(iris.StatusOK, iris.Map{
99+
"errNo" : model.ErrorCode.ERROR,
100+
"msg" : "error",
101+
"data" : iris.Map{},
102+
})
103+
return
104+
}
105+
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]
127+
if err := model.DB.Model(&property).Related(&property.PropertyValues).Error; err != nil {
128+
fmt.Println(err.Error())
129+
ctx.JSON(iris.StatusOK, iris.Map{
130+
"errNo" : model.ErrorCode.ERROR,
131+
"msg" : "error",
132+
"data" : iris.Map{},
133+
})
134+
return
135+
}
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+
}
162+
}
163+
164+
for i := 0; i < len(inventories); i++ {
165+
if err := model.DB.Create(&inventories[i]).Error; err != nil {
166+
fmt.Println(err.Error())
167+
ctx.JSON(iris.StatusOK, iris.Map{
168+
"errNo" : model.ErrorCode.ERROR,
169+
"msg" : "error",
170+
"data" : iris.Map{},
171+
})
172+
return
173+
}
174+
}
175+
}

controller/product/property.go

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

main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,7 @@ func main() {
6969
})
7070

7171
app.Listen(":" + strconv.Itoa(config.ServerConfig.Port))
72-
}
72+
}
73+
74+
75+

model/inventory.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package model
2+
3+
import "time"
4+
5+
// Inventory 商品库存
6+
type Inventory struct {
7+
ID uint `gorm:"primary_key" json:"id"`
8+
CreatedAt time.Time `json:"createdAt"`
9+
UpdatedAt time.Time `json:"updatedAt"`
10+
DeletedAt *time.Time `sql:"index" json:"deletedAt"`
11+
ProductID uint `json:"productID"`
12+
Count uint `json:"count"`
13+
PropertyValues []PropertyValue `gorm:"many2many:inventory_property_value;ForeignKey:ID;AssociationForeignKey:ID" json:"propertyValues"`
14+
}

0 commit comments

Comments
 (0)