report.go 15 KB

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