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

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