package rule import ( db "code.fnuoos.com/EggPlanet/egg_models.git/src" "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" "code.fnuoos.com/EggPlanet/egg_models.git/src/model" utils "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils" "errors" "fmt" "time" "xorm.io/xorm" ) func UserUpgradeInsert(eg *xorm.Engine, uid int) error { userDb := implement.NewUserDb(eg) user, _ := userDb.GetUser(int64(uid)) level := user.Level isNext := 0 levelDb := implement.NewUserLevelDb(eg) levels, _ := levelDb.UserLevelAllByAsc() for _, v1 := range levels { if isNext == 1 { level = v1.Id isNext = 2 } if v1.Id == level && isNext == 0 { isNext = 1 } } if level <= user.Level { return errors.New("等级相同") } task, err := UserUpgradeTask(eg, int(user.Id), level) if err != nil { return err } //升级 oldLevel := user.Level user.Level = level _, err = eg.Where("id=?", user.Id).Cols("level").Update(user) if err != nil { return err } var tmp = model.UserUpgradeLevel{ Uid: int(user.Id), Level: level, OldLv: oldLevel, CreateTime: time.Now(), Task: utils.SerializeStr(task), } eg.Insert(&tmp) return nil } /* 会员升级 uid 用户id lv 要升级的等级 */ func UserUpgradeTask(eg *xorm.Engine, uid, lv int) ([]map[string]string, error) { taskList := make([]map[string]string, 0) //获取任务 NewUserLevelTaskDb := implement.NewUserLevelTaskDb(eg) levelTask, _ := NewUserLevelTaskDb.UserLevelTaskFindByParams(map[string]interface{}{ "key": "level_id", "value": lv, }) if len(levelTask) == 0 { return taskList, errors.New("任务条件未设置") } NewUserLevelDb := implement.NewUserLevelDb(eg) level, _ := NewUserLevelDb.UserLevelByID(lv) if level == nil { return taskList, errors.New("等级不存在") } chooseNum := 0 mustTaskErr := 0 for _, v := range levelTask { isSuccess := 0 var count int64 = 0 switch v.TaskType { case 1: //直推会员人数 count = ExtendUserCount(eg, uid) if count >= int64(v.FinishCount) { isSuccess = 1 } break case 2: //直推会员活跃天数 count = ExtendUserCountWithDay(eg, uid, v.WithinDays, v.ActiveDays) if count >= int64(v.FinishCount) { isSuccess = 1 } break case 3: //直推会员等级 count = ExtendUserCountWithLv(eg, uid, v.TaskTypeLevelId) if count >= int64(v.FinishCount) { isSuccess = 1 } break } tmp := map[string]string{ "id": utils.IntToStr(v.Id), "count": utils.Int64ToStr(count), "is_success": utils.IntToStr(isSuccess), } taskList = append(taskList, tmp) if isSuccess == 1 && v.IsMustTask == 0 { //记录选做任务数量 chooseNum++ } if isSuccess == 0 && v.IsMustTask == 1 { //必做任务未完成 mustTaskErr = 1 } } if mustTaskErr == 1 { return taskList, errors.New("必做任务未完成") } if chooseNum < level.ChoosableNum { //选做任务未完成 return taskList, errors.New("选择任务未完成") } return taskList, nil } // 直推人数 func ExtendUserCount(eg *xorm.Engine, uid int) int64 { count, _ := eg.Where("parent_uid=?", uid).Count(&model.User{}) return count } // 直推人数达到对应等级 func ExtendUserCountWithLv(eg *xorm.Engine, uid, lv int) int64 { count, _ := eg.Where("parent_uid=? and level=?", uid, lv).Count(&model.User{}) return count } // X天直推X个用户活跃X天 func ExtendUserCountWithDay(eg *xorm.Engine, uid, day, activityDay int) int64 { var user []model.User today := utils.GetTimeRange("today") times := today["start"] - int64(day*86400) eg.Where("parent_uid=? and create_at>=?", uid, times).Find(&user) var num int64 = 0 if len(user) > 0 { uids := "" for _, v := range user { if uids == "" { uids += utils.Int64ToStr(v.Id) } else { uids += "," + utils.Int64ToStr(v.Id) } } sql := `select count(*) as count from egg_energy_user_activity where uid in(%s) ` sql = fmt.Sprintf(sql, uids) nativeString, _ := db.QueryNativeString(eg, sql) for _, v := range nativeString { if utils.StrToInt(v["count"]) >= activityDay { num++ } } } return num }