package consume import ( "applet/app/db" "applet/app/db/model" "applet/app/utils" "applet/app/utils/logx" "applet/consume/md" "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" "encoding/json" "errors" "fmt" "github.com/streadway/amqp" "time" "xorm.io/xorm" ) func ZhiosAcquisitionCondition(queue md.MqQueue) { fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>") ch, err := rabbit.Cfg.Pool.GetChannel() if err != nil { logx.Error(err) return } defer ch.Release() //1、将自己绑定到交换机上 ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey) //2、取出数据进行消费 ch.Qos(300) delivery := ch.Consume(queue.Name, false) var res amqp.Delivery var ok bool for { res, ok = <-delivery if ok == true { //fmt.Println(string(res.Body)) fmt.Println(">>>>>>>>>>>>>>>>ZhiosAcquisitionCondition<<<<<<<<<<<<<<<<<<<<<<<<<") err = handleZhiosAcquisition(res.Body) //_ = res.Reject(false) fmt.Println(err) if err == nil { _ = res.Ack(true) } else { var canalMsg *md.ZhiosAcquisition var tmpString string err := json.Unmarshal(res.Body, &tmpString) if err == nil { fmt.Println(tmpString) err = json.Unmarshal([]byte(tmpString), &canalMsg) if err == nil { ch.Publish(queue.ExchangeName, utils.SerializeStr(canalMsg), queue.RoutKey) } } } } else { panic(errors.New("error getting message")) } } fmt.Println("get msg done") } func handleZhiosAcquisition(msg []byte) error { //1、解析canal采集至mq中queue的数据结构体 var canalMsg *md.ZhiosAcquisition fmt.Println(string(msg)) var tmpString string err := json.Unmarshal(msg, &tmpString) if err != nil { fmt.Println(err.Error()) return err } fmt.Println(tmpString) err = json.Unmarshal([]byte(tmpString), &canalMsg) if err != nil { return err } mid := canalMsg.Mid eg := db.DBs[mid] if eg == nil { return nil } if canalMsg.Uid == "" { return nil } userInfo, _ := db.UserFindByID(eg, canalMsg.Uid) if userInfo == nil { return nil } userProfile, _ := db.UserProfileFindByID(eg, canalMsg.Uid) if userProfile == nil { return nil } cfg := db.GetAcquisitionCfg(eg, canalMsg.Id, userInfo.CreateAt) if cfg == nil { return nil } nextUserProfile, _ := db.UserProfileFindByID(eg, userProfile.ParentUid) var user = &md.User{Info: userInfo, Profile: userProfile} bools, str := checkAllCompleteTmp(eg, user, cfg) isFull := 0 fullTime := 0 toRewardTime := 0 if bools { isFull = 1 fullTime = int(time.Now().Unix()) toRewardTime = int(time.Now().Unix()) + utils.StrToInt(cfg.RewardAccountDay)*86400 } //写入奖励记录 //新的设置 读第一个新注册的奖励 lv := 0 if len(cfg.RewardRule.LvRewardList) > 0 { isEnd := 0 for k, v := range cfg.RewardRule.LvRewardList { if isEnd == 1 { continue } if utils.StrToInt(v.Id) == user.Info.Level { isEnd = 1 } lv = utils.StrToInt(v.Id) InvitedReward := v.InvitedReward if cfg.RewardRule.RewardType == "1" { InvitedReward = Rands(v.InvitedReward, v.InvitedRewardMax) } //直推 DirectSuccess := v.DirectSuccess if cfg.RewardRule.RewardType == "1" { DirectSuccess = Rands(v.DirectSuccess, v.DirectSuccessMax) } //间推 IndirectSuccess := v.IndirectSuccess if cfg.RewardRule.RewardType == "1" { IndirectSuccess = Rands(v.IndirectSuccess, v.IndirectSuccessMax) } InvitedSource := 0 DirectSource := 1 IndirectSource := 2 InvitedSourceStr := "注册奖励" DirectSourceStr := "直推好友" IndirectSourceStr := "间推好友" if k > 0 { InvitedSource = 3 DirectSource = 4 IndirectSource = 5 InvitedSourceStr = "升级" + v.Name + "奖励" DirectSourceStr = "直推好友升级" + v.Name + "奖励" IndirectSourceStr = "间推好友升级" + v.Name + "奖励" } if utils.StrToFloat64(InvitedReward) > 0 { ownRewardLog, ownhas, _ := db.GetNewAcquisitionRewardLogWhere(eg, userProfile.Uid, user.Profile.Uid, lv) if !ownhas { ownRewardLog = &model.NewAcquisitionRewardLog{ Uid: user.Profile.Uid, ToUid: user.Profile.Uid, Title: user.Info.Nickname, Source: InvitedSource, SourceText: InvitedSourceStr, Money: InvitedReward, CreatedAt: int(time.Now().Unix()), State: 0, CoinId: utils.StrToInt(cfg.RewardRule.RewardCoinId), RewardType: utils.StrToInt(cfg.RewardRule.RewardType), InviteTime: int(userInfo.CreateAt.Unix()), Lv: lv, } db.InsertNewRewardLog(eg, ownRewardLog) } if ownRewardLog.GivenAt == 0 { ownRewardLog.CompleteCon = str ownRewardLog.IsFull = isFull if ownRewardLog.FullTime == 0 { ownRewardLog.FullTime = fullTime } if ownRewardLog.ToRewardTime == 0 { ownRewardLog.ToRewardTime = toRewardTime } eg.Where("id=?", ownRewardLog.Id).Update(ownRewardLog) } } if utils.StrToFloat64(DirectSuccess) > 0 { if userProfile.ParentUid > 0 { //写入奖励记录 extendRewardLog, extendHas, _ := db.GetNewAcquisitionRewardLogWhere(eg, userProfile.ParentUid, userProfile.Uid, lv) if !extendHas { extendRewardLog = &model.NewAcquisitionRewardLog{ Uid: user.Profile.ParentUid, ToUid: user.Profile.Uid, Title: user.Info.Nickname, Source: DirectSource, SourceText: DirectSourceStr, Money: DirectSuccess, CreatedAt: int(time.Now().Unix()), State: 0, CoinId: utils.StrToInt(cfg.RewardRule.RewardCoinId), RewardType: utils.StrToInt(cfg.RewardRule.RewardType), InviteTime: int(userInfo.CreateAt.Unix()), Lv: lv, } db.InsertNewRewardLog(eg, extendRewardLog) } if extendRewardLog.GivenAt == 0 { extendRewardLog.CompleteCon = str extendRewardLog.IsFull = isFull if extendRewardLog.FullTime == 0 { extendRewardLog.FullTime = fullTime } if extendRewardLog.ToRewardTime == 0 { extendRewardLog.ToRewardTime = toRewardTime } eg.Where("id=?", extendRewardLog.Id).Update(extendRewardLog) } } } if utils.StrToFloat64(IndirectSuccess) > 0 { if nextUserProfile != nil && nextUserProfile.ParentUid > 0 { IndirectRewardLog, IndirectHas, _ := db.GetNewAcquisitionRewardLogWhere(eg, nextUserProfile.ParentUid, userProfile.Uid, lv) if !IndirectHas { IndirectRewardLog = &model.NewAcquisitionRewardLog{ Uid: nextUserProfile.ParentUid, ToUid: user.Profile.Uid, Title: user.Info.Nickname, Source: IndirectSource, SourceText: IndirectSourceStr, Money: IndirectSuccess, CreatedAt: int(time.Now().Unix()), State: 0, CoinId: utils.StrToInt(cfg.RewardRule.RewardCoinId), RewardType: utils.StrToInt(cfg.RewardRule.RewardType), InviteTime: int(userInfo.CreateAt.Unix()), Lv: lv, } db.InsertNewRewardLog(eg, IndirectRewardLog) } if IndirectRewardLog.GivenAt == 0 { IndirectRewardLog.CompleteCon = str IndirectRewardLog.IsFull = isFull if IndirectRewardLog.FullTime == 0 { IndirectRewardLog.FullTime = fullTime } if IndirectRewardLog.ToRewardTime == 0 { IndirectRewardLog.ToRewardTime = toRewardTime } eg.Where("id=?", IndirectRewardLog.Id).Update(IndirectRewardLog) } } } } } else { InvitedReward := cfg.RewardRule.InvitedReward if cfg.RewardRule.RewardType == "1" { InvitedReward = Rands(cfg.RewardRule.InvitedReward, cfg.RewardRule.InvitedRewardMax) } //直推 DirectSuccess := cfg.RewardRule.DirectSuccess if cfg.RewardRule.RewardType == "1" { DirectSuccess = Rands(cfg.RewardRule.DirectSuccess, cfg.RewardRule.DirectSuccessMax) } //间推 IndirectSuccess := cfg.RewardRule.IndirectSuccess if cfg.RewardRule.RewardType == "1" { IndirectSuccess = Rands(cfg.RewardRule.IndirectSuccess, cfg.RewardRule.IndirectSuccessMax) } if utils.StrToFloat64(InvitedReward) > 0 { ownRewardLog, ownhas, _ := db.GetNewAcquisitionRewardLog(eg, &model.NewAcquisitionRewardLog{ Uid: user.Profile.Uid, ToUid: user.Profile.Uid, }) if !ownhas { ownRewardLog = &model.NewAcquisitionRewardLog{ Uid: user.Profile.Uid, ToUid: user.Profile.Uid, Title: user.Info.Nickname, Source: 0, SourceText: "注册奖励", Money: InvitedReward, CreatedAt: int(time.Now().Unix()), State: 0, CoinId: utils.StrToInt(cfg.RewardRule.RewardCoinId), RewardType: utils.StrToInt(cfg.RewardRule.RewardType), InviteTime: int(userInfo.CreateAt.Unix()), Lv: lv, } db.InsertNewRewardLog(eg, ownRewardLog) } if ownRewardLog.GivenAt == 0 { ownRewardLog.CompleteCon = str ownRewardLog.IsFull = isFull if ownRewardLog.FullTime == 0 { ownRewardLog.FullTime = fullTime } if ownRewardLog.ToRewardTime == 0 { ownRewardLog.ToRewardTime = toRewardTime } eg.Where("id=?", ownRewardLog.Id).Update(ownRewardLog) } } if utils.StrToFloat64(DirectSuccess) > 0 { if userProfile.ParentUid > 0 { //写入奖励记录 extendRewardLog, extendHas, _ := db.GetNewAcquisitionRewardLog(eg, &model.NewAcquisitionRewardLog{ Uid: userProfile.ParentUid, ToUid: userProfile.Uid, }) if !extendHas { extendRewardLog = &model.NewAcquisitionRewardLog{ Uid: user.Profile.ParentUid, ToUid: user.Profile.Uid, Title: user.Info.Nickname, Source: 1, SourceText: "直推好友", Money: DirectSuccess, CreatedAt: int(time.Now().Unix()), State: 0, CoinId: utils.StrToInt(cfg.RewardRule.RewardCoinId), RewardType: utils.StrToInt(cfg.RewardRule.RewardType), InviteTime: int(userInfo.CreateAt.Unix()), Lv: lv, } db.InsertNewRewardLog(eg, extendRewardLog) } if extendRewardLog.GivenAt == 0 { extendRewardLog.CompleteCon = str extendRewardLog.IsFull = isFull if extendRewardLog.FullTime == 0 { extendRewardLog.FullTime = fullTime } if extendRewardLog.ToRewardTime == 0 { extendRewardLog.ToRewardTime = toRewardTime } eg.Where("id=?", extendRewardLog.Id).Update(extendRewardLog) } } } if utils.StrToFloat64(IndirectSuccess) > 0 { if nextUserProfile != nil && nextUserProfile.ParentUid > 0 { IndirectRewardLog, IndirectHas, _ := db.GetNewAcquisitionRewardLog(eg, &model.NewAcquisitionRewardLog{ Uid: nextUserProfile.ParentUid, ToUid: userProfile.Uid, }) if !IndirectHas { IndirectRewardLog = &model.NewAcquisitionRewardLog{ Uid: nextUserProfile.ParentUid, ToUid: user.Profile.Uid, Title: user.Info.Nickname, Source: 2, SourceText: "间推好友", Money: IndirectSuccess, CreatedAt: int(time.Now().Unix()), State: 0, CoinId: utils.StrToInt(cfg.RewardRule.RewardCoinId), RewardType: utils.StrToInt(cfg.RewardRule.RewardType), InviteTime: int(userInfo.CreateAt.Unix()), Lv: lv, } db.InsertNewRewardLog(eg, IndirectRewardLog) } if IndirectRewardLog.GivenAt == 0 { IndirectRewardLog.CompleteCon = str IndirectRewardLog.IsFull = isFull if IndirectRewardLog.FullTime == 0 { IndirectRewardLog.FullTime = fullTime } if IndirectRewardLog.ToRewardTime == 0 { IndirectRewardLog.ToRewardTime = toRewardTime } eg.Where("id=?", IndirectRewardLog.Id).Update(IndirectRewardLog) } } } } return nil } func Rands(minVal, maxVal string) string { min := int(utils.StrToFloat64(minVal) * 100) max := int(utils.StrToFloat64(maxVal) * 100) return utils.Float64ToStrByPrec(float64(utils.RandIntRand(min, max))/100, 3) } //判断是否符合条件 func checkAllCompleteTmp(eg *xorm.Engine, user *md.User, acqCfg *md.AcquisitionCfg) (bool, string) { res := true str := "" if acqCfg.SuccessConditions.Register.Open == "1" { res = res && AcqRegisterTmp(user, acqCfg) if res { str += ",Register" } } if acqCfg.SuccessConditions.TaobaoAuthorization.Open == "1" { res = res && AcqTaoBaoAuthTmp(user, acqCfg) if res { str += ",TaobaoAuthorization" } } if acqCfg.SuccessConditions.FirstOrder.Open == "1" { res = res && AcqFirstOrder(eg, user, acqCfg) if res { str += ",FirstOrder" } } if acqCfg.SuccessConditions.SelfOrder.Open == "1" { res = res && AcqSelfOrder(eg, user, acqCfg) if res { str += ",SelfOrder" } } if acqCfg.SuccessConditions.OrderPay.Open == "1" { res = res && AcqOrderPay(eg, user, acqCfg) if res { str += ",OrderPay" } } if len(str) > 0 { str = str[1:] } return res, str } func AcqRegisterTmp(user *md.User, acqCfg *md.AcquisitionCfg) bool { var startTime = utils.TimeStdParseUnix(acqCfg.StartTime) var endTime = utils.TimeStdParseUnix(acqCfg.EndTime) if startTime == 0 || endTime == 0 { return false } //时间不在活动范围之内返回false if user.Info.CreateAt.Unix() < startTime { return false } if user.Info.CreateAt.Unix() > endTime { return false } return true } func AcqTaoBaoAuthTmp(user *md.User, acqCfg *md.AcquisitionCfg) bool { if user.Profile.AccTaobaoAuthTime > 0 { return true } return false } func AcqFirstOrder(eg *xorm.Engine, user *md.User, acqCfg *md.AcquisitionCfg) bool { endTime := int(user.Info.CreateAt.Unix()) + utils.StrToInt(acqCfg.SuccessConditions.FirstOrder.Day)*86400 return commAmount(eg, utils.IntToStr(user.Info.Uid), endTime, 0, acqCfg) } func AcqSelfOrder(eg *xorm.Engine, user *md.User, acqCfg *md.AcquisitionCfg) bool { return commAmount(eg, utils.IntToStr(user.Info.Uid), 0, 2, acqCfg) } func AcqOrderPay(eg *xorm.Engine, user *md.User, acqCfg *md.AcquisitionCfg) bool { return commAmount(eg, utils.IntToStr(user.Info.Uid), 0, 1, acqCfg) } func sqlSelect(eg *xorm.Engine, uid string, endTime, types int, acqCfg *md.AcquisitionCfg, arr []string) int { sql := `SELECT COUNT(*) as count FROM %s ol LEFT JOIN %s olr on olr.oid=%s and olr.uid=%s WHERE ol.uid=? %s %s ` str := "" if endTime > 0 { str += " AND olr.create_at<=" + utils.IntToStr(endTime) } if types == 2 { str += " AND olr.amount>=" + acqCfg.SuccessConditions.SelfOrder.Money } if types == 1 { str += " AND %s>=" + acqCfg.SuccessConditions.OrderPay.Money str = fmt.Sprintf(str, arr[0]) } sqlOrd := fmt.Sprintf(sql, arr[1], arr[2], arr[3], arr[4], str, arr[5]) ordResult, err := db.QueryNativeString(eg, sqlOrd, uid) fmt.Println(sqlOrd) fmt.Println(err) count := 0 for _, v := range ordResult { count = utils.StrToInt(v["count"]) } return count } func commAmount(eg *xorm.Engine, uid string, endTime, types int, acqCfg *md.AcquisitionCfg) bool { state := "0,1,2,3,5" psoState := "'订单付款','订单结算'" mallState := "1,2,3" o2oState := "1,2,3,4" b2cState := "1,2,3,4" if acqCfg.SuccessConditions.FirstOrder.Status == "1" { state = "1,2,3,5" mallState = "2,3" o2oState = "2,3,4" b2cState = "2,3,4" } if acqCfg.SuccessConditions.FirstOrder.Status == "2" { state = "2,3,5" mallState = "2,3" o2oState = "2,3,4" b2cState = "2,3,4" } if acqCfg.SuccessConditions.FirstOrder.Status == "3" { state = "3,5" mallState = "3" o2oState = "3,4" b2cState = "3,4" psoState = "'订单结算'" } arr := []string{"ol.paid_price", "ord_list", "ord_list_relate", "ol.ord_id", "ol.uid", " and ol.state in(" + state + ")"} count := sqlSelect(eg, uid, endTime, types, acqCfg, arr) arr = []string{"ol.paid_price", "privilege_card_ord", "ord_list_relate", "ol.ord_id", "ol.uid", " and ol.state=1"} count += sqlSelect(eg, uid, endTime, types, acqCfg, arr) arr = []string{"ol.commission", "duoyou_ord_list", "ord_list_relate", "ol.oid", "ol.uid", " and ol.id>0"} count += sqlSelect(eg, uid, endTime, types, acqCfg, arr) arr = []string{"ol.amount", "recharge_order", "ord_list_relate", "ol.oid", "ol.uid", " and ol.status<>'已退款'"} count += sqlSelect(eg, uid, endTime, types, acqCfg, arr) arr = []string{"ol.amount", "playlet_sale_order", "ord_list_relate", "ol.custom_oid", "ol.uid", " and ol.status in(" + psoState + ")"} count += sqlSelect(eg, uid, endTime, types, acqCfg, arr) arr = []string{"ol.cost_price", "mall_ord", "mall_ord_list_relate", "ol.ord_id", "ol.uid", " and ol.state in(" + mallState + ")"} count += sqlSelect(eg, uid, endTime, types, acqCfg, arr) arr = []string{"ol.cost_price", "o2o_ord", "o2o_ord_list_relate", "ol.ord_id", "ol.uid", " and ol.state in(" + o2oState + ")"} count += sqlSelect(eg, uid, endTime, types, acqCfg, arr) arr = []string{"ol.actual_pay_amount", "o2o_pay_to_merchant", "o2o_ord_list_relate", "ol.pay_id", "ol.uid", " and ol.state >=1"} count += sqlSelect(eg, uid, endTime, types, acqCfg, arr) arr = []string{"ol.cost_price", "b2c_ord", "b2c_ord_list_relate", "ol.ord_id", "ol.uid", " and ol.state in(" + b2cState + ")"} count += sqlSelect(eg, uid, endTime, types, acqCfg, arr) if count > 0 { return true } return false }