resouce.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package http
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "mime/multipart"
  7. "net/http"
  8. "strconv"
  9. "go-common/app/admin/main/vip/model"
  10. "go-common/library/ecode"
  11. bm "go-common/library/net/http/blademaster"
  12. )
  13. func queryPool(c *bm.Context) {
  14. var (
  15. arg = new(model.ResoucePoolBo)
  16. pools []*model.VipResourcePool
  17. count int
  18. err error
  19. )
  20. if err = c.Bind(arg); err != nil {
  21. return
  22. }
  23. if pools, count, err = vipSvc.QueryPool(c, arg); err != nil {
  24. c.JSON(nil, err)
  25. return
  26. }
  27. for _, v := range pools {
  28. var business *model.VipBusinessInfo
  29. if business, err = vipSvc.BusinessInfo(c, v.BusinessID); err != nil {
  30. return
  31. }
  32. v.BusinessName = business.BusinessName
  33. }
  34. dataMap := make(map[string]interface{})
  35. dataMap["item"] = pools
  36. dataMap["count"] = count
  37. c.JSONMap(dataMap, nil)
  38. }
  39. func getPool(c *bm.Context) {
  40. var (
  41. err error
  42. arg = new(model.ArgID)
  43. pool *model.VipResourcePool
  44. business *model.VipBusinessInfo
  45. )
  46. if err = c.Bind(arg); err != nil {
  47. return
  48. }
  49. if pool, err = vipSvc.PoolInfo(c, int(arg.ID)); err != nil {
  50. c.JSON(nil, err)
  51. return
  52. }
  53. if pool == nil {
  54. c.JSON(pool, nil)
  55. return
  56. }
  57. if business, err = vipSvc.BusinessInfo(c, pool.BusinessID); err != nil {
  58. return
  59. }
  60. if business != nil {
  61. pool.BusinessName = business.BusinessName
  62. }
  63. c.JSON(pool, nil)
  64. }
  65. func savePool(c *bm.Context) {
  66. arg := new(model.ResoucePoolBo)
  67. if err := c.Bind(arg); err != nil {
  68. return
  69. }
  70. if arg.ID == 0 {
  71. c.JSON(nil, vipSvc.AddPool(c, arg))
  72. return
  73. }
  74. c.JSON(nil, vipSvc.UpdatePool(c, arg))
  75. }
  76. func getBatch(c *bm.Context) {
  77. var (
  78. err error
  79. arg = new(model.ArgID)
  80. batch *model.VipResourceBatch
  81. pool *model.VipResourcePool
  82. )
  83. if err = c.Bind(arg); err != nil {
  84. return
  85. }
  86. if batch, err = vipSvc.BatchInfo(c, int(arg.ID)); err != nil {
  87. c.JSON(nil, err)
  88. return
  89. }
  90. if batch == nil {
  91. c.JSON(nil, nil)
  92. return
  93. }
  94. if pool, err = vipSvc.PoolInfo(c, batch.PoolID); err != nil {
  95. c.JSON(nil, err)
  96. return
  97. }
  98. if pool == nil {
  99. c.JSON(nil, ecode.VipPoolIDErr)
  100. return
  101. }
  102. vo := new(model.ResouceBatchVo)
  103. vo.Unit = batch.Unit
  104. vo.ID = batch.ID
  105. vo.PoolName = pool.PoolName
  106. vo.SurplusCount = batch.SurplusCount
  107. vo.PoolID = batch.PoolID
  108. vo.Count = batch.Count
  109. vo.StartTime = batch.StartTime
  110. vo.EndTime = batch.EndTime
  111. vo.DirectUseCount = batch.DirectUseCount
  112. vo.Ver = batch.Ver
  113. vo.CodeUseCount = batch.CodeUseCount
  114. c.JSON(vo, nil)
  115. }
  116. func queryBatch(c *bm.Context) {
  117. var (
  118. err error
  119. arg = new(model.ArgPoolID)
  120. )
  121. if err = c.Bind(arg); err != nil {
  122. return
  123. }
  124. c.JSON(vipSvc.BatchInfoOfPool(c, arg.PoolID))
  125. }
  126. func addBatch(c *bm.Context) {
  127. var arg = new(model.ResouceBatchBo)
  128. if err := c.Bind(arg); err != nil {
  129. return
  130. }
  131. c.JSON(nil, vipSvc.AddBatch(c, arg))
  132. }
  133. func saveBatch(c *bm.Context) {
  134. var arg = new(model.ArgReSource)
  135. if err := c.Bind(arg); err != nil {
  136. return
  137. }
  138. c.JSON(nil, vipSvc.UpdateBatch(c, arg.ID, arg.Increment, arg.StartTime, arg.EndTime))
  139. }
  140. func grantResouce(c *bm.Context) {
  141. var (
  142. req = c.Request
  143. mids []int
  144. username string
  145. file multipart.File
  146. err error
  147. failMids []int
  148. uc *http.Cookie
  149. )
  150. if file, _, err = req.FormFile("uploadFile"); err != nil {
  151. c.JSON(nil, ecode.RequestErr)
  152. return
  153. }
  154. r := bufio.NewReader(file)
  155. for {
  156. var buf []byte
  157. buf, _, err = r.ReadLine()
  158. if err == io.EOF {
  159. break
  160. }
  161. midStr := string(buf)
  162. if len(midStr) > 0 && midStr != "0" {
  163. mid := 0
  164. if mid, err = strconv.Atoi(midStr); err != nil {
  165. c.JSON(nil, ecode.RequestErr)
  166. return
  167. } else if err == nil {
  168. mids = append(mids, mid)
  169. }
  170. }
  171. }
  172. if uc, err = c.Request.Cookie("username"); err != nil {
  173. c.JSON(nil, ecode.RequestErr)
  174. return
  175. }
  176. username = uc.Value
  177. arg := new(struct {
  178. Mid int `form:"mid"`
  179. BatchID int64 `form:"batch_id" validate:"required"`
  180. Remark string `form:"remark" validate:"required"`
  181. })
  182. if err = c.Bind(arg); err != nil {
  183. return
  184. }
  185. if arg.Mid > 0 {
  186. mids = append(mids, arg.Mid)
  187. }
  188. if failMids, err = vipSvc.GrandResouce(c, arg.Remark, arg.BatchID, mids, username); err != nil {
  189. c.JSON(nil, err)
  190. return
  191. }
  192. c.JSON(failMids, nil)
  193. }
  194. func saveBatchCode(c *bm.Context) {
  195. arg := new(model.BatchCode)
  196. if err := c.Bind(arg); err != nil {
  197. return
  198. }
  199. username, exists := c.Get("username")
  200. if exists {
  201. arg.Operator = username.(string)
  202. }
  203. c.JSON(nil, vipSvc.SaveBatchCode(c, arg))
  204. }
  205. func batchCodes(c *bm.Context) {
  206. arg := new(model.ArgBatchCode)
  207. if err := c.Bind(arg); err != nil {
  208. return
  209. }
  210. pageArg := new(struct {
  211. PN int `form:"pn"`
  212. PS int `form:"ps"`
  213. })
  214. if err := c.Bind(pageArg); err != nil {
  215. return
  216. }
  217. res, total, err := vipSvc.SelBatchCode(c, arg, pageArg.PN, pageArg.PS)
  218. result := make(map[string]interface{})
  219. result["data"] = res
  220. result["total"] = total
  221. c.JSON(result, err)
  222. }
  223. func frozenCode(c *bm.Context) {
  224. arg := new(struct {
  225. ID int64 `form:"id" validate:"required"`
  226. Status int8 `form:"status" validate:"required"`
  227. })
  228. if err := c.Bind(arg); err != nil {
  229. return
  230. }
  231. c.JSON(nil, vipSvc.FrozenCode(c, arg.ID, arg.Status))
  232. }
  233. func frozenBatchCode(c *bm.Context) {
  234. arg := new(struct {
  235. ID int64 `form:"id" validate:"required"`
  236. Status int8 `form:"status" validate:"required"`
  237. })
  238. if err := c.Bind(arg); err != nil {
  239. return
  240. }
  241. c.JSON(nil, vipSvc.FrozenBatchCode(c, arg.ID, arg.Status))
  242. }
  243. func codes(c *bm.Context) {
  244. arg := new(model.ArgCode)
  245. if err := c.Bind(arg); err != nil {
  246. return
  247. }
  248. pageArg := new(struct {
  249. PS int `form:"ps"`
  250. Cursor int64 `form:"cursor"`
  251. Username string `form:"username"`
  252. })
  253. if err := c.Bind(pageArg); err != nil {
  254. return
  255. }
  256. username, exists := c.Get("username")
  257. if exists {
  258. pageArg.Username = username.(string)
  259. }
  260. res, cursor, pre, err := vipSvc.SelCode(c, arg, pageArg.Username, pageArg.Cursor, pageArg.PS)
  261. result := make(map[string]interface{})
  262. result["data"] = res
  263. result["cursor"] = cursor
  264. result["pre"] = pre
  265. c.JSON(result, err)
  266. }
  267. func exportCodes(c *bm.Context) {
  268. var (
  269. batchCodes []*model.BatchCode
  270. err error
  271. codes []string
  272. )
  273. arg := new(struct {
  274. ID int64 `form:"id" validate:"required"`
  275. })
  276. if err = c.Bind(arg); err != nil {
  277. return
  278. }
  279. batchIds := []int64{arg.ID}
  280. if batchCodes, err = vipSvc.SelBatchCodes(c, batchIds); err != nil {
  281. c.JSON(nil, err)
  282. return
  283. }
  284. if len(batchCodes) == 0 {
  285. c.JSON(nil, ecode.VipBatchIDErr)
  286. return
  287. }
  288. if codes, err = vipSvc.ExportCode(c, arg.ID); err != nil {
  289. c.JSON(nil, err)
  290. return
  291. }
  292. writer := c.Writer
  293. header := writer.Header()
  294. header.Add("Content-disposition", "attachment; filename="+fmt.Sprintf("%v", batchCodes[0].BatchName)+".txt")
  295. header.Add("Content-Type", "application/x-download;charset=utf-8")
  296. for _, v := range codes {
  297. writer.Write([]byte(fmt.Sprintf("%v\r\n", v)))
  298. }
  299. }