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

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