smart_report_approval.go 6.0 KB

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