english_report_approval.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_hub/models"
  5. "eta/eta_hub/services"
  6. "eta/eta_hub/utils"
  7. "fmt"
  8. "github.com/rdlucklib/rdluck_tools/paging"
  9. "strings"
  10. "time"
  11. )
  12. // EnglishReportController 英文研报
  13. type EnglishReportController struct {
  14. BaseAuthController
  15. }
  16. // List
  17. // @Title 报告列表
  18. // @Description 报告列表
  19. // @Param PageSize query int true "每页数据条数"
  20. // @Param CurrentIndex query int true "当前页页码,从1开始"
  21. // @Param Keyword query string false "搜索关键词"
  22. // @Param State query int false "状态"
  23. // @Success 200 {object} models.EnglishReportListResp
  24. // @router /list [get]
  25. func (this *EnglishReportController) List() {
  26. br := new(models.BaseResponse).Init()
  27. defer func() {
  28. this.Data["json"] = br
  29. this.ServeJSON()
  30. }()
  31. pageSize, _ := this.GetInt("PageSize")
  32. currentIndex, _ := this.GetInt("CurrentIndex")
  33. var startSize int
  34. if pageSize <= 0 {
  35. pageSize = utils.PageSize20
  36. }
  37. if currentIndex <= 0 {
  38. currentIndex = 1
  39. }
  40. startSize = utils.StartIndex(currentIndex, pageSize)
  41. var condition string
  42. var pars []interface{}
  43. keyword := this.GetString("Keyword")
  44. keyword = strings.TrimSpace(keyword)
  45. if keyword != "" {
  46. kw := fmt.Sprint("%", keyword, "%")
  47. condition += ` AND title LIKE ? `
  48. pars = append(pars, kw)
  49. }
  50. state, _ := this.GetInt("State")
  51. if state > 0 {
  52. condition += ` AND state = ? `
  53. pars = append(pars, state)
  54. }
  55. reportOb := new(models.EnglishReport)
  56. total, e := reportOb.GetCountByCondition(condition, pars)
  57. if e != nil {
  58. br.Msg = "获取失败"
  59. br.ErrMsg = "获取英文报告列表总数失败, Err: " + e.Error()
  60. return
  61. }
  62. respList := make([]*models.EnglishReportItem, 0)
  63. list, e := reportOb.GetPageItemsByCondition(condition, pars, []string{}, "", startSize, pageSize)
  64. if e != nil {
  65. br.Msg = "获取失败"
  66. br.ErrMsg = "获取英文报告列表失败, Err: " + e.Error()
  67. return
  68. }
  69. for _, v := range list {
  70. t := models.FormatEnglishReport2Item(v)
  71. respList = append(respList, t)
  72. }
  73. page := paging.GetPaging(currentIndex, pageSize, total)
  74. resp := new(models.EnglishReportListResp)
  75. resp.Paging = page
  76. resp.List = respList
  77. br.Ret = 200
  78. br.Msg = "获取成功"
  79. br.Data = resp
  80. }
  81. // Detail
  82. // @Title 报告详情
  83. // @Description 报告详情
  84. // @Param ReportId query int true "报告ID"
  85. // @Success 200 {object} models.EnglishReportItem
  86. // @router /detail [get]
  87. func (this *EnglishReportController) Detail() {
  88. br := new(models.BaseResponse).Init()
  89. defer func() {
  90. this.Data["json"] = br
  91. this.ServeJSON()
  92. }()
  93. reportId, err := this.GetInt("ReportId")
  94. if err != nil {
  95. br.Msg = "获取参数失败!"
  96. br.ErrMsg = "获取参数失败,Err:" + err.Error()
  97. return
  98. }
  99. if reportId <= 0 {
  100. br.Msg = "参数错误"
  101. return
  102. }
  103. reportOb := new(models.EnglishReport)
  104. item, e := reportOb.GetItemById(reportId)
  105. if e != nil {
  106. if e.Error() == utils.ErrNoRow() {
  107. br.Msg = "报告已被删除"
  108. return
  109. }
  110. br.Msg = "获取失败"
  111. br.ErrMsg = "获取失败,Err:" + e.Error()
  112. return
  113. }
  114. detail := models.FormatEnglishReport2Item(item)
  115. br.Data = detail
  116. br.Ret = 200
  117. br.Msg = "获取成功"
  118. }
  119. // Approve
  120. // @Title 审批
  121. // @Description 审批报告
  122. // @Param request body models.EnglishReportApproveReq true "type json string"
  123. // @Success 200 string "操作成功"
  124. // @router /approve [post]
  125. func (this *EnglishReportController) Approve() {
  126. br := new(models.BaseResponse).Init()
  127. defer func() {
  128. this.Data["json"] = br
  129. this.ServeJSON()
  130. }()
  131. var req models.EnglishReportApproveReq
  132. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  133. if err != nil {
  134. br.Msg = "参数解析异常!"
  135. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  136. return
  137. }
  138. if req.ReportId <= 0 {
  139. br.Msg = "参数错误"
  140. br.ErrMsg = fmt.Sprintf("参数有误, EnglishReportId: %d", req.ReportId)
  141. return
  142. }
  143. ob := new(models.EnglishReport)
  144. item, e := ob.GetItemById(req.ReportId)
  145. if e != nil {
  146. if e.Error() == utils.ErrNoRow() {
  147. br.Msg = "报告已被删除"
  148. return
  149. }
  150. br.Msg = "操作失败"
  151. br.ErrMsg = "获取英文研报失败, Err: " + e.Error()
  152. return
  153. }
  154. if item.State != models.ReportStateWaitApprove {
  155. br.Msg = "报告状态有误"
  156. br.ErrMsg = fmt.Sprintf("报告状态有误, State: %d", item.State)
  157. return
  158. }
  159. if item.Content == "" {
  160. br.Msg = "报告内容为空,请检查内容"
  161. return
  162. }
  163. // 校验审批配置
  164. confMap, e := models.GetBusinessConf()
  165. if e != nil {
  166. br.Msg = "操作失败"
  167. br.ErrMsg = "获取审批配置失败, Err: " + e.Error()
  168. return
  169. }
  170. if confMap[models.BusinessConfIsReportApprove] != "true" {
  171. br.Msg = "未开启审批, 请开启后重新操作"
  172. return
  173. }
  174. if confMap[models.BusinessConfReportApproveType] != models.BusinessConfReportApproveTypeOther {
  175. br.Msg = "未开启第三方审批, 请开启后重新操作"
  176. return
  177. }
  178. stateMap := map[bool]int{false: models.ReportStateRefused, true: models.ReportStatePass}
  179. // 驳回
  180. now := time.Now().Local()
  181. if !req.Pass {
  182. cols := []string{"State", "ModifyTime", "ApproveTime"}
  183. item.State = stateMap[req.Pass]
  184. item.ModifyTime = now
  185. item.ApproveTime = now
  186. if e = item.Update(cols); e != nil {
  187. br.Msg = "审批失败"
  188. br.ErrMsg = "报告审批驳回失败, Err: " + e.Error()
  189. return
  190. }
  191. }
  192. // 通过审批
  193. if req.Pass {
  194. cols := []string{"State", "ModifyTime", "ApproveTime", "PublishTime"}
  195. item.State = stateMap[req.Pass]
  196. item.ModifyTime = now
  197. item.ApproveTime = now
  198. item.PublishTime = now
  199. if e = item.Update(cols); e != nil {
  200. br.Msg = "审批失败"
  201. br.ErrMsg = "报告审批通过失败, Err: " + e.Error()
  202. return
  203. }
  204. // 更新报告ES
  205. go func() {
  206. _ = services.UpdateEnglishReportEs(item.Id, models.ReportStatePublished)
  207. }()
  208. }
  209. // 记录报告变更状态
  210. go func() {
  211. recordItem := &models.ReportStateRecord{
  212. ReportId: req.ReportId,
  213. ReportType: models.ReportTypeEnglish,
  214. State: item.State,
  215. CreateTime: time.Now(),
  216. }
  217. _, _ = models.AddReportStateRecord(recordItem)
  218. }()
  219. br.Ret = 200
  220. br.Msg = "审批成功"
  221. }