|
@@ -0,0 +1,390 @@
|
|
|
+package service
|
|
|
+
|
|
|
+import (
|
|
|
+ . "app.yhyue.com/moapp/jybase/api"
|
|
|
+ qutil "app.yhyue.com/moapp/jybase/common"
|
|
|
+ "app.yhyue.com/moapp/jybase/date"
|
|
|
+ "app.yhyue.com/moapp/jybase/mongodb"
|
|
|
+ "app.yhyue.com/moapp/jybase/redis"
|
|
|
+ "app.yhyue.com/moapp/jypkg/common/src/qfw/util/jy"
|
|
|
+ "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/pb"
|
|
|
+ "fmt"
|
|
|
+ "github.com/gogf/gf/v2/os/gtime"
|
|
|
+ "github.com/gogf/gf/v2/util/gconv"
|
|
|
+ "go.mongodb.org/mongo-driver/bson"
|
|
|
+ "jy/src/jfw/modules/subscribepay/src/config"
|
|
|
+ "jy/src/jfw/modules/subscribepay/src/util"
|
|
|
+ "log"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+// 接收人信息
|
|
|
+type Received struct {
|
|
|
+ Uid string
|
|
|
+ Phone string
|
|
|
+ Duration int
|
|
|
+ BPhone string // 被赠 手机号
|
|
|
+ StartTime string // 被赠 开始时间
|
|
|
+ EndTime string // 被赠 结束时间
|
|
|
+ Itype int // 接收人 1: 续费,2: 新购
|
|
|
+ UpdateInfo map[string]interface{}
|
|
|
+}
|
|
|
+
|
|
|
+func (t *VipGift) GetSubDuration() {
|
|
|
+ userid := qutil.ObjToString(t.GetSession("mgoUserId"))
|
|
|
+ rData, errMsg := func() (interface{}, error) {
|
|
|
+ user, ok := util.MQFW.FindById("user", userid, bson.M{"i_vip_status": 1, "l_vip_endtime": 1, "l_vip_starttime": 1, "o_vipjy": 1})
|
|
|
+ if ok && len(*user) > 0 {
|
|
|
+ areacount := 0
|
|
|
+ if vip := gconv.Map((*user)["o_vipjy"]); vip != nil && vip["o_buyset"] != nil {
|
|
|
+ areacount = gconv.Int(gconv.Map(vip["o_buyset"])["areacount"])
|
|
|
+ }
|
|
|
+ if qutil.IntAll((*user)["i_vip_status"]) > 0 {
|
|
|
+ //startTime := qutil.Int64All((*user)["l_vip_starttime"])
|
|
|
+ endTime := qutil.Int64All((*user)["l_vip_endtime"])
|
|
|
+ dif, _ := difMouth(time.Now().Unix(), endTime)
|
|
|
+ return map[string]interface{}{
|
|
|
+ "gifted": dif,
|
|
|
+ "areacount": areacount,
|
|
|
+ }, nil
|
|
|
+ }
|
|
|
+ return map[string]interface{}{
|
|
|
+ "gifted": 0,
|
|
|
+ "areacount": areacount,
|
|
|
+ }, nil
|
|
|
+ }
|
|
|
+ return nil, fmt.Errorf("未查询到身份信息")
|
|
|
+ }()
|
|
|
+ t.ServeJson(NewResult(rData, errMsg))
|
|
|
+}
|
|
|
+
|
|
|
+func (t *VipGift) GetInfoByPhone() {
|
|
|
+ userid := qutil.ObjToString(t.GetSession("mgoUserId"))
|
|
|
+ phone := t.GetString("phone")
|
|
|
+ rData, errMsg := func() (interface{}, error) {
|
|
|
+ giver := qutil.ObjToString(t.GetSession("phone"))
|
|
|
+ if giver == phone {
|
|
|
+ return map[string]interface{}{
|
|
|
+ "status": -3, // 送自己
|
|
|
+ }, nil
|
|
|
+ }
|
|
|
+ user, _ := util.MQFW.FindById("user", userid, bson.M{"i_vip_status": 1, "l_vip_endtime": 1, "l_vip_starttime": 1, "o_vipjy": 1})
|
|
|
+ if len(*user) > 0 {
|
|
|
+ status, areacount := 0, 0
|
|
|
+ if vip := gconv.Map((*user)["o_vipjy"]); vip != nil && vip["o_buyset"] != nil {
|
|
|
+ areacount = gconv.Int(gconv.Map(vip["o_buyset"])["areacount"])
|
|
|
+ }
|
|
|
+ query1 := bson.M{"$or": []interface{}{
|
|
|
+ bson.M{"s_phone": phone},
|
|
|
+ bson.M{"s_m_phone": phone},
|
|
|
+ }}
|
|
|
+ buser, _ := util.MQFW.FindOneByField("user", query1, bson.M{"i_vip_status": 1, "o_vipjy": 1, "o_jy": 1})
|
|
|
+ if len(*buser) > 0 {
|
|
|
+ if qutil.IntAll((*buser)["i_vip_status"]) > 0 {
|
|
|
+ b_areacount := 0
|
|
|
+ if vip := gconv.Map((*buser)["o_vipjy"]); vip != nil && vip["o_buyset"] != nil {
|
|
|
+ b_areacount = gconv.Int(gconv.Map(vip["o_buyset"])["areacount"])
|
|
|
+ }
|
|
|
+ if areacount == b_areacount {
|
|
|
+ status = 1
|
|
|
+ } else {
|
|
|
+ status = -2 // 订阅地区不一致
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if ojy := gconv.Map((*buser)["o_jy"]); ojy != nil && ojy["i_ppstatus"] != nil {
|
|
|
+ if state := gconv.Int(ojy["i_ppstatus"]); state == 1 {
|
|
|
+ return map[string]interface{}{
|
|
|
+ "status": -4, // 省份订阅包用户
|
|
|
+ }, nil
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ status = 1
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ status = -1 // 账号未注册
|
|
|
+ }
|
|
|
+ return map[string]interface{}{
|
|
|
+ "status": status,
|
|
|
+ }, nil
|
|
|
+ }
|
|
|
+ return nil, fmt.Errorf("未查询到身份信息")
|
|
|
+ }()
|
|
|
+ t.ServeJson(NewResult(rData, errMsg))
|
|
|
+}
|
|
|
+
|
|
|
+func (t *VipGift) TransferSubDuration() {
|
|
|
+ userid := qutil.ObjToString(t.GetSession("mgoUserId"))
|
|
|
+ rData, errMsg := func() (interface{}, error) {
|
|
|
+ now := time.Now().Unix()
|
|
|
+ if now < config.Config.VipGiftStartTime && now > config.Config.VipGiftEndTime {
|
|
|
+ return nil, fmt.Errorf("不在活动时间范围内")
|
|
|
+ }
|
|
|
+ user, _ := util.MQFW.FindById("user", userid, bson.M{"i_vip_status": 1, "l_vip_endtime": 1, "l_vip_starttime": 1, "o_vipjy": 1})
|
|
|
+ if len(*user) > 0 {
|
|
|
+ // 1-1 验证个人身份 超级订阅时长
|
|
|
+ if qutil.IntAll((*user)["i_vip_status"]) > 0 {
|
|
|
+ //startTime := qutil.Int64All((*user)["l_vip_starttime"])
|
|
|
+ endTime := qutil.Int64All((*user)["l_vip_endtime"])
|
|
|
+ dif, sameDay := difMouth(time.Now().Unix(), endTime)
|
|
|
+ b_phones := gconv.Map(t.GetString("phones"))
|
|
|
+ num := 0
|
|
|
+ for k, v := range b_phones {
|
|
|
+ log.Println("TransferSubDuration---", "phone: ", k, " gift: ", v)
|
|
|
+ if !jy.PhoneReg.MatchString(k) {
|
|
|
+ return nil, fmt.Errorf("手机号格式异常")
|
|
|
+ }
|
|
|
+ num += gconv.Int(v)
|
|
|
+ }
|
|
|
+ if num == 0 {
|
|
|
+ return nil, fmt.Errorf("赠送时长异常")
|
|
|
+ }
|
|
|
+ if dif >= num {
|
|
|
+ err := transferByPhone(t, *user, b_phones, t.GetString("platform"), dif, sameDay)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ return nil, nil
|
|
|
+ }
|
|
|
+ return nil, fmt.Errorf("赠送时长大于当前用户可赠时长")
|
|
|
+ }
|
|
|
+ return nil, fmt.Errorf("当前赠送人没有超级订阅权限")
|
|
|
+ }
|
|
|
+ return nil, fmt.Errorf("未查询到身份信息")
|
|
|
+ }()
|
|
|
+ t.ServeJson(NewResult(rData, errMsg))
|
|
|
+}
|
|
|
+
|
|
|
+func difMouth(i1, i2 int64) (int, bool) {
|
|
|
+ st := time.Unix(i1, 0)
|
|
|
+ et := time.Unix(i2, 0)
|
|
|
+ y1, m1, d1 := st.Date()
|
|
|
+ y2, m2, d2 := et.Date()
|
|
|
+ difM := (y2-y1)*12 + int(m2-m1)
|
|
|
+ // 额外判断天数是否需要调整
|
|
|
+ if d1 == 31 && (d2 == 30 || d2 == 28) {
|
|
|
+ //
|
|
|
+ } else if d1 > d2 {
|
|
|
+ difM--
|
|
|
+ }
|
|
|
+ return difM, d1 == d2
|
|
|
+}
|
|
|
+
|
|
|
+// 1-2 验证手机号 未注册去注册
|
|
|
+// 2-1 赠送并验证 赠予时长
|
|
|
+func transferByPhone(t *VipGift, user, phones map[string]interface{}, platform string, dif int, sameDay bool) error {
|
|
|
+ userId := qutil.ObjToString(t.GetSession("mgoUserId"))
|
|
|
+ areacount := 0
|
|
|
+ if vip := gconv.Map(user["o_vipjy"]); vip != nil && vip["o_buyset"] != nil {
|
|
|
+ areacount = gconv.Int(gconv.Map(vip["o_buyset"])["areacount"])
|
|
|
+ }
|
|
|
+ i_vip_status := qutil.IntAll(user["i_vip_status"])
|
|
|
+ updateInfo := make(map[string]*Received)
|
|
|
+ num := 0 // 扣除
|
|
|
+ for k, v := range phones {
|
|
|
+ giver := qutil.ObjToString(t.GetSession("phone"))
|
|
|
+ if giver == k {
|
|
|
+ return fmt.Errorf("赠送人手机号与被赠送人手机号一致")
|
|
|
+ }
|
|
|
+ query1 := bson.M{"$or": []interface{}{
|
|
|
+ bson.M{"s_phone": k},
|
|
|
+ bson.M{"s_m_phone": k},
|
|
|
+ }}
|
|
|
+ buser, _ := util.MQFW.FindOneByField("user", query1, bson.M{"i_vip_status": 1, "o_vipjy": 1, "l_vip_endtime": 1, "l_vip_starttime": 1, "o_jy": 1})
|
|
|
+ var mgoUserId string
|
|
|
+ if len(*buser) == 0 {
|
|
|
+ // 新用户注册
|
|
|
+ mgoUserId, _ = regNewPhone(k, platform)
|
|
|
+ } else {
|
|
|
+ mgoUserId = mongodb.BsonIdToSId((*buser)["_id"])
|
|
|
+ }
|
|
|
+ received := &Received{
|
|
|
+ Uid: userId,
|
|
|
+ Phone: gconv.String(t.GetSession("phone")),
|
|
|
+ BPhone: k,
|
|
|
+ }
|
|
|
+ if qutil.IntAll((*buser)["i_vip_status"]) > 0 {
|
|
|
+ received.Itype = 1
|
|
|
+ b_areacount := 0
|
|
|
+ if vip := gconv.Map((*buser)["o_vipjy"]); vip != nil && vip["o_buyset"] != nil {
|
|
|
+ b_areacount = gconv.Int(gconv.Map(vip["o_buyset"])["areacount"])
|
|
|
+ }
|
|
|
+ if areacount == b_areacount {
|
|
|
+ num += gconv.Int(v)
|
|
|
+ dif -= gconv.Int(v)
|
|
|
+ if dif >= 0 {
|
|
|
+ updateVipInfo(*buser, gconv.Int(v), areacount, received)
|
|
|
+ } else {
|
|
|
+ return fmt.Errorf("赠予时长异常,赠送失败")
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return fmt.Errorf("该手机号:%s超级订阅地区与赠送人不一致", k)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if len(*buser) > 0 {
|
|
|
+ if ojy := gconv.Map((*buser)["o_jy"]); ojy != nil && ojy["i_ppstatus"] != nil {
|
|
|
+ if status := gconv.Int(ojy["i_ppstatus"]); status == 1 {
|
|
|
+ return fmt.Errorf("省份订阅包用户不能被赠送")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ received.Itype = 0
|
|
|
+ // 新用户和非超级订阅用户直接赠送
|
|
|
+ num += gconv.Int(v)
|
|
|
+ dif -= gconv.Int(v)
|
|
|
+ if dif >= 0 {
|
|
|
+ updateVipInfo(nil, gconv.Int(v), areacount, received)
|
|
|
+ } else {
|
|
|
+ return fmt.Errorf("赠予时长异常,赠送失败")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ updateInfo[mgoUserId] = received
|
|
|
+ }
|
|
|
+ if len(updateInfo) == 0 && len(updateInfo) != len(phones) {
|
|
|
+ return fmt.Errorf("未知异常")
|
|
|
+ }
|
|
|
+ // 赠送人时长修改 赠送人vip结束时间-赠送月份 对比 当前时间
|
|
|
+ endTime := qutil.Int64All(user["l_vip_endtime"])
|
|
|
+ t1 := time.Unix(endTime, 0).Local()
|
|
|
+ newTime := t1.AddDate(0, -num, 0)
|
|
|
+
|
|
|
+ if (sameDay && dif == 0) || (newTime.Unix() <= time.Now().Unix()) {
|
|
|
+ if !util.Compatible.Update(userId, bson.M{"$set": map[string]interface{}{
|
|
|
+ "i_vip_status": -i_vip_status,
|
|
|
+ "i_vip_expire_tip": 2,
|
|
|
+ "i_vip_subtips": 0,
|
|
|
+ "i_vip_fastimport": 0,
|
|
|
+ "l_vip_endtime": newTime.Unix(),
|
|
|
+ },
|
|
|
+ "$unset": map[string]interface{}{
|
|
|
+ "i_vip_expire_tip_retry": "",
|
|
|
+ }}) {
|
|
|
+ return fmt.Errorf("更新异常:%s", userId)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if !util.Compatible.Update(userId, bson.M{"$set": map[string]interface{}{
|
|
|
+ "l_vip_endtime": newTime.Unix(),
|
|
|
+ }}) {
|
|
|
+ return fmt.Errorf("更新异常:%s", userId)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //else {
|
|
|
+ // log.Println("sameDay: ", sameDay)
|
|
|
+ // log.Println("dif: ", dif)
|
|
|
+ // log.Println("newTime.Unix(): ", newTime.Unix())
|
|
|
+ // return fmt.Errorf("赠予时长异常,赠送失败, %d", newTime.Unix())
|
|
|
+ //}
|
|
|
+ clearCache(userId) // 赠送人
|
|
|
+ for uid, info := range updateInfo {
|
|
|
+ info1 := gconv.Map(info.UpdateInfo)
|
|
|
+ // 被赠 更新
|
|
|
+ if util.Compatible.Update(uid, bson.M{"$set": info.UpdateInfo}) {
|
|
|
+ if info1["l_vip_starttime"] != nil {
|
|
|
+ // 新开
|
|
|
+ util.MergeKws(uid, 0, 0, 0) //初始化vip订阅关键词
|
|
|
+ redis.Del("other", "p1_indexMessage_"+uid) //清除redis中vip状态
|
|
|
+ }
|
|
|
+ clearCache(uid)
|
|
|
+ record := map[string]interface{}{
|
|
|
+ "giftUserId": mongodb.BsonIdToSId(user["_id"]),
|
|
|
+ "giftUserPhone": info.Phone,
|
|
|
+ "createTime": date.NowFormat(date.Date_Full_Layout),
|
|
|
+ "recipientUserId": uid,
|
|
|
+ "recipientUserPhone": info.BPhone,
|
|
|
+ "duration": info.Duration,
|
|
|
+ "vipStartTime": info.StartTime,
|
|
|
+ "vipEndTime": info.EndTime,
|
|
|
+ "areaCount": areacount,
|
|
|
+ "itype": info.Itype,
|
|
|
+ }
|
|
|
+ util.Mysql.Insert("vip_gift_records", record)
|
|
|
+ } else {
|
|
|
+ return fmt.Errorf("更新异常:%s, err: %s", uid, info1)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func regNewPhone(phone, platform string) (userid string, err error) {
|
|
|
+ userAddReq := pb.UserAddReq{
|
|
|
+ Appid: "10000",
|
|
|
+ Phone: phone,
|
|
|
+ }
|
|
|
+ now := time.Now()
|
|
|
+ mgoData := map[string]interface{}{
|
|
|
+ "i_appid": 2,
|
|
|
+ "s_phone": phone,
|
|
|
+ "s_password": "",
|
|
|
+ "l_registedate": now.Unix(),
|
|
|
+ "i_ts_guide": 2,
|
|
|
+ "o_jy": map[string]interface{}{
|
|
|
+ "i_apppush": 1,
|
|
|
+ "i_ratemode": 2,
|
|
|
+ "l_modifydate": now.Unix(),
|
|
|
+ },
|
|
|
+ "s_regsource": "vipgift",
|
|
|
+ "s_platform": platform,
|
|
|
+ }
|
|
|
+ if resp := config.Middleground.UserCenter.UserAdd(userAddReq); resp != nil {
|
|
|
+ if resp.Data.Id > 0 {
|
|
|
+ mgoData["base_user_id"] = resp.Data.Id
|
|
|
+ uid := util.MQFW.Save("user", mgoData)
|
|
|
+ if uid != "" {
|
|
|
+ util.Mgo_log.Save("register_log", map[string]interface{}{
|
|
|
+ "userid": uid,
|
|
|
+ "phone": phone,
|
|
|
+ "way": "phone",
|
|
|
+ "source": "vipgift",
|
|
|
+ "create_time": gtime.Timestamp(),
|
|
|
+ })
|
|
|
+ return uid, nil
|
|
|
+ } else {
|
|
|
+ return "", fmt.Errorf("用户user表新增失败")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return "", fmt.Errorf("新手机号注册失败:%s", phone)
|
|
|
+}
|
|
|
+
|
|
|
+func updateVipInfo(buser map[string]interface{}, gift, areacount int, received *Received) {
|
|
|
+ updateMap := make(map[string]interface{})
|
|
|
+ if buser != nil {
|
|
|
+ // 被赠 续费
|
|
|
+ etTime := qutil.Int64All(buser["l_vip_endtime"])
|
|
|
+ endTime := util.GetDATE(0, gift, etTime)
|
|
|
+ updateMap["l_vip_endtime"] = endTime.Unix()
|
|
|
+ updateMap["i_vip_status"] = 2
|
|
|
+ updateMap["i_vip_expire_tip"] = 0
|
|
|
+ updateMap["o_vipjy.l_modifydate"] = time.Now().Unix()
|
|
|
+
|
|
|
+ received.Duration = gift
|
|
|
+ received.StartTime = date.FormatDateByInt64(&etTime, date.Date_Full_Layout)
|
|
|
+ received.EndTime = endTime.Format(date.Date_Full_Layout)
|
|
|
+ } else {
|
|
|
+ // 被赠 新开
|
|
|
+ startTime := time.Now().Unix()
|
|
|
+ endTime := util.GetDATE(0, gift, startTime)
|
|
|
+ updateMap["l_vip_starttime"] = startTime
|
|
|
+ updateMap["l_vip_endtime"] = endTime.Unix()
|
|
|
+ updateMap["i_vip_status"] = 2
|
|
|
+ updateMap["i_vip_expire_tip"] = 0
|
|
|
+ updateMap["o_vipjy.o_buyset"] = &map[string]interface{}{"buyerclasscount": -1, "areacount": areacount, "upgrade": 1, "newcitys": []string{}}
|
|
|
+ updateMap["o_vipjy.o_area"] = &map[string]interface{}{"北京": []string{}}
|
|
|
+ updateMap["o_vipjy.i_trial"] = -1
|
|
|
+
|
|
|
+ received.Duration = gift
|
|
|
+ received.StartTime = date.FormatDateByInt64(&startTime, date.Date_Full_Layout)
|
|
|
+ received.EndTime = endTime.Format(date.Date_Full_Layout)
|
|
|
+ }
|
|
|
+ received.UpdateInfo = updateMap
|
|
|
+}
|
|
|
+
|
|
|
+func clearCache(userId string) {
|
|
|
+ positionId := util.MongoIdToPositionId(userId)
|
|
|
+ jy.ClearBigVipUserPower(positionId)
|
|
|
+ config.Middleground.UserCenter.WorkDesktopClearUserInfo(pb.WorkDesktopClearUserInfoReq{
|
|
|
+ PositionId: positionId,
|
|
|
+ AppId: "10000",
|
|
|
+ })
|
|
|
+ config.Middleground.PowerCheckCenter.DelCheckRedis("10000", qutil.Int64All(positionId))
|
|
|
+}
|