unit.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package autocode
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
  5. autocodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
  6. "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
  7. "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
  8. "github.com/flipped-aurora/gin-vue-admin/server/service"
  9. "github.com/gin-gonic/gin"
  10. "go.uber.org/zap"
  11. "time"
  12. )
  13. type UnitApi struct {
  14. }
  15. var unitService = service.ServiceGroupApp.AutoCodeServiceGroup.UnitService
  16. // CreateUnit 创建Unit
  17. // @Tags Unit
  18. // @Summary 创建Unit
  19. // @Security ApiKeyAuth
  20. // @accept application/json
  21. // @Produce application/json
  22. // @Param data body autocode.Unit true "创建Unit"
  23. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  24. // @Router /unit/createUnit [post]
  25. func (unitApi *UnitApi) CreateUnit(c *gin.Context) {
  26. var unit autocode.Unit
  27. _ = c.ShouldBindJSON(&unit)
  28. if err, nowUnit := unitService.GetUnitTop(); err != nil {
  29. //新建期数
  30. unit.Period = time.Now().Format("20060102150405")
  31. integral := 1000
  32. unit.UnitIntegral = &integral
  33. unit.UnitMaxIntegral = &integral
  34. unitService.CreateUnit(unit)
  35. response.OkWithMessage("创建成功", c)
  36. } else {
  37. unit.UnitIntegral = nowUnit.UnitMaxIntegral
  38. unit.UnitMaxIntegral = nowUnit.UnitMaxIntegral
  39. unit.Period = nowUnit.Period
  40. if err := unitService.CreateUnit(unit); err != nil {
  41. global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
  42. response.FailWithMessage("创建失败", c)
  43. } else {
  44. response.OkWithMessage("创建成功", c)
  45. }
  46. }
  47. }
  48. // DeleteUnit 删除Unit
  49. // @Tags Unit
  50. // @Summary 删除Unit
  51. // @Security ApiKeyAuth
  52. // @accept application/json
  53. // @Produce application/json
  54. // @Param data body autocode.Unit true "删除Unit"
  55. // @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
  56. // @Router /unit/deleteUnit [delete]
  57. func (unitApi *UnitApi) DeleteUnit(c *gin.Context) {
  58. var unit autocode.Unit
  59. _ = c.ShouldBindJSON(&unit)
  60. if err := unitService.DeleteUnit(unit); err != nil {
  61. global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
  62. response.FailWithMessage("删除失败", c)
  63. } else {
  64. response.OkWithMessage("删除成功", c)
  65. }
  66. }
  67. // DeleteUnitByIds 批量删除Unit
  68. // @Tags Unit
  69. // @Summary 批量删除Unit
  70. // @Security ApiKeyAuth
  71. // @accept application/json
  72. // @Produce application/json
  73. // @Param data body request.IdsReq true "批量删除Unit"
  74. // @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
  75. // @Router /unit/deleteUnitByIds [delete]
  76. func (unitApi *UnitApi) DeleteUnitByIds(c *gin.Context) {
  77. var IDS request.IdsReq
  78. _ = c.ShouldBindJSON(&IDS)
  79. if err := unitService.DeleteUnitByIds(IDS); err != nil {
  80. global.GVA_LOG.Error("批量删除失败!", zap.Any("err", err))
  81. response.FailWithMessage("批量删除失败", c)
  82. } else {
  83. response.OkWithMessage("批量删除成功", c)
  84. }
  85. }
  86. // UpdateUnit 更新Unit
  87. // @Tags Unit
  88. // @Summary 更新Unit
  89. // @Security ApiKeyAuth
  90. // @accept application/json
  91. // @Produce application/json
  92. // @Param data body autocode.Unit true "更新Unit"
  93. // @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
  94. // @Router /unit/updateUnit [put]
  95. func (unitApi *UnitApi) UpdateUnit(c *gin.Context) {
  96. var unit autocode.Unit
  97. _ = c.ShouldBindJSON(&unit)
  98. //记录历史,更新所有
  99. if err, units := unitService.GetUnitInfoListAll(); err != nil {
  100. response.FailWithMessage("更新失败", c)
  101. return
  102. } else {
  103. for _, temp := range units {
  104. unitHistoryService.CreateUnitHistory(autocode.UnitHistory{UnitId: &temp.ID, UnitName: temp.UnitName, UnitIntegral: temp.UnitIntegral, UnitMaxIntegral: temp.UnitMaxIntegral, Period: temp.Period})
  105. }
  106. }
  107. if err := unitService.UpdateUnitNext(unit); err != nil {
  108. global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
  109. response.FailWithMessage("更新失败", c)
  110. } else {
  111. response.OkWithMessage("更新成功", c)
  112. }
  113. }
  114. // FindUnit 用id查询Unit
  115. // @Tags Unit
  116. // @Summary 用id查询Unit
  117. // @Security ApiKeyAuth
  118. // @accept application/json
  119. // @Produce application/json
  120. // @Param data query autocode.Unit true "用id查询Unit"
  121. // @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
  122. // @Router /unit/findUnit [get]
  123. func (unitApi *UnitApi) FindUnit(c *gin.Context) {
  124. var unit autocode.Unit
  125. _ = c.ShouldBindQuery(&unit)
  126. if err, reunit := unitService.GetUnit(unit.ID); err != nil {
  127. global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
  128. response.FailWithMessage("查询失败", c)
  129. } else {
  130. response.OkWithData(gin.H{"reunit": reunit}, c)
  131. }
  132. }
  133. // GetUnitList 分页获取Unit列表
  134. // @Tags Unit
  135. // @Summary 分页获取Unit列表
  136. // @Security ApiKeyAuth
  137. // @accept application/json
  138. // @Produce application/json
  139. // @Param data query autocodeReq.UnitSearch true "分页获取Unit列表"
  140. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  141. // @Router /unit/getUnitList [get]
  142. func (unitApi *UnitApi) GetUnitList(c *gin.Context) {
  143. var pageInfo autocodeReq.UnitSearch
  144. _ = c.ShouldBindQuery(&pageInfo)
  145. if err, list, total := unitService.GetUnitInfoList(pageInfo); err != nil {
  146. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  147. response.FailWithMessage("获取失败", c)
  148. } else {
  149. response.OkWithDetailed(response.PageResult{
  150. List: list,
  151. Total: total,
  152. Page: pageInfo.Page,
  153. PageSize: pageInfo.PageSize,
  154. }, "获取成功", c)
  155. }
  156. }