card_controller.go 15KB

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