-
Notifications
You must be signed in to change notification settings - Fork 306
/
Copy pathproduct_attribute.go
131 lines (121 loc) · 3.64 KB
/
product_attribute.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
package controllers
import (
service "goERP/services"
"goERP/utils"
)
// ProductAttributeController 城市模块
type ProductAttributeController struct {
BaseController
}
// Put update product attribute
func (ctl *ProductAttributeController) Put() {
response := make(map[string]interface{})
IDStr := ctl.Ctx.Input.Param(":id")
if IDStr != "" {
if id, err := utils.ToInt64(IDStr); err == nil {
if err := service.ServiceUpdateProductAttribute(&ctl.User, ctl.Ctx.Input.RequestBody, id); err == nil {
response["code"] = utils.SuccessCode
response["msg"] = utils.SuccessMsg
response["attributeID"] = id
} else {
response["code"] = utils.FailedCode
response["msg"] = utils.FailedMsg
response["err"] = err.Error()
}
} else {
response["code"] = utils.FailedCode
response["msg"] = utils.FailedMsg
response["err"] = "ID转换失败"
}
} else {
response["code"] = utils.FailedCode
response["msg"] = utils.FailedMsg
response["err"] = "ID为空"
}
ctl.Data["json"] = response
ctl.ServeJSON()
}
// Post create product attribute
func (ctl *ProductAttributeController) Post() {
response := make(map[string]interface{})
if attributeID, err := service.ServiceCreateProductAttribute(&ctl.User, ctl.Ctx.Input.RequestBody); err == nil {
response["code"] = utils.SuccessCode
response["msg"] = utils.SuccessMsg
response["attributeID"] = attributeID
} else {
response["code"] = utils.FailedCode
response["msg"] = utils.FailedMsg
response["err"] = err.Error()
}
ctl.Data["json"] = response
ctl.ServeJSON()
}
// Get get attributes
func (ctl *ProductAttributeController) Get() {
response := make(map[string]interface{})
IDStr := ctl.Ctx.Input.Param(":id")
var err error
// 获得城市列表信息
if IDStr == "" {
query := make(map[string]interface{})
exclude := make(map[string]interface{})
cond := make(map[string]map[string]interface{})
condAnd := make(map[string]interface{})
fields := make([]string, 0, 0)
sortby := make([]string, 0, 0)
order := make([]string, 0, 0)
nameStr := ctl.Input().Get("name")
if nameStr != "" {
condAnd["Name__icontains"] = nameStr
}
if len(condAnd) > 0 {
cond["and"] = condAnd
}
offsetStr := ctl.Input().Get("offset")
var offset int64
var limit int64 = 20
if offsetStr != "" {
offset, _ = utils.ToInt64(offsetStr)
}
limitStr := ctl.Input().Get("limit")
if limitStr != "" {
if limit, err = utils.ToInt64(limitStr); err != nil {
limit = 20
}
}
var attributes []map[string]interface{}
var paginator utils.Paginator
var access utils.AccessResult
if access, paginator, attributes, err = service.ServiceGetProductAttribute(&ctl.User, query, exclude, cond, fields, sortby, order, offset, limit); err == nil {
response["code"] = utils.SuccessCode
response["msg"] = utils.SuccessMsg
data := make(map[string]interface{})
data["attributes"] = &attributes
data["paginator"] = &paginator
data["access"] = access
response["data"] = data
} else {
response["code"] = utils.FailedCode
response["msg"] = utils.FailedMsg
response["err"] = err
}
} else {
// 获得某个城市的信息
if attributeID, err := utils.ToInt64(IDStr); err == nil {
if access, attribute, err := service.ServiceGetProductAttributeByID(&ctl.User, attributeID); err == nil {
response["code"] = utils.SuccessCode
response["msg"] = utils.SuccessMsg
data := make(map[string]interface{})
data["attribute"] = &attribute
data["access"] = access
response["data"] = data
} else {
response["code"] = utils.FailedCode
response["msg"] = utils.FailedMsg
response["err"] = err
}
}
}
ctl.Data["json"] = response
ctl.ServeJSON()
}