research_report.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hz_eta_api/utils"
  6. "strconv"
  7. "time"
  8. )
  9. // ResearchReport 研究报告表(晨报、周报等)结构体
  10. type ResearchReport struct {
  11. ResearchReportId int `orm:"column(research_report_id);pk" description:"研究报告id"`
  12. ResearchReportName string `description:"研究报告名称"`
  13. ResearchReportTitle string `description:"研究报告标题"`
  14. ResearchReportImg string `description:"报告缩略图URL"`
  15. ResearchReportDate time.Time `description:"报告日期"`
  16. Type string `description:"报告类型,枚举值:day 晨报 week 周报 twoweek双周报 month 月报;默认:day"`
  17. Author string `description:"作者"`
  18. ReportVariety string `description:"研究报告的品种,双周报和月报有标识"`
  19. IsHasMenu int8 `description:"报告是否含有目录"`
  20. IsSendedMsg int8 `description:"是否发送过模板消息"`
  21. Periods int `description:"期数"`
  22. Status string `description:"状态,draft:草稿,"`
  23. Enabled int8 `description:"报告状态"`
  24. CreatedTime string `description:"创建时间"`
  25. LastUpdatedTime time.Time `description:"最近一次更新时间"`
  26. Viewers int `description:"H5观看用户数"`
  27. }
  28. // Update 更新数据
  29. func (researchReport *ResearchReport) Update(updateCols []string) (err error) {
  30. o := orm.NewOrmUsingDB("weekly")
  31. _, err = o.Update(researchReport, updateCols...)
  32. return
  33. }
  34. type ResearchReportList struct {
  35. ResearchReportId int `orm:"column(research_report_id);pk" description:"研究报告id"`
  36. ResearchReportName string `description:"研究报告名称"`
  37. ResearchReportTitle string `description:"研究报告标题"`
  38. ResearchReportImg string `description:"报告缩略图URL"`
  39. ResearchReportDate time.Time `description:"报告日期"`
  40. Type string `description:"报告类型,枚举值:day 晨报 week 周报 twoweek双周报 month 月报;默认:day"`
  41. Author string `description:"作者"`
  42. ReportVariety string `description:"研究报告的品种,双周报和月报有标识"`
  43. IsHasMenu int8 `description:"报告是否含有目录"`
  44. IsSendedMsg int8 `description:"是否发送过模板消息"`
  45. Periods int `description:"期数"`
  46. Status string `description:"状态,draft:草稿,"`
  47. Enabled int8 `description:"报告状态"`
  48. CreatedTime string `description:"创建时间"`
  49. LastUpdatedTime time.Time `description:"最近一次更新时间"`
  50. Viewers int `description:"H5观看用户数"`
  51. LinkUrl string `description:"报告阅读地址"`
  52. }
  53. // 获取今年报告
  54. func GetMigrateReportList() (list []*ResearchReport, err error) {
  55. o := orm.NewOrmUsingDB("weekly")
  56. sql := ` SELECT
  57. *
  58. FROM
  59. research_report AS a
  60. WHERE
  61. a.enabled = 1
  62. AND a.status = "report"
  63. AND a.research_report_date >= "2022-01-01"
  64. ORDER BY a.research_report_date ASC `
  65. _, err = o.Raw(sql).QueryRows(&list)
  66. return
  67. }
  68. // ResearchReport 研究报告表(晨报、周报等)结构体
  69. type ResearchReportType struct {
  70. ResearchReportTypeId int `orm:"column(research_report_type_id);pk" description:"报告章节ID"`
  71. ResearchReportId int `description:"报告ID"`
  72. TypeId int `description:"分类ID"`
  73. Edit int `description:"是否编辑过"`
  74. Trend string `description:"趋势观点"`
  75. ResearchReportTypeTitle string `description:"报告标题"`
  76. CreatedTime string `description:"创建时间"`
  77. LastUpdatedTime time.Time `description:"最近一次更新时间"`
  78. }
  79. type ResearchReportTypeContent struct {
  80. ResearchReportTypeContentId int `orm:"column(research_report_type_content_id);pk" description:"章节内容ID"`
  81. ResearchReportTypeId int `description:"报告章节ID"`
  82. Sort int `description:"排序"`
  83. ContentType string `description:"内容分类类型"`
  84. Content string `description:"内容"`
  85. ImgUrl string `description:"图片路径"`
  86. CreatedTime time.Time `description:"创建时间"`
  87. LastUpdatedTime time.Time `description:"最近一次更新时间"`
  88. }
  89. type ResearchReportTypeTicker struct {
  90. ResearchReportTypeTickerId int `orm:"column(research_report_type_ticker_id);pk" description:"章节tickerID"`
  91. ResearchReportTypeId int `description:"报告章节ID"`
  92. Sort int `description:"排序"`
  93. Ticker string `description:"指标的ticker"`
  94. CreatedTime time.Time `description:"创建时间"`
  95. LastUpdatedTime time.Time `description:"最近一次更新时间"`
  96. }
  97. func GetResearchReportTypeListByReportIds(reportIds string) (list []*ResearchReportType, err error) {
  98. o := orm.NewOrmUsingDB("weekly")
  99. sql := ` SELECT * FROM research_report_type WHERE research_report_id IN (` + reportIds + `) ORDER BY created_time ASC,research_report_type_id ASC `
  100. _, err = o.Raw(sql).QueryRows(&list)
  101. return
  102. }
  103. func GetResearchReportTypeContentListByReportTypeIds(reportTypeIds string) (list []*ResearchReportTypeContent, err error) {
  104. o := orm.NewOrmUsingDB("weekly")
  105. sql := ` SELECT * FROM research_report_type_content WHERE research_report_type_id IN (` + reportTypeIds + `) ORDER BY research_report_type_id ASC,sort ASC `
  106. _, err = o.Raw(sql).QueryRows(&list)
  107. return
  108. }
  109. func GetResearchReportTypeTickerListByReportTypeIds(reportTypeIds string) (list []*ResearchReportTypeTicker, err error) {
  110. o := orm.NewOrmUsingDB("weekly")
  111. sql := ` SELECT * FROM research_report_type_ticker WHERE research_report_type_id IN (` + reportTypeIds + `) ORDER BY research_report_type_id ASC,sort ASC `
  112. _, err = o.Raw(sql).QueryRows(&list)
  113. return
  114. }
  115. type CreateDayWeekReport struct {
  116. Report *Report
  117. ChapterList []*CreateDayWeekReportChapter
  118. }
  119. type CreateDayWeekReportChapter struct {
  120. Chapter *ReportChapter
  121. TickerList []*ReportChapterTicker
  122. }
  123. // 新增迁移晨周报
  124. func CreateMigrateNewDayWeekReport(newDayWeekReport *CreateDayWeekReport) (newReportId int, err error) {
  125. o := orm.NewOrmUsingDB("rddp")
  126. to, err := o.Begin()
  127. if err != nil {
  128. return
  129. }
  130. defer func() {
  131. if err != nil {
  132. _ = to.Rollback()
  133. } else {
  134. _ = to.Commit()
  135. }
  136. }()
  137. // 新增报告
  138. reportId, err := to.Insert(newDayWeekReport.Report)
  139. if err != nil {
  140. fmt.Println("InsertReportErr:" + err.Error())
  141. return
  142. }
  143. // 新增章节
  144. for _, chapter := range newDayWeekReport.ChapterList {
  145. chapter.Chapter.ReportId = int(reportId)
  146. lastChapterId, tmpErr := to.Insert(chapter.Chapter)
  147. if tmpErr != nil {
  148. err = tmpErr
  149. return
  150. }
  151. // 新增晨报ticker
  152. for _, ticker := range chapter.TickerList {
  153. ticker.ReportChapterId = int(lastChapterId)
  154. if _, tmpErr = to.Insert(ticker); tmpErr != nil {
  155. err = tmpErr
  156. return
  157. }
  158. }
  159. }
  160. // 修改报告code
  161. reportCode := utils.MD5(strconv.Itoa(int(reportId)))
  162. sql := `UPDATE report SET report_code = ? WHERE id = ? `
  163. if _, err = to.Raw(sql, reportCode, reportId).Exec(); err != nil {
  164. fmt.Println("UpdateReportCodeErr:" + err.Error())
  165. }
  166. newReportId = int(reportId)
  167. return
  168. }
  169. // 新增迁移其他报告
  170. func CreateMigrateNewOtherReport(reportInfo *Report, mappingList []*ChartPermissionChapterMapping) (newReportId int, err error) {
  171. o := orm.NewOrmUsingDB("rddp")
  172. to, err := o.Begin()
  173. if err != nil {
  174. return
  175. }
  176. defer func() {
  177. if err != nil {
  178. _ = to.Rollback()
  179. } else {
  180. _ = to.Commit()
  181. }
  182. }()
  183. // 新增报告
  184. reportId, err := to.Insert(reportInfo)
  185. if err != nil {
  186. fmt.Println("InsertReportErr:" + err.Error())
  187. return
  188. }
  189. // 修改报告code
  190. reportCode := utils.MD5(strconv.Itoa(int(reportId)))
  191. sql := `UPDATE report SET report_code = ? WHERE id = ? `
  192. if _, err = to.Raw(sql, reportCode, reportId).Exec(); err != nil {
  193. fmt.Println("UpdateReportCodeErr:" + err.Error())
  194. return
  195. }
  196. // 新增权限
  197. newReportId = int(reportId)
  198. if len(mappingList) > 0 {
  199. r := orm.NewOrmUsingDB("weekly")
  200. for _, mapping := range mappingList {
  201. sql := ` INSERT INTO chart_permission_chapter_mapping (chart_permission_id, report_chapter_type_id,research_type) VALUES(?,?,?) `
  202. if _, err = r.Raw(sql, mapping.ChartPermissionId, newReportId, "rddp").Exec(); err != nil {
  203. fmt.Println("InsertChartPermissionErr:" + err.Error())
  204. return
  205. }
  206. }
  207. }
  208. return
  209. }
  210. type ChartPermissionChapterMapping struct {
  211. Id int `orm:"column(id);pk"`
  212. ChartPermissionId int `description:"权限ID"`
  213. ReportChapterTypeId int `description:"report_chapter_type表主键id或research_report表主键id或tactic表主键id"`
  214. ResearchType string `description:"报告类型 week;two_week;tactic;month;other;rddp; "`
  215. }
  216. func GetChapterPermissionMappingByResearchReportIds(researchReportIds string) (list []*ChartPermissionChapterMapping, err error) {
  217. o := orm.NewOrmUsingDB("weekly")
  218. sql := ` SELECT * FROM chart_permission_chapter_mapping WHERE report_chapter_type_id IN (` + researchReportIds + `) AND research_type != "rddp" ORDER BY report_chapter_type_id ASC `
  219. _, err = o.Raw(sql).QueryRows(&list)
  220. return
  221. }
  222. // GetResearchReportTypeListByReportId 根据报告ID获取报告章节
  223. func GetResearchReportTypeListByReportId(reportId int) (list []*ResearchReportType, err error) {
  224. o := orm.NewOrmUsingDB("weekly")
  225. sql := ` SELECT * FROM research_report_type WHERE research_report_id = ? ORDER BY created_time ASC,research_report_type_id ASC `
  226. _, err = o.Raw(sql, reportId).QueryRows(&list)
  227. return
  228. }
  229. // GetResearchReportById 主键获取报告
  230. func GetResearchReportById(reportId int) (item *ResearchReport, err error) {
  231. o := orm.NewOrmUsingDB("weekly")
  232. sql := `SELECT * FROM research_report WHERE research_report_id = ? LIMIT 1`
  233. err = o.Raw(sql, reportId).QueryRow(&item)
  234. return
  235. }