package svc import ( "applet/app/db" "applet/app/utils" "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" "code.fnuoos.com/EggPlanet/egg_models.git/src/model" "fmt" "github.com/shopspring/decimal" "strings" "time" ) func AutoCountTeamAssistance() { // 1.查签到表,统计今天0点后开始签到的用户 now := time.Now() 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 } detailDb := implement.NewEggEnergyAssistanceDetailDb(db.Db) assistanceDb := implement.NewEggEnergyTeamAssistanceDb(db.Db) var teamAssistance []model.EggEnergyTeamAssistance for _, signIn := range signIns { fmt.Println("uid>>>>>>", signIn.Uid) // 2. 查询这些用户的下级 sql := fmt.Sprintf("SELECT uid FROM `public_platoon_user_relation` WHERE father_uid1 = %d OR father_uid2= %d OR father_uid3= %d OR father_uid4= %d OR father_uid5= %d OR father_uid6= %d OR father_uid7= %d OR father_uid8= %d OR father_uid9= %d", signIn.Uid, signIn.Uid, signIn.Uid, signIn.Uid, signIn.Uid, signIn.Uid, signIn.Uid, signIn.Uid, signIn.Uid) nativeString, _ := db.QueryNativeString(db.Db, sql) if nativeString != nil { var userIds []string for _, v := range nativeString { userIds = append(userIds, v["uid"]) } // 3、统计流水表得出助力次数 userIdStr := strings.Join(userIds, ",") sql1 := fmt.Sprintf("select * from `user_virtual_coin_flow` where uid in (%s) and transfer_type = 2 and create_at > '%s' group by 'uid'", userIdStr, todayStr) fmt.Println("sql>>>>>", sql1) results, _ := db.QueryNativeString(db.Db, sql1) if results != nil { var rewardScore float64 for _, vv := range results { amountValue, _ := decimal.NewFromString(vv["amount"]) rateValue := decimal.NewFromFloat(0.1) rewardScoreValue, _ := amountValue.Mul(rateValue).RoundFloor(8).Float64() rewardScore += rewardScoreValue //插入助力数据 detail := model.EggEnergyAssistanceDetail{ Uid: signIn.Uid, AssistedUid: utils.StrToInt64(vv["uid"]), SignId: signIn.Id, CreatedAt: vv["create_at"], UpdatedAt: vv["create_at"], } _, err = detailDb.AssistanceDetailInsert(&detail) if err != nil { fmt.Println("err1111>>>>>>>>>>>>>>>>>", err.Error()) return } } //4、更新助力数据 assistance, _ := assistanceDb.EggEnergyTeamAssistanceGetOneByParams(map[string]interface{}{ "key": "sign_id", "value": signIn.Id, }) if assistance != nil { assistance.AssistedNum = len(results) assistance.RewardScore = utils.Float64ToStr(rewardScore) _, err = assistanceDb.EggEnergyTeamAssistanceUpdate(assistance.Id, assistance, "assisted_num", "reward_score") if err != nil { fmt.Println("err2222>>>>>>>>>>>>>>>>>", err.Error()) return } } else { m := model.EggEnergyTeamAssistance{ Uid: signIn.Uid, AssistedNum: len(results), AssistedTimes: 0, RewardScore: utils.Float64ToStr(rewardScore), Date: date, SignId: signIn.Id, CreateAt: now.Format("2006-01-02 15:04:05"), UpdateAt: now.Format("2006-01-02 15:04:05"), } _, err = assistanceDb.EggEnergyTeamAssistanceInsert(&m) if err != nil { fmt.Println("err3333>>>>>>>>>>>>>>>>>", err.Error()) return } } fmt.Println(results) } } //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) }