smart_report.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. package smart_report
  2. import (
  3. "eta/eta_api/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. "html"
  8. "strings"
  9. "time"
  10. )
  11. const (
  12. SmartReportStateWaitPublish = 1
  13. SmartReportStatePublished = 2
  14. )
  15. // SmartReport 智能研报
  16. type SmartReport struct {
  17. SmartReportId int `orm:"column(smart_report_id);pk" description:"智能研报ID"`
  18. ReportCode string `description:"报告唯一编码"`
  19. ClassifyIdFirst int `description:"一级分类ID"`
  20. ClassifyNameFirst string `description:"一级分类名称"`
  21. ClassifyIdSecond int `description:"二级分类ID"`
  22. ClassifyNameSecond string `description:"二级分类名称"`
  23. AddType int `description:"新增方式:1-新增报告;2-继承报告"`
  24. Title string `description:"标题"`
  25. Abstract string `description:"摘要"`
  26. Author string `description:"作者"`
  27. Frequency string `description:"频度"`
  28. Stage int `description:"期数"`
  29. Content string `description:"内容"`
  30. ContentSub string `description:"内容前两个章节"`
  31. ContentStruct string `description:"内容组件"`
  32. VideoUrl string `description:"音频文件URL"`
  33. VideoName string `description:"音频文件名称"`
  34. VideoPlaySeconds float64 `description:"音频播放时长"`
  35. VideoSize string `description:"音频文件大小,单位M"`
  36. AdminId int `description:"创建者ID"`
  37. AdminRealName string `description:"创建者姓名"`
  38. State int `description:"1:未发布;2:已发布;3-待提交;4-待审批;5-已驳回;6-已通过"`
  39. LastModifyAdminId int `description:"最后更新人ID"`
  40. LastModifyAdminName string `description:"最后更新人姓名"`
  41. ContentModifyTime time.Time `description:"内容更新时间"`
  42. Pv int `description:"pv"`
  43. Uv int `description:"uv"`
  44. PublishTime time.Time `description:"发布时间"`
  45. PrePublishTime time.Time `description:"预发布时间"`
  46. PreMsgSend int `description:"定时发布后是否推送模版消息:0-否;1-是"`
  47. MsgIsSend int `description:"消息是否已发送:0-否;1-是"`
  48. MsgSendTime time.Time `description:"模版消息发送时间"`
  49. DetailImgUrl string `description:"报告详情长图地址"`
  50. DetailPdfUrl string `description:"报告详情PDF地址"`
  51. CreateTime time.Time `description:"创建时间"`
  52. ModifyTime time.Time `description:"修改时间"`
  53. HeadImg string `description:"报告头图地址"`
  54. EndImg string `description:"报告尾图地址"`
  55. CanvasColor string `description:"画布颜色"`
  56. ApproveTime time.Time `description:"审批时间"`
  57. ApproveId int `description:"审批ID"`
  58. NeedSplice int `description:"0-不需要 1-需要"`
  59. HeadResourceId int `description:"版头资源ID"`
  60. EndResourceId int `description:"版尾资源ID"`
  61. }
  62. func (m *SmartReport) TableName() string {
  63. return "smart_report"
  64. }
  65. func (m *SmartReport) PrimaryId() string {
  66. return "smart_report_id"
  67. }
  68. func (m *SmartReport) Create() (err error) {
  69. o := orm.NewOrmUsingDB("rddp")
  70. id, err := o.Insert(m)
  71. if err != nil {
  72. return
  73. }
  74. m.SmartReportId = int(id)
  75. return
  76. }
  77. func (m *SmartReport) CreateMulti(items []*SmartReport) (err error) {
  78. if len(items) == 0 {
  79. return
  80. }
  81. o := orm.NewOrmUsingDB("rddp")
  82. _, err = o.InsertMulti(len(items), items)
  83. return
  84. }
  85. func (m *SmartReport) Update(cols []string) (err error) {
  86. o := orm.NewOrmUsingDB("rddp")
  87. _, err = o.Update(m, cols...)
  88. return
  89. }
  90. func (m *SmartReport) Del() (err error) {
  91. o := orm.NewOrmUsingDB("rddp")
  92. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  93. _, err = o.Raw(sql, m.SmartReportId).Exec()
  94. return
  95. }
  96. func (m *SmartReport) MultiDel(menuIds []int) (err error) {
  97. if len(menuIds) == 0 {
  98. return
  99. }
  100. o := orm.NewOrmUsingDB("rddp")
  101. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.PrimaryId(), utils.GetOrmInReplace(len(menuIds)))
  102. _, err = o.Raw(sql, menuIds).Exec()
  103. return
  104. }
  105. func (m *SmartReport) GetItemById(id int) (item *SmartReport, err error) {
  106. o := orm.NewOrmUsingDB("rddp")
  107. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  108. err = o.Raw(sql, id).QueryRow(&item)
  109. return
  110. }
  111. func (m *SmartReport) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *SmartReport, err error) {
  112. o := orm.NewOrmUsingDB("rddp")
  113. order := ``
  114. if orderRule != "" {
  115. order = ` ORDER BY ` + orderRule
  116. }
  117. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  118. err = o.Raw(sql, pars).QueryRow(&item)
  119. return
  120. }
  121. func (m *SmartReport) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  122. o := orm.NewOrmUsingDB("rddp")
  123. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  124. err = o.Raw(sql, pars).QueryRow(&count)
  125. return
  126. }
  127. func (m *SmartReport) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SmartReport, err error) {
  128. o := orm.NewOrmUsingDB("rddp")
  129. fields := strings.Join(fieldArr, ",")
  130. if len(fieldArr) == 0 {
  131. fields = `*`
  132. }
  133. order := `ORDER BY create_time DESC`
  134. if orderRule != "" {
  135. order = ` ORDER BY ` + orderRule
  136. }
  137. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  138. _, err = o.Raw(sql, pars).QueryRows(&items)
  139. return
  140. }
  141. func (m *SmartReport) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*SmartReport, err error) {
  142. o := orm.NewOrmUsingDB("rddp")
  143. fields := strings.Join(fieldArr, ",")
  144. if len(fieldArr) == 0 {
  145. fields = `*`
  146. }
  147. // 排序:1:未发布;2:已发布;3-待提交;4-待审批;5-已驳回;6-已通过
  148. order := `ORDER BY FIELD(state,3,1,4,5,6,2), modify_time DESC`
  149. if orderRule != "" {
  150. order = ` ORDER BY ` + orderRule
  151. }
  152. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  153. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  154. return
  155. }
  156. func (m *SmartReport) GetMaxStageByClassifyId(classifyId int) (stage int, err error) {
  157. o := orm.NewOrmUsingDB("rddp")
  158. sql := fmt.Sprintf(`SELECT MAX(stage) AS max_stage FROM %s WHERE classify_id_second = ?`, m.TableName())
  159. err = o.Raw(sql, classifyId).QueryRow(&stage)
  160. return
  161. }
  162. // SmartReportItem 智能研报信息
  163. type SmartReportItem struct {
  164. SmartReportId int `description:"智能研报ID"`
  165. ReportCode string `description:"报告唯一编码"`
  166. ClassifyIdFirst int `description:"一级分类ID"`
  167. ClassifyNameFirst string `description:"一级分类名称"`
  168. ClassifyIdSecond int `description:"二级分类ID"`
  169. ClassifyNameSecond string `description:"二级分类名称"`
  170. AddType int `description:"新增方式:1-新增报告;2-继承报告"`
  171. Title string `description:"标题"`
  172. Abstract string `description:"摘要"`
  173. Author string `description:"作者"`
  174. Frequency string `description:"频度"`
  175. Stage int `description:"期数"`
  176. Content string `description:"内容"`
  177. ContentSub string `description:"内容前两个章节"`
  178. ContentStruct string `description:"内容组件"`
  179. VideoUrl string `description:"音频文件URL"`
  180. VideoName string `description:"音频文件名称"`
  181. VideoPlaySeconds float64 `description:"音频播放时长"`
  182. VideoSize string `description:"音频文件大小,单位M"`
  183. AdminId int `description:"创建者姓名"`
  184. AdminRealName string `description:"创建者姓名"`
  185. LastModifyAdminId int `description:"最后更新人ID"`
  186. LastModifyAdminName string `description:"最后更新人姓名"`
  187. ContentModifyTime string `description:"内容更新时间"`
  188. Pv int `description:"pv"`
  189. Uv int `description:"uv"`
  190. State int `description:"1:未发布;2:已发布;3-待提交;4-待审批;5-已驳回;6-已通过"`
  191. PublishTime string `description:"发布时间"`
  192. PrePublishTime string `description:"预发布时间"`
  193. MsgIsSend int `description:"消息是否已发送:0-否;1-是"`
  194. MsgSendTime string `description:"模版消息发送时间"`
  195. DetailImgUrl string `description:"报告详情长图地址"`
  196. DetailPdfUrl string `description:"报告详情PDF地址"`
  197. CreateTime string `description:"创建时间"`
  198. ModifyTime string `description:"修改时间"`
  199. ApproveTime string `description:"审批时间"`
  200. CanEdit bool `description:"是否可编辑"`
  201. Editor string `description:"当前编辑人"`
  202. HeadImg string `description:"报告头图地址"`
  203. EndImg string `description:"报告尾图地址"`
  204. CanvasColor string `description:"画布颜色"`
  205. NeedSplice int `description:"0-不需要 1-需要"`
  206. HeadResourceId int `description:"版头资源ID"`
  207. EndResourceId int `description:"版尾资源ID"`
  208. HeadStyle string `description:"版头样式"`
  209. EndStyle string `description:"版尾样式"`
  210. }
  211. // FormatSmartReport2Item 格式化智能研报数据格式
  212. func FormatSmartReport2Item(origin *SmartReport) (item *SmartReportItem) {
  213. item = new(SmartReportItem)
  214. if origin == nil {
  215. return
  216. }
  217. item.SmartReportId = origin.SmartReportId
  218. item.ReportCode = origin.ReportCode
  219. item.ClassifyIdFirst = origin.ClassifyIdFirst
  220. item.ClassifyNameFirst = origin.ClassifyNameFirst
  221. item.ClassifyIdSecond = origin.ClassifyIdSecond
  222. item.ClassifyNameSecond = origin.ClassifyNameSecond
  223. item.AddType = origin.AddType
  224. item.Title = origin.Title
  225. item.Abstract = origin.Abstract
  226. item.Author = origin.Author
  227. item.Frequency = origin.Frequency
  228. item.Stage = origin.Stage
  229. item.Content = html.UnescapeString(origin.Content)
  230. item.ContentSub = html.UnescapeString(origin.ContentSub)
  231. item.ContentStruct = html.UnescapeString(origin.ContentStruct)
  232. item.VideoUrl = origin.VideoUrl
  233. item.VideoName = origin.VideoName
  234. item.VideoPlaySeconds = origin.VideoPlaySeconds
  235. item.VideoSize = origin.VideoSize
  236. item.AdminId = origin.AdminId
  237. item.AdminRealName = origin.AdminRealName
  238. item.LastModifyAdminId = origin.LastModifyAdminId
  239. item.LastModifyAdminName = origin.LastModifyAdminName
  240. item.ContentModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ContentModifyTime)
  241. item.Pv = origin.Pv
  242. item.Uv = origin.Uv
  243. item.State = origin.State
  244. item.PublishTime = utils.TimeTransferString(utils.FormatDateTime, origin.PublishTime)
  245. item.PrePublishTime = utils.TimeTransferString(utils.FormatDateTime, origin.PrePublishTime)
  246. item.MsgIsSend = origin.MsgIsSend
  247. item.MsgSendTime = utils.TimeTransferString(utils.FormatDateTime, origin.MsgSendTime)
  248. item.DetailImgUrl = origin.DetailImgUrl
  249. item.DetailPdfUrl = origin.DetailPdfUrl
  250. item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime)
  251. item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ModifyTime)
  252. item.HeadImg = origin.HeadImg
  253. item.EndImg = origin.EndImg
  254. item.CanvasColor = origin.CanvasColor
  255. item.ApproveTime = utils.TimeTransferString(utils.FormatDateTime, origin.ApproveTime)
  256. item.NeedSplice = origin.NeedSplice
  257. item.HeadResourceId = origin.HeadResourceId
  258. item.EndResourceId = origin.EndResourceId
  259. return
  260. }
  261. // SmartReportAddReq 新增智能研报请求体
  262. type SmartReportAddReq struct {
  263. AddType int `description:"新增方式:1:新增报告,2:继承报告"`
  264. ClassifyIdFirst int `description:"一级分类ID"`
  265. ClassifyNameFirst string `description:"一级分类名称"`
  266. ClassifyIdSecond int `description:"二级分类ID"`
  267. ClassifyNameSecond string `description:"二级分类名称"`
  268. Title string `description:"标题"`
  269. Abstract string `description:"摘要"`
  270. Author string `description:"作者"`
  271. Frequency string `description:"频度"`
  272. HeadImg string `description:"报告头图地址"`
  273. EndImg string `description:"报告尾图地址"`
  274. CanvasColor string `description:"画布颜色"`
  275. HeadResourceId int `description:"版头资源ID"`
  276. EndResourceId int `description:"版尾资源ID"`
  277. }
  278. // SmartReportEditReq 编辑智能研报请求体
  279. type SmartReportEditReq struct {
  280. SmartReportAddReq
  281. SmartReportId int `description:"智能研报ID"`
  282. Content string `description:"内容"`
  283. ContentStruct string `description:"内容结构"`
  284. HeadImg string `description:"报告头图地址"`
  285. EndImg string `description:"报告尾图地址"`
  286. CanvasColor string `description:"画布颜色"`
  287. HeadResourceId int `description:"版头资源ID"`
  288. EndResourceId int `description:"版尾资源ID"`
  289. }
  290. // SmartReportRemoveReq 删除智能研报请求体
  291. type SmartReportRemoveReq struct {
  292. SmartReportId int `description:"智能研报ID"`
  293. }
  294. // SmartReportPublishReq 发布智能研报请求体
  295. type SmartReportPublishReq struct {
  296. SmartReportId int `description:"智能研报ID"`
  297. PublishState int `description:"1-取消发布; 2-发布"`
  298. ReportUrl string `description:"报告Url"`
  299. }
  300. // SmartReportPrePublishReq 预发布智能研报请求体
  301. type SmartReportPrePublishReq struct {
  302. SmartReportId int `description:"智能研报ID"`
  303. PrePublishTime string `description:"预发布时间"`
  304. PreMsgSend int `description:"定时发布成功后是否立即推送模版消息:0否,1是"`
  305. ReportUrl string `description:"报告Url"`
  306. }
  307. // SmartReportSaveContentReq 保存草稿请求体
  308. type SmartReportSaveContentReq struct {
  309. SmartReportId int `description:"智能研报ID"`
  310. Content string `description:"内容"`
  311. ContentStruct string `description:"内容结构"`
  312. NoChange int `description:"内容是否未改变:1:内容未改变"`
  313. HeadImg string `description:"报告头图地址"`
  314. EndImg string `description:"报告尾图地址"`
  315. CanvasColor string `description:"画布颜色"`
  316. HeadResourceId int `description:"版头资源ID"`
  317. EndResourceId int `description:"版尾资源ID"`
  318. }
  319. // SmartReportSaveContentResp 保存草稿响应体
  320. type SmartReportSaveContentResp struct {
  321. SmartReportId int `description:"智能研报ID"`
  322. }
  323. // SmartReportSendMsgReq 消息推送请求体
  324. type SmartReportSendMsgReq struct {
  325. SmartReportId int `description:"智能研报ID"`
  326. }
  327. // SmartReportMarkEditReq 标记编辑英文研报的请求数据
  328. type SmartReportMarkEditReq struct {
  329. SmartReportId int `description:"智能研报ID"`
  330. Status int `description:"标记状态: 1-编辑中; 2-编辑完成"`
  331. }
  332. // SmartReportListResp 智能研报
  333. type SmartReportListResp struct {
  334. List []*SmartReportItem
  335. Paging *paging.PagingItem `description:"分页数据"`
  336. }
  337. // ElasticSmartReport 智能研报es
  338. type ElasticSmartReport struct {
  339. SmartReportId int `description:"智能研报ID"`
  340. Title string `description:"标题"`
  341. Abstract string `description:"摘要"`
  342. BodyContent string `description:"内容"`
  343. PublishTime string `description:"发布时间"`
  344. PublishState int `description:"发布状态 1-未发布 2-已发布"`
  345. Author string `description:"作者"`
  346. ClassifyIdFirst int `description:"一级分类ID"`
  347. ClassifyNameFirst string `description:"一级分类名称"`
  348. ClassifyIdSecond int `description:"二级分类ID"`
  349. ClassifyNameSecond string `description:"二级分类名称"`
  350. StageStr string `description:"报告期数"`
  351. Frequency string `description:"频度"`
  352. }
  353. // Report2ImgQueueReq 报告详情生成长图队列请求体
  354. type Report2ImgQueueReq struct {
  355. ReportType int `description:"报告类型: 1-研报; 2-智能研报"`
  356. ReportCode string `description:"报告唯一编码"`
  357. }
  358. // UpdateSmartReportsStateByCond 批量更新报告状态
  359. func UpdateSmartReportsStateByCond(classifyFirstId, classifySecondId, oldState, newState int) (err error) {
  360. o := orm.NewOrmUsingDB("rddp")
  361. cond := ``
  362. if classifyFirstId > 0 {
  363. cond += fmt.Sprintf(` AND classify_id_first = %d`, classifyFirstId)
  364. }
  365. if classifySecondId > 0 {
  366. cond += fmt.Sprintf(` AND classify_id_second = %d`, classifySecondId)
  367. }
  368. sql := fmt.Sprintf(`UPDATE smart_report SET state = ?, pre_publish_time = NULL WHERE state = ? %s`, cond)
  369. _, err = o.Raw(sql, newState, oldState).Exec()
  370. return
  371. }
  372. // UpdateSmartReportsStateBySecondIds 批量更新二级分类报告状态
  373. func UpdateSmartReportsStateBySecondIds(oldState, newState int, secondIds []int) (err error) {
  374. if len(secondIds) <= 0 {
  375. return
  376. }
  377. o := orm.NewOrmUsingDB("rddp")
  378. // (有审批流的)未发布->待提交
  379. sql := fmt.Sprintf(`UPDATE smart_report SET state = ?, pre_publish_time = NULL WHERE state = ? AND classify_id_second IN (%s)`, utils.GetOrmInReplace(len(secondIds)))
  380. _, err = o.Raw(sql, newState, oldState, secondIds).Exec()
  381. if err != nil {
  382. return
  383. }
  384. // (无审批流的)待提交->未发布
  385. sql = fmt.Sprintf(`UPDATE smart_report SET state = ?, pre_publish_time = NULL WHERE state = ? AND classify_id_second NOT IN (%s)`, utils.GetOrmInReplace(len(secondIds)))
  386. _, err = o.Raw(sql, oldState, newState, secondIds).Exec()
  387. return
  388. }
  389. // UpdatePdfUrlSmartReportById 清空pdf相关字段
  390. func UpdatePdfUrlSmartReportById(reportId int) (err error) {
  391. o := orm.NewOrmUsingDB("rddp")
  392. sql := `UPDATE smart_report SET detail_img_url = '',detail_pdf_url='',modify_time=NOW() WHERE smart_report_id = ? `
  393. _, err = o.Raw(sql, reportId).Exec()
  394. return
  395. }