package svc import ( "applet/app/db" "applet/app/db/model" "applet/app/md" "applet/app/utils" "applet/app/utils/logx" "fmt" "github.com/shopspring/decimal" "github.com/syyongx/php2go" "log" "strings" "time" "xorm.io/xorm" ) // 拉新活动 acquisition // 通用acq handle func commHandleAcq1(logText, acqText string, acqCfg *md.AcquisitionCfg, user *md.User, eg *xorm.Engine) { now := time.Now().Unix() startTime := utils.TimeParseStd(acqCfg.StartTime).Unix() endTime := utils.TimeParseStd(acqCfg.EndTime).Unix() //判断活动时间是否符合 if now < startTime || now > endTime || utils.StrToInt(acqCfg.Status) != 1 { return } //时间不符合条件关掉 if startTime == 0 || endTime == 0 || user.Info.CreateAt.Unix() < startTime || user.Info.CreateAt.Unix() > endTime { return } if user == nil || user.Profile.Uid == 0 { return } oneUser, err := db.UserFindByID(eg, user.Profile.Uid) if oneUser == nil || err != nil { return } //查出有没有奖励记录 acqRewardLog := &model.AcquisitionRewardLog{ ToUid: user.Profile.Uid, Source: 1, } acqRewardLog, has, err := db.GetAcquisitionRewardLog(eg, acqRewardLog) if err != nil { _ = logx.Warn(err) return } // 没有rewardLog的话插入 if !has { acqRewardLog = &model.AcquisitionRewardLog{ Uid: user.Profile.ParentUid, ToUid: user.Profile.Uid, Title: user.Info.Nickname, Source: 1, SourceText: "直推好友", Money: acqCfg.RewardRule.DirectSuccess, CreatedAt: int(time.Now().Unix()), State: 0, CoinId: utils.StrToInt(acqCfg.RewardRule.RewardCoinId), RewardType: utils.StrToInt(acqCfg.RewardRule.RewardType), } if acqCfg.RewardRule.RewardType == "1" { acqRewardLog.Money = Rands(acqCfg.RewardRule.DirectSuccess, acqCfg.RewardRule.DirectSuccessMax) } db.InsertRewardLog(eg, acqRewardLog) } // 拉新日志 acqLog, has, err := db.GetAcquisitionLog(eg, &model.AcquisitionLog{ Uid: user.Profile.Uid, }) // err 退出 if err != nil { _ = logx.Warn(err) return } if !has { // 没有拉新记录 var state int state = 0 acqLog = &model.AcquisitionLog{ ParentUid: user.Profile.ParentUid, Uid: user.Profile.Uid, InviteTime: time.Now(), State: state, CompleteCon: acqText, } db.InsertAcqLog(eg, acqLog) } else { // 有记录 completeCon := strings.Split(acqLog.CompleteCon, ",") completeCon = append(completeCon, acqText) completeConStr := php2go.Implode(",", completeCon) acqLog.CompleteCon = completeConStr db.UpdateAcqLog(eg, acqLog) } } // 拉新条件--首单条件 firstOrder func AcquisitionHookFirstOrder(eg *xorm.Engine, masterId string, ord *model.OrdList) { // 没有context用Db来查 log.Println("firstOrder hook start") cfg := db.SysCfgGetWithDb(eg, masterId, "acquisition_cfg") if cfg == "" { _ = logx.Warn("acqCfg error") return } var acqCfgLocal md.AcquisitionCfg utils.Unserialize([]byte(cfg), &acqCfgLocal) acqCfg := &acqCfgLocal user, err := db.UserAllInfoByUid(eg, ord.Uid) if err != nil { _ = logx.Warn(err) return } if user == nil { log.Println("user is nil") return } if user.Info == nil || user.Profile == nil { return } if user.Profile.ParentUid == 0 { log.Println("无上级") return } fmt.Println(utils.SerializeStr(acqCfg.SuccessConditions)) if acqCfg.SuccessConditions.FirstOrder.Open == "1" && utils.StrToInt(acqCfg.SuccessConditions.FirstOrder.Day) > 0 { now := utils.Int64ToStr(time.Now().Unix()) if (utils.StrToInt(now)-ord.CreateAt)/86400 > utils.StrToInt(acqCfg.SuccessConditions.FirstOrder.Day) { log.Println("超过天数") return } commHandleAcq1("首单条件开启", "firstOrder", acqCfg, user, eg) } if acqCfg.SuccessConditions.SelfOrder.Open == "1" { if ord.UserCommission < utils.AnyToFloat64(acqCfg.SuccessConditions.SelfOrder.Money) { log.Println("佣金不达标") return } commHandleAcq1("自购佣金条件开启", "selfOrder", acqCfg, user, eg) } if acqCfg.SuccessConditions.OrderPay.Open == "1" { if ord.PaidPrice < utils.AnyToFloat64(acqCfg.SuccessConditions.OrderPay.Money) { log.Println("支付金额不达标") return } commHandleAcq1("付款金额条件开启", "orderPay", acqCfg, user, eg) } if acqCfg.SuccessConditions.OrderStatus.Open == "1" { if ord.State < utils.StrToInt(acqCfg.SuccessConditions.OrderStatus.Status) { log.Println("订单状态不达标") return } commHandleAcq1("订单状态条件开启", "orderStatus", acqCfg, user, eg) } //检测有些记录之前没写入 checkAcqLogInsert(eg, acqCfg, user) return } //检测有些记录之前没写入 func checkAcqLogInsert(eg *xorm.Engine, acqCfg *md.AcquisitionCfg, user *md.User) { now := time.Now().Unix() startTime := utils.TimeParseStd(acqCfg.StartTime).Unix() endTime := utils.TimeParseStd(acqCfg.EndTime).Unix() //判断活动时间是否符合 if now < startTime || now > endTime || utils.StrToInt(acqCfg.Status) != 1 { return } //时间不符合条件关掉 if startTime == 0 || endTime == 0 || user.Info.CreateAt.Unix() < startTime || user.Info.CreateAt.Unix() > endTime { return } //判断有没有记录 acqLog, _, _ := db.GetAcquisitionLog(eg, &model.AcquisitionLog{ Uid: user.Profile.Uid, }) var acqText string completeCon := strings.Split(acqLog.CompleteCon, ",") completeCon = append(completeCon, acqText) //completeConStr := php2go.Implode(",", completeCon) if acqCfg.SuccessConditions.Register.Open == "1" { t := php2go.InArray("register", completeCon) if t == false { commHandleAcq1("注册条件开启", "register", acqCfg, user, eg) } } if acqCfg.SuccessConditions.BindPhone.Open == "1" { t := php2go.InArray("bindPhone", completeCon) if t == false { commHandleAcq1("绑定手机开启", "bindPhone", acqCfg, user, eg) } } if acqCfg.SuccessConditions.TaobaoAuthorization.Open == "1" { t := php2go.InArray("taobaoAuth", completeCon) if t == false { commHandleAcq1("淘宝授权条件开启", "taobaoAuth", acqCfg, user, eg) } } } func Rands(minVal, maxVal string) string { min := int(utils.StrToFloat64(minVal) * 100) max := int(utils.StrToFloat64(maxVal) * 100) return utils.Float64ToStrByPrec(float64(utils.RandInt(min, max))/100, 3) } // 发送直接奖励,间推奖励 ,插入奖励记录 func SendCommReward(eg *xorm.Engine, cfg *md.AcquisitionCfg, user *md.User, rewardLog *model.AcquisitionRewardLog) { if rewardLog == nil { return } if rewardLog.IsFrozen == 1 { return } now := time.Now().Unix() startTime := utils.TimeParseStd(cfg.StartTime).Unix() endTime := utils.TimeParseStd(cfg.EndTime).Unix() //判断活动时间是否符合 if now < startTime || now > endTime || utils.StrToInt(cfg.Status) != 1 { return } //时间不符合条件关掉 if startTime == 0 || endTime == 0 || user.Info.CreateAt.Unix() < startTime || user.Info.CreateAt.Unix() > endTime { return } //判断是否发放过 list, err := db.GetFinUserFlowByOIDANDORDTYPE(eg, "11", utils.IntToStr(rewardLog.Id), "acq") if err != nil || list != nil { return } var delUser = model.UserDeleteInfo{} eg.Where("phone=?", user.Info.Phone).Get(&delUser) if delUser.Id > 0 { return } rewardLog.CoinId = utils.StrToInt(cfg.RewardRule.RewardCoinId) rewardLog.RewardType = utils.StrToInt(cfg.RewardRule.RewardType) InvitedReward := cfg.RewardRule.InvitedReward DirectSuccess := cfg.RewardRule.DirectSuccess IndirectSuccess := cfg.RewardRule.IndirectSuccess if cfg.RewardRule.RewardType == "1" { InvitedReward = Rands(cfg.RewardRule.InvitedReward, cfg.RewardRule.InvitedRewardMax) DirectSuccess = Rands(cfg.RewardRule.DirectSuccess, cfg.RewardRule.DirectSuccessMax) IndirectSuccess = Rands(cfg.RewardRule.IndirectSuccess, cfg.RewardRule.IndirectSuccessMax) } // 被邀请人 sendAcqFin(eg, user.Profile.Uid, InvitedReward, user.Profile.Uid, rewardLog.Id, cfg.RewardRule.RewardCoinId) // 直推 _, _ = db.UpdateRewardLog(eg, rewardLog) sendAcqFin(eg, user.Profile.ParentUid, DirectSuccess, user.Profile.Uid, rewardLog.Id, cfg.RewardRule.RewardCoinId) // 间推 nextUserRelate, err := db.UserProfileFindByID(eg, user.Profile.ParentUid) if err != nil { _ = logx.Warn(err) return } if nextUserRelate.ParentUid == 0 { return } db.InsertRewardLog(eg, &model.AcquisitionRewardLog{ Uid: nextUserRelate.ParentUid, ToUid: user.Profile.Uid, Title: user.Info.Nickname, Source: 2, SourceText: "间推好友", Money: IndirectSuccess, CreatedAt: int(time.Now().Unix()), GivenAt: int(time.Now().Unix()), State: 1, CoinId: utils.StrToInt(cfg.RewardRule.RewardCoinId), RewardType: utils.StrToInt(cfg.RewardRule.RewardType), }) sendAcqFin(eg, nextUserRelate.ParentUid, IndirectSuccess, user.Profile.Uid, rewardLog.Id, cfg.RewardRule.RewardCoinId) } func sendAcqFin(eg *xorm.Engine, uid interface{}, money string, firstUid int, id int, rewardCoinId string) { if uid == 0 { return } // 发放余额 user, err := db.UserAllInfoByUid(eg, uid) if err != nil { _ = logx.Warn(err) return } if user == nil { //用户可能不见了,但是关系链还存在 return } if utils.StrToInt(rewardCoinId) > 0 { userVirtualCoin := db.GetUserVirtualCoinAmount(eg, int(utils.AnyToInt64(uid)), rewardCoinId) if userVirtualCoin == nil { userVirtualCoin = &model.UserVirtualAmount{CoinId: utils.StrToInt(rewardCoinId), Uid: int(utils.AnyToInt64(uid))} eg.Insert(userVirtualCoin) } finValid := utils.AnyToFloat64(userVirtualCoin.Amount) userVirtualCoin.Amount = utils.AnyToString(finValid + utils.StrToFloat64(money)) affect, err := db.UserVirtualCoinAmountUpdate(eg, userVirtualCoin.Id, userVirtualCoin, "amount") if err != nil || affect != 1 { return } db.NewUserVirtualCoinFlowInsert(eg, int(utils.AnyToInt64(uid)), 1, utils.StrToInt(rewardCoinId), "", "拉新奖励", 180, money, utils.Float64ToStr(finValid), userVirtualCoin.Amount) } else { moneyA, _ := decimal.NewFromString(money) moneyB, _ := decimal.NewFromString(user.Profile.FinValid) newM := moneyA.Add(moneyB) user.Profile.FinValid = newM.String() _, _ = db.UserProfileUpdate(eg, uid, user.Profile, "fin_valid") if err := db.FinUserFlowInsertOne( eg, &model.FinUserFlow{ Type: 0, SysFee: "0", Uid: user.Profile.Uid, Amount: money, BeforeAmount: utils.Float64ToStr(utils.StrToFloat64(user.Profile.FinValid) - utils.StrToFloat64(money)), AfterAmount: newM.String(), OrdType: "acq", OrdAction: 11, OrdTitle: "拉新奖励", OrdTime: int(time.Now().Unix()), State: 2, OrdDetail: utils.IntToStr(firstUid), OrdId: utils.IntToStr(id), }); err != nil { _ = logx.Warn(err) return } } }