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

448 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. InitialPrice: initialPrice,
  151. NowBasalRate: utils.Float64ToStr(nowBasalRate),
  152. NowTeamRate: utils.Float64ToStr(NowTeamRate),
  153. NickName: user.Nickname,
  154. }
  155. e.OutSuc(c, resp, nil)
  156. return
  157. }
  158. // HomePageWatchAdRule
  159. // @Summary 蛋蛋星球-主页-视频奖励规则(获取)
  160. // @Tags 主页
  161. // @Description 视频奖励规则(获取)
  162. // @Accept json
  163. // @Produce json
  164. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  165. // @Success 200 {object} md.HomePageWatchAdRuleResp "具体数据"
  166. // @Failure 400 {object} md.Response "具体错误"
  167. // @Router /api/v1/homePage/adRule [get]
  168. func HomePageWatchAdRule(c *gin.Context) {
  169. val, exists := c.Get("user")
  170. if !exists {
  171. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  172. return
  173. }
  174. user, ok := val.(*model.User)
  175. if !ok {
  176. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  177. return
  178. }
  179. // 1. 查找基础设置
  180. energyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  181. basicSetting, err := energyBasicSettingDb.EggEnergyBasicSettingGetOne()
  182. if err != nil {
  183. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  184. return
  185. }
  186. // 2. 计算当前还能看几个广告
  187. recordsDb := implement.NewEggEnergyUserWatchRecordsDb(db.Db)
  188. records, err := recordsDb.EggEnergyUserWatchRecordsGetOneByParams(map[string]interface{}{
  189. "key": "uid",
  190. "value": user.Id,
  191. })
  192. if err != nil {
  193. return
  194. }
  195. var videoRewardSystem md2.VideoRewardSystemStruct
  196. utils.Unserialize([]byte(basicSetting.VideoRewardSystem), &videoRewardSystem)
  197. redisKey := fmt.Sprintf(md2.UserNextWatchAdDate, user.Id)
  198. var residueWatchAdNum int
  199. if records == nil {
  200. residueWatchAdNum = utils.StrToInt(videoRewardSystem.RewardTotalNum)
  201. } else {
  202. now := time.Now()
  203. residueWatchAdNum = records.ResidueWatchAdNum
  204. if records.NextWatchAdDate.Before(now) {
  205. //TODO::下一轮时间已在当前时间之前
  206. residueWatchAdNum = utils.StrToInt(videoRewardSystem.RewardTotalNum)
  207. records.ResidueWatchAdNum = residueWatchAdNum
  208. _, err = recordsDb.EggEnergyUserWatchRecordsUpdate(records.Id, records, "residue_watch_ad_num")
  209. if err != nil {
  210. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  211. return
  212. }
  213. cache.SetEx(redisKey, "", 60*60*24)
  214. }
  215. }
  216. nextWatchAdDate, _ := cache.GetString(redisKey)
  217. var nextWatchAdTimes = utils.TimeParseStd(nextWatchAdDate).Unix() - time.Now().Unix()
  218. utils.FilePutContents("nextWatchAdDate", utils.SerializeStr(map[string]interface{}{
  219. "nextWatchAdDate": nextWatchAdDate,
  220. "next_watch_ad_times": nextWatchAdTimes,
  221. "uid": utils.Int64ToStr(user.Id),
  222. "time": time.Now().Format("2006-01-02 15:04:05.000"),
  223. }))
  224. resp := md.HomePageWatchAdRuleResp{
  225. RewardTotalNum: videoRewardSystem.RewardTotalNum,
  226. ResidueWatchAdNum: residueWatchAdNum,
  227. WatchAdRewardActivePoints: videoRewardSystem.RewardValue,
  228. NextWatchAdDate: nextWatchAdDate,
  229. EachRoundHour: videoRewardSystem.EachRoundHour,
  230. }
  231. e.OutSuc(c, resp, nil)
  232. }
  233. const HomePageRealTimeRedisKey = "Home_Page_Real_Time_Cache_Key"
  234. // RealTimePrice
  235. // @Summary 蛋蛋星球-主页-实时数据(获取)
  236. // @Tags 主页
  237. // @Description 实时数据(获取)
  238. // @Accept json
  239. // @Produce json
  240. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  241. // @Success 200 {object} md.RealTimePriceResp "具体数据"
  242. // @Failure 400 {object} md.Response "具体错误"
  243. // @Router /api/v1/homePage/realTimePrice [get]
  244. func RealTimePrice(c *gin.Context) {
  245. var isRises, userCount, risesValue, nowPrice interface{}
  246. var rises float64
  247. redisKey := HomePageRealTimeRedisKey
  248. redisValue, err := cache.GetString(redisKey)
  249. if err != nil {
  250. if err.Error() == "redigo: nil returned" {
  251. now := time.Now()
  252. err, rises, isRises, nowPrice, _ = svc.CalcTodayEggEnergyPriceRises(db.Db, now)
  253. if err != nil {
  254. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  255. return
  256. }
  257. userDb := implement.NewUserDb(db.Db)
  258. userCount, err = userDb.UserCount()
  259. if err != nil {
  260. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  261. return
  262. }
  263. decimalRate := decimal.NewFromInt(100) //百分比
  264. risesValue, _ = decimal.NewFromFloat(rises).Mul(decimalRate).Float64()
  265. // 缓存 5 s
  266. cache.SetEx(redisKey, utils.SerializeStr(map[string]interface{}{
  267. "user_count": userCount,
  268. "is_rises": isRises,
  269. "rises": risesValue,
  270. "egg_energy_now_price": nowPrice,
  271. }), 5)
  272. } else {
  273. e.OutErr(c, e.ERR, err.Error())
  274. return
  275. }
  276. } else {
  277. var result map[string]interface{}
  278. err1 := json.Unmarshal([]byte(redisValue), &result)
  279. if err1 != nil {
  280. e.OutErr(c, e.ERR, err1.Error())
  281. return
  282. }
  283. userCount = result["user_count"]
  284. isRises = result["is_rises"]
  285. risesValue = result["rises"]
  286. nowPrice = result["egg_energy_now_price"]
  287. }
  288. isRisesResp, ok := isRises.(bool)
  289. if !ok {
  290. e.OutErr(c, e.ERR, "断言失败")
  291. }
  292. resp := md.RealTimePriceResp{
  293. UserCount: utils.AnyToInt64(userCount),
  294. NowPrice: utils.AnyToString(nowPrice),
  295. IsRises: isRisesResp,
  296. Rises: utils.AnyToString(risesValue),
  297. }
  298. e.OutSuc(c, resp, nil)
  299. }
  300. // IsCanSignIn
  301. // @Summary 蛋蛋星球-主页-是否能签到(获取)
  302. // @Tags 主页
  303. // @Description 是否可以签到(获取)
  304. // @Accept json
  305. // @Produce json
  306. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  307. // @Success 200 {object} md.IsCanSignInResp "具体数据"
  308. // @Failure 400 {object} md.Response "具体错误"
  309. // @Router /api/v1/homePage/isCanSignIn [get]
  310. func IsCanSignIn(c *gin.Context) {
  311. val, exists := c.Get("user")
  312. if !exists {
  313. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  314. return
  315. }
  316. user, ok := val.(*model.User)
  317. if !ok {
  318. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  319. return
  320. }
  321. //1、查找 `OneCirclesPublicPlatoonBasicSetting` 基础设置
  322. energyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  323. eggEnergyBasicSetting, err := energyBasicSettingDb.EggEnergyBasicSettingGetOneByParams(map[string]interface{}{
  324. "key": "is_open",
  325. "value": 1,
  326. })
  327. if err != nil {
  328. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  329. return
  330. }
  331. var videoRewardSystem *md2.VideoRewardSystemStruct
  332. err = json.Unmarshal([]byte(eggEnergyBasicSetting.VideoRewardSystem), &videoRewardSystem)
  333. if err != nil {
  334. e.OutErr(c, e.ERR, err.Error())
  335. return
  336. }
  337. rewardTotalNum := utils.StrToInt(videoRewardSystem.RewardTotalNum)
  338. //2、查询当前用户观看视屏记录
  339. watchRecordsDb := implement.NewEggEnergyUserWatchRecordsDb(db.Db)
  340. userWatchRecords, err := watchRecordsDb.EggEnergyUserWatchRecordsGetOneByParams(map[string]interface{}{
  341. "key": "uid",
  342. "value": user.Id,
  343. })
  344. if err != nil {
  345. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  346. return
  347. }
  348. var isCan bool
  349. now := time.Now()
  350. if userWatchRecords != nil && userWatchRecords.ResidueWatchAdNum == rewardTotalNum && userWatchRecords.NextWatchAdDate.After(now) {
  351. isCan = true
  352. }
  353. utils.FilePutContents("HomePageIsCanSignIn", utils.SerializeStr(map[string]interface{}{
  354. "user_phone": user.Phone,
  355. "user_uid": user.Id,
  356. "is_can": isCan,
  357. "time": now.Format("2006-01-02 15:04:05"),
  358. }))
  359. resp := md.IsCanSignInResp{
  360. IsCan: isCan,
  361. }
  362. e.OutSuc(c, resp, nil)
  363. return
  364. }
  365. // IsCanGetRedPackage
  366. // @Summary 蛋蛋星球-主页-是否领取红包(获取)
  367. // @Tags 主页
  368. // @Description 是否可以领取红包(获取)
  369. // @Accept json
  370. // @Produce json
  371. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  372. // @Success 200 {object} md.IsCanGetRedPackageResp "具体数据"
  373. // @Failure 400 {object} md.Response "具体错误"
  374. // @Router /api/v1/homePage/isCanGetRedPackage [get]
  375. func IsCanGetRedPackage(c *gin.Context) {
  376. val, exists := c.Get("user")
  377. if !exists {
  378. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  379. return
  380. }
  381. user, ok := val.(*model.User)
  382. if !ok {
  383. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  384. return
  385. }
  386. // 1. 红包活动是否开启
  387. redPackageDb := implement.NewNewUserRedPackageDb(db.Db)
  388. redPackage, err := redPackageDb.NewUserRedPackageGetOne()
  389. if err != nil {
  390. e.OutErr(c, e.ERR, err.Error())
  391. return
  392. }
  393. // 2. 用户已有红包活动记录
  394. userRecordsDb := implement.NewNewUserRedPackageWithUserRecordsDb(db.Db)
  395. records, err := userRecordsDb.NewUserRedPackageWithUserRecordsGetLastPendingByParams(map[string]interface{}{
  396. "key": "uid",
  397. "value": user.Id,
  398. })
  399. if err != nil {
  400. e.OutErr(c, e.ERR_DB_ORM, nil)
  401. return
  402. }
  403. isCan := false
  404. if redPackage != nil {
  405. if redPackage.IsOpen == 1 && records != nil {
  406. isCan = true
  407. }
  408. }
  409. resp := md.IsCanGetRedPackageResp{
  410. IsCan: isCan,
  411. }
  412. e.OutSuc(c, resp, nil)
  413. }