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" "errors" "fmt" "time" "xorm.io/xorm" ) func CommUpdate(blockStarChain string, eg *xorm.Engine, dbName string, user *model.User, DataDetail md.DataDetail) (error, bool) { uid := DataDetail.Uid //判断有没有审核通过或审核中的记录 has, err := db.UserLevelAuditingFindByUid(eg, zhios_condition_statistics_utils.StrToInt(uid)) if err != nil { return err, false } if has != nil { var msg = "您已审核通过,请勿重复申请" if has.State == 1 { msg = "正在审核中,请稍后再查看" } return errors.New(msg), false } lv := user.Level result := CommUplv(blockStarChain, eg, dbName, user, lv, DataDetail) if result == false { return errors.New("升级失败"), false } return nil, result } func CommUplv(blockStarChain string, eg *xorm.Engine, dbName string, user *model.User, lv int, DataDetail md.DataDetail) bool { 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) if result { level, err := db.UserLevelByID(eg, zhios_condition_statistics_utils.StrToInt(DataDetail.AgentType)) if err != nil || level == nil { return false } 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 } //自动审核 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 } 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 } //过期时间在当前时间之前 并且 创建时间跟过期时间不相等 相等的话是永久会员 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 } 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: // 永久(设置为与创建时间相同) ord.ExpireAt = t } if _, err := db.UserLevelOrdUpdateOne(eg, ord); err != nil { return false } } 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 } } } return result } // IsCompleteSchemeTask 根据传入的 level 判断是否可以升级 func IsCompleteSchemeTask(blockStarChain string, engine *xorm.Engine, user *model.User, level, IsChangeTime 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, "") } func CheckSchemeTasksComplete(blockStarChain string, engine *xorm.Engine, tasks *[]model.UserLevelUpgradeTask, uid interface{}, scheme *model.UserLevel, level string, regionalId 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) 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 "小区累计自营消费金额" 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) (string, bool, int64) { t := time.Now() hasPay := false var payOrdId int64 switch task.TaskType { case 1: // 累计自购数 stime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()).Unix() etime := t.Unix() mallStime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()) mallEtime := t var total int64 var err error //联盟结算后的 if task.WithinDays <= 0 { total, err = db.OrderListCountByUIDByOrderType(engine, uid, 0, "3,5") if err != nil { return "", hasPay, payOrdId } //确认收货的才算 mallTotal, err := db.MallOrdWithCount(engine, uid, 0, "3") if err != nil { return zhios_condition_statistics_utils.Int64ToStr(total), hasPay, payOrdId } total += mallTotal } else { total, err = db.OrderListCountByUIDByOrderTypeByTime(engine, uid, 0, "3,5", stime, etime) if err != nil { return "", hasPay, payOrdId } //确认收货的才算 mallTotal, err := db.MallOrdByTimeWithCount(engine, uid, 0, "3", mallStime.Format("2006-01-02 15:04:05"), mallEtime.Format("2006-01-02 15:04:05")) if err != nil { return zhios_condition_statistics_utils.Int64ToStr(total), hasPay, payOrdId } total += mallTotal } return zhios_condition_statistics_utils.Int64ToStr(total), hasPay, payOrdId case 10: // 累计自购消费金额 var total float64 = 0 var err error if task.WithinDays <= 0 { if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "1"}) { total, err = db.OrderListPaidPriceByUIDByOrderType(engine, uid, 0, []string{"3", "5"}) 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 } //确认收货的才算 mallTotal, err := db.MallOrd(engine, uid, orderType, []string{"3"}) if err != nil { return zhios_condition_statistics_utils.Float64ToStr(total), hasPay, payOrdId } total += mallTotal } if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "3"}) { //确认收货的才算 o2oTotal, err := db.O2oOrd(engine, uid, 0, []string{"3"}) if err != nil { return zhios_condition_statistics_utils.Float64ToStr(total), hasPay, payOrdId } total += o2oTotal o2oTotal1, err := db.O2oOrdPayToMerchant(engine, uid, 0, []string{"1", "4"}) if err != nil { return zhios_condition_statistics_utils.Float64ToStr(total), hasPay, payOrdId } total += o2oTotal1 } if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "4"}) { //确认收货的才算 b2cTotal, err := db.B2cOrd(engine, uid, 0, []string{"3"}) if err != nil { return zhios_condition_statistics_utils.Float64ToStr(total), hasPay, payOrdId } total += b2cTotal } } else { if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "1"}) { stime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()).Unix() etime := t.Unix() total, err = db.OrderListPaidPriceByUIDByOrderTypeByTime(engine, uid, 0, []string{"3", "5"}, stime, etime) if err != nil { return "", hasPay, payOrdId } } if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "2", "8", "9"}) { mallStime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()) mallEtime := t orderType := 0 if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"8", "9"}) { orderType = task.TaskTypePvd } //确认收货的才算 mallTotal, err := db.MallOrdByTime(engine, uid, orderType, []string{"3"}, mallStime.Format("2006-01-02 15:04:05"), mallEtime.Format("2006-01-02 15:04:05")) if err != nil { return zhios_condition_statistics_utils.Float64ToStr(total), hasPay, payOrdId } total += mallTotal } if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "3"}) { mallStime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()) mallEtime := t //确认收货的才算 o2oTotal, err := db.O2oOrdByTime(engine, uid, 0, []string{"3"}, mallStime.Format("2006-01-02 15:04:05"), mallEtime.Format("2006-01-02 15:04:05")) if err != nil { return zhios_condition_statistics_utils.Float64ToStr(total), hasPay, payOrdId } total += o2oTotal o2oTotal1, err := db.O2oOrdPayToMerchantByTime(engine, uid, 0, []string{"1", "4"}, mallStime.Format("2006-01-02 15:04:05"), mallEtime.Format("2006-01-02 15:04:05")) if err != nil { return zhios_condition_statistics_utils.Float64ToStr(total), hasPay, payOrdId } total += o2oTotal1 } if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "4"}) { mallStime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()) mallEtime := t //确认收货的才算 b2cTotal, err := db.B2cOrdByTime(engine, uid, 0, []string{"3"}, mallStime.Format("2006-01-02 15:04:05"), mallEtime.Format("2006-01-02 15:04:05")) if err != nil { return zhios_condition_statistics_utils.Float64ToStr(total), hasPay, payOrdId } total += b2cTotal } } return zhios_condition_statistics_utils.Float64ToStr(zhios_condition_statistics_utils.FloatFormat(total, 2)), hasPay, payOrdId case 2: // 累计已结算佣金 stime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()).Unix() etime := t.Unix() mallStime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()) mallEtime := t var sum float64 if task.WithinDays <= 0 { // 用户分佣的订单 idAmountMap, err := db.OrderRelateListByState(engine, uid, "3,5") if err != nil { return "", hasPay, payOrdId } for _, item := range *idAmountMap { sum += item } _, mallSum, err := db.MallOrderRelateListByState(engine, uid, 3) if err != nil { return zhios_condition_statistics_utils.Float64ToStr(sum), hasPay, payOrdId } sum += mallSum } else { // 用户分佣的订单 idAmountMap, err := db.OrderRelateListByTimeByState(engine, uid, "3,5", stime, etime) if err != nil { return "", hasPay, payOrdId } for _, item := range *idAmountMap { sum += item } _, mallSum, err := db.MallOrderRelateListByTimeByState(engine, uid, 3, mallStime, mallEtime) if err != nil { return zhios_condition_statistics_utils.Float64ToStr(sum), hasPay, payOrdId } sum += mallSum } 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_profile as up LEFT JOIN user as u ON u.uid=up.uid WHERE %s;` 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_profile as up LEFT JOIN user as u ON u.uid=up.uid WHERE %s;` var condStr string if task.WithinDays <= 0 { condStr = fmt.Sprintf("up.parent_uid=%s and up.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 up.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 12: //累计团队消费金额 var total float64 = 0 if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "2", "8", "9"}) { sqlTpl := `SELECT SUM(ol.cost_price) AS amount FROM mall_ord_list_relate olr JOIN mall_ord ol ON olr.oid = ol.ord_id WHERE olr.uid = ? AND ol.state =3 and olr.level>0 %s; ` str := "" if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"8", "9"}) { str += " and ol.order_type=" + zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd) } sqlTpl = fmt.Sprintf(sqlTpl, str) result, err := db.QueryNativeString(engine, sqlTpl, uid, str) if err == nil { total += zhios_condition_statistics_utils.StrToFloat64(result[0]["amount"]) } } if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "3"}) { o2oSqlTpl := `SELECT SUM(ol.cost_price) AS amount FROM o2o_ord_list_relate olr JOIN o2o_ord ol ON olr.oid = ol.ord_id WHERE olr.uid = ? AND ol.state =3 and olr.level>0; ` o2oResult, err := db.QueryNativeString(engine, o2oSqlTpl, uid) if err == nil { total += zhios_condition_statistics_utils.StrToFloat64(o2oResult[0]["amount"]) } } if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "4"}) { b2cSqlTpl := `SELECT SUM(ol.cost_price) AS amount FROM b2c_ord_list_relate olr JOIN b2c_ord ol ON olr.oid = ol.ord_id WHERE olr.uid = ? AND ol.state =3 and olr.level>0; ` b2cResult, err := db.QueryNativeString(engine, b2cSqlTpl, uid) if err == nil { total += zhios_condition_statistics_utils.StrToFloat64(b2cResult[0]["amount"]) } } if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "1"}) { guideSqlTpl := `SELECT SUM(ol.paid_price) AS amount FROM ord_list_relate olr JOIN ord_list ol ON olr.oid = ol.ord_id WHERE olr.uid = ? AND ol.state in(3,5) and olr.level>0; ` guideResult, err := db.QueryNativeString(engine, guideSqlTpl, uid) if err == nil { total += zhios_condition_statistics_utils.StrToFloat64(guideResult[0]["amount"]) } } 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 } }