|
@@ -5,9 +5,11 @@ import (
|
5
|
5
|
"SCRM/enums"
|
6
|
6
|
"SCRM/models"
|
7
|
7
|
"SCRM/service/member_service"
|
8
|
|
- "time"
|
9
|
|
-
|
|
8
|
+ "SCRM/utils"
|
|
9
|
+ "encoding/json"
|
10
|
10
|
"fmt"
|
|
11
|
+ "reflect"
|
|
12
|
+ "time"
|
11
|
13
|
|
12
|
14
|
"github.com/astaxie/beego"
|
13
|
15
|
"github.com/bwmarrin/snowflake"
|
|
@@ -15,6 +17,10 @@ import (
|
15
|
17
|
|
16
|
18
|
func CardCtlRegistRouters() {
|
17
|
19
|
beego.Router("/api/member/card/edit", &CardAPIController{}, "Put:EditMemberCard")
|
|
20
|
+ beego.Router("/api/membercards", &CardAPIController{}, "Get:GetCards")
|
|
21
|
+ beego.Router("/api/membercard/create", &CardAPIController{}, "Post:CreateCard")
|
|
22
|
+ beego.Router("/api/membercard/edit", &CardAPIController{}, "Put:EditCard")
|
|
23
|
+ beego.Router("/api/membercards/delete", &CardAPIController{}, "Delete:DeleteCard")
|
18
|
24
|
}
|
19
|
25
|
|
20
|
26
|
type CardAPIController struct {
|
|
@@ -79,14 +85,14 @@ func (c *CardAPIController) EditMemberCard() {
|
79
|
85
|
}
|
80
|
86
|
cardNo := node.Generate()
|
81
|
87
|
|
82
|
|
- userCard.CardId = card.ID
|
83
|
|
- userCard.CardName = card.CardName
|
84
|
88
|
userCard.CustomerId = id
|
85
|
89
|
userCard.UserOrgId = adminUserInfo.CurrentOrgId
|
86
|
90
|
userCard.CreatedTime = timeNow
|
87
|
91
|
userCard.CardNo = fmt.Sprintf("%s", cardNo)
|
88
|
92
|
}
|
89
|
93
|
|
|
94
|
+ userCard.CardId = card.ID
|
|
95
|
+ userCard.CardName = card.CardName
|
90
|
96
|
userCard.UpdatedTime = timeNow
|
91
|
97
|
userCard.Status = 1
|
92
|
98
|
err = member_service.SaveUserCard(&userCard, member)
|
|
@@ -101,3 +107,336 @@ func (c *CardAPIController) EditMemberCard() {
|
101
|
107
|
c.ServeSuccessJSON(returnData)
|
102
|
108
|
return
|
103
|
109
|
}
|
|
110
|
+
|
|
111
|
+func (c *CardAPIController) GetCards() {
|
|
112
|
+
|
|
113
|
+ adminUserInfo := c.GetAdminUserInfo()
|
|
114
|
+ cards, _ := member_service.GetCardList(adminUserInfo.CurrentOrgId)
|
|
115
|
+
|
|
116
|
+ returnData := make(map[string]interface{}, 0)
|
|
117
|
+ returnData["cards"] = cards
|
|
118
|
+ c.ServeSuccessJSON(returnData)
|
|
119
|
+ return
|
|
120
|
+}
|
|
121
|
+
|
|
122
|
+func (c *CardAPIController) CreateCard() {
|
|
123
|
+
|
|
124
|
+ adminUserInfo := c.GetAdminUserInfo()
|
|
125
|
+ timeNow := time.Now().Unix()
|
|
126
|
+
|
|
127
|
+ dataBody := make(map[string]interface{}, 0)
|
|
128
|
+ err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
|
|
129
|
+ if err != nil {
|
|
130
|
+ utils.ErrorLog(err.Error())
|
|
131
|
+ c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误")
|
|
132
|
+ return
|
|
133
|
+ }
|
|
134
|
+
|
|
135
|
+ var card models.UserMembershipCard
|
|
136
|
+
|
|
137
|
+ if dataBody["background_type"] == nil || reflect.TypeOf(dataBody["background_type"]).String() != "float64" {
|
|
138
|
+ c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:background_type")
|
|
139
|
+ return
|
|
140
|
+ }
|
|
141
|
+ backgroundType := int64(dataBody["background_type"].(float64))
|
|
142
|
+ if backgroundType != 1 && backgroundType != 2 {
|
|
143
|
+ c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "请选择正确的卡片封面模式")
|
|
144
|
+ return
|
|
145
|
+ }
|
|
146
|
+ card.BackgroundType = backgroundType
|
|
147
|
+
|
|
148
|
+ if dataBody["background"] == nil || reflect.TypeOf(dataBody["background"]).String() != "string" {
|
|
149
|
+ c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:background")
|
|
150
|
+ return
|
|
151
|
+ }
|
|
152
|
+ background, _ := dataBody["background"].(string)
|
|
153
|
+ if len(background) == 0 {
|
|
154
|
+ c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "卡片封面不能为空")
|
|
155
|
+ return
|
|
156
|
+ }
|
|
157
|
+ if backgroundType == 1 && !utils.CheckHexColor(background) {
|
|
158
|
+ c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "背景色格式不正确")
|
|
159
|
+ return
|
|
160
|
+ }
|
|
161
|
+ card.Background = background
|
|
162
|
+
|
|
163
|
+ if dataBody["card_name"] == nil || reflect.TypeOf(dataBody["card_name"]).String() != "string" {
|
|
164
|
+ c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:card_name")
|
|
165
|
+ return
|
|
166
|
+ }
|
|
167
|
+ cardName, _ := dataBody["card_name"].(string)
|
|
168
|
+ if len(cardName) == 0 {
|
|
169
|
+ c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "会员卡名称不能为空")
|
|
170
|
+ return
|
|
171
|
+ }
|
|
172
|
+ card.CardName = cardName
|
|
173
|
+
|
|
174
|
+ if dataBody["use_notice"] != nil && reflect.TypeOf(dataBody["use_notice"]).String() == "string" {
|
|
175
|
+ useNotice, _ := dataBody["use_notice"].(string)
|
|
176
|
+ if len([]rune(useNotice)) > 300 {
|
|
177
|
+ c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "使用须知最多可输入300个字符")
|
|
178
|
+ return
|
|
179
|
+ }
|
|
180
|
+ card.UseNotice = useNotice
|
|
181
|
+ }
|
|
182
|
+
|
|
183
|
+ if dataBody["card_right"] != nil && reflect.TypeOf(dataBody["card_right"]).String() == "string" {
|
|
184
|
+ cardRight, _ := dataBody["card_right"].(string)
|
|
185
|
+ if len([]rune(cardRight)) > 300 {
|
|
186
|
+ c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "会员权益最多可输入300个字符")
|
|
187
|
+ return
|
|
188
|
+ }
|
|
189
|
+ card.CardRight = cardRight
|
|
190
|
+ }
|
|
191
|
+
|
|
192
|
+ if dataBody["service_phone"] != nil && reflect.TypeOf(dataBody["service_phone"]).String() == "string" {
|
|
193
|
+ servicePhone, _ := dataBody["service_phone"].(string)
|
|
194
|
+ if len(servicePhone) > 0 && (!utils.CheckMobile(servicePhone) && !utils.CheckPhone(servicePhone)) {
|
|
195
|
+ c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "客服电话格式不正确")
|
|
196
|
+ return
|
|
197
|
+ }
|
|
198
|
+ card.ServicePhone = servicePhone
|
|
199
|
+ }
|
|
200
|
+
|
|
201
|
+ if dataBody["upgrade_integral"] != nil && reflect.TypeOf(dataBody["upgrade_integral"]).String() == "float64" {
|
|
202
|
+ upgradeIntegral := int64(dataBody["upgrade_integral"].(float64))
|
|
203
|
+ if upgradeIntegral < 0 {
|
|
204
|
+ c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "累积积分不能小于0")
|
|
205
|
+ return
|
|
206
|
+ }
|
|
207
|
+ card.UpgradeIntegral = upgradeIntegral
|
|
208
|
+
|
|
209
|
+ }
|
|
210
|
+ card.UserOrgId = adminUserInfo.CurrentOrgId
|
|
211
|
+ card.Status = 1
|
|
212
|
+ card.CreatedTime = timeNow
|
|
213
|
+ card.UpdatedTime = timeNow
|
|
214
|
+
|
|
215
|
+ oldCard, err := member_service.FindMemberCardByName(adminUserInfo.CurrentOrgId, card.CardName)
|
|
216
|
+ if err != nil {
|
|
217
|
+ c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "添加会员卡失败:("+err.Error()+")")
|
|
218
|
+ return
|
|
219
|
+ }
|
|
220
|
+ if oldCard != nil {
|
|
221
|
+ c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "添加会员卡失败:(已经存在同名的会员卡)")
|
|
222
|
+ return
|
|
223
|
+ }
|
|
224
|
+
|
|
225
|
+ err = member_service.SaveMemberCard(&card)
|
|
226
|
+ if err != nil {
|
|
227
|
+ c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "添加会员卡失败:("+err.Error()+")")
|
|
228
|
+ return
|
|
229
|
+ }
|
|
230
|
+
|
|
231
|
+ returnData := make(map[string]interface{}, 0)
|
|
232
|
+ returnData["card"] = card
|
|
233
|
+ c.ServeSuccessJSON(returnData)
|
|
234
|
+ return
|
|
235
|
+
|
|
236
|
+}
|
|
237
|
+
|
|
238
|
+func (c *CardAPIController) EditCard() {
|
|
239
|
+ id, _ := c.GetInt64("id", 0)
|
|
240
|
+ if id <= 0 {
|
|
241
|
+ c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误:id")
|
|
242
|
+ return
|
|
243
|
+ }
|
|
244
|
+
|
|
245
|
+ adminUserInfo := c.GetAdminUserInfo()
|
|
246
|
+ timeNow := time.Now().Unix()
|
|
247
|
+
|
|
248
|
+ dataBody := make(map[string]interface{}, 0)
|
|
249
|
+ err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
|
|
250
|
+ if err != nil {
|
|
251
|
+ utils.ErrorLog(err.Error())
|
|
252
|
+ c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误")
|
|
253
|
+ return
|
|
254
|
+ }
|
|
255
|
+
|
|
256
|
+ card, err := member_service.FindOnlyMemberCardByID(adminUserInfo.CurrentOrgId, id)
|
|
257
|
+ if err != nil {
|
|
258
|
+ c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑会员卡失败:("+err.Error()+")")
|
|
259
|
+ return
|
|
260
|
+ }
|
|
261
|
+ if card == nil {
|
|
262
|
+ c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑会员卡失败:(会员卡不存在)")
|
|
263
|
+ return
|
|
264
|
+ }
|
|
265
|
+
|
|
266
|
+ if dataBody["background_type"] == nil || reflect.TypeOf(dataBody["background_type"]).String() != "float64" {
|
|
267
|
+ c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:background_type")
|
|
268
|
+ return
|
|
269
|
+ }
|
|
270
|
+ backgroundType := int64(dataBody["background_type"].(float64))
|
|
271
|
+ if backgroundType != 1 && backgroundType != 2 {
|
|
272
|
+ c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "请选择正确的卡片封面模式")
|
|
273
|
+ return
|
|
274
|
+ }
|
|
275
|
+ card.BackgroundType = backgroundType
|
|
276
|
+
|
|
277
|
+ if dataBody["background"] == nil || reflect.TypeOf(dataBody["background"]).String() != "string" {
|
|
278
|
+ c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:background")
|
|
279
|
+ return
|
|
280
|
+ }
|
|
281
|
+ background, _ := dataBody["background"].(string)
|
|
282
|
+ if len(background) == 0 {
|
|
283
|
+ c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "卡片封面不能为空")
|
|
284
|
+ return
|
|
285
|
+ }
|
|
286
|
+ if backgroundType == 1 && !utils.CheckHexColor(background) {
|
|
287
|
+ c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "背景色格式不正确")
|
|
288
|
+ return
|
|
289
|
+ }
|
|
290
|
+ card.Background = background
|
|
291
|
+
|
|
292
|
+ if dataBody["card_name"] == nil || reflect.TypeOf(dataBody["card_name"]).String() != "string" {
|
|
293
|
+ c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:card_name")
|
|
294
|
+ return
|
|
295
|
+ }
|
|
296
|
+ cardName, _ := dataBody["card_name"].(string)
|
|
297
|
+ if len(cardName) == 0 {
|
|
298
|
+ c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "会员卡名称不能为空")
|
|
299
|
+ return
|
|
300
|
+ }
|
|
301
|
+ card.CardName = cardName
|
|
302
|
+
|
|
303
|
+ if dataBody["use_notice"] != nil && reflect.TypeOf(dataBody["use_notice"]).String() == "string" {
|
|
304
|
+ useNotice, _ := dataBody["use_notice"].(string)
|
|
305
|
+ if len([]rune(useNotice)) > 300 {
|
|
306
|
+ c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "使用须知最多可输入300个字符")
|
|
307
|
+ return
|
|
308
|
+ }
|
|
309
|
+ card.UseNotice = useNotice
|
|
310
|
+ }
|
|
311
|
+
|
|
312
|
+ if dataBody["card_right"] != nil && reflect.TypeOf(dataBody["card_right"]).String() == "string" {
|
|
313
|
+ cardRight, _ := dataBody["card_right"].(string)
|
|
314
|
+ if len([]rune(cardRight)) > 300 {
|
|
315
|
+ c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "会员权益最多可输入300个字符")
|
|
316
|
+ return
|
|
317
|
+ }
|
|
318
|
+ card.CardRight = cardRight
|
|
319
|
+ }
|
|
320
|
+
|
|
321
|
+ if dataBody["service_phone"] != nil && reflect.TypeOf(dataBody["service_phone"]).String() == "string" {
|
|
322
|
+ servicePhone, _ := dataBody["service_phone"].(string)
|
|
323
|
+ if len(servicePhone) > 0 && (!utils.CheckMobile(servicePhone) && !utils.CheckPhone(servicePhone)) {
|
|
324
|
+ c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "客服电话格式不正确")
|
|
325
|
+ return
|
|
326
|
+ }
|
|
327
|
+ card.ServicePhone = servicePhone
|
|
328
|
+ }
|
|
329
|
+
|
|
330
|
+ if dataBody["upgrade_integral"] != nil && reflect.TypeOf(dataBody["upgrade_integral"]).String() == "float64" {
|
|
331
|
+ upgradeIntegral := int64(dataBody["upgrade_integral"].(float64))
|
|
332
|
+ if upgradeIntegral < 0 {
|
|
333
|
+ c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "累积积分不能小于0")
|
|
334
|
+ return
|
|
335
|
+ }
|
|
336
|
+ card.UpgradeIntegral = upgradeIntegral
|
|
337
|
+
|
|
338
|
+ }
|
|
339
|
+ card.UpdatedTime = timeNow
|
|
340
|
+
|
|
341
|
+ oldCard, err := member_service.FindMemberCardByName(adminUserInfo.CurrentOrgId, card.CardName)
|
|
342
|
+ if err != nil {
|
|
343
|
+ c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑会员卡失败:("+err.Error()+")")
|
|
344
|
+ return
|
|
345
|
+ }
|
|
346
|
+ if oldCard != nil && oldCard.ID != card.ID {
|
|
347
|
+ c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑会员卡失败:(已经存在同名的会员卡)")
|
|
348
|
+ return
|
|
349
|
+ }
|
|
350
|
+
|
|
351
|
+ err = member_service.SaveMemberCard(card)
|
|
352
|
+ if err != nil {
|
|
353
|
+ c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑会员卡失败:("+err.Error()+")")
|
|
354
|
+ return
|
|
355
|
+ }
|
|
356
|
+
|
|
357
|
+ returnData := make(map[string]interface{}, 0)
|
|
358
|
+ returnData["card"] = card
|
|
359
|
+ c.ServeSuccessJSON(returnData)
|
|
360
|
+ return
|
|
361
|
+
|
|
362
|
+}
|
|
363
|
+
|
|
364
|
+func (c *CardAPIController) DeleteCard() {
|
|
365
|
+ adminUserInfo := c.GetAdminUserInfo()
|
|
366
|
+
|
|
367
|
+ dataBody := make(map[string]interface{}, 0)
|
|
368
|
+ err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
|
|
369
|
+ if err != nil {
|
|
370
|
+ utils.ErrorLog(err.Error())
|
|
371
|
+ c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误")
|
|
372
|
+ return
|
|
373
|
+ }
|
|
374
|
+
|
|
375
|
+ idsInters := dataBody["ids"].([]interface{})
|
|
376
|
+ if len(idsInters) == 0 {
|
|
377
|
+ if err != nil {
|
|
378
|
+ c.ServeFailJsonSend(enums.ErrorCodeDBDelete, "删除会员卡失败:(没有选择会员卡)")
|
|
379
|
+ return
|
|
380
|
+ }
|
|
381
|
+ }
|
|
382
|
+
|
|
383
|
+ ids := make([]int64, 0)
|
|
384
|
+ for _, idsInter := range idsInters {
|
|
385
|
+ id := int64(idsInter.(float64))
|
|
386
|
+ ids = append(ids, id)
|
|
387
|
+ }
|
|
388
|
+
|
|
389
|
+ count, err := member_service.GetMemebrCardCount(adminUserInfo.CurrentOrgId)
|
|
390
|
+ if err != nil {
|
|
391
|
+ c.ServeFailJsonSend(enums.ErrorCodeDBDelete, "删除会员卡失败:("+err.Error()+")")
|
|
392
|
+ return
|
|
393
|
+ }
|
|
394
|
+ if count <= 1 {
|
|
395
|
+ c.ServeFailJsonSend(enums.ErrorCodeDBDelete, "删除会员卡失败:(只有1张会员卡时不能删除)")
|
|
396
|
+ return
|
|
397
|
+ }
|
|
398
|
+ if count-int64(len(ids)) < 1 {
|
|
399
|
+ c.ServeFailJsonSend(enums.ErrorCodeDBDelete, "删除会员卡失败:(至少需要保留1张会员卡)")
|
|
400
|
+ return
|
|
401
|
+ }
|
|
402
|
+
|
|
403
|
+ ccs, err := member_service.GetCardsUserCount(adminUserInfo.CurrentOrgId, ids)
|
|
404
|
+ if err != nil {
|
|
405
|
+ c.ServeFailJsonSend(enums.ErrorCodeDBDelete, "删除会员卡失败:("+err.Error()+")")
|
|
406
|
+ return
|
|
407
|
+ }
|
|
408
|
+
|
|
409
|
+ hadUserIds := make([]int64, 0)
|
|
410
|
+ willIds := make([]int64, 0)
|
|
411
|
+ if len(ccs) > 0 {
|
|
412
|
+ ccsMap := make(map[int64]int64, 0)
|
|
413
|
+ for _, cc := range ccs {
|
|
414
|
+ if cc.MemberCount > 0 {
|
|
415
|
+ hadUserIds = append(hadUserIds, cc.CardID)
|
|
416
|
+ ccsMap[cc.CardID] = cc.MemberCount
|
|
417
|
+ }
|
|
418
|
+ }
|
|
419
|
+ for _, id := range ids {
|
|
420
|
+ if _, exist := ccsMap[id]; !exist {
|
|
421
|
+ willIds = append(willIds, id)
|
|
422
|
+ }
|
|
423
|
+ }
|
|
424
|
+ if len(willIds) == 0 {
|
|
425
|
+ c.ServeFailJsonSend(enums.ErrorCodeDBDelete, "删除会员卡失败:(不能删除有会员的会员卡)")
|
|
426
|
+ return
|
|
427
|
+ }
|
|
428
|
+ }
|
|
429
|
+
|
|
430
|
+ err = member_service.DeleteMemberShipCards(adminUserInfo.CurrentOrgId, ids)
|
|
431
|
+ if err != nil {
|
|
432
|
+ c.ServeFailJsonSend(enums.ErrorCodeDBDelete, "删除会员卡失败:("+err.Error()+")")
|
|
433
|
+ return
|
|
434
|
+ }
|
|
435
|
+
|
|
436
|
+ returnData := make(map[string]interface{}, 0)
|
|
437
|
+ returnData["delete_ids"] = willIds
|
|
438
|
+ returnData["delete_number"] = len(willIds)
|
|
439
|
+
|
|
440
|
+ c.ServeSuccessJSON(returnData)
|
|
441
|
+ return
|
|
442
|
+}
|