package svc import ( "applet/app/db" "applet/app/utils" "applet/app/utils/cache" "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" "code.fnuoos.com/EggPlanet/egg_models.git/src/model" "fmt" "time" ) func AutoCountTeamAssistance() { // 1.查签到表,统计今天0点后开始签到的用户 now := time.Now() nowStr := now.Format("2006-01-02 15:04:05") date := now.Format("2006-01-02") todayStr := fmt.Sprintf("%s %s", date, "00:00:00") var signIns []model.EggSignIn err := db.Db.Where("start_time > ?", todayStr).Find(&signIns) if err != nil { fmt.Println(err) return } var teamAssistance []model.EggEnergyTeamAssistance // 2. 查询这些用户的下级 for _, signIn := range signIns { relateDb := implement.NewUserRelateDb(db.Db) relates, err := relateDb.FindUserRelateByParentUid(signIn.Uid, 0) if err != nil { fmt.Println(err) return } // 3. 判断这些下级助力次数 var uids []int64 for _, relate := range *relates { uids = append(uids, relate.Uid) } typePrefix := "incentive_eggSmash" types := fmt.Sprintf("type LIKE '%s'", typePrefix+"%") assistedNum, err := db.Db.Where("create_at > ?", signIn.CreateAt). And(types).In("uid", uids). GroupBy("uid").Count(&model.AdvertisingCallback{}) if err != nil { fmt.Println(err) return } if assistedNum > 0 { // 保存助力信息 数据库 var assistedUsers []model.AdvertisingCallback err := db.Db.Select("uid").Where("create_at > ?", signIn.CreateAt). And(types).In("uid", uids). GroupBy("uid").Find(&assistedUsers) assistedUserDetails := make([]model.EggEnergyAssistanceDetail, len(assistedUsers)) for i, assistedUser := range assistedUsers { m := model.EggEnergyAssistanceDetail{ Uid: signIn.Uid, AssistedUid: int64(assistedUser.Uid), SignId: signIn.Id, CreatedAt: nowStr, UpdatedAt: nowStr, } assistedUserDetails[i] = m } // 保存助力信息到 redis cacheKey := fmt.Sprintf("EggEnergyTeamAssistance:%d", signIn.Uid) targetTime, err1 := time.Parse("2006-01-02 15:04:05", signIn.EndTime) if err1 != nil { fmt.Println("解析时间错误:", err1) return } diff := targetTime.Sub(time.Now()).Seconds() _, err = cache.SetEx(cacheKey, nil, int(diff)) if err != nil { return } for _, assistedUser := range assistedUsers { cache.SetBit(cacheKey, int64(assistedUser.Uid), 1) } assistedTimes, err := db.Db.Where("create_at > ?", signIn.CreateAt). And(types).In("uid", uids).Count(&model.AdvertisingCallback{}) if err != nil { fmt.Println(err) return } m := model.EggEnergyTeamAssistance{ Uid: signIn.Uid, AssistedNum: int(assistedNum), AssistedTimes: int(assistedTimes), RewardScore: utils.Float64ToStr(float64(assistedTimes) * 187.5), SignId: signIn.Id, Date: date, CreateAt: nowStr, UpdateAt: nowStr, } teamAssistance = append(teamAssistance, m) } } insert, err := db.Db.Insert(&teamAssistance) if err != nil { fmt.Println(err) return } fmt.Println("insert ", insert) }