allowance.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package http
  2. import (
  3. "bytes"
  4. "io"
  5. "mime/multipart"
  6. "go-common/app/admin/main/coupon/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/http/blademaster/binding"
  11. "go-common/library/xstr"
  12. )
  13. // batchadd add.
  14. func allowanceBatchadd(c *bm.Context) {
  15. var err error
  16. arg := new(model.ArgAllowanceBatchInfo)
  17. if err = c.Bind(arg); err != nil {
  18. log.Error("c.Bind err(%+v)", err)
  19. return
  20. }
  21. operator, ok := c.Get("username")
  22. if !ok {
  23. c.JSON(nil, ecode.AccessDenied)
  24. return
  25. }
  26. if _, ok := model.ProdLimMonthMap[arg.ProdLimMonth]; !ok {
  27. c.JSON(nil, ecode.RequestErr)
  28. return
  29. }
  30. b := new(model.CouponBatchInfo)
  31. b.AppID = arg.AppID
  32. b.Name = arg.Name
  33. if arg.MaxCount == 0 {
  34. b.MaxCount = -1
  35. } else {
  36. b.MaxCount = arg.MaxCount
  37. }
  38. if arg.LimitCount == 0 {
  39. b.LimitCount = -1
  40. } else {
  41. b.LimitCount = arg.LimitCount
  42. }
  43. b.StartTime = arg.StartTime
  44. b.ExpireTime = arg.ExpireTime
  45. b.Operator = operator.(string)
  46. b.Amount = arg.Amount
  47. b.FullAmount = arg.FullAmount
  48. b.ExpireDay = arg.ExpireDay
  49. b.PlatformLimit = xstr.JoinInts(arg.PlatformLimit)
  50. b.ProdLimMonth = arg.ProdLimMonth
  51. b.ProdLimRenewal = arg.ProdLimRenewal
  52. b.CouponType = model.CouponAllowance
  53. _, err = svc.AddAllowanceBatchInfo(c, b)
  54. c.JSON(nil, err)
  55. }
  56. // batchadd allowance modify.
  57. func allowanceBatchModify(c *bm.Context) {
  58. var err error
  59. arg := new(model.ArgAllowanceBatchInfoModify)
  60. if err = c.Bind(arg); err != nil {
  61. log.Error("c.Bind err(%+v)", err)
  62. return
  63. }
  64. operator, ok := c.Get("username")
  65. if !ok {
  66. c.JSON(nil, ecode.AccessDenied)
  67. return
  68. }
  69. b := new(model.CouponBatchInfo)
  70. b.AppID = arg.AppID
  71. b.Name = arg.Name
  72. if arg.MaxCount == 0 {
  73. b.MaxCount = -1
  74. } else {
  75. b.MaxCount = arg.MaxCount
  76. }
  77. if arg.LimitCount == 0 {
  78. b.LimitCount = -1
  79. } else {
  80. b.LimitCount = arg.LimitCount
  81. }
  82. b.Operator = operator.(string)
  83. b.PlatformLimit = xstr.JoinInts(arg.PlatformLimit)
  84. b.ProdLimMonth = arg.ProdLimMonth
  85. b.ProdLimRenewal = arg.ProdLimRenewal
  86. b.ID = arg.ID
  87. c.JSON(nil, svc.UpdateAllowanceBatchInfo(c, b))
  88. }
  89. // allowanceBlock .
  90. func allowanceBatchBlock(c *bm.Context) {
  91. var err error
  92. arg := new(model.ArgAllowance)
  93. if err = c.Bind(arg); err != nil {
  94. log.Error("c.Bind err(%+v)", err)
  95. return
  96. }
  97. operator, ok := c.Get("username")
  98. if !ok {
  99. c.JSON(nil, ecode.AccessDenied)
  100. return
  101. }
  102. c.JSON(nil, svc.UpdateBatchStatus(c, model.BatchStateBlock, operator.(string), arg.ID))
  103. }
  104. // allowanceUnBlock .
  105. func allowanceBatchUnBlock(c *bm.Context) {
  106. var err error
  107. arg := new(model.ArgAllowance)
  108. if err = c.Bind(arg); err != nil {
  109. log.Error("c.Bind err(%+v)", err)
  110. return
  111. }
  112. operator, ok := c.Get("username")
  113. if !ok {
  114. c.JSON(nil, ecode.AccessDenied)
  115. return
  116. }
  117. c.JSON(nil, svc.UpdateBatchStatus(c, model.BatchStateNormal, operator.(string), arg.ID))
  118. }
  119. func allowanceSalary(c *bm.Context) {
  120. var (
  121. f multipart.File
  122. h *multipart.FileHeader
  123. err error
  124. )
  125. arg := new(model.ArgAllowanceSalary)
  126. if err = c.BindWith(arg, binding.FormMultipart); err != nil {
  127. c.JSON(nil, ecode.RequestErr)
  128. return
  129. }
  130. if len(arg.Mids) <= 0 {
  131. f, h, err = c.Request.FormFile("file")
  132. if err != nil {
  133. c.JSON(nil, ecode.RequestErr)
  134. return
  135. }
  136. }
  137. c.JSON(svc.AllowanceSalary(c, f, h, arg.Mids, arg.BatchToken, arg.MsgType))
  138. }
  139. func batchInfo(c *bm.Context) {
  140. var (
  141. err error
  142. )
  143. arg := new(model.ArgAllowanceInfo)
  144. if err = c.Bind(arg); err != nil {
  145. log.Error("c.Bind err(%+v)", err)
  146. return
  147. }
  148. _, ok := c.Get("username")
  149. if !ok {
  150. c.JSON(nil, ecode.AccessDenied)
  151. return
  152. }
  153. c.JSON(svc.BatchInfo(c, arg.BatchToken))
  154. }
  155. // func allowancePage(c *bm.Context) {
  156. // var err error
  157. // arg := new(model.ArgAllowanceSearch)
  158. // if err = c.Bind(arg); err != nil {
  159. // log.Error("c.Bind err(%+v)", err)
  160. // return
  161. // }
  162. // _, ok := c.Get("username")
  163. // if !ok {
  164. // c.JSON(nil, ecode.AccessDenied)
  165. // return
  166. // }
  167. // c.JSON(svc.AllowancePage(c, arg))
  168. // }
  169. func allowanceList(c *bm.Context) {
  170. var err error
  171. arg := new(model.ArgAllowanceSearch)
  172. if err = c.Bind(arg); err != nil {
  173. log.Error("c.Bind err(%+v)", err)
  174. return
  175. }
  176. _, ok := c.Get("username")
  177. if !ok {
  178. c.JSON(nil, ecode.AccessDenied)
  179. return
  180. }
  181. c.JSON(svc.AllowanceList(c, arg))
  182. }
  183. func allowanceBlock(c *bm.Context) {
  184. var (
  185. err error
  186. )
  187. arg := new(model.ArgAllowanceState)
  188. if err = c.Bind(arg); err != nil {
  189. log.Error("c.Bind err(%+v)", err)
  190. return
  191. }
  192. _, ok := c.Get("username")
  193. if !ok {
  194. c.JSON(nil, ecode.AccessDenied)
  195. return
  196. }
  197. c.JSON(nil, svc.UpdateAllowanceState(c, arg.Mid, model.Block, arg.CouponToken))
  198. }
  199. func allowanceUnBlock(c *bm.Context) {
  200. var err error
  201. arg := new(model.ArgAllowanceState)
  202. if err = c.Bind(arg); err != nil {
  203. log.Error("c.Bind err(%+v)", err)
  204. return
  205. }
  206. _, ok := c.Get("username")
  207. if !ok {
  208. c.JSON(nil, ecode.AccessDenied)
  209. return
  210. }
  211. c.JSON(nil, svc.UpdateAllowanceState(c, arg.Mid, model.NotUsed, arg.CouponToken))
  212. }
  213. func batchSalaryCoupon(c *bm.Context) {
  214. var err error
  215. req := new(model.ArgBatchSalaryCoupon)
  216. if err = c.Bind(req); err != nil {
  217. log.Error("c.Bind err(%+v)", err)
  218. return
  219. }
  220. _, ok := c.Get("username")
  221. if !ok {
  222. c.JSON(nil, ecode.AccessDenied)
  223. return
  224. }
  225. c.JSON(nil, svc.ActivitySalaryCoupon(c, req))
  226. }
  227. func uploadFile(c *bm.Context) {
  228. var (
  229. f multipart.File
  230. err error
  231. )
  232. _, ok := c.Get("username")
  233. if !ok {
  234. c.JSON(nil, ecode.AccessDenied)
  235. return
  236. }
  237. arg := new(model.ArgUploadFile)
  238. if err = c.BindWith(arg, binding.FormMultipart); err != nil {
  239. c.JSON(nil, ecode.RequestErr)
  240. return
  241. }
  242. f, _, err = c.Request.FormFile("file")
  243. if err != nil {
  244. c.JSON(nil, ecode.RequestErr)
  245. return
  246. }
  247. defer f.Close()
  248. buf := new(bytes.Buffer)
  249. if _, err = io.Copy(buf, f); err != nil {
  250. c.JSON(nil, err)
  251. return
  252. }
  253. c.JSON(nil, svc.OutFile(c, buf.Bytes(), arg.FileURL))
  254. }