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

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