|
- 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"
- "xorm.io/xorm"
- )
-
- // CalcTodayEggEnergyPriceRises 计算蛋蛋能量今日涨跌幅
- func CalcTodayEggEnergyPriceRises(engine *xorm.Engine, now time.Time) (err error, rises float64, isRises bool, nowPrice string, initialPrice string) {
-
- //1、查找昨日价格、今日价格 计算涨跌幅
- var m *model.EggEnergyPrice
- dateStr := now.AddDate(0, 0, -1).Format("2006-01-02")
- hourStr := now.AddDate(0, 0, -1).Hour()
- priceDb := implement.NewEggEnergyPriceDb(db.Db)
- m, err = priceDb.EggEnergyPriceGetOneByParams(dateStr, hourStr)
- if err != nil {
- return err, 0, false, "0", "0"
- }
- initialPrice = m.Price
- if initialPrice == "" {
- settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
- basicSetting, err1 := settingDb.EggEnergyBasicSettingGetOne()
- if err1 != nil {
- return err1, 0, false, "0", "0"
- }
- initialPrice = basicSetting.InitialPrice
- }
- yesterdayPrice, _ := decimal.NewFromString(initialPrice)
- coreDataDb := implement.NewEggEnergyCoreDataDb(db.Db)
- coreData, err := coreDataDb.EggEnergyCoreDataGet()
- if err != nil {
- return err, 0, false, "0", "0"
- }
- nowPrice = coreData.NowPrice
- todayPrice, _ := decimal.NewFromString(coreData.NowPrice)
- if todayPrice.GreaterThanOrEqual(yesterdayPrice) {
- isRises = true
- rises, _ = todayPrice.Sub(yesterdayPrice).Div(yesterdayPrice).Float64()
- } else {
- rises, _ = yesterdayPrice.Sub(todayPrice).Div(todayPrice).Float64()
- }
- return nil, rises, isRises, nowPrice, initialPrice
- }
-
- // IsFinishDirectPush 直推x个用户
- func IsFinishDirectPush(uid int64, num int) (bool, int64, error) {
- relateDb := implement.NewUserRelateDb(db.Db)
- count, err := relateDb.UserRelatesCountByParentUid(uid, 1)
- if err != nil {
- return false, count, err
- }
- if count >= int64(num) {
- return true, count, nil
- }
- return false, count, nil
- }
-
- // IsFinishDirectPushOfActiveDays a天内, 需要直推 b 个人, b个人需要在 a 天内每个人都活跃 c 天
- func IsFinishDirectPushOfActiveDays(uid int64, num int, activeDays int, now time.Time, withInDays int) (bool, int64, error) {
- startAt := now.AddDate(0, 0, -withInDays).Format("2006-01-02 15:04:05")
- endAt := now.Format("2006-01-02 15:04:05")
- startAtDate := now.AddDate(0, 0, withInDays).Format("2006-01-02")
- endAtDate := now.Format("2006-01-02")
- // 1. 获取邀请用户
- userDb := implement.NewUserDb(db.Db)
- users, err := userDb.UserFindInviteByDate(uid, startAt, endAt)
- if err != nil {
- return false, 0, err
- }
- if users == nil {
- return false, 0, nil
- }
- // 2. 计算活跃用户数
- userIds := make([]string, 0, len(*users))
- for _, user := range *users {
- userIds = append(userIds, utils.Int64ToStr(user.Id))
- }
- // 2.1 计算活跃天数
- sql := "select uid, COUNT(*) AS total " +
- "from egg_energy_user_activity " +
- "where uid in (%s) " +
- "and date between " + "'" + startAtDate + "'" + " and " + "'" + endAtDate + "' " +
- "group by uid"
- sql = fmt.Sprintf(sql, strings.Join(userIds, ","))
- res, err := db.QueryNativeString(db.Db, sql)
- if err != nil {
- return false, 0, err
- }
- // 2.2 用户是否活跃计数
- var count int64
- for _, node := range res {
- if utils.StrToInt(node["total"]) >= activeDays {
- count++
- }
- }
- if count >= int64(num) {
- return true, count, nil
- }
- return false, count, nil
- }
-
- // IsFinishDirectPushLevel 直推多少个等级x以上用户
- func IsFinishDirectPushLevel(uid int64, levelId int, num int) (bool, int64, error) {
- levelDb := implement.NewUserLevelDb(db.Db)
- level, err := levelDb.UserLevelByID(levelId)
- if err != nil {
- return false, 0, err
- }
- if level == nil {
- return false, 0, nil
- }
-
- count, err := db.Db.Table("user").
- Join("LEFT OUTER", []string{"user_level", "level"}, "user.level = level.id").
- Where("user.parent_uid = ?", uid).
- Where("level.level_weight >= ?", level.LevelWeight).Count(&model.User{})
- if err != nil {
- return false, 0, err
- }
- if count >= int64(num) {
- return true, count, nil
- }
- return false, count, nil
- }
|