蛋蛋星球-客户端
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

835 wiersze
27 KiB

  1. package hdl
  2. import (
  3. "applet/app/db"
  4. "applet/app/e"
  5. "applet/app/md"
  6. "applet/app/utils"
  7. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  8. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  9. md3 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
  10. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  11. "encoding/json"
  12. "fmt"
  13. "github.com/gin-gonic/gin"
  14. "github.com/shopspring/decimal"
  15. "math"
  16. "strconv"
  17. "strings"
  18. "time"
  19. )
  20. // EggEnergyDetails
  21. // @Summary 蛋蛋星球-添加好友-蛋蛋能量明细(获取)
  22. // @Tags 添加好友
  23. // @Description 蛋蛋能量明细(获取)
  24. // @Accept json
  25. // @Produce json
  26. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  27. // @Param req body md.EggEnergyDetailReq true "分页类型必填"
  28. // @Success 200 {object} md.EggEnergyDetailResp "具体数据"
  29. // @Failure 400 {object} md.Response "具体错误"
  30. // @Router /api/v1/addFriend/eggEnergyDetails [POST]
  31. func EggEnergyDetails(c *gin.Context) {
  32. val, exists := c.Get("user")
  33. if !exists {
  34. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  35. return
  36. }
  37. user, ok := val.(*model.User)
  38. if !ok {
  39. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  40. return
  41. }
  42. var req *md.EggEnergyDetailReq
  43. if err1 := c.ShouldBindJSON(&req); err1 != nil {
  44. e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error())
  45. return
  46. }
  47. //1、查找 `OneCirclesPublicPlatoonBasicSetting` 基础设置
  48. energyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  49. eggEnergyBasicSetting, err := energyBasicSettingDb.EggEnergyBasicSettingGetOneByParams(map[string]interface{}{
  50. "key": "is_open",
  51. "value": 1,
  52. })
  53. if err != nil {
  54. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  55. return
  56. }
  57. var coinID int
  58. isAsc := false
  59. if req.Kind == "1" { // 待结
  60. coinID = eggEnergyBasicSetting.PersonEggEnergyCoinId
  61. } else { // 可用
  62. coinID = eggEnergyBasicSetting.TeamEggEnergyCoinId
  63. }
  64. if req.Asc == "1" {
  65. isAsc = true
  66. }
  67. direction := 0
  68. switch req.Direction {
  69. case "1":
  70. direction = 1
  71. case "2":
  72. direction = 2
  73. default:
  74. direction = 0
  75. }
  76. transferType := 0
  77. switch req.Type {
  78. case "1":
  79. transferType = 1
  80. case "2":
  81. transferType = 2
  82. default:
  83. transferType = 0
  84. }
  85. flowDb := implement.NewUserVirtualCoinFlowDb(db.Db)
  86. flows, total, err := flowDb.UserVirtualCoinFlowFindByCoinAndUser(req.Page, req.Limit, coinID, user.Id, "", "", direction, isAsc, transferType)
  87. if err != nil {
  88. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  89. return
  90. }
  91. var list = make([]map[string]string, 0)
  92. for _, flow := range flows {
  93. var tmp = map[string]string{
  94. "title": flow.Title,
  95. "time": flow.CreateAt,
  96. "type": utils.IntToStr(flow.Direction),
  97. "value": flow.Amount,
  98. "transfer_type": utils.IntToStr(flow.TransferType),
  99. }
  100. list = append(list, tmp)
  101. }
  102. //3、获取当前用户 蛋蛋能量(可用+结算)、 预估总价值
  103. var teamRewardSetting *md2.TeamRewardSettingStruct
  104. err = json.Unmarshal([]byte(eggEnergyBasicSetting.DirectPushReward), &teamRewardSetting)
  105. if err != nil {
  106. e.OutErr(c, e.ERR, err.Error())
  107. return
  108. }
  109. virtualAmountDb := implement.NewUserVirtualAmountDb(db.Db)
  110. personEggEnergy, err := virtualAmountDb.GetUserVirtualWalletBySession(user.Id, eggEnergyBasicSetting.PersonEggEnergyCoinId)
  111. if err != nil {
  112. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  113. return
  114. }
  115. teamEggEnergy, err := virtualAmountDb.GetUserVirtualWalletBySession(user.Id, eggEnergyBasicSetting.TeamEggEnergyCoinId)
  116. if err != nil {
  117. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  118. return
  119. }
  120. coreDataDb := implement.NewEggEnergyCoreDataDb(db.Db)
  121. coreData, err := coreDataDb.EggEnergyCoreDataGet()
  122. if err != nil {
  123. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  124. return
  125. }
  126. totalEggEnergy := utils.StrToFloat64(personEggEnergy.Amount) + utils.StrToFloat64(teamEggEnergy.Amount)
  127. totalEggEnergyPrice := totalEggEnergy * utils.StrToFloat64(coreData.NowPrice)
  128. transferTypeList := md.TransferTypeList{
  129. AvailableIntegralList: md.IntegralList{
  130. IncomeList: []md.TransferTypeNode{
  131. {
  132. Value: strconv.Itoa(md3.EggPlantEggEnergySignInSettlementPersonalRewardForUserVirtualCoinFlow),
  133. Name: md3.EggPlantEggEnergySignInSettlementPersonalReward,
  134. },
  135. {
  136. Value: strconv.Itoa(md3.EggPlantBalanceExchangeForEggEnergyForUserVirtualCoinFlow),
  137. Name: md3.EggPlantBalanceExchangeForEggEnergy,
  138. },
  139. {
  140. Value: strconv.Itoa(md3.EggPlantPersonalActiveCoinExchangeEggEnergyForUserVirtualCoinFlow),
  141. Name: md3.EggPlantPersonalActiveCoinExchangeEggEnergy,
  142. },
  143. {
  144. Value: strconv.Itoa(md3.EggPlantSettlementEggEnergyExchangeEggEnergyForUserVirtualCoinFlow),
  145. Name: md3.EggPlantSettlementEggEnergyExchangeEggEnergy,
  146. },
  147. },
  148. ExpendList: []md.TransferTypeNode{
  149. {
  150. Value: strconv.Itoa(md3.EggPlantEggEnergyExchangeForBalanceForUserVirtualCoinFlow),
  151. Name: md3.EggPlantEggEnergyExchangeForBalance,
  152. },
  153. },
  154. },
  155. SettlementIntegralList: md.IntegralList{
  156. IncomeList: []md.TransferTypeNode{
  157. {
  158. Value: strconv.Itoa(md3.EggPlantEggEnergySignInSettlementTeamRewardForUserVirtualCoinFlow),
  159. Name: md3.EggPlantEggEnergySignInSettlementTeamReward,
  160. },
  161. {
  162. Value: strconv.Itoa(md3.EggPlantTeamActiveCoinExchangeEggEnergyForUserVirtualCoinFlow),
  163. Name: md3.EggPlantTeamActiveCoinExchangeEggEnergy,
  164. },
  165. {
  166. Value: strconv.Itoa(md3.EggPlantSettlementStarLevelDividendsForUserVirtualCoinFlow),
  167. Name: md3.EggPlantSettlementStarLevelDividends,
  168. },
  169. },
  170. ExpendList: []md.TransferTypeNode{
  171. {
  172. Value: strconv.Itoa(md3.EggPlantSettlementEggEnergyExchangeTobeEggEnergyForUserVirtualCoinFlow),
  173. Name: md3.EggPlantSettlementEggEnergyExchangeEggEnergy,
  174. },
  175. },
  176. },
  177. }
  178. resp := md.EggEnergyDetailResp{
  179. PersonEggEnergy: personEggEnergy.Amount,
  180. TeamEggEnergy: teamEggEnergy.Amount,
  181. TotalEggEnergy: utils.Float64ToStr(totalEggEnergy),
  182. TotalEggEnergyPrice: utils.Float64ToStr(totalEggEnergyPrice),
  183. List: list,
  184. TransferTypeList: transferTypeList,
  185. Paginate: md.Paginate{
  186. Limit: req.Limit,
  187. Page: req.Page,
  188. Total: total,
  189. },
  190. }
  191. e.OutSuc(c, resp, nil)
  192. }
  193. // EggPointDetails
  194. // @Summary 蛋蛋星球-添加好友-蛋蛋积分明细(获取)
  195. // @Tags 添加好友
  196. // @Description 蛋蛋积分明细(获取)
  197. // @Accept json
  198. // @Produce json
  199. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  200. // @Param req body md.EggPointDetailsReq true "分页类型必填"
  201. // @Success 200 {object} md.EggPointDetailsResp "具体数据"
  202. // @Failure 400 {object} md.Response "具体错误"
  203. // @Router /api/v1/addFriend/eggPointDetails [POST]
  204. func EggPointDetails(c *gin.Context) {
  205. val, exists := c.Get("user")
  206. if !exists {
  207. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  208. return
  209. }
  210. user, ok := val.(*model.User)
  211. if !ok {
  212. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  213. return
  214. }
  215. var req *md.EggPointDetailsReq
  216. if err1 := c.ShouldBindJSON(&req); err1 != nil {
  217. e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error())
  218. return
  219. }
  220. //1、查找 `OneCirclesPublicPlatoonBasicSetting` 基础设置
  221. energyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  222. eggEnergyBasicSetting, err := energyBasicSettingDb.EggEnergyBasicSettingGetOneByParams(map[string]interface{}{
  223. "key": "is_open",
  224. "value": 1,
  225. })
  226. if err != nil {
  227. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  228. return
  229. }
  230. var coinID int
  231. isAsc := false
  232. if req.Kind == "1" { // 待结
  233. coinID = eggEnergyBasicSetting.PersonEggPointsCoinId
  234. } else { // 可用
  235. coinID = eggEnergyBasicSetting.TeamEggPointsCoinId
  236. }
  237. if req.Asc == "1" {
  238. isAsc = true
  239. }
  240. direction := 0
  241. switch req.Direction {
  242. case "1":
  243. direction = 1
  244. case "2":
  245. direction = 2
  246. default:
  247. direction = 0
  248. }
  249. transferType := 0
  250. switch req.Type {
  251. case "1":
  252. transferType = 1
  253. case "2":
  254. transferType = 2
  255. default:
  256. transferType = 0
  257. }
  258. flowDb := implement.NewUserVirtualCoinFlowDb(db.Db)
  259. flows, total, err := flowDb.UserVirtualCoinFlowFindByCoinAndUser(req.Page, req.Limit, coinID, user.Id, "", "", direction, isAsc, transferType)
  260. if err != nil {
  261. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  262. return
  263. }
  264. var list = make([]map[string]string, 0)
  265. for _, flow := range flows {
  266. var tmp = map[string]string{
  267. "title": flow.Title,
  268. "time": flow.CreateAt,
  269. "type": utils.IntToStr(flow.Direction),
  270. "value": flow.Amount,
  271. "transfer_type": utils.IntToStr(flow.TransferType),
  272. }
  273. list = append(list, tmp)
  274. }
  275. var teamRewardSetting *md2.TeamRewardSettingStruct
  276. err = json.Unmarshal([]byte(eggEnergyBasicSetting.DirectPushReward), &teamRewardSetting)
  277. if err != nil {
  278. e.OutErr(c, e.ERR, err.Error())
  279. return
  280. }
  281. virtualAmountDb := implement.NewUserVirtualAmountDb(db.Db)
  282. var totalEggPoints float64
  283. personEggPoints, err := virtualAmountDb.GetUserVirtualWalletBySession(user.Id, eggEnergyBasicSetting.PersonEggEnergyCoinId)
  284. if err != nil {
  285. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  286. return
  287. }
  288. teamEggPoints, err := virtualAmountDb.GetUserVirtualWalletBySession(user.Id, eggEnergyBasicSetting.TeamEggEnergyCoinId)
  289. if err != nil {
  290. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  291. return
  292. }
  293. totalEggPoints = utils.StrToFloat64(personEggPoints.Amount) + utils.StrToFloat64(teamEggPoints.Amount)
  294. coinDb := implement.NewVirtualCoinDb(db.Db)
  295. coin, err := coinDb.VirtualCoinGetOneByParams(map[string]interface{}{
  296. "key": "id",
  297. "value": eggEnergyBasicSetting.PersonEggPointsCoinId,
  298. })
  299. if err != nil {
  300. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  301. return
  302. }
  303. exchangeRatio, _ := decimal.NewFromString(coin.ExchangeRatio)
  304. totalEggPointsValue := decimal.NewFromFloat(totalEggPoints)
  305. totalEggPointsAmount := totalEggPointsValue.Div(exchangeRatio).String()
  306. transferTypeList := md.TransferTypeList{
  307. AvailableIntegralList: md.IntegralList{
  308. IncomeList: []md.TransferTypeNode{
  309. {
  310. Value: strconv.Itoa(md3.EggPlantWatchAdRewardPersonalActiveCoinForUserVirtualCoinFlow),
  311. Name: md3.EggPlantWatchAdRewardPersonalActiveCoin,
  312. },
  313. {
  314. Value: strconv.Itoa(md3.EggPlantConsumeOrdersGiveEggEnergyToPersonForUserVirtualCoinFlow),
  315. Name: md3.EggPlantConsumeOrdersGiveEggEnergyForPerson,
  316. },
  317. },
  318. ExpendList: []md.TransferTypeNode{
  319. {
  320. Value: strconv.Itoa(md3.EggPlantPersonalActiveCoinExchangeToBeEggEnergyForUserVirtualCoinFlow),
  321. Name: md3.EggPlantPersonalActiveCoinExchangeToBeEggEnergy,
  322. },
  323. },
  324. },
  325. SettlementIntegralList: md.IntegralList{
  326. IncomeList: []md.TransferTypeNode{
  327. {
  328. Value: strconv.Itoa(md3.EggPlantWatchAdRewardTeamActiveCoinForUserVirtualCoinFlow),
  329. Name: md3.EggPlantWatchAdRewardTeamActiveCoin,
  330. },
  331. {
  332. Value: strconv.Itoa(md3.EggPlantConsumeOrdersGiveEggEnergyToTeamForUserVirtualCoinFlow),
  333. Name: md3.EggPlantConsumeOrdersGiveEggEnergyForTeam,
  334. },
  335. },
  336. ExpendList: []md.TransferTypeNode{
  337. {
  338. Value: strconv.Itoa(md3.EggPlantTeamActiveCoinExchangeToBeEggEnergyForUserVirtualCoinFlow),
  339. Name: md3.EggPlantTeamActiveCoinExchangeToBeEggEnergy,
  340. },
  341. },
  342. },
  343. }
  344. resp := md.EggPointDetailsResp{
  345. PersonEggPoints: personEggPoints.Amount,
  346. TeamEggPoints: teamEggPoints.Amount,
  347. TotalEggPoints: utils.Float64ToStr(totalEggPoints),
  348. TotalEggPointsAmount: totalEggPointsAmount,
  349. List: list,
  350. TransferTypeList: transferTypeList,
  351. Paginate: md.Paginate{
  352. Limit: req.Limit,
  353. Page: req.Page,
  354. Total: total,
  355. },
  356. }
  357. e.OutSuc(c, resp, nil)
  358. }
  359. // BasalRate
  360. // @Summary 蛋蛋星球-添加好友-基础速率(获取)
  361. // @Tags 添加好友
  362. // @Description 基础速率(获取)
  363. // @Accept json
  364. // @Produce json
  365. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  366. // @Success 200 {object} md.BasalRateResp "具体数据"
  367. // @Failure 400 {object} md.Response "具体错误"
  368. // @Router /api/v1/addFriend/basalRate [GET]
  369. func BasalRate(c *gin.Context) {
  370. val, exists := c.Get("user")
  371. if !exists {
  372. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  373. return
  374. }
  375. user, ok := val.(*model.User)
  376. if !ok {
  377. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  378. return
  379. }
  380. energyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  381. eggEnergyBasicSetting, err := energyBasicSettingDb.EggEnergyBasicSettingGetOneByParams(map[string]interface{}{
  382. "key": "is_open",
  383. "value": 1,
  384. })
  385. if err != nil {
  386. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  387. return
  388. }
  389. coinID := eggEnergyBasicSetting.PersonEggEnergyCoinId
  390. coinDb := implement.NewVirtualCoinDb(db.Db)
  391. coin, err := coinDb.VirtualCoinGetOneByParams(map[string]interface{}{
  392. "key": "id",
  393. "value": coinID,
  394. })
  395. now := time.Now()
  396. nowStr := now.Format("2006-01-02 15:04:05")
  397. signInDb := implement.NewEggSignInDb(db.Db)
  398. has, eggSignIn, err := signInDb.EggSignINGetOneByTimeAndUid(nowStr, nowStr, user.Id, 0)
  399. if err != nil {
  400. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  401. return
  402. }
  403. if !has {
  404. resp := md.BasalRateResp{
  405. ConsumedTime: "",
  406. ConsumedEggEnergy: "",
  407. RemainingTime: "",
  408. RemainingEggEnergy: "",
  409. BasalRate: "",
  410. ConsumedEggPoint: "",
  411. EstimatedRevenue: "",
  412. SignCountdown: "0000:00:00",
  413. }
  414. e.OutSuc(c, resp, nil)
  415. return
  416. }
  417. ratio, err := decimal.NewFromString(coin.ExchangeRatio)
  418. if err != nil {
  419. e.OutErr(c, e.ERR_UNMARSHAL, err.Error())
  420. return
  421. }
  422. estimatePerSecondEggEnergyValue, err := decimal.NewFromString(eggSignIn.EstimatePerSecondEggEnergyValue)
  423. if err != nil {
  424. e.OutErr(c, e.ERR_UNMARSHAL, err.Error())
  425. return
  426. }
  427. // 消耗时间、已获得收益
  428. consumedTimeSec := now.Unix() - utils.TimeParseStd(eggSignIn.StartTime).Unix()
  429. consumedTime := decimal.NewFromInt(consumedTimeSec).Div(decimal.NewFromInt(60 * 60))
  430. consumedEggEnergy := decimal.NewFromInt(consumedTimeSec).Mul(estimatePerSecondEggEnergyValue)
  431. consumedAmount := consumedEggEnergy.Div(ratio)
  432. // 剩余时间、待收益
  433. remainingTimeSec := utils.TimeParseStd(eggSignIn.EndTime).Unix() - now.Unix()
  434. remainingTime := decimal.NewFromInt(remainingTimeSec).Div(decimal.NewFromInt(60 * 60))
  435. remainingEggEnergy := decimal.NewFromInt(remainingTimeSec).Mul(estimatePerSecondEggEnergyValue)
  436. remainingAmount := remainingEggEnergy.Div(ratio)
  437. // 预估收益
  438. estimatedRevenue := consumedAmount.Add(remainingAmount)
  439. // 基础速率 / 每小时
  440. basalRateDecimal, err := decimal.NewFromString(eggSignIn.EstimatePerSecondEggEnergyValue)
  441. if err != nil {
  442. e.OutErr(c, e.ERR_UNMARSHAL, err.Error())
  443. return
  444. }
  445. basalRate := basalRateDecimal.Mul(decimal.NewFromInt(60 * 60))
  446. // 收益倒计时
  447. duration := utils.TimeParseStd(eggSignIn.EndTime).Sub(now) // 计算时间差值
  448. hours := duration / time.Hour // 获取小时部分
  449. minutes := duration % time.Hour / time.Minute // 获取分钟部分(先除去小时后再乘以60)
  450. seconds := int64(duration/time.Second) % 60
  451. signCountdown := fmt.Sprintf("%d:%d:%d", hours, minutes, seconds) //收益倒计时
  452. resp := md.BasalRateResp{
  453. ConsumedTime: consumedTime.StringFixed(2),
  454. ConsumedEggEnergy: consumedEggEnergy.StringFixed(2),
  455. RemainingTime: remainingTime.StringFixed(2),
  456. RemainingEggEnergy: remainingEggEnergy.StringFixed(2),
  457. BasalRate: basalRate.StringFixed(2),
  458. ConsumedEggPoint: eggSignIn.TotalPersonEggPoints,
  459. EstimatedRevenue: estimatedRevenue.StringFixed(2),
  460. SignCountdown: signCountdown,
  461. }
  462. e.OutSuc(c, resp, nil)
  463. }
  464. // TotalRate
  465. // @Summary 蛋蛋星球-添加好友-总速率(获取)
  466. // @Tags 添加好友
  467. // @Description 总速率(获取)
  468. // @Accept json
  469. // @Produce json
  470. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  471. // @Success 200 {object} md.TotalRateResp "具体数据"
  472. // @Failure 400 {object} md.Response "具体错误"
  473. // @Router /api/v1/addFriend/totalRate [GET]
  474. func TotalRate(c *gin.Context) {
  475. val, exists := c.Get("user")
  476. if !exists {
  477. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  478. return
  479. }
  480. user, ok := val.(*model.User)
  481. if !ok {
  482. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  483. return
  484. }
  485. energyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  486. eggEnergyBasicSetting, err := energyBasicSettingDb.EggEnergyBasicSettingGetOneByParams(map[string]interface{}{
  487. "key": "is_open",
  488. "value": 1,
  489. })
  490. if err != nil {
  491. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  492. return
  493. }
  494. var teamRewardSetting *md2.TeamRewardSettingStruct
  495. err = json.Unmarshal([]byte(eggEnergyBasicSetting.DirectPushReward), &teamRewardSetting)
  496. if err != nil {
  497. e.OutErr(c, e.ERR, err.Error())
  498. return
  499. }
  500. now := time.Now()
  501. signInDb := implement.NewEggSignInDb(db.Db)
  502. // 计算用户是否在团队收益时间段内签到
  503. var oneRoundDuration = utils.StrToInt(teamRewardSetting.OneRoundDuration)
  504. startTime := now.Add(-time.Hour * time.Duration(oneRoundDuration)).Format("2006-01-02 15:04:05")
  505. exit, signIn, err := signInDb.EggSignInGetOne(startTime, now.Format("2006-01-02 15:04:05"), user.Id)
  506. if err != nil {
  507. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  508. return
  509. }
  510. var signEggEnergy = "0.00"
  511. if exit && utils.TimeParseStd(signIn.EndTime).After(now) {
  512. //获取预估每秒获得蛋蛋能量数
  513. estimatePerSecondEggEnergyValue, err1 := decimal.NewFromString(signIn.EstimatePerSecondEggEnergyValue)
  514. if err1 != nil {
  515. e.OutErr(c, e.ERR, err1.Error())
  516. return
  517. }
  518. signEggEnergy = estimatePerSecondEggEnergyValue.String()
  519. }
  520. nowBasalRate := utils.StrToFloat64(signEggEnergy) * 60 * 60 //每小时基础速率
  521. var estimateLevel int
  522. nowBasalRateValue := decimal.NewFromFloat(nowBasalRate)
  523. for {
  524. estimateLevel++
  525. rewardDecrementValue := utils.StrToFloat64(teamRewardSetting.RewardDecrementValue) / 100
  526. tmpRewardBase := decimal.NewFromFloat(math.Pow(rewardDecrementValue, float64(estimateLevel)))
  527. tmpReward := nowBasalRateValue.Mul(tmpRewardBase)
  528. rewardEndValue, _ := decimal.NewFromString(teamRewardSetting.RewardEndValue)
  529. if !tmpReward.GreaterThanOrEqual(rewardEndValue) {
  530. estimateLevel--
  531. break
  532. }
  533. if estimateLevel == 99 {
  534. e.OutErr(c, e.ERR, "奖励结束值设置有误")
  535. return
  536. }
  537. }
  538. relateDb := implement.NewUserRelateDb(db.Db)
  539. userRelates, err := relateDb.FindUserRelateByParentUid(user.Id, estimateLevel)
  540. if err != nil {
  541. e.OutErr(c, e.ERR, err.Error())
  542. return
  543. }
  544. var userRelatesUids []string
  545. var userRelatesMap = map[int64]model.UserRelate{}
  546. if userRelates != nil {
  547. for _, userRelate := range *userRelates {
  548. userRelatesUids = append(userRelatesUids, utils.Int64ToStr(userRelate.Uid))
  549. userRelatesMap[userRelate.Uid] = userRelate
  550. }
  551. }
  552. var eggSignIns []*model.EggSignIn
  553. if len(userRelatesUids) > 0 {
  554. eggSignIns, err = signInDb.EggSignInFindByParams(map[string]interface{}{
  555. "key": "uid",
  556. "value": userRelatesUids,
  557. })
  558. if err != nil {
  559. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  560. return
  561. }
  562. }
  563. var nowTeamRate float64 //每小时基础速率
  564. for _, vv := range eggSignIns {
  565. rewardDecrementValue := utils.StrToFloat64(teamRewardSetting.RewardDecrementValue) / 100
  566. tmpRewardBase := decimal.NewFromFloat(math.Pow(rewardDecrementValue, float64(userRelatesMap[vv.Uid].Level)))
  567. tmpReward := nowBasalRateValue.Mul(tmpRewardBase)
  568. rewardEndValue, _ := decimal.NewFromString(teamRewardSetting.RewardEndValue)
  569. if tmpReward.GreaterThanOrEqual(rewardEndValue) {
  570. tmpRewardValue, _ := tmpReward.Float64()
  571. nowTeamRate += tmpRewardValue
  572. }
  573. }
  574. resp := md.TotalRateResp{
  575. NowBasalRate: utils.Float64ToStr(nowBasalRate),
  576. NowTeamRate: utils.Float64ToStr(nowTeamRate),
  577. ConsumedPersonalEggPoint: signIn.TotalPersonEggPoints,
  578. ConsumedTeamEggPoint: signIn.TotalTeamEggPoints,
  579. NowTotalRate: utils.Float64ToStr(nowTeamRate + nowBasalRate),
  580. }
  581. e.OutSuc(c, resp, nil)
  582. }
  583. // MyFans
  584. // @Summary 蛋蛋星球-添加好友-粉丝团队-我的粉丝-团队加速速率(获取)
  585. // @Tags 添加好友
  586. // @Description 团队加速速率(获取)
  587. // @Accept json
  588. // @Produce json
  589. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  590. // @Success 200 {object} md.MyFansResp "具体数据"
  591. // @Failure 400 {object} md.Response "具体错误"
  592. // @Router /api/v1/addFriend/myFans [GET]
  593. func MyFans(c *gin.Context) {
  594. val, exists := c.Get("user")
  595. if !exists {
  596. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  597. return
  598. }
  599. user, ok := val.(*model.User)
  600. if !ok {
  601. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  602. return
  603. }
  604. // 找出公排中所有关联用户
  605. userRelateDb := implement.NewUserRelateDb(db.Db)
  606. total, err := userRelateDb.UserRelatesCountByParentUid(user.Id, 0)
  607. if err != nil {
  608. e.OutErr(c, e.ERR_DB_ORM, err)
  609. return
  610. }
  611. session := db.Db.NewSession()
  612. defer session.Close()
  613. now := time.Now()
  614. assistanceDb := implement.NewEggEnergyTeamAssistanceDb(db.Db)
  615. assistance, err := assistanceDb.EggEnergyTeamAssistanceGetOneBySession(session, user.Id, now.Format("2006-01-02"))
  616. if err != nil {
  617. e.OutErr(c, e.ERR_DB_ORM, err)
  618. return
  619. }
  620. var nowTeamAssistanceNum int
  621. var nowTeamAssistanceReward string
  622. if assistance != nil {
  623. nowTeamAssistanceNum = assistance.AssistedNum
  624. nowTeamAssistanceReward = assistance.RewardScore
  625. }
  626. resp := md.MyFansResp{
  627. NowTeamUser: total,
  628. NowTeamAssistanceNum: nowTeamAssistanceNum,
  629. NowTeamAssistanceReward: nowTeamAssistanceReward,
  630. }
  631. e.OutSuc(c, resp, nil)
  632. return
  633. }
  634. // MyFansUserList
  635. // @Summary 蛋蛋星球-添加好友-粉丝团队-我的粉丝-在线好友列表(获取)
  636. // @Tags 添加好友
  637. // @Description 在线好友列表(获取)
  638. // @Accept json
  639. // @Produce json
  640. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  641. // @Success 200 {object} md.MyFansUserListResp "具体数据"
  642. // @Failure 400 {object} md.Response "具体错误"
  643. // @Router /api/v1/addFriend/myFansUserList [GET]
  644. func MyFansUserList(c *gin.Context) {
  645. page := c.DefaultQuery("page", "")
  646. limit := c.DefaultQuery("page_size", "20")
  647. now := time.Now()
  648. val, exists := c.Get("user")
  649. if !exists {
  650. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  651. return
  652. }
  653. user, ok := val.(*model.User)
  654. if !ok {
  655. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  656. return
  657. }
  658. energyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  659. eggEnergyBasicSetting, err := energyBasicSettingDb.EggEnergyBasicSettingGetOneByParams(map[string]interface{}{
  660. "key": "is_open",
  661. "value": 1,
  662. })
  663. if err != nil {
  664. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  665. return
  666. }
  667. var teamRewardSetting *md2.TeamRewardSettingStruct
  668. err = json.Unmarshal([]byte(eggEnergyBasicSetting.DirectPushReward), &teamRewardSetting)
  669. if err != nil {
  670. e.OutErr(c, e.ERR, err.Error())
  671. return
  672. }
  673. var oneRoundDuration = utils.StrToInt(teamRewardSetting.OneRoundDuration)
  674. startTime := now.Add(-time.Hour * time.Duration(oneRoundDuration)).Format("2006-01-02 15:04:05")
  675. relateDb := implement.NewUserRelateDb(db.Db)
  676. userRelates, err := relateDb.FindUserRelateByParentUid(user.Id, 1)
  677. if err != nil {
  678. e.OutErr(c, e.ERR, err.Error())
  679. return
  680. }
  681. var userRelatesUids []string
  682. var userSignInMap = map[int64]interface{}{}
  683. var results []map[string]string
  684. if userRelates != nil {
  685. for _, userRelate := range *userRelates {
  686. userRelatesUids = append(userRelatesUids, utils.Int64ToStr(userRelate.Uid))
  687. }
  688. signInDb := implement.NewEggSignInDb(db.Db)
  689. eggSignIns, err1 := signInDb.EggSignInFindByTimeAndParams(startTime, "", 0, map[string]interface{}{
  690. "key": "uid",
  691. "value": userRelatesUids,
  692. })
  693. if err1 != nil {
  694. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  695. return
  696. }
  697. for _, eggSignIn := range eggSignIns {
  698. userSignInMap[eggSignIn.Uid] = *eggSignIn
  699. }
  700. var sql string
  701. sql = "SELECT user.id AS uid, user.avatar AS avatar, user.nickname AS nickname, user_relate.invite_time AS invite_time " +
  702. "FROM `user_relate` " +
  703. "LEFT JOIN `user` ON user_relate.uid = user.uid " +
  704. "where user_relate.uid IN(%s)"
  705. sql += " ORDER BY user_relate.invite_time DESC LIMIT %d, %d;"
  706. sql = fmt.Sprintf(sql, strings.Join(userRelatesUids, ","), (utils.StrToInt64(page)-1)*utils.StrToInt64(limit), utils.StrToInt64(limit))
  707. results, err = db.QueryNativeString(db.Db, sql)
  708. if err != nil {
  709. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  710. return
  711. }
  712. }
  713. var data []md.OneLineUserNode
  714. for _, v := range results {
  715. isOnline := false
  716. if userSignInMap[utils.AnyToInt64(v["uid"])] != nil {
  717. isOnline = true
  718. }
  719. data = append(data, md.OneLineUserNode{
  720. IsOnline: isOnline,
  721. AvatarURL: v["avatar_url"],
  722. Nickname: v["nickname"],
  723. InviteTime: v["invite_time"],
  724. })
  725. }
  726. resp := md.MyFansUserListResp{
  727. List: data,
  728. }
  729. e.OutSuc(c, resp, nil)
  730. return
  731. }
  732. // NineDimensionalSpace
  733. // @Summary 蛋蛋星球-添加好友-粉丝团队-九维空间(获取)
  734. // @Tags 添加好友
  735. // @Description 九维空间(获取)
  736. // @Accept json
  737. // @Produce json
  738. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  739. // @Success 200 {object} md.NineDimensionalSpaceResp "具体数据"
  740. // @Failure 400 {object} md.Response "具体错误"
  741. // @Router /api/v1/addFriend/nineDimensionalSpace [GET]
  742. func NineDimensionalSpace(c *gin.Context) {
  743. //1、 查找 `one_circles_public_platoon_basic_setting` 基础设置
  744. settingDb := implement.NewPublicPlatoonBasicSettingDb(db.Db)
  745. setting, err := settingDb.PublicPlatoonBasicSettingGetOneByParams(map[string]interface{}{
  746. "key": "is_open",
  747. "value": 1,
  748. })
  749. if err != nil {
  750. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  751. return
  752. }
  753. if setting == nil {
  754. e.OutErr(c, e.ERR_NO_DATA, nil)
  755. return
  756. }
  757. val, exists := c.Get("user")
  758. if !exists {
  759. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  760. return
  761. }
  762. user, ok := val.(*model.User)
  763. if !ok {
  764. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  765. return
  766. }
  767. uid := user.Id
  768. var list []md.SpaceListNode
  769. for i := 1; i <= setting.SeveralRows; i++ {
  770. var tmpSql = fmt.Sprintf("SELECT COUNT(*)AS total FROM `public_platoon_user_relation` WHERE father_uid%d = %d", i, uid)
  771. tmpNativeString, _ := db.QueryNativeString(db.Db, tmpSql)
  772. nowUserCount := utils.StrToInt64(tmpNativeString[0]["total"])
  773. maxCount := math.Pow(float64(setting.SeveralTimes), float64(i))
  774. if nowUserCount > int64(maxCount) {
  775. nowUserCount = int64(maxCount)
  776. }
  777. list = append(list, md.SpaceListNode{
  778. Name: utils.IntToStr(i) + "维",
  779. MaxCount: utils.Float64ToStr(maxCount),
  780. NowCount: utils.Int64ToStr(nowUserCount),
  781. })
  782. }
  783. //2、统计当前用户下多少人数
  784. sql := fmt.Sprintf("SELECT COUNT(*)AS total FROM `public_platoon_user_relation` WHERE father_uid1 = %d OR father_uid2= %d OR father_uid3= %d OR father_uid4= %d OR father_uid5= %d OR father_uid6= %d OR father_uid7= %d OR father_uid8= %d OR father_uid9= %d", uid, uid, uid, uid, uid, uid, uid, uid, uid)
  785. nativeString1, _ := db.QueryNativeString(db.Db, sql)
  786. hasUserCount := utils.StrToInt64(nativeString1[0]["total"])
  787. resp := md.NineDimensionalSpaceResp{
  788. SpaceTotalNums: "29523",
  789. SpaceNums: utils.Int64ToStr(hasUserCount),
  790. SpaceList: list,
  791. ALotOfNums: "29523",
  792. }
  793. e.OutSuc(c, resp, nil)
  794. return
  795. }