code.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package http
  2. import (
  3. "fmt"
  4. "go-common/app/admin/main/coupon/model"
  5. "go-common/library/ecode"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/xstr"
  9. )
  10. func exportCode(c *bm.Context) {
  11. var (
  12. err error
  13. codes []string
  14. )
  15. arg := new(model.ArgCouponCode)
  16. if err = c.Bind(arg); err != nil {
  17. return
  18. }
  19. if codes, err = svc.ExportCode(c, arg); err != nil {
  20. c.JSON(nil, err)
  21. return
  22. }
  23. writer := c.Writer
  24. header := writer.Header()
  25. header.Add("Content-disposition", "attachment; filename="+fmt.Sprintf("%v", arg.BatchToken)+".txt")
  26. header.Add("Content-Type", "application/x-download;charset=utf-8")
  27. for _, v := range codes {
  28. writer.Write([]byte(fmt.Sprintf("%v\r\n", v)))
  29. }
  30. }
  31. func codePage(c *bm.Context) {
  32. var err error
  33. arg := new(model.ArgCouponCode)
  34. if err = c.Bind(arg); err != nil {
  35. return
  36. }
  37. c.JSON(svc.CodePage(c, arg))
  38. }
  39. func codeBlock(c *bm.Context) {
  40. var err error
  41. arg := new(model.ArgCouponCode)
  42. if err = c.Bind(arg); err != nil {
  43. return
  44. }
  45. c.JSON(nil, svc.CodeBlock(c, arg))
  46. }
  47. func codeUnBlock(c *bm.Context) {
  48. var err error
  49. arg := new(model.ArgCouponCode)
  50. if err = c.Bind(arg); err != nil {
  51. return
  52. }
  53. c.JSON(nil, svc.CodeUnBlock(c, arg))
  54. }
  55. func codeAddBatch(c *bm.Context) {
  56. var (
  57. err error
  58. token string
  59. )
  60. arg := new(model.ArgAllowanceBatchInfo)
  61. if err = c.Bind(arg); err != nil {
  62. log.Error("c.Bind err(%+v)", err)
  63. return
  64. }
  65. operator, ok := c.Get("username")
  66. if !ok {
  67. c.JSON(nil, ecode.AccessDenied)
  68. return
  69. }
  70. if _, ok := model.ProdLimMonthMap[arg.ProdLimMonth]; !ok {
  71. c.JSON(nil, ecode.RequestErr)
  72. return
  73. }
  74. b := new(model.CouponBatchInfo)
  75. b.AppID = arg.AppID
  76. b.Name = arg.Name
  77. if arg.MaxCount == 0 || arg.MaxCount > model.BatchCodeMaxCount {
  78. c.JSON(nil, ecode.CouponCodeMaxLimitErr)
  79. return
  80. }
  81. b.MaxCount = arg.MaxCount
  82. if arg.LimitCount == 0 {
  83. b.LimitCount = -1
  84. } else {
  85. b.LimitCount = arg.LimitCount
  86. }
  87. b.StartTime = arg.StartTime
  88. b.ExpireTime = arg.ExpireTime
  89. b.Operator = operator.(string)
  90. b.Amount = arg.Amount
  91. b.FullAmount = arg.FullAmount
  92. b.ExpireDay = arg.ExpireDay
  93. b.PlatformLimit = xstr.JoinInts(arg.PlatformLimit)
  94. b.ProdLimMonth = arg.ProdLimMonth
  95. b.ProdLimRenewal = arg.ProdLimRenewal
  96. b.CouponType = model.CouponAllowanceCode
  97. if token, err = svc.AddAllowanceBatchInfo(c, b); err != nil {
  98. c.JSON(nil, err)
  99. return
  100. }
  101. c.JSON(nil, svc.InitCodes(c, token))
  102. }
  103. func codeBatchModify(c *bm.Context) {
  104. var err error
  105. arg := new(model.ArgAllowanceBatchInfoModify)
  106. if err = c.Bind(arg); err != nil {
  107. return
  108. }
  109. operator, ok := c.Get("username")
  110. if !ok {
  111. c.JSON(nil, ecode.AccessDenied)
  112. return
  113. }
  114. b := new(model.CouponBatchInfo)
  115. b.AppID = arg.AppID
  116. b.Name = arg.Name
  117. if arg.LimitCount == 0 {
  118. b.LimitCount = -1
  119. } else {
  120. b.LimitCount = arg.LimitCount
  121. }
  122. b.Operator = operator.(string)
  123. b.PlatformLimit = xstr.JoinInts(arg.PlatformLimit)
  124. b.ProdLimMonth = arg.ProdLimMonth
  125. b.ProdLimRenewal = arg.ProdLimRenewal
  126. b.ID = arg.ID
  127. c.JSON(nil, svc.UpdateCodeBatchInfo(c, b))
  128. }
  129. func codeBatchList(c *bm.Context) {
  130. var err error
  131. arg := new(model.ArgBatchList)
  132. if err = c.Bind(arg); err != nil {
  133. return
  134. }
  135. arg.Type = model.CouponAllowanceCode
  136. c.JSON(svc.BatchList(c, arg))
  137. }
  138. func codeBatchBlock(c *bm.Context) {
  139. var err error
  140. arg := new(model.ArgAllowance)
  141. if err = c.Bind(arg); err != nil {
  142. return
  143. }
  144. operator, ok := c.Get("username")
  145. if !ok {
  146. c.JSON(nil, ecode.AccessDenied)
  147. return
  148. }
  149. c.JSON(nil, svc.UpdateBatchStatus(c, model.BatchStateBlock, operator.(string), arg.ID))
  150. }
  151. func codeBatchUnBlock(c *bm.Context) {
  152. var err error
  153. arg := new(model.ArgAllowance)
  154. if err = c.Bind(arg); err != nil {
  155. return
  156. }
  157. operator, ok := c.Get("username")
  158. if !ok {
  159. c.JSON(nil, ecode.AccessDenied)
  160. return
  161. }
  162. c.JSON(nil, svc.UpdateBatchStatus(c, model.BatchStateNormal, operator.(string), arg.ID))
  163. }