|
@@ -1,15 +1,19 @@
|
|
package subvip
|
|
package subvip
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "app.yhyue.com/moapp/jybase/common"
|
|
"app.yhyue.com/moapp/jybase/date"
|
|
"app.yhyue.com/moapp/jybase/date"
|
|
|
|
+ "app.yhyue.com/moapp/jybase/mongodb"
|
|
"context"
|
|
"context"
|
|
"fmt"
|
|
"fmt"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
"github.com/pkg/errors"
|
|
"github.com/pkg/errors"
|
|
|
|
+ "go.mongodb.org/mongo-driver/bson"
|
|
"jyOrderManager/internal/consts"
|
|
"jyOrderManager/internal/consts"
|
|
|
|
+ "jyOrderManager/internal/service"
|
|
"jyOrderManager/utility"
|
|
"jyOrderManager/utility"
|
|
- "regexp"
|
|
|
|
|
|
+ "strings"
|
|
"time"
|
|
"time"
|
|
)
|
|
)
|
|
|
|
|
|
@@ -17,22 +21,39 @@ import (
|
|
func (p jySubVipProduct) OpenService(ctx context.Context) error {
|
|
func (p jySubVipProduct) OpenService(ctx context.Context) error {
|
|
record, err := g.DB().GetOne(ctx, fmt.Sprintf("SELECT * FROM %s WHERE order_code =?", consts.OrderListTableName), p.param.OrderCode)
|
|
record, err := g.DB().GetOne(ctx, fmt.Sprintf("SELECT * FROM %s WHERE order_code =?", consts.OrderListTableName), p.param.OrderCode)
|
|
if err != nil || record.IsEmpty() {
|
|
if err != nil || record.IsEmpty() {
|
|
- return errors.Wrap(err, "未知订单编号")
|
|
|
|
|
|
+ return errors.Wrap(err, "超级订阅开通服务异常:未知订单编号")
|
|
|
|
+ }
|
|
|
|
+ userData := utility.GetCreateUserData(p.param.Phone)
|
|
|
|
+ if userData == nil {
|
|
|
|
+ return errors.Wrap(err, "超级订阅开通服务异常:用户查询创建异常")
|
|
|
|
+ }
|
|
|
|
+ if p.param.Id == 0 {
|
|
|
|
+ return errors.New("超级订阅开通服务异常:参数异常")
|
|
|
|
+ }
|
|
|
|
+ pcls, err := service.Product().GetProduct(p.param.ProductCode)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return errors.Wrap(err, "未知商品类型")
|
|
}
|
|
}
|
|
var (
|
|
var (
|
|
- userId, userData = FindUserDataByPhone(p.param.Phone)
|
|
|
|
- orderId = gconv.Int(record["id"])
|
|
|
|
|
|
+ userId = mongodb.BsonIdToSId(userData["_id"])
|
|
|
|
+ orderId = gconv.Int(record["id"])
|
|
|
|
+ buySubject = gconv.Int(record["buy_subject"])
|
|
|
|
+ companyName = gconv.String(record["company_name"])
|
|
|
|
+ entId = gconv.Int(record["ent_id"])
|
|
|
|
+ entService, userCount = utility.EntServiceOpen(entId, p.param.Phone)
|
|
|
|
+ order_ent_id = gconv.Int(record["ent_id"])
|
|
|
|
+ order_user_id = gconv.Int(record["user_id"])
|
|
|
|
+
|
|
|
|
+ buyCount = p.param.SGiftAccountNumber + p.param.SPayAccountNumber
|
|
mgoUserId, userPositionId string
|
|
mgoUserId, userPositionId string
|
|
- buySubject = gconv.Int64(record["buy_subject"])
|
|
|
|
- companyName = gconv.String(record["company_name"])
|
|
|
|
- entId = gconv.Int(record["ent_id"])
|
|
|
|
|
|
+ redisData = map[string]interface{}{}
|
|
|
|
+
|
|
|
|
+ userUpdate = map[string]interface{}{}
|
|
|
|
+ orderUpdate = map[string]interface{}{}
|
|
|
|
+ orderDetailUpdate = map[string]interface{}{}
|
|
)
|
|
)
|
|
|
|
|
|
if p.param.RecordPayType == 1 || p.param.RecordPayType == 4 { //新购
|
|
if p.param.RecordPayType == 1 || p.param.RecordPayType == 4 { //新购
|
|
- var (
|
|
|
|
- entService int
|
|
|
|
- userCount int
|
|
|
|
- )
|
|
|
|
mgoUserId = userId
|
|
mgoUserId = userId
|
|
vipStartTime, vipEndTime := utility.GetStartAndEndTime(time.Now(), utility.DateComp{
|
|
vipStartTime, vipEndTime := utility.GetStartAndEndTime(time.Now(), utility.DateComp{
|
|
Num: p.param.PayCycle,
|
|
Num: p.param.PayCycle,
|
|
@@ -44,53 +65,236 @@ func (p jySubVipProduct) OpenService(ctx context.Context) error {
|
|
if buySubject == 2 { //购买主体是企业,手机号改变,需要重新创建企业
|
|
if buySubject == 2 { //购买主体是企业,手机号改变,需要重新创建企业
|
|
entId, err, userPositionId = utility.AutomaticallyCreatingEnt(companyName, p.param.Phone, p.param.OrderCode, userId)
|
|
entId, err, userPositionId = utility.AutomaticallyCreatingEnt(companyName, p.param.Phone, p.param.OrderCode, userId)
|
|
if err != nil {
|
|
if err != nil {
|
|
- return errors.Wrap(err, fmt.Sprintf("AutomaticallyCreatingEn 自动创建企业信息失败: %s", companyName))
|
|
|
|
|
|
+ return errors.Wrap(err, fmt.Sprintf("超级订阅开通服务异常:AutomaticallyCreatingEn 自动创建企业信息失败: %s", companyName))
|
|
}
|
|
}
|
|
- var (
|
|
|
|
- userId = userPositionId
|
|
|
|
- entService, userCount = utility.EntServiceOpen(entId, p.param.Phone)
|
|
|
|
- buyCount = p.param.SGiftAccountNumber + p.param.SPayAccountNumber
|
|
|
|
- )
|
|
|
|
-
|
|
|
|
|
|
+ userId = userPositionId
|
|
if entId != 0 && buyCount > 1 {
|
|
if entId != 0 && buyCount > 1 {
|
|
- regRuler := "^1[3456789]{1}\\d{9}$"
|
|
|
|
- reg := regexp.MustCompile(regRuler)
|
|
|
|
- if reg.MatchString(p.param.Phone) && companyName != "" {
|
|
|
|
|
|
+ if consts.PhoneRegex.MatchString(p.param.Phone) && companyName != "" {
|
|
err, _ := utility.EntLicense(entId, buyCount, -1, orderId, userCount, "VIP订阅", vipStartTime.Format(date.Date_Full_Layout), vipEndTime.Format(date.Date_Full_Layout), p.param.Phone)
|
|
err, _ := utility.EntLicense(entId, buyCount, -1, orderId, userCount, "VIP订阅", vipStartTime.Format(date.Date_Full_Layout), vipEndTime.Format(date.Date_Full_Layout), p.param.Phone)
|
|
if err != nil {
|
|
if err != nil {
|
|
- return errors.Wrap(err, fmt.Sprintf("EntLicense 创建企业授权信息失败:%s", companyName))
|
|
|
|
|
|
+ return errors.Wrap(err, fmt.Sprintf("超级订阅开通服务异常:EntLicense 创建企业授权信息失败:%s", companyName))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if entService != 1 {
|
|
if entService != 1 {
|
|
- ClearBigVipUserPower(userId)
|
|
|
|
|
|
+ utility.ClearBigVipUserPower(ctx, userId)
|
|
|
|
+ }
|
|
|
|
+ } else if entService == 1 || buySubject == 1 { //企业下个人身份开通
|
|
|
|
+ userUpdate = map[string]interface{}{
|
|
|
|
+ "o_vipjy.i_trial": -1, //已激活试用
|
|
|
|
+ "o_vipjy.o_area": map[string]interface{}{}, //设置地区
|
|
|
|
+ "o_vipjy.o_buyset": g.Map{
|
|
|
|
+ "upgrade": 1,
|
|
|
|
+ "areacount": -1,
|
|
|
|
+ "newcitys": []int{},
|
|
|
|
+ "buyerclasscount": -1,
|
|
|
|
+ }, //购买内容 城市、省份、行业数量
|
|
|
|
+ "l_vip_starttime": vipStartTime.Unix(), //开始时间
|
|
|
|
+ "l_vip_endtime": vipEndTime.Unix(), //结束时间
|
|
|
|
+ "i_vip_status": common.If(vipStartTime.Unix() <= time.Now().Unix(), 2, -2), //1试用 2正式 -2 试用到期
|
|
|
|
+ "i_vip_expire_tip": 0, //消息提示初始化
|
|
|
|
+ "o_vipjy.a_buyerclass": []interface{}{}, //设置行业
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ redisData["entId"] = entId
|
|
|
|
+ redisData["entName"] = companyName
|
|
|
|
+ redisData["productType"] = "超级订阅"
|
|
|
|
+ utility.PayUserIdentitySwitchRedis(mgoUserId, redisData)
|
|
} else if p.param.RecordPayType == 2 { //续费
|
|
} else if p.param.RecordPayType == 2 { //续费
|
|
-
|
|
|
|
|
|
+ vipEndTime := time.Unix(common.Int64All(userData["l_vip_endtime"]), 0).In(time.Local)
|
|
|
|
+ var newStartTime, newEndTime time.Time
|
|
|
|
+ userUpdate = map[string]interface{}{
|
|
|
|
+ "i_vip_expire_tip": 0,
|
|
|
|
+ "i_vip_status": 2,
|
|
|
|
+ }
|
|
|
|
+ if vipEndTime.Unix() < time.Now().Unix() { //原服务已过期
|
|
|
|
+ newStartTime = time.Now()
|
|
|
|
+ userUpdate["l_vip_starttime"] = newStartTime.Unix()
|
|
|
|
+ } else { //原服务未过期
|
|
|
|
+ //开始时间为:原服务结束日期+1天
|
|
|
|
+ newStartTime = vipEndTime.AddDate(0, 0, 1)
|
|
|
|
+ }
|
|
|
|
+ _, newEndTime = utility.GetStartAndEndTime(newStartTime, utility.DateComp{
|
|
|
|
+ Num: p.param.PayCycle,
|
|
|
|
+ Unit: p.param.PayCycleType,
|
|
|
|
+ }, utility.DateComp{
|
|
|
|
+ Num: p.param.GifCycle,
|
|
|
|
+ Unit: p.param.GifCycleType,
|
|
|
|
+ })
|
|
|
|
+ userUpdate["l_vip_endtime"] = newEndTime.Unix()
|
|
} else if p.param.RecordPayType == 3 { //升级
|
|
} else if p.param.RecordPayType == 3 { //升级
|
|
|
|
+ //先判断审核通过日期是否再超级订阅有效期内
|
|
|
|
+ vipStatus := common.IntAll(userData["i_vip_status"])
|
|
|
|
+ if vipStatus != 2 {
|
|
|
|
+ return errors.New("服务已到期,权益开通失败")
|
|
|
|
+ }
|
|
|
|
+ updata := map[string]interface{}{
|
|
|
|
+ "o_vipjy.o_buyset": filterMap["newBuyset"],
|
|
|
|
+ "i_vip_expire_tip": 0,
|
|
|
|
+ "i_vip_status": 2,
|
|
|
|
+ }
|
|
|
|
+ if filterMap["area"] != nil {
|
|
|
|
+ updata["o_vipjy.o_area"] = filterMap["area"] //设置地区
|
|
|
|
+ }
|
|
|
|
+ if filterMap["industry"] != nil {
|
|
|
|
+ updata["o_vipjy.a_buyerclass"] = filterMap["industry"] //设置行业
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ if buySubject == 1 {
|
|
|
|
+ if !utility.MG.DB().UpdateById("user", userId, bson.M{
|
|
|
|
+ "$set": userUpdate,
|
|
|
|
+ }) {
|
|
|
|
+ return errors.New("开通个人超级订阅异常")
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ if !vipEntUpdate(ctx, entId, map[string]interface{}{
|
|
|
|
+ "$set": userUpdate,
|
|
|
|
+ }, p.param.Phone) {
|
|
|
|
+ return errors.New("开通企业级超级订阅异常")
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+ utility.ClearBigVipUserPower(ctx, userId)
|
|
|
|
+ if len(orderUpdate) > 0 {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ _, err := g.DB().Update(ctx, consts.OrderDetailTableName, g.Map{
|
|
|
|
+ "service_starttime": vipStartTime.Format(date.Date_Full_Layout),
|
|
|
|
+ "service_endtime": vipEndTime.Format(date.Date_Full_Layout),
|
|
|
|
+ }, " id = ? ", p.param.Id)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return errors.Wrap(err, "超级订阅开通服务异常:订单明细表更新异常")
|
|
|
|
+ }
|
|
|
|
+
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
-func GetStartAndEndTime(startTime string, cycleCount, cycleUnit int) (vipStartTime, endTime time.Time) {
|
|
|
|
- //根据开始时间计算结束时间
|
|
|
|
- month := 0
|
|
|
|
- if cycleUnit == 1 {
|
|
|
|
- month = cycleCount * 12
|
|
|
|
- } else if cycleUnit == 2 {
|
|
|
|
- month = cycleCount
|
|
|
|
- } else if cycleUnit == 4 {
|
|
|
|
- month = cycleCount * 3
|
|
|
|
|
|
+func VipServiceOpen(ctx context.Context, userId, phone string, vipStartTime, vipEndTime time.Time, buySubject, entId int) error {
|
|
|
|
+ buyArea := map[string]interface{}{}
|
|
|
|
+ set := map[string]interface{}{
|
|
|
|
+ "o_vipjy.i_trial": -1, //已激活试用
|
|
|
|
+ "o_vipjy.o_area": buyArea, //设置地区
|
|
|
|
+ "o_vipjy.o_buyset": g.Map{
|
|
|
|
+ "upgrade": 1,
|
|
|
|
+ "areacount": -1,
|
|
|
|
+ "newcitys": []int{},
|
|
|
|
+ "buyerclasscount": -1,
|
|
|
|
+ }, //购买内容 城市、省份、行业数量
|
|
|
|
+ "l_vip_starttime": vipStartTime.Unix(), //开始时间
|
|
|
|
+ "l_vip_endtime": vipEndTime.Unix(), //结束时间
|
|
|
|
+ "i_vip_status": common.If(vipStartTime.Unix() <= time.Now().Unix(), 2, -2), //1试用 2正式 -2 试用到期
|
|
|
|
+ "i_vip_expire_tip": 0, //消息提示初始化
|
|
|
|
+ "o_vipjy.a_buyerclass": []interface{}{}, //设置行业
|
|
}
|
|
}
|
|
- vipStartTime, _ = time.ParseInLocation(date.Date_Short_Layout, startTime, time.Local)
|
|
|
|
- if cycleUnit == 3 {
|
|
|
|
- _endTime := vipStartTime.AddDate(0, 0, cycleCount)
|
|
|
|
- t := _endTime.Format(date.Date_Short_Layout) + " 23:59:59"
|
|
|
|
- endTime, _ = time.ParseInLocation(date.Date_Full_Layout, t, time.Local)
|
|
|
|
|
|
+ if buySubject == 1 {
|
|
|
|
+ if !utility.MG.DB().UpdateById("user", userId, bson.M{
|
|
|
|
+ "$set": set,
|
|
|
|
+ }) {
|
|
|
|
+ return errors.New("开通个人超级订阅异常")
|
|
|
|
+ }
|
|
|
|
+ utility.ClearBigVipUserPower(ctx, userId)
|
|
} else {
|
|
} else {
|
|
- endTime = GetDATE1(month, vipStartTime.Unix())
|
|
|
|
|
|
+ if !vipEntUpdate(ctx, entId, map[string]interface{}{
|
|
|
|
+ "$set": set,
|
|
|
|
+ }, phone) {
|
|
|
|
+ return errors.New("开通企业级超级订阅异常")
|
|
|
|
+ }
|
|
|
|
+ utility.ClearBigVipUserPower(ctx, userId)
|
|
|
|
+ }
|
|
|
|
+ return nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// VipEntUpdate 企业订阅时间修改
|
|
|
|
+func vipEntUpdate(ctx context.Context, entId int, update map[string]interface{}, phone string) bool {
|
|
|
|
+ ok := false
|
|
|
|
+ if entId > 0 && phone != "" {
|
|
|
|
+ //查找之前数据是否存在
|
|
|
|
+ res, _ := g.DB().GetOne(ctx, "SELECT id FROM entniche_user WHERE ent_id=? and phone=?", entId, phone)
|
|
|
|
+ if res.IsEmpty() {
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+ up1, up2 := map[string]interface{}{}, map[string]interface{}{}
|
|
|
|
+ var tp int
|
|
|
|
+ for k, v := range update {
|
|
|
|
+ vm, ok := v.(map[string]interface{})
|
|
|
|
+ if !ok {
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+ up11, up22 := map[string]interface{}{}, map[string]interface{}{}
|
|
|
|
+ key := ""
|
|
|
|
+ for kk, vv := range vm {
|
|
|
|
+ if (key == "" || key == "o_jy") && (kk == "o_jy" || strings.HasPrefix(kk, "o_jy.")) {
|
|
|
|
+ key = "o_jy"
|
|
|
|
+ up22[strings.ReplaceAll(kk, key, "o_entniche")] = vv
|
|
|
|
+ tp = 2
|
|
|
|
+ } else if (key == "" || key == "o_vipjy") && (kk == "o_vipjy" || strings.HasPrefix(kk, "o_vipjy.")) {
|
|
|
|
+ key = "o_vipjy"
|
|
|
|
+ up22[strings.ReplaceAll(kk, key, "o_entniche")] = vv
|
|
|
|
+ tp = 1
|
|
|
|
+ } else if (key == "" || key == "o_member_jy") && (kk == "o_member_jy" || strings.HasPrefix(kk, "o_member_jy.")) {
|
|
|
|
+ key = "o_member_jy"
|
|
|
|
+ up22[strings.ReplaceAll(kk, key, "o_entniche")] = vv
|
|
|
|
+ tp = 1
|
|
|
|
+ } else if (key == "" || key == "o_entniche") && (kk == "o_entniche" || strings.HasPrefix(kk, "o_entniche.")) {
|
|
|
|
+ key = "o_entniche"
|
|
|
|
+ up22[strings.ReplaceAll(kk, key, "o_entniche")] = vv
|
|
|
|
+ tp = 0
|
|
|
|
+ } else {
|
|
|
|
+ up11[kk] = vv
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if len(up11) > 0 {
|
|
|
|
+ up1[k] = up11
|
|
|
|
+ }
|
|
|
|
+ if len(up22) > 0 {
|
|
|
|
+ up2[k] = up22
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ ok1, ok2 := false, false
|
|
|
|
+ //查看之前是否有数据
|
|
|
|
+ data, ok := utility.MG.DB().Find("entniche_rule", map[string]interface{}{
|
|
|
|
+ "i_entid": entId,
|
|
|
|
+ "i_userid": res.Map()["id"],
|
|
|
|
+ "i_type": tp,
|
|
|
|
+ }, `{"i_time":-1}`, nil, false, -1, -1)
|
|
|
|
+ if ok && len(*data) == 0 {
|
|
|
|
+ up3 := map[string]interface{}{
|
|
|
|
+ "$set": map[string]interface{}{"o_entniche.i_matchway": 1, "o_entniche.i_ratemode": 1, "o_entniche.i_wxpush": 1, "o_entniche.i_apppush": 1, "o_entniche.i_projectmatch": 0, "o_entniche.a_infotype": []string{}, "o_entniche.a_items": []string{}, "o_entniche.l_modifydate": time.Now().Unix()},
|
|
|
|
+ }
|
|
|
|
+ utility.MG.DB().Update("entniche_rule", map[string]interface{}{
|
|
|
|
+ "i_entid": entId,
|
|
|
|
+ "i_userid": res.Map()["id"],
|
|
|
|
+ "i_type": tp,
|
|
|
|
+ }, up3, true, false)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if len(up2) > 0 {
|
|
|
|
+ ok2 = utility.MG.DB().Update("entniche_rule", map[string]interface{}{
|
|
|
|
+ "i_entid": entId,
|
|
|
|
+ "i_userid": res.Map()["id"],
|
|
|
|
+ "i_type": tp,
|
|
|
|
+ }, up2, true, false)
|
|
|
|
+ }
|
|
|
|
+ if len(up1) > 0 {
|
|
|
|
+ query := bson.M{
|
|
|
|
+ "$or": []bson.M{
|
|
|
|
+ bson.M{"i_vip_status": bson.M{"$gt": 0}},
|
|
|
|
+ bson.M{"i_member_status": bson.M{"$gt": 0}},
|
|
|
|
+ },
|
|
|
|
+ "i_entid": entId,
|
|
|
|
+ "i_userid": res.Map()["id"],
|
|
|
|
+ }
|
|
|
|
+ if utility.MG.DB().Count("ent_user", query) > 0 {
|
|
|
|
+ return true
|
|
|
|
+ }
|
|
|
|
+ ok1 = utility.MG.DB().Update("ent_user", map[string]interface{}{
|
|
|
|
+ "i_entid": entId,
|
|
|
|
+ "i_userid": res.Map()["id"],
|
|
|
|
+ }, up1, true, false)
|
|
|
|
+ }
|
|
|
|
+ //默认数据处理
|
|
|
|
+ return ok1 || ok2
|
|
}
|
|
}
|
|
- return vipStartTime, endTime
|
|
|
|
|
|
+ return ok
|
|
}
|
|
}
|