smart_report.go 6.4 KB

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