蛋蛋星球-客户端
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.
 
 
 
 
 
 

451 lines
14 KiB

  1. package hdl
  2. import (
  3. "applet/app/db"
  4. "applet/app/e"
  5. "applet/app/md"
  6. "applet/app/svc"
  7. "applet/app/utils"
  8. "applet/app/utils/cache"
  9. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  10. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  11. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  12. "encoding/json"
  13. "fmt"
  14. "github.com/gin-gonic/gin"
  15. "github.com/shopspring/decimal"
  16. "time"
  17. )
  18. // HomePage
  19. // @Summary 蛋蛋星球-主页-基础信息(获取)
  20. // @Tags 主页
  21. // @Description 基础信息(获取)
  22. // @Accept json
  23. // @Produce json
  24. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  25. // @Success 200 {object} md.HomePageResp "具体数据"
  26. // @Failure 400 {object} md.Response "具体错误"
  27. // @Router /api/v1/homePage/index [get]
  28. func HomePage(c *gin.Context) {
  29. now := time.Now()
  30. val, exists := c.Get("user")
  31. if !exists {
  32. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  33. return
  34. }
  35. user, ok := val.(*model.User)
  36. if !ok {
  37. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  38. return
  39. }
  40. //1、查找 `egg_energy_basic_setting` 基础设置
  41. eggEnergyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  42. eggEnergyBasicSetting, err := eggEnergyBasicSettingDb.EggEnergyBasicSettingGetOneByParams(map[string]interface{}{
  43. "key": "is_open",
  44. "value": 1,
  45. })
  46. if err != nil {
  47. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  48. return
  49. }
  50. //2、计算今日涨幅
  51. err, rises, isRise, nowPrice, initialPrice := svc.CalcTodayEggEnergyPriceRises(db.Db, now)
  52. if err != nil {
  53. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  54. return
  55. }
  56. //3、获取当前用户 蛋蛋能量(可用+结算+今日签到预估蛋蛋能量)
  57. var isSign bool
  58. var signCountdown string
  59. var signPersonalEggEnergy = "0.00"
  60. var signTeamEggEnergy = "0.00"
  61. var totalEggEnergy float64
  62. nowStr := now.Format("2006-01-02 15:04:05")
  63. signInDb := implement.NewEggSignInDb(db.Db)
  64. has, signIn, err := signInDb.EggSignInGetOne(nowStr, nowStr, user.Id)
  65. if err != nil {
  66. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  67. return
  68. }
  69. if has && utils.TimeParseStd(signIn.EndTime).After(now) {
  70. //3.2.1 获取预估每秒获得蛋蛋能量数
  71. estimatePerSecondPersonEggEnergyValue, err1 := decimal.NewFromString(signIn.EstimatePerSecondPersonEggEnergyValue)
  72. if err1 != nil {
  73. e.OutErr(c, e.ERR, err1.Error())
  74. return
  75. }
  76. estimatePerSecondTeamEggEnergyValue, err1 := decimal.NewFromString(signIn.EstimatePerSecondTeamEggEnergyValue)
  77. if err1 != nil {
  78. e.OutErr(c, e.ERR, err1.Error())
  79. return
  80. }
  81. signPersonalEggEnergy = estimatePerSecondPersonEggEnergyValue.String()
  82. signTeamEggEnergy = estimatePerSecondTeamEggEnergyValue.String()
  83. isSign = true
  84. signCountdown = signIn.EndTime
  85. }
  86. // 3.3 查询个人及团队蛋蛋能量
  87. amountDb := implement.NewUserVirtualAmountDb(db.Db)
  88. personEggEnergy, err := amountDb.GetUserVirtualWalletBySession(user.Id, eggEnergyBasicSetting.PersonEggEnergyCoinId)
  89. if err != nil {
  90. e.OutErr(c, e.ERR_DB_ORM, nil)
  91. return
  92. }
  93. teamEggEnergy, err := amountDb.GetUserVirtualWalletBySession(user.Id, eggEnergyBasicSetting.TeamEggEnergyCoinId)
  94. if err != nil {
  95. e.OutErr(c, e.ERR_DB_ORM, nil)
  96. return
  97. }
  98. signEggEnergyValue, _ := decimal.NewFromString(signPersonalEggEnergy)
  99. personEggEnergyValue, _ := decimal.NewFromString(personEggEnergy.Amount)
  100. teamEggEnergyValue, _ := decimal.NewFromString(teamEggEnergy.Amount)
  101. totalEggEnergy, _ = signEggEnergyValue.Add(personEggEnergyValue).Add(teamEggEnergyValue).Float64()
  102. //4、活跃积分(个人+团队)
  103. var totalActivePoints float64
  104. personActivePoints, err := amountDb.GetUserVirtualWalletBySession(user.Id, eggEnergyBasicSetting.PersonEggPointsCoinId)
  105. if err != nil {
  106. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  107. return
  108. }
  109. teamActivePoints, err := amountDb.GetUserVirtualWalletBySession(user.Id, eggEnergyBasicSetting.TeamEggPointsCoinId)
  110. if err != nil {
  111. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  112. return
  113. }
  114. totalActivePoints = utils.StrToFloat64(personActivePoints.Amount) + utils.StrToFloat64(teamActivePoints.Amount)
  115. //5、计算当前基础速率、团队速率
  116. nowBasalRate := utils.StrToFloat64(signPersonalEggEnergy) * 60 * 60 //每小时基础速率
  117. NowTeamRate := utils.StrToFloat64(signTeamEggEnergy) * 60 * 60 // 每小时团队速率
  118. userDb := implement.NewUserDb(db.Db)
  119. userCount, err := userDb.UserCount()
  120. if err != nil {
  121. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  122. return
  123. }
  124. var residueTimes = utils.TimeParseStd(signCountdown).Unix() - time.Now().Unix()
  125. utils.FilePutContents("HomePage", utils.SerializeStr(map[string]interface{}{
  126. "user_phone": user.Phone,
  127. "user_uid": user.Id,
  128. "data": map[string]interface{}{
  129. "is_sign": isSign,
  130. "sign_end_date": signCountdown,
  131. "sign_end_timestamp": utils.TimeParseStd(signCountdown).Unix(),
  132. "residue_times": residueTimes,
  133. "total_egg_energy": totalEggEnergy,
  134. "total_active_points": totalActivePoints,
  135. "egg_energy_now_price": nowPrice,
  136. "initial_price": initialPrice,
  137. "is_rises": isRise,
  138. "rises": rises,
  139. "now_basal_rate": nowBasalRate,
  140. "now_team_rate": NowTeamRate,
  141. "user_count": userCount,
  142. "nickname": user.Nickname,
  143. },
  144. }))
  145. resp := md.HomePageResp{
  146. IsSign: isSign,
  147. SignEndTime: signCountdown,
  148. TotalEggEnergy: utils.Float64ToStr(totalEggEnergy),
  149. TotalActivePoints: utils.Float64ToStr(totalActivePoints),
  150. EggEnergyNowPrice: nowPrice,
  151. InitialPrice: initialPrice,
  152. IsRises: isRise,
  153. Rises: utils.Float64ToStr(rises),
  154. NowBasalRate: utils.Float64ToStr(nowBasalRate),
  155. NowTeamRate: utils.Float64ToStr(NowTeamRate),
  156. UserCount: userCount,
  157. NickName: user.Nickname,
  158. }
  159. e.OutSuc(c, resp, nil)
  160. return
  161. }
  162. // HomePageWatchAdRule
  163. // @Summary 蛋蛋星球-主页-视频奖励规则(获取)
  164. // @Tags 主页
  165. // @Description 视频奖励规则(获取)
  166. // @Accept json
  167. // @Produce json
  168. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  169. // @Success 200 {object} md.HomePageWatchAdRuleResp "具体数据"
  170. // @Failure 400 {object} md.Response "具体错误"
  171. // @Router /api/v1/homePage/adRule [get]
  172. func HomePageWatchAdRule(c *gin.Context) {
  173. val, exists := c.Get("user")
  174. if !exists {
  175. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  176. return
  177. }
  178. user, ok := val.(*model.User)
  179. if !ok {
  180. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  181. return
  182. }
  183. // 1. 查找基础设置
  184. energyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  185. basicSetting, err := energyBasicSettingDb.EggEnergyBasicSettingGetOne()
  186. if err != nil {
  187. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  188. return
  189. }
  190. // 2. 计算当前还能看几个广告
  191. recordsDb := implement.NewEggEnergyUserWatchRecordsDb(db.Db)
  192. records, err := recordsDb.EggEnergyUserWatchRecordsGetOneByParams(map[string]interface{}{
  193. "key": "uid",
  194. "value": user.Id,
  195. })
  196. if err != nil {
  197. return
  198. }
  199. var videoRewardSystem md2.VideoRewardSystemStruct
  200. utils.Unserialize([]byte(basicSetting.VideoRewardSystem), &videoRewardSystem)
  201. redisKey := fmt.Sprintf(md2.UserNextWatchAdDate, user.Id)
  202. var residueWatchAdNum int
  203. if records == nil {
  204. residueWatchAdNum = utils.StrToInt(videoRewardSystem.RewardTotalNum)
  205. } else {
  206. now := time.Now()
  207. residueWatchAdNum = records.ResidueWatchAdNum
  208. if records.NextWatchAdDate.Before(now) {
  209. //TODO::下一轮时间已在当前时间之前
  210. residueWatchAdNum = utils.StrToInt(videoRewardSystem.RewardTotalNum)
  211. records.ResidueWatchAdNum = residueWatchAdNum
  212. _, err = recordsDb.EggEnergyUserWatchRecordsUpdate(records.Id, records, "residue_watch_ad_num")
  213. if err != nil {
  214. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  215. return
  216. }
  217. cache.SetEx(redisKey, "", 60*60*24)
  218. }
  219. }
  220. nextWatchAdDate, _ := cache.GetString(redisKey)
  221. var nextWatchAdTimes = utils.TimeParseStd(nextWatchAdDate).Unix() - time.Now().Unix()
  222. utils.FilePutContents("nextWatchAdDate", utils.SerializeStr(map[string]interface{}{
  223. "nextWatchAdDate": nextWatchAdDate,
  224. "next_watch_ad_times": nextWatchAdTimes,
  225. "uid": utils.Int64ToStr(user.Id),
  226. "time": time.Now().Format("2006-01-02 15:04:05.000"),
  227. }))
  228. resp := md.HomePageWatchAdRuleResp{
  229. ResidueWatchAdNum: residueWatchAdNum,
  230. WatchAdRewardActivePoints: videoRewardSystem.RewardValue,
  231. NextWatchAdDate: nextWatchAdDate,
  232. EachRoundHour: videoRewardSystem.EachRoundHour,
  233. }
  234. e.OutSuc(c, resp, nil)
  235. }
  236. const HomePageRealTimeRedisKey = "Home_Page_Real_Time_Cache_Key"
  237. // RealTimePrice
  238. // @Summary 蛋蛋星球-主页-实时数据(获取)
  239. // @Tags 主页
  240. // @Description 实时数据(获取)
  241. // @Accept json
  242. // @Produce json
  243. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  244. // @Success 200 {object} md.RealTimePriceResp "具体数据"
  245. // @Failure 400 {object} md.Response "具体错误"
  246. // @Router /api/v1/homePage/realTimePrice [get]
  247. func RealTimePrice(c *gin.Context) {
  248. var isRises, userCount, risesValue, nowPrice interface{}
  249. var rises float64
  250. redisKey := HomePageRealTimeRedisKey
  251. redisValue, err := cache.GetString(redisKey)
  252. if err != nil {
  253. if err.Error() == "redigo: nil returned" {
  254. now := time.Now()
  255. err, rises, isRises, nowPrice, _ = svc.CalcTodayEggEnergyPriceRises(db.Db, now)
  256. if err != nil {
  257. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  258. return
  259. }
  260. userDb := implement.NewUserDb(db.Db)
  261. userCount, err = userDb.UserCount()
  262. if err != nil {
  263. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  264. return
  265. }
  266. decimalRate := decimal.NewFromInt(100) //百分比
  267. risesValue, _ = decimal.NewFromFloat(rises).Mul(decimalRate).Float64()
  268. // 缓存 5 s
  269. cache.SetEx(redisKey, utils.SerializeStr(map[string]interface{}{
  270. "user_count": userCount,
  271. "is_rises": isRises,
  272. "rises": risesValue,
  273. "egg_energy_now_price": nowPrice,
  274. }), 5)
  275. } else {
  276. e.OutErr(c, e.ERR, err.Error())
  277. return
  278. }
  279. } else {
  280. var result map[string]interface{}
  281. err1 := json.Unmarshal([]byte(redisValue), &result)
  282. if err1 != nil {
  283. e.OutErr(c, e.ERR, err1.Error())
  284. return
  285. }
  286. userCount = result["user_count"]
  287. isRises = result["is_rises"]
  288. risesValue = result["rises"]
  289. nowPrice = result["egg_energy_now_price"]
  290. }
  291. isRisesResp, ok := isRises.(bool)
  292. if !ok {
  293. e.OutErr(c, e.ERR, "断言失败")
  294. }
  295. resp := md.RealTimePriceResp{
  296. UserCount: utils.AnyToString(userCount),
  297. NowPrice: utils.AnyToString(nowPrice),
  298. IsRises: isRisesResp,
  299. Rises: utils.AnyToString(risesValue),
  300. }
  301. e.OutSuc(c, resp, nil)
  302. }
  303. // IsCanSignIn
  304. // @Summary 蛋蛋星球-主页-是否能签到(获取)
  305. // @Tags 主页
  306. // @Description 是否可以签到(获取)
  307. // @Accept json
  308. // @Produce json
  309. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  310. // @Success 200 {object} md.IsCanSignInResp "具体数据"
  311. // @Failure 400 {object} md.Response "具体错误"
  312. // @Router /api/v1/homePage/isCanSignIn [get]
  313. func IsCanSignIn(c *gin.Context) {
  314. val, exists := c.Get("user")
  315. if !exists {
  316. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  317. return
  318. }
  319. user, ok := val.(*model.User)
  320. if !ok {
  321. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  322. return
  323. }
  324. //1、查找 `OneCirclesPublicPlatoonBasicSetting` 基础设置
  325. energyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  326. eggEnergyBasicSetting, err := energyBasicSettingDb.EggEnergyBasicSettingGetOneByParams(map[string]interface{}{
  327. "key": "is_open",
  328. "value": 1,
  329. })
  330. if err != nil {
  331. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  332. return
  333. }
  334. var videoRewardSystem *md2.VideoRewardSystemStruct
  335. err = json.Unmarshal([]byte(eggEnergyBasicSetting.VideoRewardSystem), &videoRewardSystem)
  336. if err != nil {
  337. e.OutErr(c, e.ERR, err.Error())
  338. return
  339. }
  340. rewardTotalNum := utils.StrToInt(videoRewardSystem.RewardTotalNum)
  341. //2、查询当前用户观看视屏记录
  342. watchRecordsDb := implement.NewEggEnergyUserWatchRecordsDb(db.Db)
  343. userWatchRecords, err := watchRecordsDb.EggEnergyUserWatchRecordsGetOneByParams(map[string]interface{}{
  344. "key": "uid",
  345. "value": user.Id,
  346. })
  347. if err != nil {
  348. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  349. return
  350. }
  351. var isCan bool
  352. now := time.Now()
  353. if userWatchRecords != nil && userWatchRecords.ResidueWatchAdNum == rewardTotalNum && userWatchRecords.NextWatchAdDate.After(now) {
  354. isCan = true
  355. }
  356. utils.FilePutContents("HomePageIsCanSignIn", utils.SerializeStr(map[string]interface{}{
  357. "user_phone": user.Phone,
  358. "user_uid": user.Id,
  359. "is_can": isCan,
  360. "time": now.Format("2006-01-02 15:04:05"),
  361. }))
  362. resp := md.IsCanSignInResp{
  363. IsCan: isCan,
  364. }
  365. e.OutSuc(c, resp, nil)
  366. return
  367. }
  368. // IsCanGetRedPackage
  369. // @Summary 蛋蛋星球-主页-是否领取红包(获取)
  370. // @Tags 主页
  371. // @Description 是否可以领取红包(获取)
  372. // @Accept json
  373. // @Produce json
  374. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  375. // @Success 200 {object} md.IsCanGetRedPackageResp "具体数据"
  376. // @Failure 400 {object} md.Response "具体错误"
  377. // @Router /api/v1/homePage/isCanGetRedPackage [get]
  378. func IsCanGetRedPackage(c *gin.Context) {
  379. val, exists := c.Get("user")
  380. if !exists {
  381. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  382. return
  383. }
  384. user, ok := val.(*model.User)
  385. if !ok {
  386. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  387. return
  388. }
  389. // 1. 红包活动是否开启
  390. redPackageDb := implement.NewNewUserRedPackageDb(db.Db)
  391. redPackage, err := redPackageDb.NewUserRedPackageGetOne()
  392. if err != nil {
  393. e.OutErr(c, e.ERR, err.Error())
  394. return
  395. }
  396. // 2. 用户已有红包活动记录
  397. userRecordsDb := implement.NewNewUserRedPackageWithUserRecordsDb(db.Db)
  398. records, err := userRecordsDb.NewUserRedPackageWithUserRecordsGetLastPendingByParams(map[string]interface{}{
  399. "key": "uid",
  400. "value": user.Id,
  401. })
  402. if err != nil {
  403. e.OutErr(c, e.ERR_DB_ORM, nil)
  404. return
  405. }
  406. isCan := false
  407. if redPackage != nil {
  408. if redPackage.IsOpen == 1 && records != nil {
  409. isCan = true
  410. }
  411. }
  412. resp := md.IsCanGetRedPackageResp{
  413. IsCan: isCan,
  414. }
  415. e.OutSuc(c, resp, nil)
  416. }