蛋蛋星球-客户端
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

hdl_points_center.go 26 KiB

4 settimane fa
4 settimane fa
1 settimana fa
1 settimana fa
1 settimana fa
1 settimana fa
1 settimana fa
1 settimana fa
1 settimana fa
2 settimane fa
1 mese fa
1 settimana fa
1 mese fa
1 mese fa
1 settimana fa
1 mese fa
1 mese fa
4 settimane fa
3 settimane fa
4 settimane fa
1 mese fa
3 settimane fa
1 mese fa
1 mese fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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.TimeParseDateStd(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.TimeParseDateStd(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]["date"]+" "+results[0]["hour"]+":00")
  350. }
  351. }
  352. yData = append(yData, m.Price)
  353. xData = append(xData, m.Date+" "+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. year, week, _, _ := utils.GetWeekInfoByDate(results[0]["date"])
  364. yData = append(yData, results[0]["price"])
  365. xData = append(xData, fmt.Sprintf("%s年%s周", year, week))
  366. }
  367. }
  368. yData = append(yData, m.Price)
  369. year, week, _, _ := utils.GetWeekInfoByDate(m.Date)
  370. xData = append(xData, fmt.Sprintf("%s年%s周", year, week))
  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, _ := energyAmount.Mul(nowPrice).Float64()
  437. // 3. 获取用户蛋蛋能量余额
  438. eggEnergyAmount, err := rule.GetUserCoinAmount(session, coinID, user.Id)
  439. if err != nil {
  440. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  441. return
  442. }
  443. // 4. 判断蛋蛋能量是否足够兑换
  444. if utils.StrToFloat64(eggEnergyAmount) < utils.StrToFloat64(req.EnergyAmount) {
  445. e.OutErr(c, e.ERR_BALANCE_NOT_ENOUGH, nil)
  446. return
  447. }
  448. // 5. 调用降价公式
  449. err, calcPriceReductionFormula := egg_energy.CalcPriceReductionFormula(req.EnergyAmount, eggEnergyCoreData)
  450. if err != nil {
  451. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  452. return
  453. }
  454. // 6. 更改动态数据
  455. err = egg_energy.DealAvailableEggEnergyCoin(session, int(enum.EggEnergyExchangeAccountBalance), eggEnergyCoreData, md3.DealAvailableEggEnergyCoinReq{
  456. Amount: calcPriceReductionFormula.GetEggEnergyAmount,
  457. AmountFee: "",
  458. BeforePrice: calcPriceReductionFormula.BeforePrice,
  459. AfterPrice: calcPriceReductionFormula.AfterPrice,
  460. BeforePlanetTotalValue: calcPriceReductionFormula.BeforePlanetTotalValue,
  461. AfterPlanetTotalValue: calcPriceReductionFormula.AfterPlanetTotalValue,
  462. BeforeEnergyTotalNums: calcPriceReductionFormula.BeforeEnergyTotalNums,
  463. AfterEnergyTotalNums: calcPriceReductionFormula.AfterEnergyTotalNums,
  464. })
  465. if err != nil {
  466. fmt.Println("ActivityCoinAutoExchangeEggPersonEnergy:::::err111:::", err)
  467. _ = session.Rollback()
  468. return
  469. }
  470. // 7. 扣除蛋蛋能量
  471. dealUserVirtualCoinReq := md2.DealUserVirtualCoinReq{
  472. Kind: "sub",
  473. Title: enum.EggEnergyToExchangeToAmount.String(),
  474. TransferType: int(enum.EggEnergyToExchangeToAmount),
  475. CoinId: coinID,
  476. Uid: user.Id,
  477. Amount: utils.StrToFloat64(req.EnergyAmount),
  478. }
  479. err = rule.DealUserVirtualCoin(session, dealUserVirtualCoinReq)
  480. if err != nil {
  481. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  482. session.Rollback()
  483. return
  484. }
  485. // 8. 增加账户余额
  486. dealUserWalletReq := md2.DealUserWalletReq{
  487. Direction: "add",
  488. Kind: int(enum.EggEnergyExchangeAccountBalance),
  489. Title: enum.EggEnergyExchangeAccountBalance.String(),
  490. Uid: user.Id,
  491. Amount: amount,
  492. }
  493. err = rule.DealUserWallet(session, dealUserWalletReq)
  494. if err != nil {
  495. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  496. session.Rollback()
  497. return
  498. }
  499. err = session.Commit()
  500. if err != nil {
  501. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  502. return
  503. }
  504. e.OutSuc(c, "success", nil)
  505. }
  506. // GetContributionValue
  507. // @Summary 蛋蛋星球-积分中心-贡献值(获取)
  508. // @Tags 积分中心
  509. // @Description 贡献值(获取)
  510. // @Accept json
  511. // @Produce json
  512. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  513. // @Success 200 {object} md.GetContributionValueResp "具体数据"
  514. // @Failure 400 {object} md.Response "具体错误"
  515. // @Router /api/v1/pointsCenter/contributionValue [GET]
  516. func GetContributionValue(c *gin.Context) {
  517. val, exists := c.Get("user")
  518. if !exists {
  519. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  520. return
  521. }
  522. user, ok := val.(*model.User)
  523. if !ok {
  524. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  525. return
  526. }
  527. settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  528. setting, err := settingDb.EggEnergyBasicSettingGetOne()
  529. if err != nil {
  530. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  531. return
  532. }
  533. coinID := setting.ContributionCoinId
  534. virtualAmountDb := implement.NewUserVirtualAmountDb(db.Db)
  535. virtualAmount, err := virtualAmountDb.GetUserVirtualWalletBySession(user.Id, coinID)
  536. if err != nil {
  537. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  538. return
  539. }
  540. var contributionValue string
  541. if virtualAmount != nil {
  542. contributionValueDecimal, err := decimal.NewFromString(virtualAmount.Amount)
  543. if err != nil {
  544. e.OutErr(c, e.ERR_UNMARSHAL, err.Error())
  545. return
  546. }
  547. contributionValue = contributionValueDecimal.StringFixed(0)
  548. }
  549. coinDb := implement.NewVirtualCoinDb(db.Db)
  550. coin, err := coinDb.VirtualCoinGetOneByParams(map[string]interface{}{
  551. "key": "id",
  552. "value": coinID,
  553. })
  554. if err != nil {
  555. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  556. return
  557. }
  558. var ratio string
  559. if coin != nil {
  560. ratio = coin.ExchangeRatio
  561. }
  562. resp := md.GetContributionValueResp{
  563. ContributionValue: contributionValue,
  564. Ratio: ratio,
  565. }
  566. e.OutSuc(c, resp, nil)
  567. }
  568. // GetContributionValueFlow
  569. // @Summary 蛋蛋星球-积分中心-贡献值明细(获取)
  570. // @Tags 积分中心
  571. // @Description 贡献值明细(获取)
  572. // @Accept json
  573. // @Produce json
  574. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  575. // @Param limit query string true "每页大小"
  576. // @Param page query string true "页数"
  577. // @Success 200 {object} md.GetContributionValueFlowResp "具体数据"
  578. // @Failure 400 {object} md.Response "具体错误"
  579. // @Router /api/v1/pointsCenter/contributionValueFlow [GET]
  580. func GetContributionValueFlow(c *gin.Context) {
  581. page := c.DefaultQuery("page", "1")
  582. limit := c.DefaultQuery("limit", "10")
  583. val, exists := c.Get("user")
  584. if !exists {
  585. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  586. return
  587. }
  588. user, ok := val.(*model.User)
  589. if !ok {
  590. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  591. return
  592. }
  593. settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  594. setting, err := settingDb.EggEnergyBasicSettingGetOne()
  595. if err != nil {
  596. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  597. return
  598. }
  599. coinID := setting.ContributionCoinId
  600. flowDb := implement.NewUserVirtualCoinFlowDb(db.Db)
  601. flows, total, err := flowDb.UserVirtualCoinFlowFindByCoinAndUser(utils.StrToInt(page), utils.StrToInt(limit), coinID, user.Id, "", "", 0, false, 0)
  602. if err != nil {
  603. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  604. return
  605. }
  606. list := make([]md.ContributionValueFlowNode, len(flows))
  607. for i, flow := range flows {
  608. list[i].Title = flow.Title
  609. amountDecimal, err1 := decimal.NewFromString(flow.Amount)
  610. if err1 != nil {
  611. e.OutErr(c, e.ERR_UNMARSHAL, err1.Error())
  612. return
  613. }
  614. list[i].Amount = amountDecimal.StringFixed(0)
  615. list[i].Direction = utils.IntToStr(flow.Direction)
  616. list[i].CreateAt = flow.CreateAt
  617. }
  618. resp := md.GetContributionValueFlowResp{
  619. List: list,
  620. Paginate: md.Paginate{
  621. Limit: utils.StrToInt(limit),
  622. Page: utils.StrToInt(page),
  623. Total: total,
  624. },
  625. }
  626. e.OutSuc(c, resp, nil)
  627. }
  628. func GetPointMap(c *gin.Context) {
  629. val, exists := c.Get("user")
  630. if !exists {
  631. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  632. return
  633. }
  634. user, ok := val.(*model.User)
  635. if !ok {
  636. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  637. return
  638. }
  639. now := time.Now()
  640. // 1. 获取当期有效蛋蛋分
  641. nowIndex := md2.EggEnergyUserEggScoreEsAlias + "_" + es2.GetLatestEffectiveIndexFromAlias(now)
  642. boolQuery := elastic.NewBoolQuery()
  643. boolQuery.Filter(elastic.NewTermQuery("uid", user.Id))
  644. searchResult, err := es.EsClient.Search().
  645. Index(nowIndex).
  646. Query(boolQuery).
  647. Pretty(true).
  648. Do(context.Background())
  649. if searchResult == nil {
  650. e.OutErr(c, e.ERR_DB_ORM, errors.New("failed to get current egg score"))
  651. return
  652. }
  653. var results []md.UserEggFlowReqRespList
  654. if searchResult.Hits.TotalHits.Value != 0 {
  655. // 解析结果
  656. for _, hit := range searchResult.Hits.Hits {
  657. var doc md.UserEggFlowReqRespList
  658. err = json.Unmarshal(hit.Source, &doc)
  659. if err != nil {
  660. e.OutErr(c, e.ERR_UNMARSHAL, err.Error())
  661. return
  662. }
  663. results = append(results, doc)
  664. }
  665. }
  666. // nowScore := utils.Float64ToStr(results[0].ScoreValue)
  667. // todo 计算分数权重
  668. }
  669. // GetEggPointRecord
  670. // @Summary 蛋蛋星球-积分中心-蛋蛋分明细(获取)
  671. // @Tags 积分中心
  672. // @Description 蛋蛋分明细(获取)
  673. // @Accept json
  674. // @Produce json
  675. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  676. // @Param limit query string true "每页大小"
  677. // @Param page query string true "页数"
  678. // @Success 200 {object} md.GetEggPointRecordResp "具体数据"
  679. // @Failure 400 {object} md.Response "具体错误"
  680. // @Router /api/v1/pointsCenter/record [GET]
  681. func GetEggPointRecord(c *gin.Context) {
  682. // todo 待补充
  683. pageStr := c.DefaultQuery("page", "1")
  684. limitStr := c.DefaultQuery("limit", "5")
  685. page := utils.StrToInt(pageStr)
  686. limit := utils.StrToInt(limitStr)
  687. val, exists := c.Get("user")
  688. if !exists {
  689. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  690. return
  691. }
  692. user, ok := val.(*model.User)
  693. if !ok {
  694. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  695. return
  696. }
  697. now := time.Now()
  698. list, nowScore, indexNum, err := svc.GetEggPointRecordBase(now, user.Id, page, limit)
  699. if err != nil {
  700. resp := md.GetEggPointRecordResp{
  701. NowScore: "0",
  702. List: nil,
  703. Paginate: md.Paginate{
  704. Limit: limit,
  705. Page: page,
  706. Total: 0,
  707. },
  708. }
  709. e.OutSuc(c, resp, nil)
  710. return
  711. }
  712. resp := md.GetEggPointRecordResp{
  713. NowScore: nowScore,
  714. List: list,
  715. Paginate: md.Paginate{
  716. Limit: limit,
  717. Page: page,
  718. Total: int64(indexNum),
  719. },
  720. }
  721. e.OutSuc(c, resp, nil)
  722. }
  723. // GetEggEnergyFlow
  724. // @Summary 蛋蛋星球-积分中心-收支明细(获取)
  725. // @Tags 积分中心
  726. // @Description 收支明细(获取)
  727. // @Accept json
  728. // @Produce json
  729. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  730. // @Param limit query string true "每页大小"
  731. // @Param page query string true "页数"
  732. // @Param startAt query string false "开始时间"
  733. // @Param endAt query string false "结束时间"
  734. // @Param direction query string false "流水方向(1.收入 2.支出 0.全部)"
  735. // @Success 200 {object} md.GetEggEnergyFlowResp "具体数据"
  736. // @Failure 400 {object} md.Response "具体错误"
  737. // @Router /api/v1/pointsCenter/energyFlow [GET]
  738. func GetEggEnergyFlow(c *gin.Context) {
  739. pageStr := c.DefaultQuery("page", "1")
  740. limitStr := c.DefaultQuery("limit", "10")
  741. startAt := c.Query("startAt")
  742. endAt := c.Query("endAt")
  743. directionStr := c.Query("direction")
  744. val, exists := c.Get("user")
  745. if !exists {
  746. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  747. return
  748. }
  749. user, ok := val.(*model.User)
  750. if !ok {
  751. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  752. return
  753. }
  754. coinDb := implement.NewVirtualCoinDb(db.Db)
  755. coins, err := coinDb.VirtualCoinFindAll()
  756. if err != nil {
  757. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  758. return
  759. }
  760. if coins == nil {
  761. e.OutErr(c, e.ERR_NO_DATA, errors.New("未初始化货币"))
  762. return
  763. }
  764. coinsList := make([]map[string]interface{}, len(coins))
  765. coinsMap := map[int]string{}
  766. for i, coin := range coins {
  767. coinsList[i] = map[string]interface{}{
  768. "coinID": coin.Id,
  769. "name": coin.Name,
  770. }
  771. coinsMap[coin.Id] = coin.Name
  772. }
  773. direction := 0
  774. switch directionStr {
  775. case "1":
  776. direction = 1
  777. case "2":
  778. direction = 2
  779. }
  780. page := utils.StrToInt(pageStr)
  781. limit := utils.StrToInt(limitStr)
  782. flowDb := implement.NewUserVirtualCoinFlowDb(db.Db)
  783. flows, total, err := flowDb.UserVirtualCoinFlowFindByCoinAndUser(page, limit, 0, user.Id, startAt, endAt, direction, false, 0)
  784. if err != nil {
  785. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  786. return
  787. }
  788. list := make([]md.EggEnergyFlowNode, len(flows))
  789. for i, flow := range flows {
  790. list[i].Title = flow.Title
  791. list[i].Amount = flow.Amount
  792. list[i].Direction = flow.Direction
  793. list[i].CreateAt = flow.CreateAt
  794. list[i].Id = flow.Id
  795. list[i].BeforeAmount = flow.BeforeAmount
  796. list[i].AfterAmount = flow.AfterAmount
  797. list[i].CoinName = coinsMap[flow.CoinId]
  798. list[i].SysFee = flow.SysFee
  799. list[i].TransferType = enum.UserVirtualAmountFlowTransferType(flow.TransferType).String()
  800. }
  801. resp := md.GetEggEnergyFlowResp{
  802. List: list,
  803. Paginate: md.Paginate{
  804. Limit: limit,
  805. Page: page,
  806. Total: total,
  807. },
  808. }
  809. e.OutSuc(c, resp, nil)
  810. }