research_report.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 []int) (list []*ResearchReportType, err error) {
  98. if len(reportIds) == 0 {
  99. return
  100. }
  101. o := orm.NewOrmUsingDB("weekly")
  102. sql := ` SELECT * FROM research_report_type WHERE research_report_id IN (` + utils.GetOrmInReplace(len(reportIds)) + `) ORDER BY created_time ASC,research_report_type_id ASC `
  103. _, err = o.Raw(sql, reportIds).QueryRows(&list)
  104. return
  105. }
  106. func GetResearchReportTypeContentListByReportTypeIds(reportTypeIds []int) (list []*ResearchReportTypeContent, err error) {
  107. if len(reportTypeIds) == 0 {
  108. return
  109. }
  110. o := orm.NewOrmUsingDB("weekly")
  111. sql := ` SELECT * FROM research_report_type_content WHERE research_report_type_id IN (` + utils.GetOrmInReplace(len(reportTypeIds)) + `) ORDER BY research_report_type_id ASC,sort ASC `
  112. _, err = o.Raw(sql, reportTypeIds).QueryRows(&list)
  113. return
  114. }
  115. func GetResearchReportTypeTickerListByReportTypeIds(reportTypeIds []int) (list []*ResearchReportTypeTicker, err error) {
  116. if len(reportTypeIds) == 0 {
  117. return
  118. }
  119. o := orm.NewOrmUsingDB("weekly")
  120. sql := ` SELECT * FROM research_report_type_ticker WHERE research_report_type_id IN (` + utils.GetOrmInReplace(len(reportTypeIds)) + `) ORDER BY research_report_type_id ASC,sort ASC `
  121. _, err = o.Raw(sql, reportTypeIds).QueryRows(&list)
  122. return
  123. }
  124. type CreateDayWeekReport struct {
  125. Report *Report
  126. ChapterList []*CreateDayWeekReportChapter
  127. }
  128. type CreateDayWeekReportChapter struct {
  129. Chapter *ReportChapter
  130. TickerList []*ReportChapterTicker
  131. }
  132. // 新增迁移晨周报
  133. func CreateMigrateNewDayWeekReport(newDayWeekReport *CreateDayWeekReport) (newReportId int, err error) {
  134. o := orm.NewOrmUsingDB("rddp")
  135. to, err := o.Begin()
  136. if err != nil {
  137. return
  138. }
  139. defer func() {
  140. if err != nil {
  141. _ = to.Rollback()
  142. } else {
  143. _ = to.Commit()
  144. }
  145. }()
  146. // 新增报告
  147. reportId, err := to.Insert(newDayWeekReport.Report)
  148. if err != nil {
  149. fmt.Println("InsertReportErr:" + err.Error())
  150. return
  151. }
  152. // 新增章节
  153. for _, chapter := range newDayWeekReport.ChapterList {
  154. chapter.Chapter.ReportId = int(reportId)
  155. lastChapterId, tmpErr := to.Insert(chapter.Chapter)
  156. if tmpErr != nil {
  157. err = tmpErr
  158. return
  159. }
  160. // 新增晨报ticker
  161. for _, ticker := range chapter.TickerList {
  162. ticker.ReportChapterId = int(lastChapterId)
  163. if _, tmpErr = to.Insert(ticker); tmpErr != nil {
  164. err = tmpErr
  165. return
  166. }
  167. }
  168. }
  169. // 修改报告code
  170. reportCode := utils.MD5(strconv.Itoa(int(reportId)))
  171. sql := `UPDATE report SET report_code = ? WHERE id = ? `
  172. if _, err = to.Raw(sql, reportCode, reportId).Exec(); err != nil {
  173. fmt.Println("UpdateReportCodeErr:" + err.Error())
  174. }
  175. newReportId = int(reportId)
  176. return
  177. }
  178. // 新增迁移其他报告
  179. func CreateMigrateNewOtherReport(reportInfo *Report, mappingList []*ChartPermissionChapterMapping) (newReportId int, err error) {
  180. o := orm.NewOrmUsingDB("rddp")
  181. to, err := o.Begin()
  182. if err != nil {
  183. return
  184. }
  185. defer func() {
  186. if err != nil {
  187. _ = to.Rollback()
  188. } else {
  189. _ = to.Commit()
  190. }
  191. }()
  192. // 新增报告
  193. reportId, err := to.Insert(reportInfo)
  194. if err != nil {
  195. fmt.Println("InsertReportErr:" + err.Error())
  196. return
  197. }
  198. // 修改报告code
  199. reportCode := utils.MD5(strconv.Itoa(int(reportId)))
  200. sql := `UPDATE report SET report_code = ? WHERE id = ? `
  201. if _, err = to.Raw(sql, reportCode, reportId).Exec(); err != nil {
  202. fmt.Println("UpdateReportCodeErr:" + err.Error())
  203. return
  204. }
  205. // 新增权限
  206. newReportId = int(reportId)
  207. if len(mappingList) > 0 {
  208. r := orm.NewOrmUsingDB("weekly")
  209. for _, mapping := range mappingList {
  210. sql := ` INSERT INTO chart_permission_chapter_mapping (chart_permission_id, report_chapter_type_id,research_type) VALUES(?,?,?) `
  211. if _, err = r.Raw(sql, mapping.ChartPermissionId, newReportId, "rddp").Exec(); err != nil {
  212. fmt.Println("InsertChartPermissionErr:" + err.Error())
  213. return
  214. }
  215. }
  216. }
  217. return
  218. }
  219. type ChartPermissionChapterMapping struct {
  220. Id int `orm:"column(id);pk"`
  221. ChartPermissionId int `description:"权限ID"`
  222. ReportChapterTypeId int `description:"report_chapter_type表主键id或research_report表主键id或tactic表主键id"`
  223. ResearchType string `description:"报告类型 week;two_week;tactic;month;other;rddp; "`
  224. }
  225. func GetChapterPermissionMappingByResearchReportIds(researchReportIds []int) (list []*ChartPermissionChapterMapping, err error) {
  226. if len(researchReportIds) == 0 {
  227. return
  228. }
  229. o := orm.NewOrmUsingDB("weekly")
  230. sql := ` SELECT * FROM chart_permission_chapter_mapping WHERE report_chapter_type_id IN (` + utils.GetOrmInReplace(len(researchReportIds)) + `) AND research_type != "rddp" ORDER BY report_chapter_type_id ASC `
  231. _, err = o.Raw(sql, researchReportIds).QueryRows(&list)
  232. return
  233. }
  234. // GetResearchReportTypeListByReportId 根据报告ID获取报告章节
  235. func GetResearchReportTypeListByReportId(reportId int) (list []*ResearchReportType, err error) {
  236. o := orm.NewOrmUsingDB("weekly")
  237. sql := ` SELECT * FROM research_report_type WHERE research_report_id = ? ORDER BY created_time ASC,research_report_type_id ASC `
  238. _, err = o.Raw(sql, reportId).QueryRows(&list)
  239. return
  240. }
  241. // GetResearchReportById 主键获取报告
  242. func GetResearchReportById(reportId int) (item *ResearchReport, err error) {
  243. o := orm.NewOrmUsingDB("weekly")
  244. sql := `SELECT * FROM research_report WHERE research_report_id = ? LIMIT 1`
  245. err = o.Raw(sql, reportId).QueryRow(&item)
  246. return
  247. }