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

434 lines
13 KiB

  1. package hdl
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/db"
  5. "applet/app/e"
  6. "applet/app/md"
  7. "applet/app/utils"
  8. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  9. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  10. "code.fnuoos.com/EggPlanet/egg_system_rules.git"
  11. "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
  12. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
  13. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule"
  14. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy"
  15. md3 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  16. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/svc"
  17. "fmt"
  18. "github.com/gin-gonic/gin"
  19. "github.com/shopspring/decimal"
  20. "time"
  21. )
  22. // PointsCenterGetBasic
  23. // @Summary 蛋蛋星球-积分中心-基础信息(获取)
  24. // @Tags 积分中心
  25. // @Description 基础信息(获取)
  26. // @Accept json
  27. // @Produce json
  28. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  29. // @Success 200 {object} md.PointsCenterGetBasicResp "具体数据"
  30. // @Failure 400 {object} md.Response "具体错误"
  31. // @Router /api/v1/pointsCenter/getBasic [GET]
  32. func PointsCenterGetBasic(c *gin.Context) {
  33. val, exists := c.Get("user")
  34. if !exists {
  35. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  36. return
  37. }
  38. user, ok := val.(*model.User)
  39. if !ok {
  40. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  41. return
  42. }
  43. // 1. 获取蛋蛋能量货币类型
  44. settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  45. setting, err := settingDb.EggEnergyBasicSettingGetOne()
  46. if err != nil {
  47. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  48. return
  49. }
  50. coinID := setting.PersonEggEnergyCoinId
  51. // 2. 获取可兑蛋蛋能量
  52. virtualAmountDb := implement.NewUserVirtualAmountDb(db.Db)
  53. eggEnergyAmount, err := virtualAmountDb.GetUserVirtualWalletBySession(user.Id, coinID)
  54. if err != nil {
  55. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  56. return
  57. }
  58. // 3. 可用现金
  59. walletDb := implement.NewUserWalletDb(db.Db)
  60. wallet, err := walletDb.GetUserVirtualWallet(user.Id)
  61. if err != nil {
  62. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  63. return
  64. }
  65. resp := md.PointsCenterGetBasicResp{
  66. AvailableEnergy: eggEnergyAmount.Amount,
  67. AvailableCash: wallet.Amount,
  68. }
  69. e.OutSuc(c, resp, nil)
  70. }
  71. // GetPriceCurve
  72. // @Summary 蛋蛋星球-积分中心-价格趋势(获取)
  73. // @Tags 积分中心
  74. // @Description 价格趋势(获取)
  75. // @Accept json
  76. // @Produce json
  77. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  78. // @Param kind query string false "1:按天 2:按小时 3:按周"
  79. // @Success 200 {object} md.GetPriceCurveResp "具体数据"
  80. // @Failure 400 {object} md.Response "具体错误"
  81. // @Router /api/v1/pointsCenter/priceCurve [GET]
  82. func GetPriceCurve(c *gin.Context) {
  83. kind := c.DefaultQuery("kind", "1")
  84. now := time.Now()
  85. priceDb := implement.NewEggEnergyPriceDb(db.Db)
  86. m, has, err := priceDb.EggEnergyPriceGetLastOne()
  87. if err != nil {
  88. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  89. return
  90. }
  91. if has == false {
  92. e.OutErr(c, e.ERR_NO_DATA, "未查询到数据")
  93. return
  94. }
  95. var yData []interface{}
  96. var xData []interface{}
  97. switch kind {
  98. case "1":
  99. var date = now.AddDate(0, 0, -30).Format("2006-01-02")
  100. 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"))
  101. results, _ := db.Db.QueryString(sql)
  102. for _, v := range results {
  103. tmpDate := utils.TimeParseStd(v["date"])
  104. yData = append(yData, v["price"])
  105. xData = append(xData, tmpDate.Format("01-02"))
  106. }
  107. yData = append(yData, m.Price)
  108. tmpDate := utils.TimeParseStd(m.Date)
  109. xData = append(xData, tmpDate.Format("01-02"))
  110. break
  111. case "2":
  112. for i := 29; i >= 1; i-- {
  113. date := now.Add(-time.Hour * 4 * time.Duration(i)).Format("2006-01-02")
  114. hour := now.Add(-time.Hour * 4 * time.Duration(i)).Hour()
  115. var sql = "SELECT price,date,hour FROM `egg_energy_price` WHERE HOUR = %d AND DATE = \"%s\" "
  116. sql = fmt.Sprintf(sql, hour, date)
  117. results, _ := db.Db.QueryString(sql)
  118. if results != nil {
  119. //if results[0]["date"] != now.Format("2006-01-02") {
  120. // continue
  121. //}
  122. yData = append(yData, results[0]["price"])
  123. xData = append(xData, results[0]["hour"]+":00")
  124. }
  125. }
  126. yData = append(yData, m.Price)
  127. xData = append(xData, m.Hour+":00")
  128. break
  129. case "3":
  130. var nums = 29
  131. for i := nums; i >= 1; i-- {
  132. var date = now.AddDate(0, 0, -7*i).Format("2006-01-02")
  133. var sql = "SELECT price, date FROM `egg_energy_price` WHERE HOUR = 23 AND DATE = \"%s\" "
  134. sql = fmt.Sprintf(sql, date)
  135. results, _ := db.Db.QueryString(sql)
  136. if results != nil {
  137. tmpDate := utils.TimeParseStd(results[0]["date"])
  138. yData = append(yData, results[0]["price"])
  139. xData = append(xData, tmpDate.Format("01-02"))
  140. }
  141. }
  142. yData = append(yData, m.Price)
  143. tmpDate := utils.TimeParseStd(m.Date)
  144. xData = append(xData, tmpDate.Format("01-02"))
  145. break
  146. }
  147. e.OutSuc(c, md.GetPriceCurveResp{
  148. YData: yData,
  149. XData: xData,
  150. }, nil)
  151. return
  152. }
  153. // ExchangeEnergy
  154. // @Summary 蛋蛋星球-积分中心-能量兑换
  155. // @Tags 积分中心
  156. // @Description 能量兑换
  157. // @Accept json
  158. // @Produce json
  159. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  160. // @Param req body md.ExchangeEnergyReq true "需要兑换的能量值"
  161. // @Success 200 {string} "success"
  162. // @Failure 400 {object} md.Response "具体错误"
  163. // @Router /api/v1/pointsCenter/exchangeEnergy [POST]
  164. func ExchangeEnergy(c *gin.Context) {
  165. val, exists := c.Get("user")
  166. if !exists {
  167. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  168. return
  169. }
  170. user, ok := val.(*model.User)
  171. if !ok {
  172. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  173. return
  174. }
  175. var req *md.ExchangeEnergyReq
  176. if err1 := c.ShouldBindJSON(&req); err1 != nil {
  177. e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error())
  178. return
  179. }
  180. // 1. 获取蛋蛋能量货币类型
  181. settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  182. setting, err := settingDb.EggEnergyBasicSettingGetOne()
  183. if err != nil {
  184. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  185. return
  186. }
  187. coinID := setting.PersonEggEnergyCoinId
  188. session := db.Db.NewSession()
  189. defer session.Close()
  190. // 2. 计算蛋蛋能量兑换的金额
  191. egg_system_rules.Init(cfg.RedisAddr)
  192. eggEnergyCoreData, cb, err1 := svc.GetEggEnergyCoreData(db.Db)
  193. if err1 != nil {
  194. fmt.Println("EggEnergyAutoRecordPrices_ERR:::::", err1.Error())
  195. return
  196. }
  197. if cb != nil {
  198. defer cb() // 释放锁
  199. }
  200. energyAmount, err := decimal.NewFromString(req.EnergyAmount)
  201. if err != nil {
  202. e.OutErr(c, e.ERR_UNMARSHAL, err.Error())
  203. return
  204. }
  205. nowPrice, err := decimal.NewFromString(eggEnergyCoreData.NowPrice)
  206. if err != nil {
  207. e.OutErr(c, e.ERR_UNMARSHAL, err.Error())
  208. return
  209. }
  210. amount, ok := energyAmount.Div(nowPrice).Float64()
  211. if !ok {
  212. e.OutErr(c, e.ERR_UNMARSHAL, nil)
  213. return
  214. }
  215. // 3. 获取用户蛋蛋能量余额
  216. eggEnergyAmount, err := rule.GetUserCoinAmount(session, coinID, user.Id)
  217. if err != nil {
  218. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  219. return
  220. }
  221. // 4. 判断蛋蛋能量是否足够兑换
  222. if utils.StrToFloat64(eggEnergyAmount) < utils.StrToFloat64(req.EnergyAmount) {
  223. e.OutErr(c, e.ERR_BALANCE_NOT_ENOUGH, nil)
  224. }
  225. // 5. 调用降价公式
  226. err, calcPriceReductionFormula := egg_energy.CalcPriceReductionFormula(req.EnergyAmount, eggEnergyCoreData)
  227. if err != nil {
  228. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  229. return
  230. }
  231. // 6. 更改动态数据
  232. err = egg_energy.DealAvailableEggEnergyCoin(session, int(enum.EggEnergyExchangeAccountBalance), eggEnergyCoreData, md3.DealAvailableEggEnergyCoinReq{
  233. Amount: calcPriceReductionFormula.GetEggEnergyAmount,
  234. AmountFee: "",
  235. BeforePrice: calcPriceReductionFormula.BeforePrice,
  236. AfterPrice: calcPriceReductionFormula.AfterPrice,
  237. BeforePlanetTotalValue: calcPriceReductionFormula.BeforePlanetTotalValue,
  238. AfterPlanetTotalValue: calcPriceReductionFormula.AfterPlanetTotalValue,
  239. BeforeEnergyTotalNums: calcPriceReductionFormula.BeforeEnergyTotalNums,
  240. AfterEnergyTotalNums: calcPriceReductionFormula.AfterEnergyTotalNums,
  241. })
  242. if err != nil {
  243. fmt.Println("ActivityCoinAutoExchangeEggPersonEnergy:::::err111:::", err)
  244. _ = session.Rollback()
  245. return
  246. }
  247. // 7. 扣除蛋蛋能量
  248. dealUserVirtualCoinReq := md2.DealUserVirtualCoinReq{
  249. Kind: "sub",
  250. Title: enum.EggEnergyToExchangeToAmount.String(),
  251. TransferType: int(enum.EggEnergyToExchangeToAmount),
  252. CoinId: coinID,
  253. Uid: user.Id,
  254. Amount: utils.StrToFloat64(req.EnergyAmount),
  255. }
  256. err = rule.DealUserVirtualCoin(session, dealUserVirtualCoinReq)
  257. if err != nil {
  258. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  259. session.Rollback()
  260. return
  261. }
  262. // 8. 增加账户余额
  263. dealUserWalletReq := md2.DealUserWalletReq{
  264. Direction: "add",
  265. Kind: int(enum.EggEnergyExchangeAccountBalance),
  266. Title: enum.EggEnergyExchangeAccountBalance.String(),
  267. Uid: user.Id,
  268. Amount: amount,
  269. }
  270. err = rule.DealUserWallet(session, dealUserWalletReq)
  271. if err != nil {
  272. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  273. session.Rollback()
  274. return
  275. }
  276. err = session.Commit()
  277. if err != nil {
  278. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  279. return
  280. }
  281. e.OutSuc(c, "success", nil)
  282. }
  283. // GetContributionValue
  284. // @Summary 蛋蛋星球-积分中心-贡献值(获取)
  285. // @Tags 积分中心
  286. // @Description 贡献值(获取)
  287. // @Accept json
  288. // @Produce json
  289. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  290. // @Success 200 {object} md.GetContributionValueResp "具体数据"
  291. // @Failure 400 {object} md.Response "具体错误"
  292. // @Router /api/v1/pointsCenter/contributionValue [GET]
  293. func GetContributionValue(c *gin.Context) {
  294. val, exists := c.Get("user")
  295. if !exists {
  296. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  297. return
  298. }
  299. user, ok := val.(*model.User)
  300. if !ok {
  301. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  302. return
  303. }
  304. settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  305. setting, err := settingDb.EggEnergyBasicSettingGetOne()
  306. if err != nil {
  307. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  308. return
  309. }
  310. coinID := setting.ContributionCoinId
  311. virtualAmountDb := implement.NewUserVirtualAmountDb(db.Db)
  312. virtualAmount, err := virtualAmountDb.GetUserVirtualWalletBySession(user.Id, coinID)
  313. if err != nil {
  314. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  315. return
  316. }
  317. var contributionValue string
  318. if virtualAmount != nil {
  319. contributionValue = virtualAmount.Amount
  320. }
  321. coinDb := implement.NewVirtualCoinDb(db.Db)
  322. coin, err := coinDb.VirtualCoinGetOneByParams(map[string]interface{}{
  323. "key": "id",
  324. "value": coinID,
  325. })
  326. if err != nil {
  327. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  328. return
  329. }
  330. var ratio string
  331. if coin != nil {
  332. ratio = coin.ExchangeRatio
  333. }
  334. resp := md.GetContributionValueResp{
  335. ContributionValue: contributionValue,
  336. Ratio: ratio,
  337. }
  338. e.OutSuc(c, resp, nil)
  339. }
  340. // GetContributionValueFlow
  341. // @Summary 蛋蛋星球-积分中心-贡献值明细(获取)
  342. // @Tags 积分中心
  343. // @Description 贡献值明细(获取)
  344. // @Accept json
  345. // @Produce json
  346. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  347. // @Param limit query int true "每页大小"
  348. // @Param page query int true "页数"
  349. // @Success 200 {object} md.GetContributionValueFlowResp "具体数据"
  350. // @Failure 400 {object} md.Response "具体错误"
  351. // @Router /api/v1/pointsCenter/contributionValueFlow [GET]
  352. func GetContributionValueFlow(c *gin.Context) {
  353. page := c.DefaultQuery("page", "1")
  354. limit := c.DefaultQuery("limit", "10")
  355. val, exists := c.Get("user")
  356. if !exists {
  357. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  358. return
  359. }
  360. user, ok := val.(*model.User)
  361. if !ok {
  362. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  363. return
  364. }
  365. settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  366. setting, err := settingDb.EggEnergyBasicSettingGetOne()
  367. if err != nil {
  368. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  369. return
  370. }
  371. coinID := setting.ContributionCoinId
  372. flowDb := implement.NewUserVirtualCoinFlowDb(db.Db)
  373. flows, total, err := flowDb.UserVirtualCoinFlowFindByCoinAndUser(utils.StrToInt(page), utils.StrToInt(limit), coinID, user.Id, "", "", 0, false, 0)
  374. if err != nil {
  375. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  376. return
  377. }
  378. list := make([]md.ContributionValueFlowNode, len(flows))
  379. for i, flow := range flows {
  380. list[i].Title = flow.Title
  381. list[i].Amount = flow.Amount
  382. list[i].Direction = utils.IntToStr(flow.Direction)
  383. list[i].CreateAt = flow.CreateAt
  384. }
  385. resp := md.GetContributionValueFlowResp{
  386. List: list,
  387. Paginate: md.Paginate{
  388. Limit: utils.StrToInt(limit),
  389. Page: utils.StrToInt(page),
  390. Total: total,
  391. },
  392. }
  393. e.OutSuc(c, resp, nil)
  394. }
  395. func GetEggPointRecord(c *gin.Context) {
  396. }