package exchcodeser import ( "fmt" "strconv" "strings" "time" "91porn-server/common" "91porn-server/common/stderr" "91porn-server/models/commod" "91porn-server/models/v/adminmod" "91porn-server/models/v/exchcodemod" ) const SuperAdmin = "superAdmin" // 获取兑换码列表 func GetExchangeCodeList(param exchcodemod.ListReqParam, manager string) (code stderr.Code, data interface{}, err error) { var filterParams = exchcodemod.FilterDoc{ BatchNum: param.BatchNum, Channel: param.Channel, Code: param.Code, App: param.App, Authority: param.Authority, } // 获取管理员角色 admin, err := adminmod.FindOneByName(manager) if err != nil || admin.ID.IsZero() { code = stderr.AdminIDErr return } var isSuperAdmin bool if admin.Role == SuperAdmin { isSuperAdmin = true } total, cList, err := exchcodemod.GetCodeList(filterParams, param.Status, param.Page, isSuperAdmin) if err != nil { code = stderr.Failure return } list := make([]exchcodemod.RespList, len(cList)) for i, v := range cList { tmp := exchcodemod.RespList(v) if v.InvalidAt.Before(time.Now()) { tmp.Status = exchcodemod.StatusExpired } list[i] = tmp } if len(list) == 0 { data = commod.ListResp{Total: 0, List: []exchcodemod.RespList{}} } else { data = commod.ListResp{Total: total, List: list} } code = stderr.Success return } // ExchangeCodes 获取兑换码列表,导出 func ExchangeCodes(param exchcodemod.ListReqParam) []exchcodemod.RespList { var filterParams = exchcodemod.FilterDoc{ BatchNum: param.BatchNum, Channel: param.Channel, Code: param.Code, App: param.App, Authority: param.Authority, } var isSuperAdmin bool if param.Role != nil && *param.Role == SuperAdmin { isSuperAdmin = true } _, cList, err := exchcodemod.GetCodeList(filterParams, param.Status, param.Page, isSuperAdmin) if err != nil { return nil } list := make([]exchcodemod.RespList, len(cList)) for i, v := range cList { var tmp = exchcodemod.RespList(v) if v.InvalidAt.Before(time.Now()) { tmp.Status = exchcodemod.StatusExpired } list[i] = tmp } return list } // 增加兑换码 func AddExchangeCode(param exchcodemod.AddReqParam, manager string) (code stderr.Code, err error) { // 检查app类型 switch param.App { case exchcodemod.AppTypeYSVideo: case exchcodemod.AppTypePFVideo: case exchcodemod.AppTypeLTVpn: default: code = stderr.ExchangeCodeInvalidApp return code, err } // 检查权限 switch param.Authority { case exchcodemod.AuthorityShortVideoVip: case exchcodemod.AuthorityFilmVip: case exchcodemod.AuthoritySuper: case exchcodemod.AuthorityVideoCoupon: case exchcodemod.Authority3dPermanentVIP: param.Channels = []string{"XUA01"} case exchcodemod.AuthorityGold: // 获取管理员角色 admin, err := adminmod.FindOneByName(manager) if err != nil || admin.ID.IsZero() { code = stderr.AdminIDErr return code, err } if admin.Role != SuperAdmin { code = stderr.ExchangeCodeOperateDenied return code, err } default: code = stderr.ExchangeCodeInvalidAuthority return code, err } // 检查渠道 if param.Channels == nil || len(param.Channels) == 0 { param.Channels = []string{"system"} } // 获取批次号 eMod, err := exchcodemod.GetLatestBatchNumber(param.App) if err != nil { code = stderr.Failure return } var batchNum = produceBatchNumber(param.App, eMod.BatchNum) if batchNum == "" { code = stderr.ExchangeCodeInvalidBatchNumber return } var docs []exchcodemod.ExchCode for i := 0; i < len(param.Channels); i++ { for i2 := 0; int64(i2) < param.Count; i2++ { var exchangeCode = common.InvitePromotionCodeGenera() if exchangeCode == "" { code = stderr.ExchangeCodeProduceCodeFailed return } var now = time.Now() var doc = exchcodemod.ExchCode{ BatchNum: batchNum, App: param.App, Channel: param.Channels[i], Code: exchangeCode, Authority: param.Authority, Status: exchcodemod.StatusUnused, Reward: param.Reward, RewardCount: param.RewardCount, UsableNum: param.UsableNum, Operator: manager, Remark: param.Remark, EffectiveAt: param.EffectiveAt, InvalidAt: param.InvalidAt, CreatedAt: now, UpdatedAt: now, } docs = append(docs, doc) } } if err = exchcodemod.BulkWrite(docs); err != nil { code = stderr.Failure return } code = stderr.Success return } // 生成兑换码 func ProduceExchangeCode(n int) string { if n >= 5 { return "" } var length = 12 c := common.RandBase32Upper(length) if mod, err := exchcodemod.GetExchangeCodeByCode(c); err != nil || mod != nil { c = ProduceExchangeCode(n + 1) } return c } // 根据兑换类型和批次号删除兑换码你 func UpdateExchangeCode(param exchcodemod.UpdateReqParam, manager string) (code stderr.Code, err error) { // 获取管理员角色 admin, err := adminmod.FindOneByName(manager) if err != nil || admin.ID.IsZero() { code = stderr.AdminIDErr return } if param.Authority != nil && *param.Authority == exchcodemod.AuthorityGold && admin.Role != SuperAdmin { code = stderr.ExchangeCodeOperateDenied return } // 获取兑换码状态 cMode, err := exchcodemod.GetExchangeCodeByID(param.ID) if err != nil || cMode == nil { code = stderr.ExchangeCodeInvalidCode return } if cMode.InvalidAt.Before(time.Now()) || cMode.Status == exchcodemod.StatusUsed { code = stderr.ExchangeCodeForbidUpdate return } var update = exchcodemod.UpdateDoc{ Status: param.Status, Authority: param.Authority, Channel: param.Channel, Reward: param.Reward, EffectiveAt: param.EffectiveAt, InvalidAt: param.InvalidAt, Remark: param.Remark, UsableNum: param.UsableNum, } if err = exchcodemod.UpdateExchangeCode(param.ID, update); err != nil { code = stderr.Failure return } code = stderr.Success return } // 生成批次号 func produceBatchNumber(app exchcodemod.AppType, lastBatchNum string) string { var suffix string switch app { case exchcodemod.AppTypeYSVideo: suffix = "YS-" case exchcodemod.AppTypePFVideo: suffix = "PF-" case exchcodemod.AppTypeLTVpn: suffix = "LT-" } if lastBatchNum == "" { return suffix + "0001" } s := strings.Split(lastBatchNum, "-") if len(s) < 2 { return "" } n := s[1] num, _ := strconv.ParseInt(n, 10, 64) num++ var batchNum string if num >= 1000 { batchNum = suffix + strconv.FormatInt(num, 10) } else if num >= 100 && num < 1000 { batchNum = suffix + fmt.Sprintf("0%d", num) } else if num >= 10 && num < 100 { batchNum = suffix + fmt.Sprintf("00%d", num) } else { batchNum = suffix + fmt.Sprintf("000%d", num) } return batchNum }