research_report.go 11 KB

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