package handler import ( "fmt" jsoniter "github.com/json-iterator/go" "reflect" "server/call" "server/common" "server/modules/backend/app" "server/modules/backend/values" "server/natsClient" "server/pb" "server/util" "sort" "strings" "time" "github.com/gin-gonic/gin" ) // ConfigControlCommon 所有调控配置通用逻辑 func ConfigControlCommon(c *gin.Context) { a := app.NewApp(c) defer func() { a.Response() }() path := c.Request.URL.Path path = strings.ReplaceAll(path, "/gm/control/", "") all := strings.Split(path, "/") if len(all) > 2 { a.Code = values.CodeRetry return } t := all[0] opt := all[1] reloadType := values.GetControlType(t) element, list := common.GetConfigStructByType(reloadType) var resp interface{} if opt == "list" { if !a.MGetAll(element, list) { return } resp = CheckSpecial(reloadType, list) if resp == nil { resp = values.GMConfigCommonListResp{Config: list} } } else if opt == "edit" { req := new(values.GMConfigCommonEditReq) if !a.S(req) { return } err := CheckEditSpecial(reloadType, element, req.Config, a) if err != nil { a.Msg = err.Error() return } if !a.MUpdateAll(req.Config, element) { return } call.Publish(natsClient.TopicReloadConfig, &pb.ReloadGameConfig{Type: int32(reloadType)}) } else if opt == "del" { req := new(values.GMConfigCommonDelReq) if !a.S(req) { return } if !a.MDel(req.ID, element) { return } call.Publish(natsClient.TopicReloadConfig, &pb.ReloadGameConfig{Type: int32(reloadType)}) } a.Data = resp } func CheckSpecial(t int, list interface{}) interface{} { switch t { } return nil } func haveUpdatedBase(obj interface{}) (have bool) { if obj == nil { return } t := reflect.TypeOf(obj) if t.Kind() != reflect.Ptr { return } elemType := t.Elem() if elemType.Kind() == reflect.Struct { for i := 0; i < elemType.NumField(); i++ { field := elemType.Field(i) if field.Anonymous && field.Type.Name() == "UpdatedBase" { have = true break } } } return } func CheckEditSpecial(t int, obj interface{}, config []map[string]interface{}, a *app.Gin) (err error) { if haveUpdatedBase(obj) { for _, update := range config { update["UpdatedAt"] = time.Now().Unix() update["Operator"] = a.User.Name _, ok := update["ID"] if !ok { update["CreatedAt"] = time.Now().Unix() } } } switch t { case common.ReloadConfigGameList: for _, update := range config { delete(update, "GameProvider") delete(update, "GameID") delete(update, "GameCode") delete(update, "Icon") delete(update, "Name") delete(update, "GameType") } case common.ReloadConfigRank: // todo 校验活动是否存在 for _, update := range config { rankId, ok := update["ID"] rankCycle := update["RankCycle"].(string) rankType := util.ToInt(update["RankType"]) rankCycleMap := make(map[string]struct{}) _ = jsoniter.UnmarshalFromString(rankCycle, &rankCycleMap) oldRanks := call.GetConfigRank(rankType) if ok { for _, oldRank := range oldRanks { for oldRankType := range oldRank.RankCycleMap { _, exist := rankCycleMap[oldRankType] if exist && rankId != oldRank.ID { err = fmt.Errorf("周期类型[%s] 已经在活动[%d]存在", oldRankType, oldRank.ID) return } } } } else { // 新增,同等类型的活动不能重复 for _, oldRank := range oldRanks { for oldRankType := range oldRank.RankCycleMap { _, exist := rankCycleMap[oldRankType] if exist { err = fmt.Errorf("周期类型[%s] 已经在活动[%d]存在", oldRankType, oldRank.ID) return } } } } } } return nil } // ConfigPayWeight 支付渠道权重配置 func ConfigPayWeight(c *gin.Context) { a := app.NewApp(c) defer func() { a.Response() }() path := c.Request.URL.Path index := strings.LastIndex(path, "/") if index < 0 { a.Code = values.CodeParam return } opt := path[index+1:] reloadType := common.ReloadConfigPayWeight element, list := common.GetConfigStructByType(reloadType) resp := values.GMConfigPayWeightResp{} if opt == "list" { if !a.MGetAll(element, list) { return } list = reflect.ValueOf(list).Elem().Interface() retList := []common.ConfigPayChannels{} for _, v := range list.([]common.ConfigPayChannels) { if v.CurrencyType == common.CurrencyINR { retList = append(retList, v) } } sort.Slice(retList, func(i, j int) bool { return retList[i].PayPer > retList[j].PayPer }) resp.Config = retList // resp.Status, _ = db.Redis().GetInt(common.RedisKeyPayStatus) } else if opt == "edit" { req := new(values.GMConfigCommonEditReq) if !a.S(req) { return } if !a.MUpdateAll(req.Config, element) { return } call.Publish(natsClient.TopicReloadConfig, &pb.ReloadGameConfig{Type: int32(reloadType)}) } else if opt == "del" { req := new(values.GMConfigCommonDelReq) if !a.S(req) { return } if !a.MDel(req.ID, element) { return } call.Publish(natsClient.TopicReloadConfig, &pb.ReloadGameConfig{Type: int32(reloadType)}) } a.Data = resp } // ConfigWithdrawWeight 退出渠道配置 func ConfigWithdrawWeight(c *gin.Context) { a := app.NewApp(c) defer func() { a.Response() }() path := c.Request.URL.Path index := strings.LastIndex(path, "/") if index < 0 { a.Code = values.CodeParam return } opt := path[index+1:] reloadType := common.ReloadConfigWithdrawWeight element, list := common.GetConfigStructByType(reloadType) var resp interface{} if opt == "list" { if !a.MGetAll(element, list) { return } list = reflect.ValueOf(list).Elem().Interface() retList := []common.ConfigWithdrawChannels{} for _, v := range list.([]common.ConfigWithdrawChannels) { if v.CurrencyType == common.CurrencyINR { retList = append(retList, v) } } sort.Slice(retList, func(i, j int) bool { return retList[i].WithdrawPer > retList[j].WithdrawPer }) resp = values.GMConfigCommonListResp{Config: retList} } else if opt == "edit" { req := new(values.GMConfigCommonEditReq) if !a.S(req) { return } if !a.MUpdateAll(req.Config, element) { return } call.Publish(natsClient.TopicReloadConfig, &pb.ReloadGameConfig{Type: int32(reloadType)}) } else if opt == "del" { req := new(values.GMConfigCommonDelReq) if !a.S(req) { return } if !a.MDel(req.ID, element) { return } call.Publish(natsClient.TopicReloadConfig, &pb.ReloadGameConfig{Type: int32(reloadType)}) } a.Data = resp }