report.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. package models
  2. import (
  3. "eta_gn/eta_task/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "time"
  7. )
  8. type Report struct {
  9. Id int `orm:"column(id)" description:"报告Id"`
  10. AddType int `description:"新增方式:1:新增报告,2:继承报告"`
  11. ClassifyIdFirst int `description:"一级分类id"`
  12. ClassifyNameFirst string `description:"一级分类名称"`
  13. ClassifyIdSecond int `description:"二级分类id"`
  14. ClassifyNameSecond string `description:"二级分类名称"`
  15. Title string `description:"标题"`
  16. Abstract string `description:"摘要"`
  17. Author string `description:"作者"`
  18. Frequency string `description:"频度"`
  19. CreateTime string `description:"创建时间"`
  20. ModifyTime time.Time `description:"修改时间"`
  21. State int `description:"1:未发布,2:已发布"`
  22. PublishTime time.Time `description:"发布时间"`
  23. Stage int `description:"期数"`
  24. MsgIsSend int `description:"消息是否已发送,0:否,1:是"`
  25. Content string `description:"内容"`
  26. VideoUrl string `description:"音频文件URL"`
  27. VideoName string `description:"音频文件名称"`
  28. VideoPlaySeconds string `description:"音频播放时长"`
  29. ContentSub string `description:"内容前两个章节"`
  30. HasChapter int `description:"是否有章节 0-否 1-是"`
  31. ChapterType string `description:"章节类型 day-晨报 week-周报"`
  32. OldReportId int `description:"research_report表ID(后续一两个版本过渡需要,之后可移除)"`
  33. PreMsgSend int `description:"定时发布成功后是否立即推送模版消息:0否,1是"`
  34. AdminId int
  35. AdminName string `description:"系统用户名称"`
  36. ClassifyIdThird int `description:"三级分类id"`
  37. ClassifyNameThird string `description:"三级分类名称"`
  38. }
  39. func GetReportById(reportId int) (item *ReportDetail, err error) {
  40. o := orm.NewOrmUsingDB("rddp")
  41. sql := `SELECT * FROM report WHERE id=?`
  42. err = o.Raw(sql, reportId).QueryRow(&item)
  43. return
  44. }
  45. func GetReport() (items []*Report, err error) {
  46. sql := `SELECT * FROM report ORDER BY id ASC `
  47. o := orm.NewOrmUsingDB("rddp")
  48. _, err = o.Raw(sql).QueryRows(&items)
  49. return
  50. }
  51. func ModifyReportContentSub(id int, contentSub string) (err error) {
  52. sql := `UPDATE report SET content_sub=? WHERE id=? `
  53. o := orm.NewOrmUsingDB("rddp")
  54. _, err = o.Raw(sql, contentSub, id).Exec()
  55. return
  56. }
  57. func GetReportLimit() (items []*Report, err error) {
  58. sql := `SELECT * FROM report WHERE state=2 ORDER BY id DESC LIMIT 50 `
  59. o := orm.NewOrmUsingDB("rddp")
  60. _, err = o.Raw(sql).QueryRows(&items)
  61. return
  62. }
  63. func EditReportContent(reportId int, content, contentSub string) (err error) {
  64. o := orm.NewOrmUsingDB("rddp")
  65. sql := ` UPDATE report SET content=?,content_sub=?,modify_time=NOW() WHERE id=? `
  66. _, err = o.Raw(sql, content, contentSub, reportId).Exec()
  67. return
  68. }
  69. // 删除报告日志记录-保留3个月
  70. func DeleteReportSaveLog() {
  71. startDateTime := time.Now().AddDate(0, -3, 0).Format(utils.FormatDateTime)
  72. fmt.Println(startDateTime)
  73. }
  74. func EditReportContentHtml(reportId int, content string) (err error) {
  75. o := orm.NewOrmUsingDB("rddp")
  76. sql := ` UPDATE report SET content=?,modify_time=NOW() WHERE id=? `
  77. _, err = o.Raw(sql, content, reportId).Exec()
  78. return
  79. }
  80. // GetPrePublishedReports 获取定时发布时间为当前时间的未发布的报告列表
  81. func GetPrePublishedReports(startTime, endTime, afterDate string) (list []*Report, err error) {
  82. o := orm.NewOrmUsingDB("rddp")
  83. sql := `SELECT * FROM report WHERE state = 1 and pre_publish_time >= ? and pre_publish_time <=? and modify_time >= ?`
  84. _, err = o.Raw(sql, startTime, endTime, afterDate).QueryRows(&list)
  85. return
  86. }
  87. // PublishReportById 发布报告
  88. func PublishReportById(reportId int, publishTime time.Time) (err error) {
  89. o := orm.NewOrmUsingDB("rddp")
  90. sql := `UPDATE report SET state = 2, publish_time = ?, modify_time = NOW() WHERE id = ? `
  91. _, err = o.Raw(sql, publishTime, reportId).Exec()
  92. return
  93. }
  94. type ReportDetail struct {
  95. Id int `orm:"column(id)" description:"报告Id"`
  96. AddType int `description:"新增方式:1:新增报告,2:继承报告"`
  97. ClassifyIdFirst int `description:"一级分类id"`
  98. ClassifyNameFirst string `description:"一级分类名称"`
  99. ClassifyIdSecond int `description:"二级分类id"`
  100. ClassifyNameSecond string `description:"二级分类名称"`
  101. Title string `description:"标题"`
  102. Abstract string `description:"摘要"`
  103. Author string `description:"作者"`
  104. Frequency string `description:"频度"`
  105. CreateTime string `description:"创建时间"`
  106. ModifyTime string `description:"修改时间"`
  107. State int `description:"1:未发布,2:已发布"`
  108. PublishTime string `description:"发布时间"`
  109. Stage int `description:"期数"`
  110. MsgIsSend int `description:"消息是否已发送,0:否,1:是"`
  111. Content string `description:"内容"`
  112. VideoUrl string `description:"音频文件URL"`
  113. VideoName string `description:"音频文件名称"`
  114. VideoPlaySeconds string `description:"音频播放时长"`
  115. ContentSub string `description:"内容前两个章节"`
  116. ThsMsgIsSend int `description:"客户群消息是否已发送,0:否,1:是"`
  117. HasChapter int `description:"是否有章节 0-否 1-是"`
  118. ChapterType string `description:"章节类型 day-晨报 week-周报"`
  119. OldReportId int `description:"research_report表ID(后续一两个版本过渡需要,之后可移除)"`
  120. PreMsgSend int `description:"定时发布成功后是否立即推送模版消息:0否,1是"`
  121. }
  122. func ModifyReportVideo(reportId int, videoUrl, videoName, videoSize string, playSeconds float64) (err error) {
  123. o := orm.NewOrmUsingDB("rddp")
  124. sql := `UPDATE report SET video_url=?,video_name=?,video_play_seconds=?,video_size=? WHERE id=? `
  125. _, err = o.Raw(sql, videoUrl, videoName, playSeconds, videoSize, reportId).Exec()
  126. return
  127. }
  128. type ReportChapterTypePermission struct {
  129. Id int `orm:"column(id);pk" description:"主键ID"`
  130. ReportChapterTypeId int `description:"报告章节类型ID"`
  131. ReportChapterTypeName string `description:"章节名称"`
  132. ChartPermissionId int `description:"大分类ID"`
  133. PermissionName string `description:"权限名称"`
  134. ResearchType string `description:"研报类型"`
  135. CreatedTime time.Time `description:"创建时间"`
  136. }
  137. // GetChapterTypePermissionByTypeIdAndResearchType 根据章节类型ID及研报类型获取章节类型权限列表
  138. func GetChapterTypePermissionByTypeIdAndResearchType(typeId int, researchType string) (list []*ReportChapterTypePermission, err error) {
  139. o := orm.NewOrm()
  140. sql := ` SELECT * FROM report_chapter_type_permission WHERE report_chapter_type_id = ? AND research_type = ? ORDER BY chart_permission_id ASC `
  141. _, err = o.Raw(sql, typeId, researchType).QueryRows(&list)
  142. return
  143. }
  144. type ElasticReportDetail struct {
  145. ReportId int `description:"报告ID"`
  146. ReportChapterId int `description:"报告章节ID"`
  147. Title string `description:"标题"`
  148. Abstract string `description:"摘要"`
  149. BodyContent string `description:"内容"`
  150. PublishTime string `description:"发布时间"`
  151. PublishState int `description:"发布状态 1-未发布 2-已发布"`
  152. Author string `description:"作者"`
  153. ClassifyIdFirst int `description:"一级分类ID"`
  154. ClassifyNameFirst string `description:"一级分类名称"`
  155. ClassifyIdSecond int `description:"二级分类ID"`
  156. ClassifyNameSecond string `description:"二级分类名称"`
  157. ClassifyId int `description:"最小单元的分类ID"`
  158. ClassifyName string `description:"最小单元的分类名称"`
  159. Categories string `description:"关联的品种名称(包括品种别名)"`
  160. StageStr string `description:"报告期数"`
  161. }
  162. type ChartPermissionMappingIdName struct {
  163. PermissionId int
  164. PermissionName string
  165. }
  166. func GetChartPermissionNameFromMappingByKeyword(source string, classifyId int) (list []*ChartPermissionMappingIdName, err error) {
  167. o := orm.NewOrmUsingDB("rddp")
  168. 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 = ? "
  169. _, err = o.Raw(sql, source, classifyId).QueryRows(&list)
  170. return
  171. }
  172. func UpdateReportPublishTime(reportId int, videoNameDate string) (err error) {
  173. o := orm.NewOrmUsingDB("rddp")
  174. sql1 := ` UPDATE report SET publish_time = NOW() WHERE id = ? `
  175. _, err = o.Raw(sql1, reportId).Exec()
  176. if err != nil {
  177. return
  178. }
  179. //修改音频标题
  180. sql2 := ` UPDATE report SET video_name=CONCAT(SUBSTRING_INDEX(video_name,"(",1),"` + videoNameDate + `") WHERE id = ? and (video_name !="" and video_name is not null)`
  181. _, err = o.Raw(sql2, reportId).Exec()
  182. return
  183. }
  184. func UpdateReportChapterPublishTime(reportId int, videoNameDate string) (err error) {
  185. o := orm.NewOrmUsingDB("rddp")
  186. sql1 := ` UPDATE report_chapter SET publish_time = NOW() WHERE report_id = ? `
  187. _, err = o.Raw(sql1, reportId).Exec()
  188. if err != nil {
  189. return
  190. }
  191. //修改音频标题
  192. 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)`
  193. _, err = o.Raw(sql2, reportId).Exec()
  194. return
  195. }
  196. func ModifyReportMsgIsSend(reportId int) (err error) {
  197. o := orm.NewOrmUsingDB("rddp")
  198. sql := `UPDATE report SET msg_is_send = 1, msg_send_time=NOW() WHERE id = ? and msg_is_send=0`
  199. _, err = o.Raw(sql, reportId).Exec()
  200. return
  201. }
  202. // ClearReportSaveLog 清理报告保存日志
  203. func ClearReportSaveLog(date string) (err error) {
  204. o := orm.NewOrmUsingDB("rddp")
  205. sql := `DELETE FROM report_save_log WHERE create_time <= ?`
  206. _, err = o.Raw(sql, date).Exec()
  207. return
  208. }
  209. // PublishReportAndChapter 发布报告及章节
  210. func PublishReportAndChapter(reportInfo *Report, isPublishReport bool, cols []string) (err error) {
  211. o := orm.NewOrmUsingDB("rddp")
  212. to, err := o.Begin()
  213. if err != nil {
  214. return
  215. }
  216. defer func() {
  217. if err != nil {
  218. _ = to.Rollback()
  219. } else {
  220. _ = to.Commit()
  221. }
  222. }()
  223. // 更新报告
  224. if isPublishReport {
  225. _, err = to.Update(reportInfo, cols...)
  226. if err != nil {
  227. return
  228. }
  229. }
  230. // 发布该报告的所有章节
  231. sql := ` UPDATE report_chapter SET publish_state = 2, publish_time = ? WHERE report_id = ? `
  232. _, err = to.Raw(sql, reportInfo.PublishTime, reportInfo.Id).Exec()
  233. // 发布章节
  234. //if len(publishIds) > 0 {
  235. // sql := ` UPDATE report_chapter SET publish_state = 2, publish_time = ? WHERE report_id = ? AND report_chapter_id IN (` + utils.GetOrmInReplace(len(publishIds)) + `) `
  236. // _, err = to.Raw(sql, reportInfo.PublishTime, reportInfo.Id, publishIds).Exec()
  237. //}
  238. //if len(unPublishIds) > 0 {
  239. // 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)) + `) `
  240. // _, err = to.Raw(sql, reportInfo.Id, unPublishIds).Exec()
  241. //}
  242. return
  243. }