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

hdl_points_center.go 26 KiB

пре 1 месец
пре 1 месец
пре 1 месец
пре 4 недеља
пре 1 месец
пре 4 недеља
пре 1 месец
пре 1 месец
пре 1 месец
пре 1 месец
пре 1 месец
пре 1 месец
пре 1 месец
пре 1 месец
пре 2 недеља
пре 1 месец
пре 2 недеља
пре 1 месец
пре 1 месец
пре 1 месец
пре 1 месец
пре 4 недеља
пре 3 недеља
пре 4 недеља
пре 1 месец
пре 3 недеља
пре 1 месец
пре 1 месец
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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, _ := 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. contributionValue = virtualAmount.Amount
  543. }
  544. coinDb := implement.NewVirtualCoinDb(db.Db)
  545. coin, err := coinDb.VirtualCoinGetOneByParams(map[string]interface{}{
  546. "key": "id",
  547. "value": coinID,
  548. })
  549. if err != nil {
  550. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  551. return
  552. }
  553. var ratio string
  554. if coin != nil {
  555. ratio = coin.ExchangeRatio
  556. }
  557. resp := md.GetContributionValueResp{
  558. ContributionValue: contributionValue,
  559. Ratio: ratio,
  560. }
  561. e.OutSuc(c, resp, nil)
  562. }
  563. // GetContributionValueFlow
  564. // @Summary 蛋蛋星球-积分中心-贡献值明细(获取)
  565. // @Tags 积分中心
  566. // @Description 贡献值明细(获取)
  567. // @Accept json
  568. // @Produce json
  569. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  570. // @Param limit query string true "每页大小"
  571. // @Param page query string true "页数"
  572. // @Success 200 {object} md.GetContributionValueFlowResp "具体数据"
  573. // @Failure 400 {object} md.Response "具体错误"
  574. // @Router /api/v1/pointsCenter/contributionValueFlow [GET]
  575. func GetContributionValueFlow(c *gin.Context) {
  576. page := c.DefaultQuery("page", "1")
  577. limit := c.DefaultQuery("limit", "10")
  578. val, exists := c.Get("user")
  579. if !exists {
  580. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  581. return
  582. }
  583. user, ok := val.(*model.User)
  584. if !ok {
  585. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  586. return
  587. }
  588. settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  589. setting, err := settingDb.EggEnergyBasicSettingGetOne()
  590. if err != nil {
  591. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  592. return
  593. }
  594. coinID := setting.ContributionCoinId
  595. flowDb := implement.NewUserVirtualCoinFlowDb(db.Db)
  596. flows, total, err := flowDb.UserVirtualCoinFlowFindByCoinAndUser(utils.StrToInt(page), utils.StrToInt(limit), coinID, user.Id, "", "", 0, false, 0)
  597. if err != nil {
  598. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  599. return
  600. }
  601. list := make([]md.ContributionValueFlowNode, len(flows))
  602. for i, flow := range flows {
  603. list[i].Title = flow.Title
  604. list[i].Amount = flow.Amount
  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. }