123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497 |
- 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
-
- }
|