蛋蛋星球-客户端
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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