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

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