research_report.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. // ResearchReport 研究报告表(晨报、周报等)结构体
  54. type ResearchReportType struct {
  55. ResearchReportTypeId int `orm:"column(research_report_type_id);pk" description:"报告章节ID"`
  56. ResearchReportId int `description:"报告ID"`
  57. TypeId int `description:"分类ID"`
  58. Edit int `description:"是否编辑过"`
  59. Trend string `description:"趋势观点"`
  60. ResearchReportTypeTitle string `description:"报告标题"`
  61. CreatedTime string `description:"创建时间"`
  62. LastUpdatedTime time.Time `description:"最近一次更新时间"`
  63. }
  64. type ResearchReportTypeContent struct {
  65. ResearchReportTypeContentId int `orm:"column(research_report_type_content_id);pk" description:"章节内容ID"`
  66. ResearchReportTypeId int `description:"报告章节ID"`
  67. Sort int `description:"排序"`
  68. ContentType string `description:"内容分类类型"`
  69. Content string `description:"内容"`
  70. ImgUrl string `description:"图片路径"`
  71. CreatedTime time.Time `description:"创建时间"`
  72. LastUpdatedTime time.Time `description:"最近一次更新时间"`
  73. }
  74. type ResearchReportTypeTicker struct {
  75. ResearchReportTypeTickerId int `orm:"column(research_report_type_ticker_id);pk" description:"章节tickerID"`
  76. ResearchReportTypeId int `description:"报告章节ID"`
  77. Sort int `description:"排序"`
  78. Ticker string `description:"指标的ticker"`
  79. CreatedTime time.Time `description:"创建时间"`
  80. LastUpdatedTime time.Time `description:"最近一次更新时间"`
  81. }
  82. func GetResearchReportTypeListByReportIds(reportIds string) (list []*ResearchReportType, err error) {
  83. o := orm.NewOrmUsingDB("weekly")
  84. sql := ` SELECT * FROM research_report_type WHERE research_report_id IN (` + reportIds + `) ORDER BY created_time ASC,research_report_type_id ASC `
  85. _, err = o.Raw(sql).QueryRows(&list)
  86. return
  87. }
  88. func GetResearchReportTypeContentListByReportTypeIds(reportTypeIds string) (list []*ResearchReportTypeContent, err error) {
  89. o := orm.NewOrmUsingDB("weekly")
  90. sql := ` SELECT * FROM research_report_type_content WHERE research_report_type_id IN (` + reportTypeIds + `) ORDER BY research_report_type_id ASC,sort ASC `
  91. _, err = o.Raw(sql).QueryRows(&list)
  92. return
  93. }
  94. func GetResearchReportTypeTickerListByReportTypeIds(reportTypeIds string) (list []*ResearchReportTypeTicker, err error) {
  95. o := orm.NewOrmUsingDB("weekly")
  96. sql := ` SELECT * FROM research_report_type_ticker WHERE research_report_type_id IN (` + reportTypeIds + `) ORDER BY research_report_type_id ASC,sort ASC `
  97. _, err = o.Raw(sql).QueryRows(&list)
  98. return
  99. }
  100. type CreateDayWeekReport struct {
  101. Report *Report
  102. ChapterList []*CreateDayWeekReportChapter
  103. }
  104. type CreateDayWeekReportChapter struct {
  105. Chapter *ReportChapter
  106. TickerList []*ReportChapterTicker
  107. }
  108. // 新增迁移晨周报
  109. func CreateMigrateNewDayWeekReport(newDayWeekReport *CreateDayWeekReport) (newReportId int, err error) {
  110. o := orm.NewOrmUsingDB("rddp")
  111. to, err := o.Begin()
  112. if err != nil {
  113. return
  114. }
  115. defer func() {
  116. if err != nil {
  117. _ = to.Rollback()
  118. } else {
  119. _ = to.Commit()
  120. }
  121. }()
  122. // 新增报告
  123. reportId, err := to.Insert(newDayWeekReport.Report)
  124. if err != nil {
  125. fmt.Println("InsertReportErr:" + err.Error())
  126. return
  127. }
  128. // 新增章节
  129. for _, chapter := range newDayWeekReport.ChapterList {
  130. chapter.Chapter.ReportId = int(reportId)
  131. lastChapterId, tmpErr := to.Insert(chapter.Chapter)
  132. if tmpErr != nil {
  133. err = tmpErr
  134. return
  135. }
  136. // 新增晨报ticker
  137. for _, ticker := range chapter.TickerList {
  138. ticker.ReportChapterId = int(lastChapterId)
  139. if _, tmpErr = to.Insert(ticker); tmpErr != nil {
  140. err = tmpErr
  141. return
  142. }
  143. }
  144. }
  145. // 修改报告code
  146. reportCode := utils.MD5(strconv.Itoa(int(reportId)))
  147. sql := `UPDATE report SET report_code = ? WHERE id = ? `
  148. if _, err = to.Raw(sql, reportCode, reportId).Exec(); err != nil {
  149. fmt.Println("UpdateReportCodeErr:" + err.Error())
  150. }
  151. newReportId = int(reportId)
  152. return
  153. }
  154. // 新增迁移其他报告
  155. func CreateMigrateNewOtherReport(reportInfo *Report, mappingList []*ChartPermissionChapterMapping) (newReportId int, err error) {
  156. o := orm.NewOrmUsingDB("rddp")
  157. to, err := o.Begin()
  158. if err != nil {
  159. return
  160. }
  161. defer func() {
  162. if err != nil {
  163. _ = to.Rollback()
  164. } else {
  165. _ = to.Commit()
  166. }
  167. }()
  168. // 新增报告
  169. reportId, err := to.Insert(reportInfo)
  170. if err != nil {
  171. fmt.Println("InsertReportErr:" + err.Error())
  172. return
  173. }
  174. // 修改报告code
  175. reportCode := utils.MD5(strconv.Itoa(int(reportId)))
  176. sql := `UPDATE report SET report_code = ? WHERE id = ? `
  177. if _, err = to.Raw(sql, reportCode, reportId).Exec(); err != nil {
  178. fmt.Println("UpdateReportCodeErr:" + err.Error())
  179. return
  180. }
  181. // 新增权限
  182. newReportId = int(reportId)
  183. if len(mappingList) > 0 {
  184. r := orm.NewOrmUsingDB("weekly")
  185. for _, mapping := range mappingList {
  186. sql := ` INSERT INTO chart_permission_chapter_mapping (chart_permission_id, report_chapter_type_id,research_type) VALUES(?,?,?) `
  187. if _, err = r.Raw(sql, mapping.ChartPermissionId, newReportId, "rddp").Exec(); err != nil {
  188. fmt.Println("InsertChartPermissionErr:" + err.Error())
  189. return
  190. }
  191. }
  192. }
  193. return
  194. }
  195. type ChartPermissionChapterMapping struct {
  196. Id int `orm:"column(id);pk"`
  197. ChartPermissionId int `description:"权限ID"`
  198. ReportChapterTypeId int `description:"report_chapter_type表主键id或research_report表主键id或tactic表主键id"`
  199. ResearchType string `description:"报告类型 week;two_week;tactic;month;other;rddp; "`
  200. }
  201. func GetChapterPermissionMappingByResearchReportIds(researchReportIds string) (list []*ChartPermissionChapterMapping, err error) {
  202. o := orm.NewOrmUsingDB("weekly")
  203. 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 `
  204. _, err = o.Raw(sql).QueryRows(&list)
  205. return
  206. }
  207. // GetResearchReportTypeListByReportId 根据报告ID获取报告章节
  208. func GetResearchReportTypeListByReportId(reportId int) (list []*ResearchReportType, err error) {
  209. o := orm.NewOrmUsingDB("weekly")
  210. sql := ` SELECT * FROM research_report_type WHERE research_report_id = ? ORDER BY created_time ASC,research_report_type_id ASC `
  211. _, err = o.Raw(sql, reportId).QueryRows(&list)
  212. return
  213. }
  214. // GetResearchReportById 主键获取报告
  215. func GetResearchReportById(reportId int) (item *ResearchReport, err error) {
  216. o := orm.NewOrmUsingDB("weekly")
  217. sql := `SELECT * FROM research_report WHERE research_report_id = ? LIMIT 1`
  218. err = o.Raw(sql, reportId).QueryRow(&item)
  219. return
  220. }