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

868 lines
26 KiB

  1. package hdl
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/db"
  5. "applet/app/e"
  6. "applet/app/md"
  7. "applet/app/svc"
  8. "applet/app/utils"
  9. "applet/app/utils/cache"
  10. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  11. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  12. "code.fnuoos.com/EggPlanet/egg_system_rules.git"
  13. "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
  14. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
  15. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule"
  16. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy"
  17. enum2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/enum"
  18. md3 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  19. svc2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/svc"
  20. es2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/es"
  21. "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es"
  22. "context"
  23. "encoding/json"
  24. "errors"
  25. "fmt"
  26. "github.com/gin-gonic/gin"
  27. "github.com/olivere/elastic/v7"
  28. "github.com/shopspring/decimal"
  29. "time"
  30. )
  31. const PointsCenterCalcExchangeRedisKey = "Points_Center_Cache_Key"
  32. // PointsCenterGetBasic
  33. // @Summary 蛋蛋星球-积分中心-上部分基础信息(获取)
  34. // @Tags 积分中心
  35. // @Description 上部分基础信息(获取)
  36. // @Accept json
  37. // @Produce json
  38. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  39. // @Success 200 {object} md.PointsCenterGetBasicResp "具体数据"
  40. // @Failure 400 {object} md.Response "具体错误"
  41. // @Router /api/v1/pointsCenter/basic [GET]
  42. func PointsCenterGetBasic(c *gin.Context) {
  43. val, exists := c.Get("user")
  44. if !exists {
  45. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  46. return
  47. }
  48. user, ok := val.(*model.User)
  49. if !ok {
  50. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  51. return
  52. }
  53. settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  54. setting, err := settingDb.EggEnergyBasicSettingGetOne()
  55. if err != nil {
  56. e.OutErr(c, e.ERR_DB_ORM, nil)
  57. return
  58. }
  59. virtualAmountDb := implement.NewUserVirtualAmountDb(db.Db)
  60. // 蛋蛋能量 (个人 + 团队)
  61. eggPersonalEnergy, err := virtualAmountDb.GetUserVirtualWalletBySession(user.Id, setting.PersonEggEnergyCoinId)
  62. if err != nil {
  63. e.OutErr(c, e.ERR_DB_ORM, nil)
  64. return
  65. }
  66. eggTeamEnergy, err := virtualAmountDb.GetUserVirtualWalletBySession(user.Id, setting.TeamEggEnergyCoinId)
  67. if err != nil {
  68. e.OutErr(c, e.ERR_DB_ORM, nil)
  69. return
  70. }
  71. eggEnergy := utils.StrToFloat64(eggPersonalEnergy.Amount) + utils.StrToFloat64(eggTeamEnergy.Amount)
  72. // 蛋蛋能量价值
  73. coreDataDb := implement.NewEggEnergyCoreDataDb(db.Db)
  74. coreData, err := coreDataDb.EggEnergyCoreDataGet()
  75. if err != nil {
  76. e.OutErr(c, e.ERR_DB_ORM, nil)
  77. return
  78. }
  79. eggEnergyAmount := utils.StrToFloat64(coreData.NowPrice) * eggEnergy
  80. // 活跃积分(蛋蛋积分 = 团队 + 个人)
  81. eggPersonalPoint, err := virtualAmountDb.GetUserVirtualWalletBySession(user.Id, setting.PersonEggPointsCoinId)
  82. if err != nil {
  83. e.OutErr(c, e.ERR_DB_ORM, nil)
  84. return
  85. }
  86. eggTeamPoint, err := virtualAmountDb.GetUserVirtualWalletBySession(user.Id, setting.TeamEggPointsCoinId)
  87. if err != nil {
  88. e.OutErr(c, e.ERR_DB_ORM, nil)
  89. return
  90. }
  91. eggPoint := utils.StrToFloat64(eggPersonalPoint.Amount) + utils.StrToFloat64(eggTeamPoint.Amount)
  92. // 蛋蛋积分价值
  93. coinDb := implement.NewVirtualCoinDb(db.Db)
  94. coin, err := coinDb.VirtualCoinGetOneByParams(map[string]interface{}{
  95. "key": "id",
  96. "value": setting.PersonEggPointsCoinId,
  97. })
  98. if err != nil {
  99. e.OutErr(c, e.ERR_DB_ORM, nil)
  100. return
  101. }
  102. var eggPointValue decimal.Decimal
  103. if coin != nil {
  104. ratio, _ := decimal.NewFromString(coin.ExchangeRatio)
  105. eggPointValue = decimal.NewFromFloat(eggPoint).Div(ratio)
  106. }
  107. eggPointValue = decimal.NewFromInt(0)
  108. // 账户余额
  109. walletDb := implement.NewUserWalletDb(db.Db)
  110. wallet, err := walletDb.GetUserVirtualWallet(user.Id)
  111. if err != nil {
  112. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  113. return
  114. }
  115. resp := md.PointsCenterGetBasicResp{
  116. Energy: utils.Float64ToStr(eggEnergy),
  117. EnergyValue: utils.Float64ToStr(eggEnergyAmount),
  118. WalletAmount: wallet.Amount,
  119. EggPoint: utils.Float64ToStr(eggPoint),
  120. EggPointValue: eggPointValue.String(),
  121. }
  122. e.OutSuc(c, resp, nil)
  123. }
  124. // InitialData
  125. // @Summary 蛋蛋星球-积分中心-初始数据(获取)
  126. // @Tags 积分中心
  127. // @Description 初始数据(获取)
  128. // @Accept json
  129. // @Produce json
  130. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  131. // @Success 200 {object} md.InitialDataResp "具体数据"
  132. // @Failure 400 {object} md.Response "具体错误"
  133. // @Router /api/v1/pointsCenter/initialData [GET]
  134. func InitialData(c *gin.Context) {
  135. settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  136. setting, err := settingDb.EggEnergyBasicSettingGetOne()
  137. if err != nil {
  138. e.OutErr(c, e.ERR_DB_ORM, nil)
  139. return
  140. }
  141. resp := md.InitialDataResp{
  142. InitialPrice: setting.InitialPrice,
  143. TotalIssuanceAmount: setting.TotalIssuanceAmount,
  144. TotalTechnologyTeam: setting.TotalTechnologyTeam,
  145. TotalAngelInvestor: setting.TotalAngelInvestor,
  146. TotalOperateFund: setting.TotalOperateFund,
  147. TotalEcologicalDevelopment: setting.TotalEcologicalDevelopment,
  148. }
  149. e.OutSuc(c, resp, nil)
  150. }
  151. // DynamicData
  152. // @Summary 蛋蛋星球-积分中心-动态数据(获取)
  153. // @Tags 积分中心
  154. // @Description 动态数据(获取)
  155. // @Accept json
  156. // @Produce json
  157. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  158. // @Success 200 {object} md.DynamicDataResp "具体数据"
  159. // @Failure 400 {object} md.Response "具体错误"
  160. // @Router /api/v1/pointsCenter/dynamicData [GET]
  161. func DynamicData(c *gin.Context) {
  162. settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  163. setting, err := settingDb.EggEnergyBasicSettingGetOne()
  164. if err != nil {
  165. e.OutErr(c, e.ERR_DB_ORM, nil)
  166. return
  167. }
  168. // 1.用户持有总量
  169. redisKey1 := PointsCenterCalcExchangeRedisKey + "_1"
  170. redisValue1, err := cache.GetString(redisKey1)
  171. if err != nil {
  172. if err.Error() == "redigo: nil returned" {
  173. amountDb := implement.NewUserVirtualAmountDb(db.Db)
  174. total, err := amountDb.UserVirtualAmountGetSumByCoinKind(setting.PersonEggEnergyCoinId)
  175. if err != nil {
  176. e.OutErr(c, e.ERR_DB_ORM, nil)
  177. return
  178. }
  179. //将获取到的余额值缓存至redis
  180. redisValue1 = utils.Float64ToStr(total)
  181. cache.SetEx(redisKey1, redisValue1, 60)
  182. } else {
  183. e.OutErr(c, e.ERR, err.Error())
  184. return
  185. }
  186. }
  187. // 2.星级分红
  188. redisKey2 := PointsCenterCalcExchangeRedisKey + "_2"
  189. redisValue2, err := cache.GetString(redisKey2)
  190. if err != nil {
  191. if err.Error() == "redigo: nil returned" {
  192. coreDataDb := implement.NewEggEnergyCoreDataDb(db.Db)
  193. coreData, err := coreDataDb.EggEnergyCoreDataGet()
  194. if err != nil {
  195. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  196. return
  197. }
  198. redisValue2 = coreData.StarLevelDividends
  199. cache.SetEx(redisKey2, redisValue2, 60)
  200. } else {
  201. e.OutErr(c, e.ERR, err.Error())
  202. return
  203. }
  204. }
  205. // 3.发展委员会
  206. redisKey3 := PointsCenterCalcExchangeRedisKey + "_3"
  207. redisValue3, err := cache.GetString(redisKey3)
  208. if err != nil {
  209. if err.Error() == "redigo: nil returned" {
  210. coreDataDb := implement.NewEggEnergyCoreDataDb(db.Db)
  211. coreData, err := coreDataDb.EggEnergyCoreDataGet()
  212. if err != nil {
  213. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  214. return
  215. }
  216. redisValue3 = coreData.DevelopmentCommittee
  217. cache.SetEx(redisKey3, redisValue3, 60)
  218. } else {
  219. e.OutErr(c, e.ERR, err.Error())
  220. return
  221. }
  222. }
  223. // 4.公益基金
  224. redisKey4 := PointsCenterCalcExchangeRedisKey + "_4"
  225. redisValue4, err := cache.GetString(redisKey4)
  226. if err != nil {
  227. if err.Error() == "redigo: nil returned" {
  228. coreDataDb := implement.NewEggEnergyCoreDataDb(db.Db)
  229. coreData, err := coreDataDb.EggEnergyCoreDataGet()
  230. if err != nil {
  231. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  232. return
  233. }
  234. redisValue4 = coreData.PublicWelfareAndCharity
  235. cache.SetEx(redisKey4, redisValue4, 60)
  236. } else {
  237. e.OutErr(c, e.ERR, err.Error())
  238. return
  239. }
  240. }
  241. resp := md.DynamicDataResp{
  242. UserTotalHold: redisValue1,
  243. StarLevelDividends: redisValue2,
  244. DevelopmentCommittee: redisValue3,
  245. PublicWelfareAndCharity: redisValue4,
  246. }
  247. e.OutSuc(c, resp, nil)
  248. }
  249. // PointsExchangeGetBasic
  250. // @Summary 蛋蛋星球-积分中心-积分兑换基础信息(获取)
  251. // @Tags 积分中心
  252. // @Description 积分兑换基础信息(获取)
  253. // @Accept json
  254. // @Produce json
  255. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  256. // @Success 200 {object} md.PointsExchangeGetBasicResp "具体数据"
  257. // @Failure 400 {object} md.Response "具体错误"
  258. // @Router /api/v1/pointsCenter/pointsExchangeBasic [GET]
  259. func PointsExchangeGetBasic(c *gin.Context) {
  260. val, exists := c.Get("user")
  261. if !exists {
  262. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  263. return
  264. }
  265. user, ok := val.(*model.User)
  266. if !ok {
  267. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  268. return
  269. }
  270. // 1. 获取蛋蛋能量货币类型
  271. settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  272. setting, err := settingDb.EggEnergyBasicSettingGetOne()
  273. if err != nil {
  274. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  275. return
  276. }
  277. coinID := setting.PersonEggEnergyCoinId
  278. // 2. 获取可兑蛋蛋能量
  279. virtualAmountDb := implement.NewUserVirtualAmountDb(db.Db)
  280. eggEnergyAmount, err := virtualAmountDb.GetUserVirtualWalletBySession(user.Id, coinID)
  281. if err != nil {
  282. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  283. return
  284. }
  285. // 3. 可用现金
  286. walletDb := implement.NewUserWalletDb(db.Db)
  287. wallet, err := walletDb.GetUserVirtualWallet(user.Id)
  288. if err != nil {
  289. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  290. return
  291. }
  292. resp := md.PointsExchangeGetBasicResp{
  293. AvailableEnergy: eggEnergyAmount.Amount,
  294. AvailableCash: wallet.Amount,
  295. }
  296. e.OutSuc(c, resp, nil)
  297. }
  298. // GetPriceCurve
  299. // @Summary 蛋蛋星球-积分中心-价格趋势(获取)
  300. // @Tags 积分中心
  301. // @Description 价格趋势(获取)
  302. // @Accept json
  303. // @Produce json
  304. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  305. // @Param kind query string false "1:按天 2:按小时 3:按周"
  306. // @Success 200 {object} md.GetPriceCurveResp "具体数据"
  307. // @Failure 400 {object} md.Response "具体错误"
  308. // @Router /api/v1/pointsCenter/priceCurve [GET]
  309. func GetPriceCurve(c *gin.Context) {
  310. kind := c.DefaultQuery("kind", "1")
  311. now := time.Now()
  312. priceDb := implement.NewEggEnergyPriceDb(db.Db)
  313. m, has, err := priceDb.EggEnergyPriceGetLastOne()
  314. if err != nil {
  315. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  316. return
  317. }
  318. if has == false {
  319. e.OutErr(c, e.ERR_NO_DATA, "未查询到数据")
  320. return
  321. }
  322. var yData []interface{}
  323. var xData []interface{}
  324. switch kind {
  325. case "1":
  326. var date = now.AddDate(0, 0, -6).Format("2006-01-02")
  327. var sql = fmt.Sprintf("SELECT price,date FROM `egg_energy_price` WHERE HOUR = 23 AND DATE >= \"%s\" AND DATE != \"%s\" ORDER BY DATE ASC ", date, now.Format("2006-01-02"))
  328. results, _ := db.Db.QueryString(db.Db, sql)
  329. for _, v := range results {
  330. tmpDate := utils.String2TimeV2(v["date"])
  331. yData = append(yData, v["price"])
  332. xData = append(xData, tmpDate.Format("01-02"))
  333. }
  334. yData = append(yData, m.Price)
  335. tmpDate := utils.String2TimeV2(m.Date)
  336. xData = append(xData, tmpDate.Format("01-02"))
  337. break
  338. case "2":
  339. for i := 29; i >= 1; i-- {
  340. date := now.Add(-time.Hour * 4 * time.Duration(i)).Format("2006-01-02")
  341. hour := now.Add(-time.Hour * 4 * time.Duration(i)).Hour()
  342. var sql = "SELECT price,date,hour FROM `egg_energy_price` WHERE HOUR = %d AND DATE = \"%s\" "
  343. sql = fmt.Sprintf(sql, hour, date)
  344. results, _ := db.Db.QueryString(sql)
  345. if results != nil {
  346. //if results[0]["date"] != now.Format("2006-01-02") {
  347. // continue
  348. //}
  349. yData = append(yData, results[0]["price"])
  350. xData = append(xData, results[0]["hour"]+":00")
  351. }
  352. }
  353. yData = append(yData, m.Price)
  354. xData = append(xData, m.Hour+":00")
  355. break
  356. case "3":
  357. var nums = 6
  358. for i := nums; i >= 1; i-- {
  359. var date = now.AddDate(0, 0, -7*i).Format("2006-01-02")
  360. var sql = "SELECT price,date FROM `egg_energy_price` WHERE HOUR = 23 AND DATE = \"%s\" "
  361. sql = fmt.Sprintf(sql, date)
  362. results, _ := db.Db.QueryString(db.Db, sql)
  363. if results != nil {
  364. tmpDate := utils.String2TimeV2(results[0]["date"])
  365. yData = append(yData, results[0]["price"])
  366. xData = append(xData, tmpDate.Format("01-02"))
  367. }
  368. }
  369. yData = append(yData, m.Price)
  370. tmpDate := utils.String2TimeV2(m.Date)
  371. xData = append(xData, tmpDate.Format("01-02"))
  372. break
  373. }
  374. e.OutSuc(c, md.GetPriceCurveResp{
  375. YData: yData,
  376. XData: xData,
  377. }, nil)
  378. return
  379. }
  380. // ExchangeEnergy
  381. // @Summary 蛋蛋星球-积分中心-能量兑换
  382. // @Tags 积分中心
  383. // @Description 能量兑换
  384. // @Accept json
  385. // @Produce json
  386. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  387. // @Param req body md.ExchangeEnergyReq true "需要兑换的能量值"
  388. // @Success 200 {string} "success"
  389. // @Failure 400 {object} md.Response "具体错误"
  390. // @Router /api/v1/pointsCenter/exchangeEnergy [POST]
  391. func ExchangeEnergy(c *gin.Context) {
  392. val, exists := c.Get("user")
  393. if !exists {
  394. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  395. return
  396. }
  397. user, ok := val.(*model.User)
  398. if !ok {
  399. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  400. return
  401. }
  402. var req *md.ExchangeEnergyReq
  403. if err1 := c.ShouldBindJSON(&req); err1 != nil {
  404. e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error())
  405. return
  406. }
  407. // 1. 获取蛋蛋能量货币类型
  408. settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  409. setting, err := settingDb.EggEnergyBasicSettingGetOne()
  410. if err != nil {
  411. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  412. return
  413. }
  414. coinID := setting.PersonEggEnergyCoinId
  415. session := db.Db.NewSession()
  416. defer session.Close()
  417. // 2. 计算蛋蛋能量兑换的金额
  418. egg_system_rules.Init(cfg.RedisAddr)
  419. eggEnergyCoreData, cb, err1 := svc2.GetEggEnergyCoreData(db.Db)
  420. if err1 != nil {
  421. fmt.Println("EggEnergyAutoRecordPrices_ERR:::::", err1.Error())
  422. return
  423. }
  424. if cb != nil {
  425. defer cb() // 释放锁
  426. }
  427. // 3. 获取用户蛋蛋能量余额
  428. eggEnergyAmount, err := rule.GetUserCoinAmount(session, coinID, user.Id)
  429. if err != nil {
  430. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  431. return
  432. }
  433. // 4. 判断蛋蛋能量是否足够兑换
  434. if utils.StrToFloat64(eggEnergyAmount) < utils.StrToFloat64(req.EnergyAmount) {
  435. e.OutErr(c, e.ERR_BALANCE_NOT_ENOUGH, nil)
  436. return
  437. }
  438. // 5. 调用降价公式
  439. err, calcPriceReductionFormula := egg_energy.CalcPriceReductionFormula(req.EnergyAmount, eggEnergyCoreData, utils.IntToStr(user.Level), *setting)
  440. if err != nil {
  441. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  442. return
  443. }
  444. // 6. 更改动态数据
  445. err = egg_energy.DealAvailableEggEnergyCoin(session, int(enum2.EggEnergyExchangeAccountBalance), eggEnergyCoreData, md3.DealAvailableEggEnergyCoinReq{
  446. Amount: req.EnergyAmount,
  447. AmountFee: calcPriceReductionFormula.AmountFee,
  448. BeforePrice: calcPriceReductionFormula.BeforePrice,
  449. AfterPrice: calcPriceReductionFormula.AfterPrice,
  450. BeforePlanetTotalValue: calcPriceReductionFormula.BeforePlanetTotalValue,
  451. AfterPlanetTotalValue: calcPriceReductionFormula.AfterPlanetTotalValue,
  452. BeforeEnergyTotalNums: calcPriceReductionFormula.BeforeEnergyTotalNums,
  453. AfterEnergyTotalNums: calcPriceReductionFormula.AfterEnergyTotalNums,
  454. })
  455. if err != nil {
  456. fmt.Println("ActivityCoinAutoExchangeEggPersonEnergy:::::err111:::", err)
  457. _ = session.Rollback()
  458. return
  459. }
  460. // 7. 扣除蛋蛋能量
  461. dealUserVirtualCoinReq := md2.DealUserVirtualCoinReq{
  462. Kind: "sub",
  463. Title: enum.EggEnergyToExchangeToAmount.String(),
  464. TransferType: int(enum.EggEnergyToExchangeToAmount),
  465. CoinId: coinID,
  466. Uid: user.Id,
  467. Amount: utils.StrToFloat64(req.EnergyAmount),
  468. }
  469. err = rule.DealUserVirtualCoin(session, dealUserVirtualCoinReq)
  470. if err != nil {
  471. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  472. session.Rollback()
  473. return
  474. }
  475. // 8. 增加账户余额
  476. dealUserWalletReq := md2.DealUserWalletReq{
  477. Direction: "add",
  478. Kind: int(enum.EggEnergyExchangeAccountBalance),
  479. Title: enum.EggEnergyExchangeAccountBalance.String(),
  480. Uid: user.Id,
  481. Amount: utils.StrToFloat64(calcPriceReductionFormula.GetEggEnergyAmount),
  482. }
  483. err = rule.DealUserWallet(session, dealUserWalletReq)
  484. if err != nil {
  485. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  486. session.Rollback()
  487. return
  488. }
  489. err = session.Commit()
  490. if err != nil {
  491. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  492. return
  493. }
  494. e.OutSuc(c, "success", nil)
  495. }
  496. // GetContributionValue
  497. // @Summary 蛋蛋星球-积分中心-贡献值(获取)
  498. // @Tags 积分中心
  499. // @Description 贡献值(获取)
  500. // @Accept json
  501. // @Produce json
  502. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  503. // @Success 200 {object} md.GetContributionValueResp "具体数据"
  504. // @Failure 400 {object} md.Response "具体错误"
  505. // @Router /api/v1/pointsCenter/contributionValue [GET]
  506. func GetContributionValue(c *gin.Context) {
  507. val, exists := c.Get("user")
  508. if !exists {
  509. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  510. return
  511. }
  512. user, ok := val.(*model.User)
  513. if !ok {
  514. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  515. return
  516. }
  517. settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  518. setting, err := settingDb.EggEnergyBasicSettingGetOne()
  519. if err != nil {
  520. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  521. return
  522. }
  523. coinID := setting.ContributionCoinId
  524. virtualAmountDb := implement.NewUserVirtualAmountDb(db.Db)
  525. virtualAmount, err := virtualAmountDb.GetUserVirtualWalletBySession(user.Id, coinID)
  526. if err != nil {
  527. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  528. return
  529. }
  530. var contributionValue string
  531. if virtualAmount != nil {
  532. contributionValueDecimal, err := decimal.NewFromString(virtualAmount.Amount)
  533. if err != nil {
  534. e.OutErr(c, e.ERR_UNMARSHAL, err.Error())
  535. return
  536. }
  537. contributionValue = contributionValueDecimal.StringFixed(0)
  538. }
  539. coinDb := implement.NewVirtualCoinDb(db.Db)
  540. coin, err := coinDb.VirtualCoinGetOneByParams(map[string]interface{}{
  541. "key": "id",
  542. "value": coinID,
  543. })
  544. if err != nil {
  545. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  546. return
  547. }
  548. var ratio string
  549. if coin != nil {
  550. ratio = coin.ExchangeRatio
  551. }
  552. resp := md.GetContributionValueResp{
  553. ContributionValue: contributionValue,
  554. Ratio: ratio,
  555. }
  556. e.OutSuc(c, resp, nil)
  557. }
  558. // GetContributionValueFlow
  559. // @Summary 蛋蛋星球-积分中心-贡献值明细(获取)
  560. // @Tags 积分中心
  561. // @Description 贡献值明细(获取)
  562. // @Accept json
  563. // @Produce json
  564. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  565. // @Param limit query string true "每页大小"
  566. // @Param page query string true "页数"
  567. // @Success 200 {object} md.GetContributionValueFlowResp "具体数据"
  568. // @Failure 400 {object} md.Response "具体错误"
  569. // @Router /api/v1/pointsCenter/contributionValueFlow [GET]
  570. func GetContributionValueFlow(c *gin.Context) {
  571. page := c.DefaultQuery("page", "1")
  572. limit := c.DefaultQuery("limit", "10")
  573. val, exists := c.Get("user")
  574. if !exists {
  575. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  576. return
  577. }
  578. user, ok := val.(*model.User)
  579. if !ok {
  580. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  581. return
  582. }
  583. settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  584. setting, err := settingDb.EggEnergyBasicSettingGetOne()
  585. if err != nil {
  586. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  587. return
  588. }
  589. coinID := setting.ContributionCoinId
  590. flowDb := implement.NewUserVirtualCoinFlowDb(db.Db)
  591. flows, total, err := flowDb.UserVirtualCoinFlowFindByCoinAndUser(utils.StrToInt(page), utils.StrToInt(limit), coinID, user.Id, "", "", 0, false, 0)
  592. if err != nil {
  593. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  594. return
  595. }
  596. list := make([]md.ContributionValueFlowNode, len(flows))
  597. for i, flow := range flows {
  598. list[i].Title = flow.Title
  599. amountDecimal, err1 := decimal.NewFromString(flow.Amount)
  600. if err1 != nil {
  601. e.OutErr(c, e.ERR_UNMARSHAL, err1.Error())
  602. return
  603. }
  604. list[i].Amount = amountDecimal.StringFixed(0)
  605. list[i].Direction = utils.IntToStr(flow.Direction)
  606. list[i].CreateAt = flow.CreateAt
  607. }
  608. resp := md.GetContributionValueFlowResp{
  609. List: list,
  610. Paginate: md.Paginate{
  611. Limit: utils.StrToInt(limit),
  612. Page: utils.StrToInt(page),
  613. Total: total,
  614. },
  615. }
  616. e.OutSuc(c, resp, nil)
  617. }
  618. func GetPointMap(c *gin.Context) {
  619. val, exists := c.Get("user")
  620. if !exists {
  621. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  622. return
  623. }
  624. user, ok := val.(*model.User)
  625. if !ok {
  626. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  627. return
  628. }
  629. now := time.Now()
  630. // 1. 获取当期有效蛋蛋分
  631. nowIndex := md2.EggEnergyUserEggScoreEsAlias + "_" + es2.GetLatestEffectiveIndexFromAlias(now)
  632. boolQuery := elastic.NewBoolQuery()
  633. boolQuery.Filter(elastic.NewTermQuery("uid", user.Id))
  634. searchResult, err := es.EsClient.Search().
  635. Index(nowIndex).
  636. Query(boolQuery).
  637. Pretty(true).
  638. Do(context.Background())
  639. if searchResult == nil {
  640. e.OutErr(c, e.ERR_DB_ORM, errors.New("failed to get current egg score"))
  641. return
  642. }
  643. var results []md.UserEggFlowReqRespList
  644. if searchResult.Hits.TotalHits.Value != 0 {
  645. // 解析结果
  646. for _, hit := range searchResult.Hits.Hits {
  647. var doc md.UserEggFlowReqRespList
  648. err = json.Unmarshal(hit.Source, &doc)
  649. if err != nil {
  650. e.OutErr(c, e.ERR_UNMARSHAL, err.Error())
  651. return
  652. }
  653. results = append(results, doc)
  654. }
  655. }
  656. // nowScore := utils.Float64ToStr(results[0].ScoreValue)
  657. // todo 计算分数权重
  658. }
  659. // GetEggPointRecord
  660. // @Summary 蛋蛋星球-积分中心-蛋蛋分明细(获取)
  661. // @Tags 积分中心
  662. // @Description 蛋蛋分明细(获取)
  663. // @Accept json
  664. // @Produce json
  665. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  666. // @Param limit query string true "每页大小"
  667. // @Param page query string true "页数"
  668. // @Success 200 {object} md.GetEggPointRecordResp "具体数据"
  669. // @Failure 400 {object} md.Response "具体错误"
  670. // @Router /api/v1/pointsCenter/record [GET]
  671. func GetEggPointRecord(c *gin.Context) {
  672. // todo 待补充
  673. pageStr := c.DefaultQuery("page", "1")
  674. limitStr := c.DefaultQuery("limit", "5")
  675. page := utils.StrToInt(pageStr)
  676. limit := utils.StrToInt(limitStr)
  677. val, exists := c.Get("user")
  678. if !exists {
  679. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  680. return
  681. }
  682. user, ok := val.(*model.User)
  683. if !ok {
  684. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  685. return
  686. }
  687. now := time.Now()
  688. list, nowScore, indexNum, err := svc.GetEggPointRecordBase(now, user.Id, page, limit)
  689. if err != nil {
  690. resp := md.GetEggPointRecordResp{
  691. NowScore: "0",
  692. List: nil,
  693. Paginate: md.Paginate{
  694. Limit: limit,
  695. Page: page,
  696. Total: 0,
  697. },
  698. }
  699. e.OutSuc(c, resp, nil)
  700. return
  701. }
  702. resp := md.GetEggPointRecordResp{
  703. NowScore: nowScore,
  704. List: list,
  705. Paginate: md.Paginate{
  706. Limit: limit,
  707. Page: page,
  708. Total: int64(indexNum),
  709. },
  710. }
  711. e.OutSuc(c, resp, nil)
  712. }
  713. // GetEggEnergyFlow
  714. // @Summary 蛋蛋星球-积分中心-收支明细(获取)
  715. // @Tags 积分中心
  716. // @Description 收支明细(获取)
  717. // @Accept json
  718. // @Produce json
  719. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  720. // @Param limit query string true "每页大小"
  721. // @Param page query string true "页数"
  722. // @Param startAt query string false "开始时间"
  723. // @Param endAt query string false "结束时间"
  724. // @Param direction query string false "流水方向(1.收入 2.支出 0.全部)"
  725. // @Success 200 {object} md.GetEggEnergyFlowResp "具体数据"
  726. // @Failure 400 {object} md.Response "具体错误"
  727. // @Router /api/v1/pointsCenter/energyFlow [GET]
  728. func GetEggEnergyFlow(c *gin.Context) {
  729. pageStr := c.DefaultQuery("page", "1")
  730. limitStr := c.DefaultQuery("limit", "10")
  731. startAt := c.Query("startAt")
  732. endAt := c.Query("endAt")
  733. directionStr := c.Query("direction")
  734. val, exists := c.Get("user")
  735. if !exists {
  736. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  737. return
  738. }
  739. user, ok := val.(*model.User)
  740. if !ok {
  741. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  742. return
  743. }
  744. coinDb := implement.NewVirtualCoinDb(db.Db)
  745. coins, err := coinDb.VirtualCoinFindAll()
  746. if err != nil {
  747. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  748. return
  749. }
  750. if coins == nil {
  751. e.OutErr(c, e.ERR_NO_DATA, errors.New("未初始化货币"))
  752. return
  753. }
  754. coinsList := make([]map[string]interface{}, len(coins))
  755. coinsMap := map[int]string{}
  756. for i, coin := range coins {
  757. coinsList[i] = map[string]interface{}{
  758. "coinID": coin.Id,
  759. "name": coin.Name,
  760. }
  761. coinsMap[coin.Id] = coin.Name
  762. }
  763. direction := 0
  764. switch directionStr {
  765. case "1":
  766. direction = 1
  767. case "2":
  768. direction = 2
  769. }
  770. page := utils.StrToInt(pageStr)
  771. limit := utils.StrToInt(limitStr)
  772. flowDb := implement.NewUserVirtualCoinFlowDb(db.Db)
  773. flows, total, err := flowDb.UserVirtualCoinFlowFindByCoinAndUser(page, limit, 0, user.Id, startAt, endAt, direction, false, 0)
  774. if err != nil {
  775. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  776. return
  777. }
  778. list := make([]md.EggEnergyFlowNode, len(flows))
  779. for i, flow := range flows {
  780. list[i].Title = flow.Title
  781. list[i].Amount = flow.Amount
  782. list[i].Direction = flow.Direction
  783. list[i].CreateAt = flow.CreateAt
  784. list[i].Id = flow.Id
  785. list[i].BeforeAmount = flow.BeforeAmount
  786. list[i].AfterAmount = flow.AfterAmount
  787. list[i].CoinName = coinsMap[flow.CoinId]
  788. list[i].SysFee = flow.SysFee
  789. list[i].TransferType = enum.UserVirtualAmountFlowTransferType(flow.TransferType).String()
  790. }
  791. resp := md.GetEggEnergyFlowResp{
  792. List: list,
  793. Paginate: md.Paginate{
  794. Limit: limit,
  795. Page: page,
  796. Total: total,
  797. },
  798. }
  799. e.OutSuc(c, resp, nil)
  800. }