report.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package models
  2. import (
  3. sql2 "database/sql"
  4. "errors"
  5. "eta/eta_task/global"
  6. "eta/eta_task/utils"
  7. "gorm.io/gorm"
  8. "time"
  9. )
  10. type Report struct {
  11. Id int `orm:"column(id)" description:"报告Id"`
  12. AddType int `description:"新增方式:1:新增报告,2:继承报告"`
  13. ClassifyIdFirst int `description:"一级分类id"`
  14. ClassifyNameFirst string `description:"一级分类名称"`
  15. ClassifyIdSecond int `description:"二级分类id"`
  16. ClassifyNameSecond string `description:"二级分类名称"`
  17. Title string `description:"标题"`
  18. Abstract string `description:"摘要"`
  19. Author string `description:"作者"`
  20. Frequency string `description:"频度"`
  21. CreateTime string `description:"创建时间"`
  22. ModifyTime string `description:"修改时间"`
  23. State int `description:"1:未发布;2:已发布;3-待提交;4-待审批;5-已驳回;6-已通过"`
  24. PublishTime time.Time `description:"发布时间"`
  25. Stage int `description:"期数"`
  26. MsgIsSend int `description:"消息是否已发送,0:否,1:是"`
  27. ThsMsgIsSend int `description:"客户群消息是否已发送,0:否,1:是"`
  28. Content string `description:"内容"`
  29. VideoUrl string `description:"音频文件URL"`
  30. VideoName string `description:"音频文件名称"`
  31. VideoPlaySeconds string `description:"音频播放时长"`
  32. VideoSize string `description:"音频文件大小,单位M"`
  33. ContentSub string `description:"内容前两个章节"`
  34. ReportCode string `description:"报告唯一编码"`
  35. ReportVersion int `description:"1:旧版,2:新版"`
  36. HasChapter int `description:"是否有章节 0-否 1-是"`
  37. ChapterType string `description:"章节类型 day-晨报 week-周报"`
  38. OldReportId int `description:"research_report表ID, 大于0则表示该报告为老后台同步过来的"`
  39. MsgSendTime time.Time `description:"模版消息发送时间"`
  40. AdminId int `description:"创建者账号"`
  41. AdminRealName string `description:"创建者姓名"`
  42. ApproveTime time.Time `description:"审批时间"`
  43. ApproveId int `description:"审批ID"`
  44. DetailImgUrl string `description:"报告详情长图地址"`
  45. DetailPdfUrl string `description:"报告详情PDF地址"`
  46. DetailImgUrlMobile string `description:"报告详情长图地址-手机端"`
  47. DetailPdfUrlMobile string `description:"报告详情PDF地址-手机端"`
  48. ContentStruct string `description:"内容组件"`
  49. LastModifyAdminId int `description:"最后更新人ID"`
  50. LastModifyAdminName string `description:"最后更新人姓名"`
  51. ContentModifyTime time.Time `description:"内容更新时间"`
  52. Pv int `description:"pv"`
  53. Uv int `description:"uv"`
  54. HeadImg string `description:"报告头图地址"`
  55. EndImg string `description:"报告尾图地址"`
  56. CanvasColor string `description:"画布颜色"`
  57. NeedSplice int `description:"是否拼接版头版位的标记,主要是为了兼容历史报告。0-不需要 1-需要"`
  58. HeadResourceId int `description:"版头资源ID"`
  59. EndResourceId int `description:"版尾资源ID"`
  60. ClassifyIdThird int `description:"三级分类id"`
  61. ClassifyNameThird string `description:"三级分类名称"`
  62. CollaborateType int8 `description:"协作方式,1:个人,2:多人协作。默认:1"`
  63. ReportLayout int8 `description:"报告布局,1:常规布局,2:智能布局。默认:1"`
  64. IsPublicPublish int8 `description:"是否公开发布,1:是,2:否"`
  65. ReportCreateTime time.Time `description:"报告时间创建时间"`
  66. InheritReportId int `description:"待继承的报告ID"`
  67. VoiceGenerateType int `description:"音频生成方式,0:系统生成,1:人工上传"`
  68. RaiReportId int `description:"RAI报告ID"`
  69. FreeLayoutConfig string `description:"'自由布局配置"`
  70. MiniShow int `description:"是否在C端展示:0-否;1-是"`
  71. PreMsgSend int `description:"定时发布成功后是否立即推送模版消息:0否,1是"`
  72. }
  73. func (m *Report) AfterFind(db *gorm.DB) (err error) {
  74. m.CreateTime = utils.GormDateStrToDateTimeStr(m.CreateTime)
  75. return
  76. }
  77. // GetPrePublishedReports 获取定时发布时间为当前时间的未发布的报告列表
  78. func GetPrePublishedReports(startTime, endTime, afterDate string) (list []*Report, err error) {
  79. o := global.DbMap[utils.DbNameReport]
  80. sql := `SELECT * FROM report WHERE state = 1 and pre_publish_time >= ? and pre_publish_time <=? and modify_time >= ?`
  81. err = o.Raw(sql, startTime, endTime, afterDate).Find(&list).Error
  82. return
  83. }
  84. // PublishReportById 发布报告
  85. func PublishReportById(reportId int, publishTime time.Time) (err error) {
  86. o := global.DbMap[utils.DbNameReport]
  87. sql := `UPDATE report SET state = 2, publish_time = ?, modify_time = NOW() WHERE id = ? `
  88. err = o.Exec(sql, publishTime, reportId).Error
  89. return
  90. }
  91. func ModifyReportVideo(reportId int, videoUrl, videoName, videoSize string, playSeconds float64) (err error) {
  92. o := global.DbMap[utils.DbNameReport]
  93. sql := `UPDATE report SET video_url=?,video_name=?,video_play_seconds=?,video_size=? WHERE id=? `
  94. err = o.Exec(sql, videoUrl, videoName, playSeconds, videoSize, reportId).Error
  95. return
  96. }
  97. // ModifyReportVideoByNoVideo
  98. // @Description: 修改无音频的报告音频信息
  99. // @author: Roc
  100. // @datetime 2024-07-25 18:03:05
  101. // @param reportId int
  102. // @param videoUrl string
  103. // @param videoName string
  104. // @param videoSize string
  105. // @param playSeconds float64
  106. // @return err error
  107. func ModifyReportVideoByNoVideo(reportId int, videoUrl, videoName, videoSize string, playSeconds float64) (err error) {
  108. o := global.DbMap[utils.DbNameReport]
  109. sql := `UPDATE report SET video_url=?,video_name=?,video_play_seconds=?,video_size=? WHERE id=? AND video_url=""`
  110. err = o.Exec(sql, videoUrl, videoName, playSeconds, videoSize, reportId).Error
  111. return
  112. }
  113. type ElasticReportDetail struct {
  114. ReportId int `description:"报告ID"`
  115. ReportChapterId int `description:"报告章节ID"`
  116. Title string `description:"标题"`
  117. Abstract string `description:"摘要"`
  118. BodyContent string `description:"内容"`
  119. PublishTime string `description:"发布时间"`
  120. PublishState int `description:"发布状态 1-未发布 2-已发布"`
  121. Author string `description:"作者"`
  122. ClassifyIdFirst int `description:"一级分类ID"`
  123. ClassifyNameFirst string `description:"一级分类名称"`
  124. ClassifyIdSecond int `description:"二级分类ID"`
  125. ClassifyNameSecond string `description:"二级分类名称"`
  126. ClassifyId int `description:"最小单元的分类ID"`
  127. ClassifyName string `description:"最小单元的分类名称"`
  128. Categories string `description:"关联的品种名称(包括品种别名)"`
  129. StageStr string `description:"报告期数"`
  130. }
  131. type ChartPermissionMappingIdName struct {
  132. PermissionId int
  133. PermissionName string
  134. }
  135. func GetChartPermissionNameFromMappingByKeyword(source string, classifyId int) (list []*ChartPermissionMappingIdName, err error) {
  136. o := global.DbMap[utils.DbNameReport]
  137. sql := " SELECT b.chart_permission_id AS permission_id,b.permission_name FROM chart_permission_search_key_word_mapping AS a INNER JOIN chart_permission AS b ON a.chart_permission_id = b.chart_permission_id WHERE a.from = ? AND a.classify_id = ? "
  138. sql = utils.ReplaceDriverKeywords("", sql)
  139. err = o.Raw(sql, source, classifyId).Find(&list).Error
  140. return
  141. }
  142. func UpdateReportPublishTime(reportId int, videoNameDate string) (err error) {
  143. o := global.DbMap[utils.DbNameReport]
  144. sql1 := ` UPDATE report SET publish_time = NOW() WHERE id = ? `
  145. err = o.Exec(sql1, reportId).Error
  146. if err != nil {
  147. return
  148. }
  149. //修改音频标题
  150. sql2 := ` UPDATE report SET video_name=CONCAT(SUBSTRING_INDEX(video_name,"(",1),"` + videoNameDate + `") WHERE id = ? and (video_name !="" and video_name is not null)`
  151. err = o.Exec(sql2, reportId).Error
  152. return
  153. }
  154. func UpdateReportChapterPublishTime(reportId int, videoNameDate string) (err error) {
  155. o := global.DbMap[utils.DbNameReport]
  156. sql1 := ` UPDATE report_chapter SET publish_time = NOW() WHERE report_id = ? `
  157. err = o.Exec(sql1, reportId).Error
  158. if err != nil {
  159. return
  160. }
  161. //修改音频标题
  162. sql2 := ` UPDATE report_chapter SET video_name=CONCAT(SUBSTRING_INDEX(video_name,"(",1),"` + videoNameDate + `") WHERE report_id = ? and (video_name !="" and video_name is not null)`
  163. err = o.Exec(sql2, reportId).Error
  164. return
  165. }
  166. func ModifyReportMsgIsSend(reportId int) (err error) {
  167. o := global.DbMap[utils.DbNameReport]
  168. sql := `UPDATE report SET msg_is_send = 1, msg_send_time=NOW() WHERE id = ? and msg_is_send=0`
  169. err = o.Exec(sql, reportId).Error
  170. return
  171. }
  172. // ClearReportSaveLog 清理报告保存日志
  173. func ClearReportSaveLog(date string) (err error) {
  174. o := global.DbMap[utils.DbNameReport]
  175. sql := `DELETE FROM report_save_log WHERE create_time <= ?`
  176. err = o.Exec(sql, date).Error
  177. return
  178. }
  179. // PublishReportAndChapter 发布报告及章节
  180. func PublishReportAndChapter(reportInfo *Report, isPublishReport bool, cols []string) (err error) {
  181. o := global.DbMap[utils.DbNameReport]
  182. to := o.Begin()
  183. defer func() {
  184. if err != nil {
  185. _ = to.Rollback()
  186. } else {
  187. _ = to.Commit()
  188. }
  189. }()
  190. // 更新报告
  191. if isPublishReport {
  192. err = to.Select(cols).Updates(reportInfo).Error
  193. if err != nil {
  194. return
  195. }
  196. }
  197. // 发布该报告的所有章节
  198. sql := ` UPDATE report_chapter SET publish_state = 2, publish_time = ? WHERE report_id = ? `
  199. err = to.Exec(sql, reportInfo.PublishTime, reportInfo.Id).Error
  200. // 发布章节
  201. //if len(publishIds) > 0 {
  202. // sql := ` UPDATE report_chapter SET publish_state = 2, publish_time = ? WHERE report_id = ? AND report_chapter_id IN (` + utils.GetOrmInReplace(len(publishIds)) + `) `
  203. // err = to.Raw(sql, reportInfo.PublishTime, reportInfo.Id, publishIds).Error
  204. //}
  205. //if len(unPublishIds) > 0 {
  206. // sql := ` UPDATE report_chapter SET publish_state = 1, publish_time = NULL, is_edit = 0 WHERE report_id = ? AND report_chapter_id IN (` + utils.GetOrmInReplace(len(unPublishIds)) + `) `
  207. // err = to.Raw(sql, reportInfo.Id, unPublishIds).Error
  208. //}
  209. return
  210. }
  211. func GetReport(classifyIdFirst int, startTime, endTime string) (item *Report, err error) {
  212. o := global.DbMap[utils.DbNameReport]
  213. sql := ` SELECT * FROM report WHERE classify_id_first = ? AND create_time >= ? AND create_time <= ? `
  214. err = o.Raw(sql, classifyIdFirst, startTime, endTime).Scan(&item).Error
  215. return item, err
  216. }
  217. // @return count int
  218. // @return err error
  219. func GetReportStage(classifyIdFirst, classifyIdSecond, classifyIdThird int) (count int, err error) {
  220. o := global.DbMap[utils.DbNameReport]
  221. classifyId := classifyIdThird
  222. if classifyId <= 0 {
  223. classifyId = classifyIdSecond
  224. }
  225. if classifyId <= 0 {
  226. classifyId = classifyIdFirst
  227. }
  228. if classifyId <= 0 {
  229. err = errors.New("错误的分类id")
  230. return
  231. }
  232. yearStart := time.Date(time.Now().Local().Year(), 1, 1, 0, 0, 0, 0, time.Local)
  233. sql := `SELECT MAX(stage) AS max_stage FROM report WHERE create_time > ? `
  234. if classifyIdThird > 0 {
  235. sql += " AND classify_id_third = ? "
  236. } else if classifyIdSecond > 0 {
  237. sql += " AND classify_id_second = ? "
  238. } else {
  239. sql += " AND classify_id_first = ? "
  240. }
  241. var maxNull sql2.NullInt64
  242. err = o.Raw(sql, yearStart, classifyId).Scan(&maxNull).Error
  243. if err != nil {
  244. return
  245. }
  246. if maxNull.Valid {
  247. count = int(maxNull.Int64)
  248. }
  249. return
  250. }
  251. func AddReport(reportInfo *Report) (err error) {
  252. o := global.DbMap[utils.DbNameReport]
  253. err = o.Create(reportInfo).Error
  254. if err != nil {
  255. return err
  256. }
  257. return
  258. }
  259. func ModifyReportContent(reportId int, content string) (err error) {
  260. o := global.DbMap[utils.DbNameReport]
  261. sql := `UPDATE report SET content = ?,modify_time = NOW() WHERE id = ?`
  262. err = o.Exec(sql, content, reportId).Error
  263. return
  264. }