smart_report.go 18 KB

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