smart_report.go 6.8 KB

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