package members import ( base_ctl "SCRM/controllers" "SCRM/enums" "SCRM/models" "SCRM/service/member_service" "SCRM/utils" "encoding/json" "fmt" "reflect" "time" "github.com/astaxie/beego" "github.com/bwmarrin/snowflake" ) func CardCtlRegistRouters() { beego.Router("/api/member/card/edit", &CardAPIController{}, "Put:EditMemberCard") beego.Router("/api/membercards", &CardAPIController{}, "Get:GetCards") beego.Router("/api/membercard/create", &CardAPIController{}, "Post:CreateCard") beego.Router("/api/membercard/edit", &CardAPIController{}, "Put:EditCard") beego.Router("/api/membercard/right/edit", &CardAPIController{}, "Put:EditRight") beego.Router("/api/membercards/delete", &CardAPIController{}, "Delete:DeleteCard") } type CardAPIController struct { base_ctl.BaseAuthAPIController } func (c *CardAPIController) EditMemberCard() { id, _ := c.GetInt64("id", 0) if id <= 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误:id") return } cardID, _ := c.GetInt64("card_id", 0) if cardID <= 0 { //设置为无卡 cardID = 0 } adminUserInfo := c.GetAdminUserInfo() timeNow := time.Now().Unix() member, err := member_service.GetOnlyMemberByID(adminUserInfo.CurrentOrgId, id) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "设置会员卡失败:("+err.Error()+")") return } if member == nil { c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "设置会员卡失败:(会员记录不存在)") return } returnData := make(map[string]interface{}, 0) if cardID == 0 { err = member_service.DeleteUserCard(adminUserInfo.CurrentOrgId, id) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "设置会员卡失败:("+err.Error()+")") return } returnData["user_card"] = nil } else { card, err := member_service.GetCardByID(adminUserInfo.CurrentOrgId, cardID) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "设置会员卡失败:("+err.Error()+")") return } if card == nil { c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "设置会员卡失败:(会员卡不存在)") return } oldUserCard, err := member_service.GetUserCardByID(adminUserInfo.CurrentOrgId, id) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "设置会员卡失败:("+err.Error()+")") return } var userCard models.UserCard if oldUserCard != nil { userCard = *oldUserCard } else { node, err := snowflake.NewNode(1) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "设置会员卡失败:(生成卡号时出错)") return } cardNo := node.Generate() userCard.CustomerId = id userCard.UserOrgId = adminUserInfo.CurrentOrgId userCard.CreatedTime = timeNow userCard.CardNo = fmt.Sprintf("%s", cardNo) } userCard.CardId = card.ID userCard.CardName = card.CardName userCard.UpdatedTime = timeNow userCard.Status = 1 err = member_service.SaveUserCard(&userCard, member) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "设置会员卡失败:("+err.Error()+")") return } returnData["user_card"] = userCard returnData["member"] = member } c.ServeSuccessJSON(returnData) return } func (c *CardAPIController) GetCards() { adminUserInfo := c.GetAdminUserInfo() cards, _ := member_service.GetCardList(adminUserInfo.CurrentOrgId) right, _ := member_service.GetMembershipRights(adminUserInfo.CurrentOrgId) if right != nil && right.Status != 1 { right = nil } returnData := make(map[string]interface{}, 0) returnData["cards"] = cards returnData["right"] = right c.ServeSuccessJSON(returnData) return } func (c *CardAPIController) CreateCard() { adminUserInfo := c.GetAdminUserInfo() timeNow := time.Now().Unix() dataBody := make(map[string]interface{}, 0) err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody) if err != nil { utils.ErrorLog(err.Error()) c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误") return } var card models.UserMembershipCard if dataBody["background_type"] == nil || reflect.TypeOf(dataBody["background_type"]).String() != "float64" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:background_type") return } backgroundType := int64(dataBody["background_type"].(float64)) if backgroundType != 1 && backgroundType != 2 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "请选择正确的卡片封面模式") return } card.BackgroundType = backgroundType if dataBody["background"] == nil || reflect.TypeOf(dataBody["background"]).String() != "string" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:background") return } background, _ := dataBody["background"].(string) if len(background) == 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "卡片封面不能为空") return } if backgroundType == 1 && !utils.CheckHexColor(background) { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "背景色格式不正确") return } card.Background = background if dataBody["card_name"] == nil || reflect.TypeOf(dataBody["card_name"]).String() != "string" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:card_name") return } cardName, _ := dataBody["card_name"].(string) if len(cardName) == 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "会员卡名称不能为空") return } card.CardName = cardName if dataBody["use_notice"] != nil && reflect.TypeOf(dataBody["use_notice"]).String() == "string" { useNotice, _ := dataBody["use_notice"].(string) if len([]rune(useNotice)) > 300 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "使用须知最多可输入300个字符") return } card.UseNotice = useNotice } if dataBody["card_right"] != nil && reflect.TypeOf(dataBody["card_right"]).String() == "string" { cardRight, _ := dataBody["card_right"].(string) if len([]rune(cardRight)) > 300 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "会员权益最多可输入300个字符") return } card.CardRight = cardRight } if dataBody["service_phone"] != nil && reflect.TypeOf(dataBody["service_phone"]).String() == "string" { servicePhone, _ := dataBody["service_phone"].(string) if len(servicePhone) > 0 && (!utils.CheckMobile(servicePhone) && !utils.CheckPhone(servicePhone)) { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "客服电话格式不正确") return } card.ServicePhone = servicePhone } if dataBody["upgrade_integral"] != nil && reflect.TypeOf(dataBody["upgrade_integral"]).String() == "float64" { upgradeIntegral := int64(dataBody["upgrade_integral"].(float64)) if upgradeIntegral < 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "累积积分不能小于0") return } card.UpgradeIntegral = upgradeIntegral } card.UserOrgId = adminUserInfo.CurrentOrgId card.Status = 1 card.CreatedTime = timeNow card.UpdatedTime = timeNow oldCard, err := member_service.FindMemberCardByName(adminUserInfo.CurrentOrgId, card.CardName) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "添加会员卡失败:("+err.Error()+")") return } if oldCard != nil { c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "添加会员卡失败:(已经存在同名的会员卡)") return } err = member_service.SaveMemberCard(&card) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "添加会员卡失败:("+err.Error()+")") return } returnData := make(map[string]interface{}, 0) returnData["card"] = card c.ServeSuccessJSON(returnData) return } func (c *CardAPIController) EditCard() { id, _ := c.GetInt64("id", 0) if id <= 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误:id") return } adminUserInfo := c.GetAdminUserInfo() timeNow := time.Now().Unix() dataBody := make(map[string]interface{}, 0) err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody) if err != nil { utils.ErrorLog(err.Error()) c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误") return } card, err := member_service.FindOnlyMemberCardByID(adminUserInfo.CurrentOrgId, id) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑会员卡失败:("+err.Error()+")") return } if card == nil { c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑会员卡失败:(会员卡不存在)") return } if dataBody["background_type"] == nil || reflect.TypeOf(dataBody["background_type"]).String() != "float64" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:background_type") return } backgroundType := int64(dataBody["background_type"].(float64)) if backgroundType != 1 && backgroundType != 2 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "请选择正确的卡片封面模式") return } card.BackgroundType = backgroundType if dataBody["background"] == nil || reflect.TypeOf(dataBody["background"]).String() != "string" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:background") return } background, _ := dataBody["background"].(string) if len(background) == 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "卡片封面不能为空") return } if backgroundType == 1 && !utils.CheckHexColor(background) { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "背景色格式不正确") return } card.Background = background if dataBody["card_name"] == nil || reflect.TypeOf(dataBody["card_name"]).String() != "string" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:card_name") return } cardName, _ := dataBody["card_name"].(string) if len(cardName) == 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "会员卡名称不能为空") return } card.CardName = cardName if dataBody["use_notice"] != nil && reflect.TypeOf(dataBody["use_notice"]).String() == "string" { useNotice, _ := dataBody["use_notice"].(string) if len([]rune(useNotice)) > 300 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "使用须知最多可输入300个字符") return } card.UseNotice = useNotice } if dataBody["card_right"] != nil && reflect.TypeOf(dataBody["card_right"]).String() == "string" { cardRight, _ := dataBody["card_right"].(string) if len([]rune(cardRight)) > 300 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "会员权益最多可输入300个字符") return } card.CardRight = cardRight } if dataBody["service_phone"] != nil && reflect.TypeOf(dataBody["service_phone"]).String() == "string" { servicePhone, _ := dataBody["service_phone"].(string) if len(servicePhone) > 0 && (!utils.CheckMobile(servicePhone) && !utils.CheckPhone(servicePhone)) { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "客服电话格式不正确") return } card.ServicePhone = servicePhone } if dataBody["upgrade_integral"] != nil && reflect.TypeOf(dataBody["upgrade_integral"]).String() == "float64" { upgradeIntegral := int64(dataBody["upgrade_integral"].(float64)) if upgradeIntegral < 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "累积积分不能小于0") return } card.UpgradeIntegral = upgradeIntegral } card.UpdatedTime = timeNow oldCard, err := member_service.FindMemberCardByName(adminUserInfo.CurrentOrgId, card.CardName) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑会员卡失败:("+err.Error()+")") return } if oldCard != nil && oldCard.ID != card.ID { c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑会员卡失败:(已经存在同名的会员卡)") return } err = member_service.SaveMemberCard(card) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑会员卡失败:("+err.Error()+")") return } returnData := make(map[string]interface{}, 0) returnData["card"] = card c.ServeSuccessJSON(returnData) return } func (c *CardAPIController) DeleteCard() { adminUserInfo := c.GetAdminUserInfo() dataBody := make(map[string]interface{}, 0) err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody) if err != nil { utils.ErrorLog(err.Error()) c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误") return } idsInters := dataBody["ids"].([]interface{}) if len(idsInters) == 0 { if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBDelete, "删除会员卡失败:(没有选择会员卡)") return } } ids := make([]int64, 0) for _, idsInter := range idsInters { id := int64(idsInter.(float64)) ids = append(ids, id) } count, err := member_service.GetMemebrCardCount(adminUserInfo.CurrentOrgId) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBDelete, "删除会员卡失败:("+err.Error()+")") return } if count <= 1 { c.ServeFailJsonSend(enums.ErrorCodeDBDelete, "删除会员卡失败:(只有1张会员卡时不能删除)") return } if count-int64(len(ids)) < 1 { c.ServeFailJsonSend(enums.ErrorCodeDBDelete, "删除会员卡失败:(至少需要保留1张会员卡)") return } ccs, err := member_service.GetCardsUserCount(adminUserInfo.CurrentOrgId, ids) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBDelete, "删除会员卡失败:("+err.Error()+")") return } hadUserIds := make([]int64, 0) willIds := make([]int64, 0) if len(ccs) > 0 { ccsMap := make(map[int64]int64, 0) for _, cc := range ccs { if cc.MemberCount > 0 { hadUserIds = append(hadUserIds, cc.CardID) ccsMap[cc.CardID] = cc.MemberCount } } for _, id := range ids { if _, exist := ccsMap[id]; !exist { willIds = append(willIds, id) } } if len(willIds) == 0 { c.ServeFailJsonSend(enums.ErrorCodeDBDelete, "删除会员卡失败:(不能删除有会员的会员卡)") return } } err = member_service.DeleteMemberShipCards(adminUserInfo.CurrentOrgId, ids) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBDelete, "删除会员卡失败:("+err.Error()+")") return } returnData := make(map[string]interface{}, 0) returnData["delete_ids"] = willIds returnData["delete_number"] = len(willIds) c.ServeSuccessJSON(returnData) return } func (c *CardAPIController) EditRight() { adminUserInfo := c.GetAdminUserInfo() timeNow := time.Now().Unix() dataBody := make(map[string]interface{}, 0) err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody) if err != nil { utils.ErrorLog(err.Error()) c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误") return } var saveRight models.MembershipRights right, err := member_service.GetMembershipRights(adminUserInfo.CurrentOrgId) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑会员权益失败:("+err.Error()+")") return } if right != nil { saveRight = *right } else { saveRight.UserOrgId = adminUserInfo.CurrentOrgId saveRight.Status = 1 saveRight.CreatedTime = timeNow } if dataBody["rights"] == nil || reflect.TypeOf(dataBody["rights"]).String() != "string" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:rights") return } rights, _ := dataBody["rights"].(string) saveRight.Rights = rights saveRight.UpdatedTime = timeNow err = member_service.SaveMembershipRight(&saveRight) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑会员权益失败:("+err.Error()+")") return } returnData := make(map[string]interface{}, 0) returnData["right"] = saveRight c.ServeSuccessJSON(returnData) return }