|
@@ -8,6 +8,8 @@ import (
|
|
|
qutil "qfw/util"
|
|
|
"qfw/util/jy"
|
|
|
"qfw/util/redis"
|
|
|
+ "sort"
|
|
|
+ "strings"
|
|
|
"time"
|
|
|
"util"
|
|
|
)
|
|
@@ -39,25 +41,47 @@ func freeSet(area map[string]interface{}, industry []string, userId string) *Fun
|
|
|
if len(area) == 0 {
|
|
|
return &FuncResult{false, errors.New("地区参数异常"), nil}
|
|
|
}
|
|
|
-
|
|
|
- //地区调整已用次数
|
|
|
- pnum := redis.GetInt(jy.PowerCacheDb, fmt.Sprintf(jy.UserUpdateAreaKey, userId))
|
|
|
- log.Println(len(area), "pnum:", pnum)
|
|
|
- //是否购买省级订阅包 省份订阅包购买的省份数量-1:全国 无限制调整;isfirst:是否第一次设置地区
|
|
|
- isfirst, ppb, areacount := IsCanSet(userId)
|
|
|
- //第一次设置地区不扣次数
|
|
|
- if !isfirst {
|
|
|
- //
|
|
|
- if len(area) > areacount+jy.BaseAreaNum {
|
|
|
+ //isfirst:是否第一次设置地区;ppb:是否购买省级订阅包; areacount:省份订阅包购买的省份数量-1:全国 无限制调整
|
|
|
+ isfirst, ppb, areacount := IsCanSet(userId, area)
|
|
|
+ //购买省份订阅包 已调整地区次数
|
|
|
+ ppnum := 0
|
|
|
+ //免费用户购买省份订阅包 按月算
|
|
|
+ ym := fmt.Sprint(time.Now().Year()) + "-" + fmt.Sprint(time.Now().Month())
|
|
|
+ if ppb {
|
|
|
+ ppnum = redis.GetInt(jy.PowerCacheDb, fmt.Sprintf(jy.UserUpdateAreaKey, userId, ym))
|
|
|
+ }
|
|
|
+ //免费用户未购买省份订阅包 按年算
|
|
|
+ year := fmt.Sprint(time.Now().Year())
|
|
|
+ //免费用户 已调整地区次数
|
|
|
+ fpnum := redis.GetInt(jy.PowerCacheDb, fmt.Sprintf(jy.UserUpdateAreaKey, userId, year))
|
|
|
+ // log.Println(len(area), "pnum:", pnum)
|
|
|
+ //第一次设置地区 || 购买全国 不扣次数
|
|
|
+ var issetredis = areacount > -1 && !isfirst
|
|
|
+ if issetredis {
|
|
|
+ //地区范围判断
|
|
|
+ if areacount+jy.BaseAreaNum < len(area) {
|
|
|
return &FuncResult{false, errors.New("超出地区可设置范围"), nil}
|
|
|
}
|
|
|
- //
|
|
|
- if areacount > -1 && areacount+jy.BaseAreaNum <= pnum {
|
|
|
+ //地区调整次数判断
|
|
|
+ if areacount+jy.BaseAreaNum <= ppnum+fpnum {
|
|
|
return &FuncResult{false, errors.New(fmt.Sprintf("订阅地区调整次数(%d)已用完", areacount+jy.BaseAreaNum)), nil}
|
|
|
}
|
|
|
}
|
|
|
if !FreeSubChange(userId, &area, industry, ppb, isfirst) {
|
|
|
return &FuncResult{false, errors.New("保存修改出错"), nil}
|
|
|
+ } else if issetredis { //更新调整次数 非全国(第一次 不消耗次数)
|
|
|
+ //清楚缓存
|
|
|
+ jy.ClearBigVipUserPower(userId)
|
|
|
+ //优先使用省份订阅包次数
|
|
|
+ // log.Println(areacount, "---", ppnum)
|
|
|
+ // log.Println(jy.BaseAreaNum, "---", fpnum)
|
|
|
+ if ppb && areacount > ppnum {
|
|
|
+ redis.Put(jy.PowerCacheDb, fmt.Sprintf(jy.UserUpdateAreaKey, userId, ym), ppnum+1, 31*24*60*60)
|
|
|
+ } else if jy.BaseAreaNum > fpnum {
|
|
|
+ redis.Put(jy.PowerCacheDb, fmt.Sprintf(jy.UserUpdateAreaKey, userId, year), fpnum+1, 365*24*60*60)
|
|
|
+ } else {
|
|
|
+ log.Println(fmt.Sprintf("免费用户设置地区 扣除次数异常 userId:%s", userId))
|
|
|
+ }
|
|
|
}
|
|
|
SetLog(userId, "o_jy")
|
|
|
return &FuncResult{true, nil, map[string]interface{}{
|
|
@@ -80,29 +104,52 @@ func FreeSubChange(userId string, area *map[string]interface{}, industry []strin
|
|
|
updateOk := db.Mgo.UpdateById("user", userId, setMap)
|
|
|
if !updateOk {
|
|
|
return false
|
|
|
- } else if !isfirst && redis.GetInt(jy.PowerCacheDb, fmt.Sprintf(jy.UserUpdateAreaKey, userId)) > -1 { //更新调整次数 非全国(第一次 不消耗次数)
|
|
|
- //清楚缓存
|
|
|
- jy.ClearBigVipUserPower(userId)
|
|
|
- redis.Incr(jy.PowerCacheDb, fmt.Sprintf(jy.UserUpdateAreaKey, userId))
|
|
|
}
|
|
|
return true
|
|
|
}
|
|
|
|
|
|
//是否可进行地区调整
|
|
|
-func IsCanSet(userId string) (bool, bool, int) {
|
|
|
+func IsCanSet(userId string, areamap map[string]interface{}) (bool, bool, int) {
|
|
|
areacount := 0
|
|
|
+ myarea := []string{}
|
|
|
+ setarea := []string{}
|
|
|
+ myareamap := map[string]interface{}{}
|
|
|
mData, ok := db.Mgo.FindById("user", userId, `{"o_jy":1}`)
|
|
|
if !ok || len(*mData) == 0 || mData == nil {
|
|
|
return false, false, areacount
|
|
|
}
|
|
|
o_jy, _ := (*mData)["o_jy"].(map[string]interface{})
|
|
|
+ //地区是否完全一致
|
|
|
+ if o_jy["o_area"] != nil {
|
|
|
+ myareamap = *qutil.ObjToMap(o_jy["o_area"])
|
|
|
+
|
|
|
+ }
|
|
|
//是否购买省份订阅包
|
|
|
if o_jy["i_ppstatus"] != nil && qutil.IntAll(o_jy["i_ppstatus"]) > 0 {
|
|
|
o_buyset_p, _ := o_jy["o_buyset_p"].(map[string]interface{})
|
|
|
areacount = qutil.IntAll(o_buyset_p["areacount"]) //-1 全国无限制
|
|
|
+ myareamap = *qutil.ObjToMap(o_jy["o_area_p"])
|
|
|
+ }
|
|
|
+ for oak, _ := range myareamap {
|
|
|
+ myarea = append(myarea, oak)
|
|
|
+ }
|
|
|
+ for sak, _ := range areamap {
|
|
|
+ setarea = append(setarea, sak)
|
|
|
}
|
|
|
- log.Println(jy.BaseAreaNum, "---areacount:", areacount)
|
|
|
- return o_jy["o_area"] == nil, o_jy["i_ppstatus"] != nil && qutil.IntAll(o_jy["i_ppstatus"]) > 0, areacount
|
|
|
+ if areaSort(setarea) == areaSort(myarea) {
|
|
|
+ log.Println(setarea, "-地区选择前后一致-", myarea)
|
|
|
+ return true, qutil.IntAll(o_jy["i_ppstatus"]) > 0, areacount
|
|
|
+ }
|
|
|
+ // log.Println(areaSort(setarea) == areaSort(myarea), "---areacount:", areacount)
|
|
|
+ return o_jy["o_area"] == nil, qutil.IntAll(o_jy["i_ppstatus"]) > 0, areacount
|
|
|
+}
|
|
|
+
|
|
|
+//
|
|
|
+func areaSort(area []string) string {
|
|
|
+ sort.Slice(area, func(i, j int) bool {
|
|
|
+ return area[i] < area[j]
|
|
|
+ })
|
|
|
+ return strings.Join(area, ",")
|
|
|
}
|
|
|
|
|
|
//
|