蛋蛋星球-客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

133 lines
4.1 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/utils"
  5. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  6. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  7. "fmt"
  8. "github.com/shopspring/decimal"
  9. "strings"
  10. "time"
  11. "xorm.io/xorm"
  12. )
  13. // CalcTodayEggEnergyPriceRises 计算蛋蛋能量今日涨跌幅
  14. func CalcTodayEggEnergyPriceRises(engine *xorm.Engine, now time.Time) (err error, rises float64, isRises bool, nowPrice string, initialPrice string) {
  15. //1、查找昨日价格、今日价格 计算涨跌幅
  16. var m *model.EggEnergyPrice
  17. dateStr := now.AddDate(0, 0, -1).Format("2006-01-02")
  18. hourStr := now.AddDate(0, 0, -1).Hour()
  19. priceDb := implement.NewEggEnergyPriceDb(db.Db)
  20. m, err = priceDb.EggEnergyPriceGetOneByParams(dateStr, hourStr)
  21. if err != nil {
  22. return err, 0, false, "0", "0"
  23. }
  24. initialPrice = m.Price
  25. if initialPrice == "" {
  26. settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  27. basicSetting, err1 := settingDb.EggEnergyBasicSettingGetOne()
  28. if err1 != nil {
  29. return err1, 0, false, "0", "0"
  30. }
  31. initialPrice = basicSetting.InitialPrice
  32. }
  33. yesterdayPrice, _ := decimal.NewFromString(initialPrice)
  34. coreDataDb := implement.NewEggEnergyCoreDataDb(db.Db)
  35. coreData, err := coreDataDb.EggEnergyCoreDataGet()
  36. if err != nil {
  37. return err, 0, false, "0", "0"
  38. }
  39. nowPrice = coreData.NowPrice
  40. todayPrice, _ := decimal.NewFromString(coreData.NowPrice)
  41. if todayPrice.GreaterThanOrEqual(yesterdayPrice) {
  42. isRises = true
  43. rises, _ = todayPrice.Sub(yesterdayPrice).Div(yesterdayPrice).Float64()
  44. } else {
  45. rises, _ = yesterdayPrice.Sub(todayPrice).Div(todayPrice).Float64()
  46. }
  47. return nil, rises, isRises, nowPrice, initialPrice
  48. }
  49. // IsFinishDirectPush 直推x个用户
  50. func IsFinishDirectPush(uid int64, num int) (bool, int64, error) {
  51. relateDb := implement.NewUserRelateDb(db.Db)
  52. count, err := relateDb.UserRelatesCountByParentUid(uid, 1)
  53. if err != nil {
  54. return false, count, err
  55. }
  56. if count >= int64(num) {
  57. return true, count, nil
  58. }
  59. return false, count, nil
  60. }
  61. // IsFinishDirectPushOfActiveDays a天内, 需要直推 b 个人, b个人需要在 a 天内每个人都活跃 c 天
  62. func IsFinishDirectPushOfActiveDays(uid int64, num int, activeDays int, now time.Time, withInDays int) (bool, int64, error) {
  63. startAt := now.AddDate(0, 0, -withInDays).Format("2006-01-02 15:04:05")
  64. endAt := now.Format("2006-01-02 15:04:05")
  65. startAtDate := now.AddDate(0, 0, withInDays).Format("2006-01-02")
  66. endAtDate := now.Format("2006-01-02")
  67. // 1. 获取邀请用户
  68. userDb := implement.NewUserDb(db.Db)
  69. users, err := userDb.UserFindInviteByDate(uid, startAt, endAt)
  70. if err != nil {
  71. return false, 0, err
  72. }
  73. if users == nil {
  74. return false, 0, nil
  75. }
  76. // 2. 计算活跃用户数
  77. userIds := make([]string, 0, len(*users))
  78. for _, user := range *users {
  79. userIds = append(userIds, utils.Int64ToStr(user.Id))
  80. }
  81. // 2.1 计算活跃天数
  82. sql := "select uid, COUNT(*) AS total " +
  83. "from egg_energy_user_activity " +
  84. "where uid in (%s) " +
  85. "and date between " + "'" + startAtDate + "'" + " and " + "'" + endAtDate + "' " +
  86. "group by uid"
  87. sql = fmt.Sprintf(sql, strings.Join(userIds, ","))
  88. res, err := db.QueryNativeString(db.Db, sql)
  89. if err != nil {
  90. return false, 0, err
  91. }
  92. // 2.2 用户是否活跃计数
  93. var count int64
  94. for _, node := range res {
  95. if utils.StrToInt(node["total"]) >= activeDays {
  96. count++
  97. }
  98. }
  99. if count >= int64(num) {
  100. return true, count, nil
  101. }
  102. return false, count, nil
  103. }
  104. // IsFinishDirectPushLevel 直推多少个等级x以上用户
  105. func IsFinishDirectPushLevel(uid int64, levelId int, num int) (bool, int64, error) {
  106. levelDb := implement.NewUserLevelDb(db.Db)
  107. level, err := levelDb.UserLevelByID(levelId)
  108. if err != nil {
  109. return false, 0, err
  110. }
  111. if level == nil {
  112. return false, 0, nil
  113. }
  114. count, err := db.Db.Table("user").
  115. Join("LEFT OUTER", []string{"user_level", "level"}, "user.level = level.id").
  116. Where("user.parent_uid = ?", uid).
  117. Where("level.level_weight >= ?", level.LevelWeight).Count(&model.User{})
  118. if err != nil {
  119. return false, 0, err
  120. }
  121. if count >= int64(num) {
  122. return true, count, nil
  123. }
  124. return false, count, nil
  125. }