magic_order.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package admin
  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) StakeByAdmin(ctx *gin.Context) {
  11. c := s.FromContext(ctx)
  12. type request struct {
  13. UserId int64 `json:"userId"`
  14. Quantity decimal.Decimal `json:"quantity"` // 消耗币种的数量
  15. }
  16. req := new(request)
  17. if err := c.ShouldBindBodyWithJSON(req); err != nil {
  18. c.Fail(constant.ErrorParams)
  19. return
  20. }
  21. // 所有 post 接口 必须做 频繁控制
  22. if !c.RepeatFilter(fmt.Sprintf("Buy:%d", c.UserId()), time.Second*3) {
  23. c.Fail(constant.ErrorFrequent)
  24. return
  25. }
  26. // tips: 必须做非零限制
  27. if req.Quantity.LessThanOrEqual(decimal.Zero) {
  28. c.Fail(constant.ErrorResponseExceedAmountLimit)
  29. return
  30. }
  31. // 检查币种是否支持闪兑
  32. user, err := s.GetUserByUserId(req.UserId)
  33. if err != nil {
  34. c.Fail(constant.ErrorInfo)
  35. return
  36. }
  37. if !user.Enable {
  38. c.Fail(constant.ErrorAccount)
  39. return
  40. }
  41. currentProduct, err := s.FirstStakeProduct(s.DB().Where("pledge_mode", entity.StakeProductTypeCurrent))
  42. if err != nil {
  43. c.Fail(constant.ErrorNotOpen)
  44. return
  45. }
  46. if !currentProduct.Enable {
  47. c.Fail(constant.ErrorNotOpen)
  48. return
  49. }
  50. // todo: 开始质押
  51. err = s.CurrentStake(user, currentProduct, req.Quantity, c.AdminId())
  52. if err != nil {
  53. c.Fail(err.Error())
  54. return
  55. }
  56. //go s.PledgeStockHandler() // 触发股票业绩处理 与 直推加速释放
  57. c.Resp(nil)
  58. }