order.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package magic
  2. import (
  3. "app/commons/constant"
  4. "app/commons/model/entity"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "github.com/shopspring/decimal"
  8. "time"
  9. )
  10. func (s *Server) StakeByUser(ctx *gin.Context) {
  11. c := s.FromContext(ctx)
  12. type request struct {
  13. Quantity decimal.Decimal `json:"quantity"` // 消耗币种的数量
  14. }
  15. req := new(request)
  16. if err := c.ShouldBindBodyWithJSON(req); err != nil {
  17. c.Fail(constant.ErrorParams)
  18. return
  19. }
  20. // 所有 post 接口 必须做 频繁控制
  21. if !c.RepeatFilter(fmt.Sprintf("StakeByUser:%d", c.UserId()), time.Second*3) {
  22. c.Fail(constant.ErrorFrequent)
  23. return
  24. }
  25. // tips: 必须做非零限制
  26. if req.Quantity.LessThanOrEqual(decimal.Zero) {
  27. c.Fail(constant.ErrorResponseExceedAmountLimit)
  28. return
  29. }
  30. user, err := s.GetUserByUserUid(c.Uid())
  31. if err != nil {
  32. c.Fail(constant.ErrorInfo)
  33. return
  34. }
  35. if !user.Enable {
  36. c.Fail(constant.ErrorAccount)
  37. return
  38. }
  39. paySymbol := constant.CoinSystemSymbol
  40. // todo: 检查用户是否合法
  41. userAsset, err := s.GetAssetBySymbol(c.UserId(), paySymbol)
  42. if err != nil {
  43. c.Fail(constant.ErrorAccount)
  44. return
  45. }
  46. // todo: 检查用户资产
  47. if userAsset.Balance.LessThan(req.Quantity) {
  48. c.Fail(constant.ErrorResponseBalanceNotEnough)
  49. return
  50. }
  51. currentProduct, err := s.FirstStakeProduct(s.DB().Where("pledge_mode", entity.StakeProductTypeCurrent))
  52. if err != nil {
  53. c.Fail(constant.ErrorNotOpen)
  54. return
  55. }
  56. if !currentProduct.Enable {
  57. c.Fail(constant.ErrorNotOpen)
  58. return
  59. }
  60. // todo: 开始质押
  61. err = s.CurrentStake(user, currentProduct, req.Quantity, 0)
  62. if err != nil {
  63. c.Fail(err.Error())
  64. return
  65. }
  66. go s.StakeHandler() // 异步处理相关质押业务
  67. c.Resp(nil)
  68. }