package zyos_go_condition_hdl import ( "code.fnuoos.com/go_rely_warehouse/zyos_go_condition_statistics.git/db" "code.fnuoos.com/go_rely_warehouse/zyos_go_condition_statistics.git/db/model" "code.fnuoos.com/go_rely_warehouse/zyos_go_condition_statistics.git/md" zhios_condition_statistics_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_condition_statistics.git/utils" "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles" "errors" "fmt" "time" "xorm.io/xorm" ) func CommUpdate(blockStarChain string, eg *xorm.Engine, dbName string, user *model.User, DataDetail md.DataDetail) (error, bool, int) { uid := DataDetail.Uid //判断有没有审核通过或审核中的记录 has, err := db.UserLevelAuditingFindByUid(eg, zhios_condition_statistics_utils.StrToInt(uid)) if err != nil { return err, false, 0 } if has != nil { var msg = "您已审核通过,请勿重复申请" if has.State == 1 { msg = "正在审核中,请稍后再查看" } return errors.New(msg), false, 0 } lv := user.Level result, AutoAudit := CommUplv(blockStarChain, eg, dbName, user, lv, DataDetail) if result == false { return errors.New("升级失败,请查看任务是否完成"), false, AutoAudit } return nil, result, AutoAudit } func CommUplv(blockStarChain string, eg *xorm.Engine, dbName string, user *model.User, lv int, DataDetail md.DataDetail) (bool, int) { levelExpireAt := user.LevelExpireAt levelArriveAt := user.LevelArriveAt oldLevel := user.Level uid := zhios_condition_statistics_utils.IntToStr(user.Uid) result, _, payOrdId := IsCompleteSchemeTask(blockStarChain, eg, user, DataDetail.AgentType, DataDetail.IsChangeTime, dbName) AutoAudit := 0 if result { level, err := db.UserLevelByID(eg, zhios_condition_statistics_utils.StrToInt(DataDetail.AgentType)) if err != nil || level == nil { return false, 0 } date := time.Now() var auditData = model.UserLevelAudit{ Uid: zhios_condition_statistics_utils.StrToInt(uid), CurrentLevelId: lv, NextLevelId: zhios_condition_statistics_utils.StrToInt(DataDetail.AgentType), ConditionType: 2, AutoAudit: level.AutoAudit, DateType: 5, State: 1, RelateOid: payOrdId, CreateAt: date, UpdateAt: date, LevelDate: level.LevelDate, } has, err := db.UserLevelAuditInsertWithEngine(eg, &auditData) if has == 0 || err != nil { return false, 0 } //自动审核 if level.AutoAudit == 1 { //写入日志 var log = &model.UserLevelChangeLog{ Uid: user.Uid, BeforeLv: oldLevel, AfterLv: zhios_condition_statistics_utils.StrToInt(DataDetail.AgentType), Time: int(time.Now().Unix()), BeforeLevelExpireAt: levelExpireAt, AfterLevelExpireAt: user.LevelExpireAt, BeforeLevelArriveAt: levelArriveAt, AfterLevelArriveAt: user.LevelArriveAt, Type: "level_up", } fmt.Println(log) eg.InsertOne(log) auditData.State = 2 //审核记录修改 if _, err := db.UserLevelAuditUpdateById(eg, auditData.Id, &auditData, "state"); err != nil { return false, 0 } t := time.Now() var expire time.Time //订单时间修改 if payOrdId > 0 { ord, err := db.UserLevelOrderById(eg, zhios_condition_statistics_utils.Int64ToStr(payOrdId)) if err != nil { return false, 0 } //过期时间在当前时间之前 并且 创建时间跟过期时间不相等 相等的话是永久会员 if ord.ExpireAt.Unix() < t.Unix() && ord.CreateAt.Format("2006-01-02 15:04:05") != ord.ExpireAt.Format("2006-01-02 15:04:05") && ord.DateType != 4 { return false, 0 } switch ord.DateType { case 1: // 月 expire = t.AddDate(0, 1, 0) ord.ExpireAt = expire case 2: // 季 expire = t.AddDate(0, 3, 0) ord.ExpireAt = expire case 3: // 年 expire = t.AddDate(1, 0, 0) ord.ExpireAt = expire case 4: expire = t // 永久(设置为与创建时间相同) ord.ExpireAt = t } if _, err := db.UserLevelOrdUpdateOne(eg, ord); err != nil { return false, 0 } } user.Level = zhios_condition_statistics_utils.StrToInt(DataDetail.AgentType) user.LevelArriveAt = t if user.FirstArriveAt.IsZero() { user.FirstArriveAt = t } if payOrdId > 0 { user.LevelExpireAt = expire } else { if level.LevelDate == 0 { // 永久 user.LevelExpireAt = t } else { user.LevelExpireAt = t.AddDate(0, level.LevelDate, 0) } } //用户等级修改 // 更新user level 和 过期时间 if _, err := db.UserUpdate(eg, user.Uid, user, "level,level_expire_at,level_arrive_at,first_arrive_at"); err != nil { return false, 0 } eg.Where("uid=?", user.Uid).Cols("date").Update(&model.OneCirclesUserLvTime{Date: time.Now()}) AutoAudit = 1 } } return result, AutoAudit } // IsCompleteSchemeTask 根据传入的 level 判断是否可以升级 func IsCompleteSchemeTask(blockStarChain string, engine *xorm.Engine, user *model.User, level, IsChangeTime, dbName string) (bool, bool, int64) { uid := zhios_condition_statistics_utils.IntToStr(user.Uid) userLevel, err := db.UserLevelByID(engine, user.Level) if err != nil { return false, false, 0 } becomeLevel, err := db.UserLevelByID(engine, level) if err != nil { return false, false, 0 } if userLevel == nil { if level == "" { level = "1" } } else { if becomeLevel.LevelWeight <= userLevel.LevelWeight && IsChangeTime != "1" { return false, false, 0 } } //获取用户等级方案内容 schemeTasks, err := db.BatchSelectUserLevelUpgradeTasks(engine, map[string]interface{}{ "key": "scheme_id", "value": becomeLevel.Id, }) if err != nil { return false, false, 0 } return CheckSchemeTasksComplete(blockStarChain, engine, schemeTasks, uid, becomeLevel, level, "", dbName) } func CheckSchemeTasksComplete(blockStarChain string, engine *xorm.Engine, tasks *[]model.UserLevelUpgradeTask, uid interface{}, scheme *model.UserLevel, level string, regionalId, dbName string) (bool, bool, int64) { //分离出 必做/选做 途径(任务组) && 分离出 必做/选做 var tasksGroup []map[string]interface{} mustTasks := make(map[string][]interface{}) selectTasks := make(map[string][]interface{}) var ( hasLevelPay = false LevelPayOrdId int64 ) allLvMap := db.UserLevelByAllMap(engine) for _, item := range *tasks { var temp = map[string]interface{}{} temp["id"] = item.Id temp["pid"] = item.Pid temp["schemeId"] = item.SchemeId temp["isMustTask"] = item.IsMustTask temp["taskType"] = item.TaskType temp["taskTypeLevelId"] = item.TaskTypeLevelId temp["withinDays"] = item.WithinDays temp["finishCount"] = item.FinishCount temp["payLevels"] = item.PayLevels temp["finish_task_num"] = item.FinishTaskNum if item.Pid == 0 && item.IsMustTask == 1 { temp["is_must"] = "1" tasksGroup = append(tasksGroup, temp) } if item.Pid == 0 && item.IsMustTask != 1 { temp["is_must"] = "0" tasksGroup = append(tasksGroup, temp) } name := GetTaskName(item.TaskType) finish, hasPay, payOrdId := GetFinishCount(blockStarChain, engine, uid, &item, level, regionalId, allLvMap, dbName) total := "" // 总共需完成的数 if item.TaskType == 6 { // 当是付费条件是否,total的指标只能是1 total = "1" hasLevelPay = hasPay LevelPayOrdId = payOrdId } else { total = item.FinishCount } if item.TaskType == 19 { total = zhios_condition_statistics_utils.IntToStr(zhios_condition_statistics_utils.StrToInt(total) + 1) } temp["name"] = name temp["total"] = total //需完成数 temp["finish"] = finish //已完成数 //fmt.Println("finish:::::::::::::", finish) //fmt.Println("total:::::::::::::", total) if zhios_condition_statistics_utils.AnyToFloat64(finish) >= zhios_condition_statistics_utils.AnyToFloat64(total) { temp["result"] = "1" } else { temp["result"] = "0" } if item.Pid != 0 && item.IsMustTask == 1 { mustTasks[zhios_condition_statistics_utils.AnyToString(item.Pid)] = append(mustTasks[zhios_condition_statistics_utils.AnyToString(item.Pid)], temp) } if item.Pid != 0 && item.IsMustTask == 0 { selectTasks[zhios_condition_statistics_utils.AnyToString(item.Pid)] = append(selectTasks[zhios_condition_statistics_utils.AnyToString(item.Pid)], temp) } } alreadyCompleteTasksGroup := 0 // 完成“选做途径”数量 for _, v := range tasksGroup { fmt.Println(">>>>>>>>>>>>>>>>>>>>", v) if v["finish_task_num"] == nil { v["finishCount"] = 0 } //处理必做途径 if zhios_condition_statistics_utils.AnyToString(v["isMustTask"]) == "1" { if mustTasks[zhios_condition_statistics_utils.AnyToString(v["id"])] != nil { needFinishTaskNum := v["finish_task_num"] // 需完成选做任务数 alreadyFinishTaskNum := 0 // 已完成选做任务数 for _, vv := range mustTasks[zhios_condition_statistics_utils.AnyToString(v["id"])] { tmp := vv.(map[string]interface{}) // 必做任务 且 未完成 if tmp["isMustTask"] == 1 && tmp["result"] != "1" { return false, hasLevelPay, LevelPayOrdId } // 非必做任务 且 已完成 if tmp["isMustTask"] != 1 && tmp["result"] == "1" { alreadyFinishTaskNum++ } } // 需完成选做任务数 大于 已完成选做任务数 if zhios_condition_statistics_utils.AnyToInt64(needFinishTaskNum) > zhios_condition_statistics_utils.AnyToInt64(alreadyFinishTaskNum) { return false, hasLevelPay, LevelPayOrdId } } } //处理选做途径 if selectTasks[zhios_condition_statistics_utils.AnyToString(v["id"])] != nil { needFinishTaskNum := v["finish_task_num"] // 需完成选做任务数 alreadyFinishTaskNum := 0 // 已完成选做任务数 alreadyFinishMustTask := true // 是否已完成必做任务 for _, vv := range selectTasks[zhios_condition_statistics_utils.AnyToString(v["id"])] { tmp := vv.(map[string]interface{}) // 非必做任务 且 已完成 if tmp["isMustTask"] != "1" && tmp["result"] == "1" { alreadyFinishTaskNum++ } // 必做任务 且 未完成 if tmp["isMustTask"] == "1" && tmp["result"] != "1" { alreadyFinishMustTask = false } } // 已完成必做任务 且 需完成选做任务数 小于等于 已完成选做任务数 if alreadyFinishMustTask && zhios_condition_statistics_utils.AnyToInt64(needFinishTaskNum) <= zhios_condition_statistics_utils.AnyToInt64(alreadyFinishTaskNum) { alreadyCompleteTasksGroup++ } } } //方案中设定的 需完成“选做途径”数 与 实际完成“选做途径”数 比较 if scheme.ChoosableNum > alreadyCompleteTasksGroup { return false, hasLevelPay, LevelPayOrdId } return true, hasLevelPay, LevelPayOrdId } // GetTaskName is 获取任务名 func GetTaskName(id int) string { switch id { case 1: return "累计自购订单总数" case 2: return "累计到账佣金" case 3: return "累计直推粉丝数量" case 4: return "累计团队有效直推人数" case 5: return "累计团队符合相应等级的人数" case 11: return "累计直推符合相应等级的人数" case 6: return "月付价格" case 7: return "季付价格" case 8: return "年付价格" case 9: return "永久价格" case 10: return "累计自购消费金额" case 12: return "累计团队消费金额" case 13: return "累计小市场团队符合等级人数" case 15: return "直推前N个店铺营业额" case 17: return "团队N条线中各有N个相应等级用户" case 18: return "小区累计自营消费金额" case 20: return "累计收益(结算)" case 22: return "连续活跃" default: return "无效名称" } } // // GetFinishCount is 计算规定时间内完成的指标的数量 func GetFinishCount(blockStarChain string, engine *xorm.Engine, uid interface{}, task *model.UserLevelUpgradeTask, level string, regionalId string, allLvMap map[int]*model.UserLevel, dbName string) (string, bool, int64) { t := time.Now() hasPay := false var payOrdId int64 switch task.TaskType { case 20: sum, _ := engine.Where("type=0 and uid=?", uid).Sum(&model.FinUserFlow{}, "amount") mallSum, _ := engine.Where("uid=? AND type=? and ord_title=?", uid, 1, "订单退款").Sum(&model.FinUserFlow{}, "amount") deSum2, _ := engine.Where("uid=? AND type=? and ord_type=?", uid, 0, "withdraw").Sum(&model.FinUserFlow{}, "amount") deSum3, _ := engine.Where("uid=? AND type=? and ord_title like ?", uid, 1, "%扣除%").Sum(&model.FinUserFlow{}, "amount") dsCheck, _ := db.SysCfgGetOne(engine, "ds_check") money := sum - deSum2 - deSum3 + mallSum if dsCheck != nil && dsCheck.Val == "1" { withdrawingSuccess, _ := engine.Where("uid=? ", uid).In("state", []string{"1", "2"}).Sum(&model.FinWithdrawApply{}, "amount") sql := `SELECT cast(SUM(LEFT(olr.amount,LENGTH(olr.amount)-2)) as decimal(50,4)) AS amount FROM ord_list_relate olr JOIN ord_list ol ON olr.oid = ol.ord_id WHERE olr.level=0 and olr.uid = " . $uid . " AND ol.state IN (0,1,2,3) and ol.is_transfer=1;` nativeString, _ := db.QueryNativeString(engine, sql) money = withdrawingSuccess for _, v := range nativeString { money += zhios_condition_statistics_utils.StrToFloat64(v["amount"]) } } return zhios_condition_statistics_utils.Float64ToStr(money), hasPay, payOrdId case 22: lvTime := db.GetOneCirclesUserLvTime(engine, uid) stime := time.Date(t.Year(), t.Month(), t.Day()-1, 0, 0, 0, 0, t.Location()).Format("2006-01-02 15:04:05") if lvTime != nil && lvTime.Date.IsZero() == false { stime = lvTime.Date.Format("2006-01-02 15:04:05") } etime := t.Format("2006-01-02 15:04:05") _, days, activity := one_circles.CalcUserContinuousDailyActivityDays(engine, dbName, int(zhios_condition_statistics_utils.AnyToInt64(uid)), stime, etime) if activity == false { days = 0 } return zhios_condition_statistics_utils.IntToStr(days), hasPay, payOrdId case 1: // 累计自购数 total, err := db.OrderListCountByUIDByOrderTypeByTimeNew(engine, uid, task) if err != nil { return "", hasPay, payOrdId } mallTotal, err := db.MallOrdWithCountNew(engine, uid, task) if err != nil { return zhios_condition_statistics_utils.Int64ToStr(total), hasPay, payOrdId } total += mallTotal o2oTotal, err := db.O2oOrdWithCountNew(engine, uid, task) if err != nil { return zhios_condition_statistics_utils.Int64ToStr(total), hasPay, payOrdId } total += o2oTotal b2cTotal, err := db.B2cOrdWithCountNew(engine, uid, task) if err != nil { return zhios_condition_statistics_utils.Int64ToStr(total), hasPay, payOrdId } total += b2cTotal return zhios_condition_statistics_utils.Int64ToStr(total), hasPay, payOrdId case 10: // 累计自购消费金额 var total float64 = 0 var err error if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "1"}) { total, err = db.OrderListSumByUIDByOrderTypeByTimeNew(engine, uid, task) if err != nil { return "", hasPay, payOrdId } } if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "2", "8", "9"}) { orderType := 0 if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"8", "9"}) { orderType = task.TaskTypePvd } secondTotal, err := db.MallOrdWithSumNew(engine, uid, orderType, task) if err != nil { return zhios_condition_statistics_utils.Float64ToStr(total), hasPay, payOrdId } total += secondTotal } if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "3"}) { secondTotal, err := db.O2oOrdWithSumNew(engine, uid, task) if err != nil { return zhios_condition_statistics_utils.Float64ToStr(total), hasPay, payOrdId } total += secondTotal } if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "4"}) { secondTotal, err := db.B2cOrdWithSumNew(engine, uid, task) if err != nil { return zhios_condition_statistics_utils.Float64ToStr(total), hasPay, payOrdId } total += secondTotal } return zhios_condition_statistics_utils.Float64ToStr(zhios_condition_statistics_utils.FloatFormat(total, 2)), hasPay, payOrdId case 2: // 累计自购佣金 sum, err := db.OrderRelateListByTimeByStateNew(engine, uid, task) if err != nil { return "", hasPay, payOrdId } mallSum, err := db.MallOrderRelateListByTimeByStateNew(engine, uid, task) if err != nil { return zhios_condition_statistics_utils.Float64ToStr(sum), hasPay, payOrdId } sum += mallSum o2oSum, err := db.O2oOrderRelateListByTimeByStateNew(engine, uid, task) if err != nil { return zhios_condition_statistics_utils.Float64ToStr(sum), hasPay, payOrdId } sum += o2oSum b2cSum, err := db.B2cOrderRelateListByTimeByStateNew(engine, uid, task) if err != nil { return zhios_condition_statistics_utils.Float64ToStr(sum), hasPay, payOrdId } sum += b2cSum return zhios_condition_statistics_utils.Float64ToStr(sum), hasPay, payOrdId case 3: // 累计直推人数 stime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()).Format("2006-1-02 15:04:05") etime := t.Format("2006-1-02 15:04:05") sqlTpl := `SELECT count(*) as count FROM user_relate as up LEFT JOIN user as u ON u.uid=up.uid WHERE %s and up.level=1;` var condStr string if task.WithinDays <= 0 { condStr = fmt.Sprintf("up.parent_uid=%s ", zhios_condition_statistics_utils.AnyToString(uid)) } else { condStr = fmt.Sprintf("up.parent_uid=%s AND u.create_at>'%s' AND u.create_at<'%s'", zhios_condition_statistics_utils.AnyToString(uid), stime, etime) } sql := fmt.Sprintf(sqlTpl, condStr) results, err := db.QueryNativeString(engine, sql) if err != nil { return "", hasPay, payOrdId } count := "" if len(results) > 0 { count = zhios_condition_statistics_utils.AnyToString(results[0]["count"]) } return count, hasPay, payOrdId case 4: // 累计团队有效直推人数 stime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()).Format("2006-1-02 15:04:05") etime := t.Format("2006-1-02 15:04:05") sqlTpl := `SELECT count(*) as count FROM user_relate as up LEFT JOIN user as u ON u.uid=up.uid LEFT JOIN user_profile as ups ON up.uid=ups.uid WHERE %s and up.level=1;` var condStr string if task.WithinDays <= 0 { condStr = fmt.Sprintf("up.parent_uid=%s and ups.is_verify=%d", zhios_condition_statistics_utils.AnyToString(uid), 1) } else { condStr = fmt.Sprintf("up.parent_uid=%s AND u.create_at>'%s' AND u.create_at<'%s' and ups.is_verify=%d", zhios_condition_statistics_utils.AnyToString(uid), stime, etime, 1) } sql := fmt.Sprintf(sqlTpl, condStr) results, err := db.QueryNativeString(engine, sql) if err != nil { return "", hasPay, payOrdId } count := "" if len(results) > 0 { count = zhios_condition_statistics_utils.AnyToString(results[0]["count"]) } return count, hasPay, payOrdId case 5: // 累计团队符合相应等级的人数 stime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()).Format("2006-1-02 15:04:05") etime := t.Format("2006-1-02 15:04:05") sqlTpl := `SELECT count(*) as count FROM user_relate as ur LEFT JOIN user as u ON u.uid=ur.uid LEFT JOIN user_level ul on u.level=ul.id WHERE %s;` levelList := allLvMap[task.TaskTypeLevelId] levelWeight := 0 if levelList != nil { levelWeight = levelList.LevelWeight } var condStr string if task.WithinDays <= 0 { condStr = fmt.Sprintf("ur.parent_uid=%s AND ul.level_weight>='%s' ", zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(levelWeight)) if blockStarChain == "1" { condStr = fmt.Sprintf("ur.parent_uid=%s AND ul.level_weight>='%s' ", zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(levelWeight)) } } else { condStr = fmt.Sprintf("ur.parent_uid=%s AND ul.level_weight>='%s' AND u.create_at>'%s' AND u.create_at<'%s'", zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(levelWeight), stime, etime) if blockStarChain == "1" { condStr = fmt.Sprintf("ur.parent_uid=%s AND ul.level_weight>='%s' AND u.level_arrive_at>'%s' AND u.level_arrive_at<'%s'", zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(levelWeight), stime, etime) } } sql := fmt.Sprintf(sqlTpl, condStr) fmt.Println(sql) results, err := db.QueryNativeString(engine, sql) if err != nil { return "", hasPay, payOrdId } count := "" if len(results) > 0 { count = zhios_condition_statistics_utils.AnyToString(results[0]["count"]) } return count, hasPay, payOrdId case 11: // 累计直推符合相应等级的人数 stime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()).Format("2006-1-02 15:04:05") etime := t.Format("2006-1-02 15:04:05") sqlTpl := `SELECT count(*) as count FROM user_relate as ur LEFT JOIN user as u ON u.uid=ur.uid LEFT JOIN user_level ul on u.level=ul.id WHERE %s;` levelList := allLvMap[task.TaskTypeLevelId] levelWeight := 0 if levelList != nil { levelWeight = levelList.LevelWeight } var condStr string if task.WithinDays <= 0 { condStr = fmt.Sprintf("ur.parent_uid=%s AND ur.level=%s AND ul.level_weight>='%s' ", zhios_condition_statistics_utils.AnyToString(uid), "1", zhios_condition_statistics_utils.IntToStr(levelWeight)) if blockStarChain == "1" { condStr = fmt.Sprintf("ur.parent_uid=%s AND ur.level=%s AND ul.level_weight>='%s' ", zhios_condition_statistics_utils.AnyToString(uid), "1", zhios_condition_statistics_utils.IntToStr(levelWeight)) } } else { condStr = fmt.Sprintf("ur.parent_uid=%s AND ur.level=%s AND ul.level_weight>='%s' AND u.create_at>'%s' AND u.create_at<'%s'", zhios_condition_statistics_utils.AnyToString(uid), "1", zhios_condition_statistics_utils.IntToStr(levelWeight), stime, etime) if blockStarChain == "1" { condStr = fmt.Sprintf("ur.parent_uid=%s AND ur.level=%s AND ul.level_weight>='%s' AND u.level_arrive_at>'%s' AND u.level_arrive_at<'%s'", zhios_condition_statistics_utils.AnyToString(uid), "1", zhios_condition_statistics_utils.IntToStr(levelWeight), stime, etime) } } sql := fmt.Sprintf(sqlTpl, condStr) fmt.Println(sql) results, err := db.QueryNativeString(engine, sql) if err != nil { return "", hasPay, payOrdId } count := "" if len(results) > 0 { count = zhios_condition_statistics_utils.AnyToString(results[0]["count"]) } return count, hasPay, payOrdId case 21: // 累计所有下级 stime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()).Format("2006-1-02 15:04:05") etime := t.Format("2006-1-02 15:04:05") sqlTpl := `SELECT count(*) as count FROM user_relate as ur LEFT JOIN user as u ON u.uid=ur.uid WHERE %s;` var condStr string if task.WithinDays <= 0 { condStr = fmt.Sprintf("ur.parent_uid=%s ", zhios_condition_statistics_utils.AnyToString(uid)) } else { condStr = fmt.Sprintf("ur.parent_uid=%s AND u.create_at>'%s' AND u.create_at<'%s'", zhios_condition_statistics_utils.AnyToString(uid), stime, etime) } sql := fmt.Sprintf(sqlTpl, condStr) fmt.Println(sql) results, err := db.QueryNativeString(engine, sql) if err != nil { return "", hasPay, payOrdId } count := "" if len(results) > 0 { count = zhios_condition_statistics_utils.AnyToString(results[0]["count"]) } return count, hasPay, payOrdId case 12: //累计团队消费金额 var total float64 = 0 if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "2", "8", "9"}) { secondTotal, _ := db.MallOrderTeamRelateListByTimeByStateNew(engine, uid, task) total += secondTotal } if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "3"}) { secondTotal, _ := db.O2oOrderTeamRelateListByTimeByStateNew(engine, uid, task) total += secondTotal } if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "4"}) { secondTotal, _ := db.B2cOrderTeamRelateListByTimeByStateNew(engine, uid, task) total += secondTotal } if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "1"}) { secondTotal, _ := db.OrderTeamRelateListByTimeByStateNew(engine, uid, task) total += secondTotal } return zhios_condition_statistics_utils.Float64ToStr(zhios_condition_statistics_utils.FloatFormat(total, 2)), hasPay, payOrdId case 13: //小市场团队符合条件人数 smallUid := TotalSmallTeam(engine, zhios_condition_statistics_utils.AnyToString(uid)) sqlTpl := `SELECT count(*) as count FROM user_relate as ur LEFT JOIN user as u ON u.uid=ur.uid WHERE %s;` condStr := fmt.Sprintf("ur.parent_uid=%s AND u.level='%s' ", smallUid, zhios_condition_statistics_utils.IntToStr(task.TaskTypeLevelId)) sql := fmt.Sprintf(sqlTpl, condStr) results, err := db.QueryNativeString(engine, sql) if err != nil { return "", hasPay, payOrdId } count := "" if len(results) > 0 { count = zhios_condition_statistics_utils.AnyToString(results[0]["count"]) } return count, hasPay, payOrdId case 15: if task.TaskTypeCount == 0 { return "0", hasPay, payOrdId } //stime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()).Format("2006-01-02 15:04:05") //etime := t.Format("2006-01-02 15:04:05") if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"1"}) { //o2o sqlTpl := `SELECT os.id FROM user_relate ur LEFT JOIN o2o_merchant om ON ur.uid = om.uid LEFT JOIN o2o_store os on om.id=os.store_manager WHERE ur.parent_uid = ? AND ur.level =1 ORDER BY ur.invite_time asc LIMIT 0,?; ` var ids = make([]string, 0) result, err := db.QueryNativeString(engine, sqlTpl, uid, task.TaskTypeCount) if err == nil { for _, v := range result { ids = append(ids, v["id"]) } } if len(ids) > 0 { sum, _ := engine.In("belong_store_id", ids).In("state", []string{"3", "4"}).Sum(&model.O2oOrd{}, "cost_price") sum1, _ := engine.In("belong_store_id", ids).Sum(&model.O2oPayToMerchant{}, "actual_pay_amount") return zhios_condition_statistics_utils.Float64ToStrByPrec(sum+sum1, 4), hasPay, payOrdId } } if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"2"}) { //b2c sqlTpl := `SELECT os.id FROM user_relate ur LEFT JOIN o2o_merchant om ON ur.uid = om.uid LEFT JOIN b2c_ord os on om.id=os.store_manager WHERE ur.parent_uid = ? AND ur.level =1 ORDER BY ur.invite_time asc LIMIT 0,?; ` var ids = make([]string, 0) result, err := db.QueryNativeString(engine, sqlTpl, uid, task.TaskTypeCount) if err == nil { for _, v := range result { ids = append(ids, v["id"]) } } if len(ids) > 0 { sum, _ := engine.In("belong_store_id", ids).In("state", []string{"4"}).Sum(&model.B2cOrd{}, "cost_price") return zhios_condition_statistics_utils.Float64ToStrByPrec(sum, 4), hasPay, payOrdId } } return "0", hasPay, payOrdId case 6: hasPay = true //TODO::根据 RegionalAgentSchemeTask 表中的 task_id 判断是否完成付费任务 regionalAgentUserOrd, err := db.UserLevelOrderByTaskId(engine, zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(task.Id)) if err != nil { return "", hasPay, payOrdId } if regionalAgentUserOrd != nil { if regionalAgentUserOrd.ExpireAt.Unix() < t.Unix() && regionalAgentUserOrd.CreateAt.Format("2006-01-02 15:04:05") != regionalAgentUserOrd.ExpireAt.Format("2006-01-02 15:04:05") && regionalAgentUserOrd.DateType != 4 { return "0", hasPay, payOrdId } payOrdId = regionalAgentUserOrd.Id return "1", hasPay, payOrdId } else { return "0", hasPay, payOrdId } return "1", hasPay, payOrdId case 17: stime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()).Format("2006-01-02 15:04:05") etime := t.Format("2006-01-02 15:04:05") // 团队N条线中各有N个相应等级用户 sqlTpl := `select ur.parent_uid,COUNT(*) as count FROM user_relate ur LEFT JOIN user u on u.uid=ur.uid LEFT JOIN user_level ul on u.level=ul.id where ur.parent_uid in(SELECT uid FROM user_relate WHERE parent_uid=%s and level=1 ) and ul.level_weight>=%s %s GROUP BY ur.parent_uid;` levelList := allLvMap[task.TaskTypeLevelId] levelWeight := 0 if levelList != nil { levelWeight = levelList.LevelWeight } sql := "" if task.WithinDays <= 0 { sql = fmt.Sprintf(sqlTpl, zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(levelWeight), "") } else { sql = fmt.Sprintf(sqlTpl, zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(levelWeight), "and u.level_arrive_at>='"+stime+"' and u.level_arrive_at<='"+etime+"'") } results, err := db.QueryNativeString(engine, sql) if err != nil { return "", hasPay, payOrdId } sqlTpl1 := `select ur.uid FROM user_relate ur LEFT JOIN user u on u.uid=ur.uid LEFT JOIN user_level ul on u.level=ul.id where ur.parent_uid=%s and ur.level=1 and ul.level_weight>=%s %s ;` sql1 := "" if task.WithinDays <= 0 { sql1 = fmt.Sprintf(sqlTpl1, zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(levelWeight), "") } else { sql1 = fmt.Sprintf(sqlTpl1, zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(levelWeight), "and u.level_arrive_at>='"+stime+"' and u.level_arrive_at<='"+etime+"'") } results1, err1 := db.QueryNativeString(engine, sql1) if err1 != nil { return "", hasPay, payOrdId } var firstList = make(map[string]string) if len(results1) > 0 { for _, v := range results1 { firstList[v["uid"]] = "1" } } if len(results) > 0 { for _, v := range results { firstList[v["parent_uid"]] = zhios_condition_statistics_utils.IntToStr(zhios_condition_statistics_utils.StrToInt(v["count"]) + zhios_condition_statistics_utils.StrToInt(firstList[v["parent_uid"]])) } } count := 0 if len(firstList) > 0 { for _, v := range firstList { if zhios_condition_statistics_utils.StrToInt(v) >= task.TaskTypeSecondCount && task.TaskTypeSecondCount > 0 { count++ } } } return zhios_condition_statistics_utils.IntToStr(count), hasPay, payOrdId case 18: stime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()).Format("2006-01-02 15:04:05") etime := t.Format("2006-01-02 15:04:05") // 小区消费金额 sqlTpl := `SELECT SUM(molr.goods_price) as price FROM mall_ord_list_relate molr LEFT JOIN mall_ord mo on mo.ord_id=molr.oid and mo.state in (%s) WHERE molr.uid in(SELECT uid FROM user_relate WHERE parent_uid=%s and level=1) %s GROUP BY molr.uid ;` state := "1,2,3" if task.TaskTypePvd == 1 { state = "2,3" } if task.TaskTypePvd == 2 { state = "3" } sql := "" if task.WithinDays <= 0 { sql = fmt.Sprintf(sqlTpl, state, zhios_condition_statistics_utils.AnyToString(uid), "") } else { sql = fmt.Sprintf(sqlTpl, state, zhios_condition_statistics_utils.AnyToString(uid), "and mo.create_time>='"+stime+"' and mo.create_time<='"+etime+"'") } results, err := db.QueryNativeString(engine, sql) if err != nil { return "", hasPay, payOrdId } var count float64 = 0 if len(results) > 0 { var max float64 = 0 for _, v := range results { if zhios_condition_statistics_utils.StrToFloat64(v["price"]) > max { max = zhios_condition_statistics_utils.StrToFloat64(v["price"]) } count += zhios_condition_statistics_utils.StrToFloat64(v["price"]) } count -= max } return zhios_condition_statistics_utils.Float64ToStrByPrec(count, 2), hasPay, payOrdId case 19: teamCountMinData, _ := db.SysCfgGetOne(engine, "team_count_min") teamCountMin := 15 if teamCountMinData != nil { teamCountMin = zhios_condition_statistics_utils.StrToInt(teamCountMinData.Val) } teamtotal := db.GetUserTeamTotal(engine, zhios_condition_statistics_utils.AnyToString(uid)) var count = 0 if teamtotal != nil && teamtotal.FirstCount >= teamCountMin && teamtotal.SecondCount >= teamCountMin { count++ } sql := `SELECT * FROM user_profile up LEFT JOIN user_team_total utt ON utt.uid=up.uid WHERE up.parent_uid=? and utt.first_count>=? and utt.second_count>=? ` nativeString, _ := db.QueryNativeString(engine, sql, zhios_condition_statistics_utils.AnyToString(uid), teamCountMin, teamCountMin) if len(nativeString) > 0 { count += len(nativeString) } return zhios_condition_statistics_utils.IntToStr(count), hasPay, payOrdId default: return "", hasPay, payOrdId } }