|
@@ -6,23 +6,23 @@ import (
|
|
|
"entity"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
+ "github.com/go-xweb/xweb"
|
|
|
+ "go.mongodb.org/mongo-driver/bson"
|
|
|
"log"
|
|
|
"math"
|
|
|
"pay"
|
|
|
qutil "qfw/util"
|
|
|
+ "qfw/util/jy"
|
|
|
"strings"
|
|
|
"time"
|
|
|
"util"
|
|
|
-
|
|
|
- "github.com/go-xweb/xweb"
|
|
|
- "go.mongodb.org/mongo-driver/bson"
|
|
|
)
|
|
|
|
|
|
//订阅修改 or 升级
|
|
|
type SubscribeChange struct {
|
|
|
*xweb.Action
|
|
|
getSubBuyMsg xweb.Mapper `xweb:"/vipsubscribe/getSubBuyMsg"` //获取vip订阅详情
|
|
|
- saveChange xweb.Mapper `xweb:"/vipsubscribe/saveChange"` //订阅修改 or 升级
|
|
|
+ saveChange xweb.Mapper `xweb:"/vipsubscribe/saveChange"` //订阅修改
|
|
|
mergeIndustry xweb.Mapper `xweb:"/vipsubscribe/mergeIndustry"` //行业合并
|
|
|
}
|
|
|
|
|
@@ -71,21 +71,14 @@ func (this *SubscribeChange) GetSubBuyMsg() {
|
|
|
this.ServeJson(r.Format())
|
|
|
}
|
|
|
|
|
|
-//修改 or 升级
|
|
|
+//订阅修改
|
|
|
func (this *SubscribeChange) SaveChange() {
|
|
|
area := qutil.ObjToMap(this.GetString("area"))
|
|
|
industry := strings.Split(this.GetString("industry"), ",")
|
|
|
userId := qutil.ObjToString(this.GetSession("userId"))
|
|
|
- timeRenew := this.GetString("time")
|
|
|
- req_price, _ := this.GetInteger("price")
|
|
|
- openId := qutil.ObjToString(this.GetSession("s_m_openid"))
|
|
|
- //用户当前使用卡卷
|
|
|
- userLotteryId := ""
|
|
|
- //卡卷信息的id
|
|
|
- lotteryId := this.GetString("lotteryId")
|
|
|
- discount_price := 0
|
|
|
+
|
|
|
r := func() *entity.FuncResult {
|
|
|
- if len(industry) == 1 && industry[0] == "" { //去掉[""]
|
|
|
+ if len(industry) == 1 && industry[0] == "" {
|
|
|
industry = []string{}
|
|
|
}
|
|
|
rData, oldBuyset, isVip := entity.JyVipSubStruct.GetVipDetail(userId)
|
|
@@ -95,20 +88,50 @@ func (this *SubscribeChange) SaveChange() {
|
|
|
if rData == nil || len(*rData) == 0 {
|
|
|
return &entity.FuncResult{false, errors.New("获取信息失败"), nil}
|
|
|
}
|
|
|
- newBuyset := entity.JyVipSubStruct.NewBuySet(area, industry)
|
|
|
- needUpgrade, upgradeBuyset := needUpgrade(newBuyset, oldBuyset)
|
|
|
- log.Println(oldBuyset, newBuyset, upgradeBuyset)
|
|
|
+ newBuyset := entity.JyVipSubStruct.NewBuySet(area, industry, oldBuyset.Upgrade == 1)
|
|
|
+
|
|
|
+ if needUpgrade := needUpgrade(newBuyset, oldBuyset); needUpgrade {
|
|
|
+ return &entity.FuncResult{false, errors.New("订阅内容超出套餐"), nil}
|
|
|
+ }
|
|
|
+ if !entity.JyVipSubStruct.SubChange(userId, qutil.ObjToMap((*rData)["o_vipjy"]), area, industry) {
|
|
|
+ return &entity.FuncResult{false, errors.New("保存修改出错"), nil}
|
|
|
+ }
|
|
|
+ return &entity.FuncResult{true, nil, map[string]interface{}{
|
|
|
+ "doSuccess": true,
|
|
|
+ }}
|
|
|
+ }()
|
|
|
+ if r.Err != nil {
|
|
|
+ log.Printf("%s SaveChange err:%v\n", userId, r.Err.Error())
|
|
|
+ }
|
|
|
+ this.ServeJson(r.Format())
|
|
|
+}
|
|
|
+
|
|
|
+//升级订阅
|
|
|
+func (this *SubscribeChange) Upgrade() {
|
|
|
+ userId := qutil.ObjToString(this.GetSession("userId"))
|
|
|
+ r := func() *entity.FuncResult {
|
|
|
+ area := qutil.ObjToMap(this.GetString("area")) //地区
|
|
|
+ timeRenew := this.GetString("time") //周期
|
|
|
+ req_price, _ := this.GetInteger("price") //前端展示金额
|
|
|
+ openId := qutil.ObjToString(this.GetSession("s_m_openid"))
|
|
|
+ order_phone := this.GetString("order_phone") //p19.3用户信息采集 手机号
|
|
|
+
|
|
|
+ rData, oldBuyset, isVip := entity.JyVipSubStruct.GetVipDetail(userId)
|
|
|
+ if !isVip {
|
|
|
+ return &entity.FuncResult{false, errors.New("请求异常,非vip状态"), nil}
|
|
|
+ }
|
|
|
+
|
|
|
now := time.Now()
|
|
|
- needRenew := false
|
|
|
- date_count, date_unit := 0, 0
|
|
|
+ date_count, date_unit, needRenew := 0, 0, false
|
|
|
endUnix := qutil.Int64All((*rData)["l_vip_endtime"])
|
|
|
- isTrail := qutil.IntAll((*rData)["i_vip_status"]) == 1
|
|
|
+
|
|
|
if timeRenew != "" {
|
|
|
var err error
|
|
|
date_count, date_unit, err = checkReqDate(timeRenew)
|
|
|
if err != nil {
|
|
|
return &entity.FuncResult{false, errors.New("格式化日期出错"), nil}
|
|
|
}
|
|
|
+ isTrail := qutil.IntAll((*rData)["i_vip_status"]) == 1
|
|
|
//校验是否超过三年
|
|
|
if time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 59, now.Location()).
|
|
|
AddDate(3, 0, 0).Before(util.GetDATE(date_unit, date_count, endUnix)) && !isTrail {
|
|
@@ -116,108 +139,58 @@ func (this *SubscribeChange) SaveChange() {
|
|
|
}
|
|
|
needRenew = true
|
|
|
}
|
|
|
- //订阅修改(不需要升级,不需要续费)
|
|
|
- if !needUpgrade && !needRenew {
|
|
|
- if !entity.JyVipSubStruct.SubChange(userId, qutil.ObjToMap((*rData)["o_vipjy"]), area, industry) {
|
|
|
- return &entity.FuncResult{false, errors.New("保存修改出错"), nil}
|
|
|
+
|
|
|
+ newBuyset := entity.JyVipSubStruct.NewBuySet(area, nil, true) //改版后只能购买升级版超级订阅
|
|
|
+
|
|
|
+ //升级校验
|
|
|
+ totalPrice, subtotail := getNewUpgradeDetail(userId, newBuyset, oldBuyset, endUnix, date_count, date_unit)
|
|
|
+ if totalPrice < 0 {
|
|
|
+ if totalPrice == -1 {
|
|
|
+ return &entity.FuncResult{false, errors.New("不能升级此状态"), nil}
|
|
|
+ } else {
|
|
|
+ return &entity.FuncResult{false, errors.New("查询续费订单出错"), nil}
|
|
|
}
|
|
|
- return &entity.FuncResult{true, nil, map[string]interface{}{
|
|
|
- "doSuccess": true,
|
|
|
- "needPay": false,
|
|
|
- }}
|
|
|
}
|
|
|
|
|
|
- //升级or续费
|
|
|
filter := entity.VipSimpleMsg{
|
|
|
Area: area,
|
|
|
- Industry: industry,
|
|
|
+ Industry: nil, //行业保存默认选择
|
|
|
Cyclecount: date_count,
|
|
|
Cycleunit: date_unit,
|
|
|
- OldBuyset: nil,
|
|
|
- NewBuyset: nil,
|
|
|
- UpgradeSubtotail: nil,
|
|
|
- OrderType: 0,
|
|
|
+ OldBuyset: oldBuyset,
|
|
|
+ NewBuyset: newBuyset,
|
|
|
+ UpgradeSubtotail: subtotail,
|
|
|
+ OrderType: 3,
|
|
|
DisWord: "",
|
|
|
}
|
|
|
- final_price := 0
|
|
|
var startTime, endTime time.Time
|
|
|
- full_price, reduce_price := 0, 0
|
|
|
- log.Println("lotteryId:", lotteryId)
|
|
|
- if lotteryId != "" {
|
|
|
- full_price, reduce_price, userLotteryId = util.GetCouponInfo(userId, lotteryId, config.CouponConfig.Products["超级订阅"])
|
|
|
- }
|
|
|
- if isTrail { //试用用户购买
|
|
|
- if date_count == 0 || date_unit == 0 {
|
|
|
- return &entity.FuncResult{false, errors.New("时间选择异常"), nil}
|
|
|
- }
|
|
|
- filter.OrderType = 1
|
|
|
- filter.NewBuyset = newBuyset
|
|
|
- final_price = entity.JyVipSubStruct.GetSubVipPriceByBuySet(newBuyset.NewCitys, newBuyset.AreaCount, newBuyset.BuyerclassCount, date_count, date_unit, false)
|
|
|
- //双十一活动
|
|
|
- if entity.JyVipSubStruct.IsActiving() && entity.JyVipSubStruct.IsHaveCycle(userId) {
|
|
|
- filter.OriginalPrice = final_price * 2
|
|
|
- if date_unit == 1 {
|
|
|
- filter.GiveCycle = 12
|
|
|
- if date_count > 1 {
|
|
|
- filter.OriginalPrice = final_price / date_count * (date_count + 1)
|
|
|
- }
|
|
|
- } else if date_unit == 2 {
|
|
|
- filter.GiveCycle = date_count
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- if !needUpgrade && needRenew { //只续费,不升级
|
|
|
- //保存修改
|
|
|
- if !entity.JyVipSubStruct.SubChange(userId, qutil.ObjToMap((*rData)["o_vipjy"]), area, industry) {
|
|
|
- return &entity.FuncResult{false, errors.New("保存修改出错"), nil}
|
|
|
- }
|
|
|
- final_price = entity.JyVipSubStruct.GetSubVipPriceByBuySet(oldBuyset.NewCitys, oldBuyset.AreaCount, oldBuyset.BuyerclassCount, date_count, date_unit, false)
|
|
|
- filter.OrderType = 5
|
|
|
- filter.NewBuyset = oldBuyset
|
|
|
-
|
|
|
- log.Println("续费", final_price)
|
|
|
- //生效时间
|
|
|
- startTime = time.Unix(endUnix, 0)
|
|
|
+ if needRenew {
|
|
|
+ startTime = now
|
|
|
+ if needRenew {
|
|
|
endTime = util.GetDATE(filter.Cycleunit, filter.Cyclecount, endUnix)
|
|
|
- } else { //升级
|
|
|
- //升级(若存在续费,升级单子必须高于每个续费单子;否则不能升级)
|
|
|
- totalPrice, subtotail := getUpgradeDetail(userId, upgradeBuyset, oldBuyset, endUnix, date_count, date_unit)
|
|
|
- if totalPrice < 0 {
|
|
|
- if totalPrice == -1 {
|
|
|
- return &entity.FuncResult{false, errors.New("不能升级此状态"), nil}
|
|
|
- } else {
|
|
|
- return &entity.FuncResult{false, errors.New("查询续费订单出错"), nil}
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- final_price = totalPrice
|
|
|
- filter.OrderType = 3
|
|
|
- filter.OldBuyset = oldBuyset
|
|
|
- filter.NewBuyset = upgradeBuyset
|
|
|
- filter.UpgradeSubtotail = subtotail
|
|
|
-
|
|
|
- //生效时间
|
|
|
- startTime = now
|
|
|
- if needRenew {
|
|
|
- endTime = util.GetDATE(filter.Cycleunit, filter.Cyclecount, endUnix)
|
|
|
- } else {
|
|
|
- endTime = time.Unix(endUnix, 0)
|
|
|
- }
|
|
|
+ } else {
|
|
|
+ endTime = time.Unix(endUnix, 0)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ final_price, discount_price := totalPrice, 0
|
|
|
+ if req_price != final_price {
|
|
|
+ log.Println(userId+" 前台计算", req_price, "后台计算", final_price)
|
|
|
+ return &entity.FuncResult{false, errors.New(fmt.Sprintf("金额校验异常[%d,%d]", final_price, req_price)), nil}
|
|
|
+ }
|
|
|
//卡卷使用
|
|
|
+ userLotteryId, lotteryId := "", this.GetString("lotteryId")
|
|
|
+ full_price, reduce_price := 0, 0
|
|
|
+
|
|
|
+ if lotteryId != "" {
|
|
|
+ full_price, reduce_price, userLotteryId = util.GetCouponInfo(userId, lotteryId, config.CouponConfig.Products["超级订阅"])
|
|
|
+ }
|
|
|
if reduce_price > 0 && full_price <= final_price {
|
|
|
final_price = final_price - reduce_price
|
|
|
discount_price = reduce_price
|
|
|
}
|
|
|
- log.Println("reduce_price:", reduce_price)
|
|
|
- log.Printf("是否需要升级%v,是否需要续费%v\n", needUpgrade, needRenew)
|
|
|
- log.Printf("price: %d \n req_price:%d filter:%+v\n", final_price, req_price, filter)
|
|
|
- //
|
|
|
- if req_price != final_price {
|
|
|
- log.Println(userId+" 前台计算", req_price, "后台计算", final_price)
|
|
|
- return &entity.FuncResult{false, errors.New(fmt.Sprintf("金额校验异常[%d,%d]", final_price, req_price)), nil}
|
|
|
- }
|
|
|
+
|
|
|
+ //测试打折
|
|
|
if entity.SubVipPrice.Discount < 1 {
|
|
|
tmp := float64(final_price) * entity.SubVipPrice.Discount
|
|
|
if tmp < 1 {
|
|
@@ -259,201 +232,44 @@ func (this *SubscribeChange) SaveChange() {
|
|
|
"dis_word": disWord,
|
|
|
"discount_price": discount_price,
|
|
|
"d_relation_id": userLotteryId,
|
|
|
- //"vip_type": qutil.If(filter.OrderType == 5, 1, 2), //1:续费 2:升级
|
|
|
+ "vip_type": 2, //1:续费 2:升级
|
|
|
+ "user_phone": order_phone,
|
|
|
}
|
|
|
- if filter.OrderType != 1 {
|
|
|
- insertMap["vip_type"] = 2
|
|
|
+ if order_phone != "" {
|
|
|
+ if !jy.PhoneReg.MatchString(order_phone) {
|
|
|
+ return &entity.FuncResult{false, errors.New("手机号格式异常"), nil}
|
|
|
+ }
|
|
|
+ insertMap["user_phone"] = order_phone
|
|
|
}
|
|
|
+
|
|
|
orderid := util.Mysql.Insert("dataexport_order", insertMap)
|
|
|
if orderid == -1 {
|
|
|
return &entity.FuncResult{false, errors.New("数据库操作异常"), nil}
|
|
|
- } else {
|
|
|
- //绑定卡卷
|
|
|
- if userLotteryId != "" && lotteryId != "" {
|
|
|
- go func(userId, userLotteryId, order_code string) {
|
|
|
- nickname, _ := this.Session().Get("s_nickname").(string)
|
|
|
- if !util.UpdateCouponState(userId, userLotteryId, nickname, order_code, 3) {
|
|
|
- log.Println(fmt.Sprintf("单号%s-绑定失败-卡卷%s", order_code, userLotteryId))
|
|
|
- }
|
|
|
- }(userId, userLotteryId, ordercode)
|
|
|
- }
|
|
|
}
|
|
|
- //支付提醒
|
|
|
- util.MsgRemind.Add(qutil.IntAll(orderid), final_price, qutil.If(filter.OrderType == 5, 1, -1).(int), 0, ordercode, userId, now.Unix())
|
|
|
+ //绑定卡卷
|
|
|
+ if userLotteryId != "" && lotteryId != "" {
|
|
|
+ go func(userId, userLotteryId, order_code string) {
|
|
|
+ nickname, _ := this.Session().Get("s_nickname").(string)
|
|
|
+ if !util.UpdateCouponState(userId, userLotteryId, nickname, order_code, 3) {
|
|
|
+ log.Println(fmt.Sprintf("单号%s-绑定失败-卡卷%s", order_code, userLotteryId))
|
|
|
+ }
|
|
|
+ }(userId, userLotteryId, ordercode)
|
|
|
+ }
|
|
|
return &entity.FuncResult{true, nil, map[string]interface{}{"code": ordercode, "needPay": true}}
|
|
|
}()
|
|
|
if r.Err != nil {
|
|
|
- log.Printf("%s CreateOrder err:%v\n", userId, r.Err.Error())
|
|
|
+ log.Printf("%s Upgrade err:%v\n", userId, r.Err.Error())
|
|
|
}
|
|
|
this.ServeJson(r.Format())
|
|
|
}
|
|
|
|
|
|
-//是否需要升级
|
|
|
-func needUpgrade(newBuySet, oldBuySet *entity.SubvipBuySet) (bool, *entity.SubvipBuySet) {
|
|
|
- BuyerclassUpgrade, AreaNeedUpgrade := false, false
|
|
|
- UpgradeBuyset := &entity.SubvipBuySet{
|
|
|
- oldBuySet.AreaCount,
|
|
|
- oldBuySet.NewCitys,
|
|
|
- oldBuySet.BuyerclassCount,
|
|
|
- }
|
|
|
- //UpgradeBuyset.BuyerclassCount = newBuySet.BuyerclassCount
|
|
|
- //比较行业
|
|
|
- if oldBuySet.BuyerclassCount != -1 {
|
|
|
- //若当前买的不是全行业,并且当前行业数量小于新行业数量时
|
|
|
- if oldBuySet.BuyerclassCount < newBuySet.BuyerclassCount || newBuySet.BuyerclassCount == -1 {
|
|
|
- BuyerclassUpgrade = true
|
|
|
- //计算升级后的buySet
|
|
|
- UpgradeBuyset.BuyerclassCount = newBuySet.BuyerclassCount
|
|
|
- }
|
|
|
- }
|
|
|
- //比较地区
|
|
|
- if oldBuySet.AreaCount != -1 {
|
|
|
- if newBuySet.AreaCount == -1 { //升级为全国
|
|
|
- UpgradeBuyset.AreaCount = newBuySet.AreaCount
|
|
|
- UpgradeBuyset.NewCitys = newBuySet.NewCitys
|
|
|
- AreaNeedUpgrade = true
|
|
|
- } else {
|
|
|
- oldCitysBuyOne, oldCitysBuyTwo := 0, 0
|
|
|
- newCitysBuyOne, newCitysBuyTwo := 0, 0
|
|
|
- oldCopy := []int{} //复制(防止影响原数组)
|
|
|
- for _, v := range oldBuySet.NewCitys {
|
|
|
- if v == 1 {
|
|
|
- oldCitysBuyOne++
|
|
|
- } else {
|
|
|
- oldCitysBuyTwo++
|
|
|
- }
|
|
|
- oldCopy = append(oldCopy, v)
|
|
|
- }
|
|
|
- UpgradeBuyset.NewCitys = oldCopy
|
|
|
- for _, v := range newBuySet.NewCitys {
|
|
|
- if v == 1 {
|
|
|
- newCitysBuyOne++
|
|
|
- } else {
|
|
|
- newCitysBuyTwo++
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- p_Diff := newBuySet.AreaCount - oldBuySet.AreaCount
|
|
|
- c_1_diff := newCitysBuyOne - oldCitysBuyOne
|
|
|
- c_2_diff := newCitysBuyTwo - oldCitysBuyTwo
|
|
|
-
|
|
|
- //log.Println(p_Diff, c_2_diff, c_1_diff)
|
|
|
-
|
|
|
- if p_Diff > 0 {
|
|
|
- UpgradeBuyset.AreaCount = newBuySet.AreaCount
|
|
|
- }
|
|
|
-
|
|
|
- if p_Diff > 0 || p_Diff+c_2_diff > 0 || p_Diff+c_2_diff+c_1_diff > 0 {
|
|
|
- //有多余未使用名额保留
|
|
|
- if p_Diff+c_2_diff+c_1_diff < 0 {
|
|
|
- if c_1_diff < 0 && p_Diff+c_2_diff >= 0 {
|
|
|
- c_1_diff = -(p_Diff + c_2_diff)
|
|
|
- }
|
|
|
- if c_2_diff < 0 && p_Diff+c_1_diff >= 0 {
|
|
|
- c_2_diff = -(p_Diff + c_1_diff)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //需要升级
|
|
|
- AreaNeedUpgrade = true
|
|
|
- //计算升级后的buySet
|
|
|
- if p_Diff < 0 { //有剩余省份未使用
|
|
|
- if c_2_diff > 0 {
|
|
|
- c_2_diff += p_Diff //转移给两个市使用
|
|
|
- } else {
|
|
|
- c_1_diff += p_Diff //转移给一个市使用
|
|
|
- }
|
|
|
- } else { //省份数量增加
|
|
|
- UpgradeBuyset.AreaCount = newBuySet.AreaCount
|
|
|
- }
|
|
|
- del_2_flag, del_1_flag := 0, 0 //被占用
|
|
|
- if c_2_diff < 0 { //有两个城市的名额未使用完
|
|
|
- if p_Diff > 0 {
|
|
|
- //转移给一个省使用
|
|
|
- if c_2_diff+p_Diff > 0 {
|
|
|
- del_2_flag += -c_2_diff
|
|
|
- c_2_diff = 0
|
|
|
- //p_Diff -= -c_2_diff;
|
|
|
- } else {
|
|
|
- c_2_diff += p_Diff
|
|
|
- del_2_flag += p_Diff
|
|
|
- //p_Diff = 0
|
|
|
- }
|
|
|
- }
|
|
|
- if c_1_diff > 0 && c_2_diff < 0 { //转移给一个市使用
|
|
|
- c_1_diff += c_2_diff
|
|
|
- }
|
|
|
- }
|
|
|
- if c_1_diff < 0 { //有一个城市的名额未使用完
|
|
|
- if p_Diff > 0 {
|
|
|
- if c_1_diff+p_Diff > 0 {
|
|
|
- del_1_flag += -c_1_diff
|
|
|
- c_1_diff = 0
|
|
|
- //p_Diff -= -c_1_diff
|
|
|
- } else {
|
|
|
- c_1_diff += p_Diff
|
|
|
- del_1_flag += p_Diff
|
|
|
- //p_Diff = 0
|
|
|
- }
|
|
|
- }
|
|
|
- if c_2_diff > 0 && c_1_diff < 0 {
|
|
|
- if c_1_diff+c_2_diff > 0 {
|
|
|
- del_1_flag += -c_1_diff
|
|
|
- c_1_diff = 0
|
|
|
- //c_2_diff -= -c_1_diff;
|
|
|
- } else {
|
|
|
- c_1_diff += c_2_diff
|
|
|
- del_1_flag += c_2_diff
|
|
|
- //c_2_diff = 0
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- //删除顶替
|
|
|
- log.Println("start", UpgradeBuyset.NewCitys, del_2_flag, del_1_flag)
|
|
|
- index_flag := 0
|
|
|
- for del_2_flag > 0 || del_1_flag > 0 {
|
|
|
- valueFlag := UpgradeBuyset.NewCitys[index_flag]
|
|
|
- if del_2_flag > 0 && valueFlag == 2 {
|
|
|
- UpgradeBuyset.NewCitys = append(UpgradeBuyset.NewCitys[:index_flag], UpgradeBuyset.NewCitys[index_flag+1:]...)
|
|
|
- del_2_flag--
|
|
|
- continue
|
|
|
- } else if del_1_flag > 0 && valueFlag == 1 {
|
|
|
- UpgradeBuyset.NewCitys = append(UpgradeBuyset.NewCitys[:index_flag], UpgradeBuyset.NewCitys[index_flag+1:]...)
|
|
|
- del_1_flag--
|
|
|
- continue
|
|
|
- }
|
|
|
- index_flag++
|
|
|
- }
|
|
|
- log.Println("end", UpgradeBuyset.NewCitys, c_2_diff, c_1_diff)
|
|
|
- //增加新增
|
|
|
- for i := c_2_diff; i > 0; i-- { //添加购买两个市数量
|
|
|
- UpgradeBuyset.NewCitys = append(UpgradeBuyset.NewCitys, 2)
|
|
|
- }
|
|
|
-
|
|
|
- for i := c_1_diff; i > 0; i-- { //添加购买一个市数量
|
|
|
- UpgradeBuyset.NewCitys = append(UpgradeBuyset.NewCitys, 1)
|
|
|
- }
|
|
|
- } else {
|
|
|
- //不需要升级
|
|
|
- AreaNeedUpgrade = false
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- if BuyerclassUpgrade || AreaNeedUpgrade {
|
|
|
- return true, UpgradeBuyset
|
|
|
- } else {
|
|
|
- return false, nil
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-//升级详情单
|
|
|
-func getUpgradeDetail(userId string, newBuySet, oldBuySet *entity.SubvipBuySet, oldEndtime int64, count, unit int) (totalPrice int, subtotals []map[string]interface{}) {
|
|
|
+//获取新版超级订阅升级价格、清单
|
|
|
+func getNewUpgradeDetail(userId string, newBuySet, oldBuySet *entity.SubvipBuySet, oldEndtime int64, count, unit int) (totalPrice int, subtotals []map[string]interface{}) {
|
|
|
rResult, ok := util.MQFW.Find("vip_upgrade", &bson.M{"s_userid": userId, "i_isvalid": 0}, `{"l_validtime":1}`, `{"o_buyset":1,"l_validtime":1}`, false, -1, -1)
|
|
|
if !ok {
|
|
|
return -2, nil
|
|
|
}
|
|
|
nowEndTime := oldEndtime
|
|
|
- //已经续费升级价格详单
|
|
|
if len(*rResult) != 0 {
|
|
|
for i := len(*rResult) - 1; i >= 0; i-- {
|
|
|
thisObj := qutil.ObjToMap((*rResult)[i])
|
|
@@ -494,7 +310,7 @@ func getUpgradeDetail(userId string, newBuySet, oldBuySet *entity.SubvipBuySet,
|
|
|
})
|
|
|
//此次新增续费价格计算
|
|
|
if count > 0 {
|
|
|
- renew_price := entity.JyVipSubStruct.GetSubVipPriceByBuySet(newBuySet.NewCitys, newBuySet.AreaCount, newBuySet.BuyerclassCount, count, unit, false)
|
|
|
+ renew_price := entity.JyVipSubStruct.GetSubVipPriceByBuySet(newBuySet, count, unit, false)
|
|
|
if renew_price < 0 {
|
|
|
log.Println("~~~~~~~~3", renew_price)
|
|
|
return -1, nil
|
|
@@ -510,37 +326,187 @@ func getUpgradeDetail(userId string, newBuySet, oldBuySet *entity.SubvipBuySet,
|
|
|
return totalPrice, subtotals
|
|
|
}
|
|
|
|
|
|
+//是否需要升级
|
|
|
+func needUpgrade(newBuySet, oldBuySet *entity.SubvipBuySet) bool {
|
|
|
+ BuyerclassUpgrade, AreaNeedUpgrade := false, false
|
|
|
+ UpgradeBuyset := &entity.SubvipBuySet{
|
|
|
+ oldBuySet.Upgrade,
|
|
|
+ oldBuySet.AreaCount,
|
|
|
+ oldBuySet.NewCitys,
|
|
|
+ oldBuySet.BuyerclassCount,
|
|
|
+ }
|
|
|
+ if newBuySet.Upgrade != 1 { //旧版本订阅是否需要升级校验
|
|
|
+ //比较行业
|
|
|
+ if oldBuySet.BuyerclassCount != -1 {
|
|
|
+ //若当前买的不是全行业,并且当前行业数量小于新行业数量时
|
|
|
+ if oldBuySet.BuyerclassCount < newBuySet.BuyerclassCount || newBuySet.BuyerclassCount == -1 {
|
|
|
+ BuyerclassUpgrade = true
|
|
|
+ //计算升级后的buySet
|
|
|
+ UpgradeBuyset.BuyerclassCount = newBuySet.BuyerclassCount
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //比较地区
|
|
|
+ if oldBuySet.AreaCount != -1 {
|
|
|
+ if newBuySet.AreaCount == -1 { //升级为全国
|
|
|
+ UpgradeBuyset.AreaCount = newBuySet.AreaCount
|
|
|
+ UpgradeBuyset.NewCitys = newBuySet.NewCitys
|
|
|
+ AreaNeedUpgrade = true
|
|
|
+ } else {
|
|
|
+ oldCitysBuyOne, oldCitysBuyTwo := 0, 0
|
|
|
+ newCitysBuyOne, newCitysBuyTwo := 0, 0
|
|
|
+ oldCopy := []int{} //复制(防止影响原数组)
|
|
|
+ for _, v := range oldBuySet.NewCitys {
|
|
|
+ if v == 1 {
|
|
|
+ oldCitysBuyOne++
|
|
|
+ } else {
|
|
|
+ oldCitysBuyTwo++
|
|
|
+ }
|
|
|
+ oldCopy = append(oldCopy, v)
|
|
|
+ }
|
|
|
+ UpgradeBuyset.NewCitys = oldCopy
|
|
|
+ for _, v := range newBuySet.NewCitys {
|
|
|
+ if v == 1 {
|
|
|
+ newCitysBuyOne++
|
|
|
+ } else {
|
|
|
+ newCitysBuyTwo++
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ p_Diff := newBuySet.AreaCount - oldBuySet.AreaCount
|
|
|
+ c_1_diff := newCitysBuyOne - oldCitysBuyOne
|
|
|
+ c_2_diff := newCitysBuyTwo - oldCitysBuyTwo
|
|
|
+
|
|
|
+ if p_Diff > 0 {
|
|
|
+ UpgradeBuyset.AreaCount = newBuySet.AreaCount
|
|
|
+ }
|
|
|
+
|
|
|
+ if p_Diff > 0 || p_Diff+c_2_diff > 0 || p_Diff+c_2_diff+c_1_diff > 0 {
|
|
|
+ //有多余未使用名额保留
|
|
|
+ if p_Diff+c_2_diff+c_1_diff < 0 {
|
|
|
+ if c_1_diff < 0 && p_Diff+c_2_diff >= 0 {
|
|
|
+ c_1_diff = -(p_Diff + c_2_diff)
|
|
|
+ }
|
|
|
+ if c_2_diff < 0 && p_Diff+c_1_diff >= 0 {
|
|
|
+ c_2_diff = -(p_Diff + c_1_diff)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //需要升级
|
|
|
+ AreaNeedUpgrade = true
|
|
|
+ //计算升级后的buySet
|
|
|
+ if p_Diff < 0 { //有剩余省份未使用
|
|
|
+ if c_2_diff > 0 {
|
|
|
+ c_2_diff += p_Diff //转移给两个市使用
|
|
|
+ } else {
|
|
|
+ c_1_diff += p_Diff //转移给一个市使用
|
|
|
+ }
|
|
|
+ } else { //省份数量增加
|
|
|
+ UpgradeBuyset.AreaCount = newBuySet.AreaCount
|
|
|
+ }
|
|
|
+ del_2_flag, del_1_flag := 0, 0 //被占用
|
|
|
+ if c_2_diff < 0 { //有两个城市的名额未使用完
|
|
|
+ if p_Diff > 0 {
|
|
|
+ //转移给一个省使用
|
|
|
+ if c_2_diff+p_Diff > 0 {
|
|
|
+ del_2_flag += -c_2_diff
|
|
|
+ c_2_diff = 0
|
|
|
+ //p_Diff -= -c_2_diff;
|
|
|
+ } else {
|
|
|
+ c_2_diff += p_Diff
|
|
|
+ del_2_flag += p_Diff
|
|
|
+ //p_Diff = 0
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if c_1_diff > 0 && c_2_diff < 0 { //转移给一个市使用
|
|
|
+ c_1_diff += c_2_diff
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if c_1_diff < 0 { //有一个城市的名额未使用完
|
|
|
+ if p_Diff > 0 {
|
|
|
+ if c_1_diff+p_Diff > 0 {
|
|
|
+ del_1_flag += -c_1_diff
|
|
|
+ c_1_diff = 0
|
|
|
+ //p_Diff -= -c_1_diff
|
|
|
+ } else {
|
|
|
+ c_1_diff += p_Diff
|
|
|
+ del_1_flag += p_Diff
|
|
|
+ //p_Diff = 0
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if c_2_diff > 0 && c_1_diff < 0 {
|
|
|
+ if c_1_diff+c_2_diff > 0 {
|
|
|
+ del_1_flag += -c_1_diff
|
|
|
+ c_1_diff = 0
|
|
|
+ //c_2_diff -= -c_1_diff;
|
|
|
+ } else {
|
|
|
+ c_1_diff += c_2_diff
|
|
|
+ del_1_flag += c_2_diff
|
|
|
+ //c_2_diff = 0
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //删除顶替
|
|
|
+ log.Println("start", UpgradeBuyset.NewCitys, del_2_flag, del_1_flag)
|
|
|
+ index_flag := 0
|
|
|
+ for del_2_flag > 0 || del_1_flag > 0 {
|
|
|
+ valueFlag := UpgradeBuyset.NewCitys[index_flag]
|
|
|
+ if del_2_flag > 0 && valueFlag == 2 {
|
|
|
+ UpgradeBuyset.NewCitys = append(UpgradeBuyset.NewCitys[:index_flag], UpgradeBuyset.NewCitys[index_flag+1:]...)
|
|
|
+ del_2_flag--
|
|
|
+ continue
|
|
|
+ } else if del_1_flag > 0 && valueFlag == 1 {
|
|
|
+ UpgradeBuyset.NewCitys = append(UpgradeBuyset.NewCitys[:index_flag], UpgradeBuyset.NewCitys[index_flag+1:]...)
|
|
|
+ del_1_flag--
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ index_flag++
|
|
|
+ }
|
|
|
+ log.Println("end", UpgradeBuyset.NewCitys, c_2_diff, c_1_diff)
|
|
|
+ //增加新增
|
|
|
+ for i := c_2_diff; i > 0; i-- { //添加购买两个市数量
|
|
|
+ UpgradeBuyset.NewCitys = append(UpgradeBuyset.NewCitys, 2)
|
|
|
+ }
|
|
|
+
|
|
|
+ for i := c_1_diff; i > 0; i-- { //添加购买一个市数量
|
|
|
+ UpgradeBuyset.NewCitys = append(UpgradeBuyset.NewCitys, 1)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //不需要升级
|
|
|
+ AreaNeedUpgrade = false
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if BuyerclassUpgrade || AreaNeedUpgrade {
|
|
|
+ return true
|
|
|
+ } else {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return newBuySet.AreaCount > oldBuySet.AreaCount
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
//获取升级小计价格
|
|
|
func getSubtotalPrice(oldBuyset, newBuyset *entity.SubvipBuySet, startTime, endtime int64) int {
|
|
|
//不能降级
|
|
|
- oldCitysBuyTwo := 0
|
|
|
- newCitysBuyTwo := 0
|
|
|
- for _, v := range oldBuyset.NewCitys {
|
|
|
- if v == 2 {
|
|
|
- oldCitysBuyTwo++
|
|
|
- }
|
|
|
+ pCount := oldBuyset.AreaCount
|
|
|
+ if oldBuyset.Upgrade != 1 {
|
|
|
+ pCount = oldBuyset.AreaCount + len(oldBuyset.NewCitys)
|
|
|
}
|
|
|
- for _, v := range newBuyset.NewCitys {
|
|
|
- if v == 2 {
|
|
|
- newCitysBuyTwo++
|
|
|
- }
|
|
|
- }
|
|
|
- if (oldBuyset.AreaCount != -1 && newBuyset.AreaCount != -1 && (oldBuyset.AreaCount > newBuyset.AreaCount || len(oldBuyset.NewCitys)+oldBuyset.AreaCount > len(newBuyset.NewCitys)+newBuyset.AreaCount || oldCitysBuyTwo+oldBuyset.AreaCount > newCitysBuyTwo+newBuyset.AreaCount)) ||
|
|
|
- (oldBuyset.AreaCount == -1 && newBuyset.AreaCount != -1) || //旧套餐全国 新套餐非全国
|
|
|
- (oldBuyset.BuyerclassCount != -1 && newBuyset.BuyerclassCount != -1 && oldBuyset.BuyerclassCount > newBuyset.BuyerclassCount) || //非全行业下 新套餐行业数量小于旧套餐数量
|
|
|
- oldBuyset.BuyerclassCount == -1 && newBuyset.BuyerclassCount != -1 { //旧套餐全行业 新套餐非全行业
|
|
|
+ if newBuyset.AreaCount < pCount {
|
|
|
return -1
|
|
|
}
|
|
|
+
|
|
|
//计价方式为计算差额
|
|
|
beforePrice, newPrice := 0, 0
|
|
|
yearNum, monthNum := getDateSub(startTime, endtime)
|
|
|
if monthNum != 0 {
|
|
|
- beforePrice += entity.JyVipSubStruct.GetSubVipPriceByBuySet(oldBuyset.NewCitys, oldBuyset.AreaCount, oldBuyset.BuyerclassCount, monthNum, 2, false)
|
|
|
- newPrice += entity.JyVipSubStruct.GetSubVipPriceByBuySet(newBuyset.NewCitys, newBuyset.AreaCount, newBuyset.BuyerclassCount, monthNum, 2, false)
|
|
|
+ beforePrice += entity.JyVipSubStruct.GetSubVipPriceByBuySet(oldBuyset, monthNum, 2, false)
|
|
|
+ newPrice += entity.JyVipSubStruct.GetSubVipPriceByBuySet(newBuyset, monthNum, 2, false)
|
|
|
}
|
|
|
if yearNum != 0 {
|
|
|
- beforePrice += entity.JyVipSubStruct.GetSubVipPriceByBuySet(oldBuyset.NewCitys, oldBuyset.AreaCount, oldBuyset.BuyerclassCount, yearNum, 1, false)
|
|
|
- newPrice += entity.JyVipSubStruct.GetSubVipPriceByBuySet(newBuyset.NewCitys, newBuyset.AreaCount, newBuyset.BuyerclassCount, yearNum, 1, false)
|
|
|
+ beforePrice += entity.JyVipSubStruct.GetSubVipPriceByBuySet(oldBuyset, yearNum, 1, false)
|
|
|
+ newPrice += entity.JyVipSubStruct.GetSubVipPriceByBuySet(newBuyset, yearNum, 1, false)
|
|
|
}
|
|
|
log.Printf("old %+v \n new %+v \n %d-%d 相差%d年 %d个月\n price: %d-%d=%d \n", oldBuyset, newBuyset, startTime, endtime, yearNum, monthNum, newPrice, beforePrice, newPrice-beforePrice)
|
|
|
return newPrice - beforePrice
|