report.go 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. package models
  2. import (
  3. "eta/eta_api/utils"
  4. "github.com/beego/beego/v2/client/orm"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "strings"
  7. "time"
  8. )
  9. type Report struct {
  10. Id int `orm:"column(id)" description:"报告Id"`
  11. AddType int `description:"新增方式:1:新增报告,2:继承报告"`
  12. ClassifyIdFirst int `description:"一级分类id"`
  13. ClassifyNameFirst string `description:"一级分类名称"`
  14. ClassifyIdSecond int `description:"二级分类id"`
  15. ClassifyNameSecond string `description:"二级分类名称"`
  16. Title string `description:"标题"`
  17. Abstract string `description:"摘要"`
  18. Author string `description:"作者"`
  19. Frequency string `description:"频度"`
  20. CreateTime string `description:"创建时间"`
  21. ModifyTime time.Time `description:"修改时间"`
  22. State int `description:"1:未发布,2:已发布"`
  23. PublishTime time.Time `description:"发布时间"`
  24. Stage int `description:"期数"`
  25. MsgIsSend int `description:"消息是否已发送,0:否,1:是"`
  26. ThsMsgIsSend int `description:"客户群消息是否已发送,0:否,1:是"`
  27. Content string `description:"内容"`
  28. VideoUrl string `description:"音频文件URL"`
  29. VideoName string `description:"音频文件名称"`
  30. VideoPlaySeconds string `description:"音频播放时长"`
  31. VideoSize string `description:"音频文件大小,单位M"`
  32. ContentSub string `description:"内容前两个章节"`
  33. ReportCode string `description:"报告唯一编码"`
  34. ReportVersion int `description:"1:旧版,2:新版"`
  35. HasChapter int `description:"是否有章节 0-否 1-是"`
  36. ChapterType string `description:"章节类型 day-晨报 week-周报"`
  37. OldReportId int `description:"research_report表ID, 大于0则表示该报告为老后台同步过来的"`
  38. MsgSendTime time.Time `description:"模版消息发送时间"`
  39. AdminId int `description:"创建者账号"`
  40. AdminRealName string `description:"创建者姓名"`
  41. }
  42. type ReportList struct {
  43. Id int `description:"报告Id"`
  44. AddType int `description:"新增方式:1:新增报告,2:继承报告"`
  45. ClassifyIdFirst int `description:"一级分类id"`
  46. ClassifyNameFirst string `description:"一级分类名称"`
  47. ClassifyIdSecond int `description:"二级分类id"`
  48. ClassifyNameSecond string `description:"二级分类名称"`
  49. Title string `description:"标题"`
  50. Abstract string `description:"摘要"`
  51. Author string `description:"作者"`
  52. Frequency string `description:"频度"`
  53. CreateTime string `description:"创建时间"`
  54. ModifyTime time.Time `description:"修改时间"`
  55. State int `description:"1:未发布,2:已发布 3:已驳回 4:已审批"`
  56. PublishTime string `description:"发布时间"`
  57. PrePublishTime string `description:"预发布时间"`
  58. Stage int `description:"期数"`
  59. MsgIsSend int `description:"模板消息是否已发送,0:否,1:是"`
  60. Content string `description:"内容"`
  61. VideoUrl string `description:"音频文件URL"`
  62. VideoName string `description:"音频文件名称"`
  63. VideoPlaySeconds string `description:"音频播放时长"`
  64. ContentSub string `description:"内容前两个章节"`
  65. Pv int `description:"Pv"`
  66. Uv int `description:"Uv"`
  67. ReportCode string `description:"报告唯一编码"`
  68. ReportVersion int `description:"1:旧版,2:新版"`
  69. ThsMsgIsSend int `description:"客户群消息是否已发送,0:否,1:是"`
  70. NeedThsMsg int `description:"是否需要推送客群消息 0-否 1-是"`
  71. HasChapter int `description:"是否有章节 0-否 1-是"`
  72. ChapterType string `description:"章节类型 day-晨报 week-周报"`
  73. ChapterVideoList []*ReportChapterVideoList `description:"章节音频列表"`
  74. OldReportId int `description:"research_report表ID, 大于0则表示该报告为老后台同步过来的"`
  75. MsgSendTime string `description:"模版消息发送时间"`
  76. CanEdit bool `description:"是否可编辑"`
  77. Editor string `description:"编辑人"`
  78. AdminId int `description:"创建者账号"`
  79. AdminRealName string `description:"创建者姓名"`
  80. }
  81. type ReportListResp struct {
  82. List []*ReportList
  83. Paging *paging.PagingItem `description:"分页数据"`
  84. }
  85. func GetReportListCount(condition string, pars []interface{}, companyType string) (count int, err error) {
  86. //产品权限
  87. companyTypeSqlStr := ``
  88. if companyType == "ficc" {
  89. companyTypeSqlStr = " AND classify_id_first != 40 "
  90. } else if companyType == "权益" {
  91. companyTypeSqlStr = " AND classify_id_first = 40 "
  92. }
  93. oRddp := orm.NewOrmUsingDB("rddp")
  94. sql := `SELECT COUNT(1) AS count FROM report WHERE 1=1 ` + companyTypeSqlStr
  95. if condition != "" {
  96. sql += condition
  97. }
  98. err = oRddp.Raw(sql, pars).QueryRow(&count)
  99. return
  100. }
  101. func GetReportList(condition string, pars []interface{}, companyType string, startSize, pageSize int) (items []*ReportList, err error) {
  102. o := orm.NewOrmUsingDB("rddp")
  103. //产品权限
  104. companyTypeSqlStr := ``
  105. if companyType == "ficc" {
  106. companyTypeSqlStr = " AND classify_id_first != 40 "
  107. } else if companyType == "权益" {
  108. companyTypeSqlStr = " AND classify_id_first = 40 "
  109. }
  110. sql := `SELECT *,
  111. (SELECT COUNT(1) FROM report_view_record AS rvr WHERE rvr.report_id=report.id) AS pv,
  112. (SELECT COUNT(DISTINCT user_id) FROM report_view_record AS rvr WHERE rvr.report_id=report.id) AS uv
  113. FROM report WHERE 1=1 ` + companyTypeSqlStr
  114. if condition != "" {
  115. sql += condition
  116. }
  117. sql += `ORDER BY FIELD(state,3,1,2,4), modify_time DESC LIMIT ?,?`
  118. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  119. return
  120. }
  121. // PublishReport 发布报告
  122. func PublishReport(reportIds []int) (err error) {
  123. if len(reportIds) == 0 {
  124. return
  125. }
  126. o := orm.NewOrmUsingDB("rddp")
  127. sql := `UPDATE report SET state=2,publish_time=now(),modify_time=NOW() WHERE id IN (` + utils.GetOrmInReplace(len(reportIds)) + `)`
  128. _, err = o.Raw(sql).Exec()
  129. return
  130. }
  131. // PublishCancleReport 取消发布报告
  132. func PublishCancleReport(reportIds int, publishTimeNullFlag bool) (err error) {
  133. o := orm.NewOrmUsingDB("rddp")
  134. var sql string
  135. if publishTimeNullFlag {
  136. sql = ` UPDATE report SET state=1, publish_time=null, pre_publish_time=null, pre_msg_send=0 WHERE id =?`
  137. } else {
  138. sql = ` UPDATE report SET state=1, pre_publish_time=null, pre_msg_send=0 WHERE id =?`
  139. }
  140. _, err = o.Raw(sql, reportIds).Exec()
  141. return
  142. }
  143. // 删除报告
  144. func DeleteReport(reportIds int) (err error) {
  145. o := orm.NewOrmUsingDB("rddp")
  146. sql := ` DELETE FROM report WHERE id =? `
  147. _, err = o.Raw(sql, reportIds).Exec()
  148. return
  149. }
  150. type ReportDetail struct {
  151. Id int `orm:"column(id)" description:"报告Id"`
  152. AddType int `description:"新增方式:1:新增报告,2:继承报告"`
  153. ClassifyIdFirst int `description:"一级分类id"`
  154. ClassifyNameFirst string `description:"一级分类名称"`
  155. ClassifyIdSecond int `description:"二级分类id"`
  156. ClassifyNameSecond string `description:"二级分类名称"`
  157. Title string `description:"标题"`
  158. Abstract string `description:"摘要"`
  159. Author string `description:"作者"`
  160. Frequency string `description:"频度"`
  161. CreateTime string `description:"创建时间"`
  162. ModifyTime string `description:"修改时间"`
  163. State int `description:"1:未发布,2:已发布"`
  164. PublishTime string `description:"发布时间"`
  165. PrePublishTime string `description:"预发布时间"`
  166. Stage int `description:"期数"`
  167. MsgIsSend int `description:"消息是否已发送,0:否,1:是"`
  168. PreMsgSend int `description:"定时发布成功后是否立即推送模版消息:0否,1是"`
  169. Content string `description:"内容"`
  170. VideoUrl string `description:"音频文件URL"`
  171. VideoName string `description:"音频文件名称"`
  172. VideoPlaySeconds string `description:"音频播放时长"`
  173. ContentSub string `description:"内容前两个章节"`
  174. ThsMsgIsSend int `description:"客户群消息是否已发送,0:否,1:是"`
  175. HasChapter int `description:"是否有章节 0-否 1-是"`
  176. ChapterType string `description:"章节类型 day-晨报 week-周报"`
  177. }
  178. func GetReportById(reportId int) (item *ReportDetail, err error) {
  179. o := orm.NewOrmUsingDB("rddp")
  180. sql := `SELECT * FROM report WHERE id=?`
  181. err = o.Raw(sql, reportId).QueryRow(&item)
  182. return
  183. }
  184. func GetReportByIds(reportIds string) (list []*ReportDetail, err error) {
  185. o := orm.NewOrmUsingDB("rddp")
  186. sql := `SELECT * FROM report WHERE id in ` + reportIds
  187. _, err = o.Raw(sql).QueryRows(&list)
  188. return
  189. }
  190. // GetSimpleReportByIds 根据报告ID查询报告基本信息
  191. func GetSimpleReportByIds(reportIds []int) (list []*ReportDetail, err error) {
  192. if len(reportIds) == 0 {
  193. return
  194. }
  195. o := orm.NewOrmUsingDB("rddp")
  196. sql := `SELECT id, title FROM report WHERE id IN (` + utils.GetOrmInReplace(len(reportIds)) + `)`
  197. _, err = o.Raw(sql, reportIds).QueryRows(&list)
  198. return
  199. }
  200. func GetReportStage(classifyIdFirst, classifyIdSecond int) (count int, err error) {
  201. o := orm.NewOrmUsingDB("rddp")
  202. sql := ``
  203. if classifyIdSecond > 0 {
  204. sql = "SELECT MAX(stage) AS max_stage FROM report WHERE classify_id_second=? "
  205. o.Raw(sql, classifyIdSecond).QueryRow(&count)
  206. } else {
  207. sql = "SELECT MAX(stage) AS max_stage FROM report WHERE classify_id_first=? "
  208. o.Raw(sql, classifyIdFirst).QueryRow(&count)
  209. }
  210. return
  211. }
  212. func GetReportStageEdit(classifyIdFirst, classifyIdSecond, reportId int) (count int, err error) {
  213. o := orm.NewOrmUsingDB("rddp")
  214. sql := ``
  215. if classifyIdSecond > 0 {
  216. sql = "SELECT MAX(stage) AS max_stage FROM report WHERE classify_id_second=? AND id<>? "
  217. o.Raw(sql, classifyIdSecond, reportId).QueryRow(&count)
  218. } else {
  219. sql = "SELECT MAX(stage) AS max_stage FROM report WHERE classify_id_first=? AND id<>? "
  220. o.Raw(sql, classifyIdFirst, reportId).QueryRow(&count)
  221. }
  222. return
  223. }
  224. type PublishReq struct {
  225. ReportIds string `description:"报告id,多个用英文逗号隔开"`
  226. }
  227. type PublishCancelReq struct {
  228. ReportIds int `description:"报告id"`
  229. }
  230. type DeleteReq struct {
  231. ReportIds int `description:"报告id"`
  232. }
  233. type AddReq struct {
  234. AddType int `description:"新增方式:1:新增报告,2:继承报告"`
  235. ClassifyIdFirst int `description:"一级分类id"`
  236. ClassifyNameFirst string `description:"一级分类名称"`
  237. ClassifyIdSecond int `description:"二级分类id"`
  238. ClassifyNameSecond string `description:"二级分类名称"`
  239. Title string `description:"标题"`
  240. Abstract string `description:"摘要"`
  241. Author string `description:"作者"`
  242. Frequency string `description:"频度"`
  243. State int `description:"状态:1:未发布,2:已发布"`
  244. Content string `description:"内容"`
  245. CreateTime string `description:"创建时间"`
  246. ReportVersion int `description:"1:旧版,2:新版"`
  247. }
  248. type PrePublishReq struct {
  249. ReportId int `description:"报告id"`
  250. PrePublishTime string `description:"预发布时间"`
  251. PreMsgSend int `description:"定时发布成功后是否立即推送模版消息:0否,1是"`
  252. }
  253. type AddResp struct {
  254. ReportId int64 `description:"报告id"`
  255. ReportCode string `description:"报告code"`
  256. }
  257. func AddReport(item *Report) (lastId int64, err error) {
  258. o := orm.NewOrmUsingDB("rddp")
  259. lastId, err = o.Insert(item)
  260. return
  261. }
  262. type EditReq struct {
  263. ReportId int64 `description:"报告id"`
  264. ClassifyIdFirst int `description:"一级分类id"`
  265. ClassifyNameFirst string `description:"一级分类名称"`
  266. ClassifyIdSecond int `description:"二级分类id"`
  267. ClassifyNameSecond string `description:"二级分类名称"`
  268. Title string `description:"标题"`
  269. Abstract string `description:"摘要"`
  270. Author string `description:"作者"`
  271. Frequency string `description:"频度"`
  272. State int `description:"状态:1:未发布,2:已发布"`
  273. Content string `description:"内容"`
  274. CreateTime string `description:"创建时间"`
  275. }
  276. type EditResp struct {
  277. ReportId int64 `description:"报告id"`
  278. ReportCode string `description:"报告code"`
  279. }
  280. func EditReport(item *Report, reportId int64) (err error) {
  281. o := orm.NewOrmUsingDB("rddp")
  282. sql := `UPDATE report
  283. SET
  284. classify_id_first =?,
  285. classify_name_first = ?,
  286. classify_id_second = ?,
  287. classify_name_second = ?,
  288. title = ?,
  289. abstract = ?,
  290. author = ?,
  291. frequency = ?,
  292. state = ?,
  293. content = ?,
  294. content_sub = ?,
  295. stage =?,
  296. create_time = ?,
  297. modify_time = ?
  298. WHERE id = ? `
  299. _, err = o.Raw(sql, item.ClassifyIdFirst, item.ClassifyNameFirst, item.ClassifyIdSecond, item.ClassifyNameSecond, item.Title,
  300. item.Abstract, item.Author, item.Frequency, item.State, item.Content, item.ContentSub, item.Stage, item.CreateTime, time.Now(), reportId).Exec()
  301. return
  302. }
  303. type ReportDetailReq struct {
  304. ReportId int `description:"报告id"`
  305. }
  306. type ClassifyIdDetailReq struct {
  307. ClassifyIdFirst int `description:"报告一级分类id"`
  308. ClassifyIdSecond int `description:"报告二级分类id"`
  309. }
  310. func GetReportDetailByClassifyId(classifyIdFirst, classifyIdSecond int) (item *Report, err error) {
  311. o := orm.NewOrmUsingDB("rddp")
  312. sql := ` SELECT * FROM report WHERE 1=1 `
  313. if classifyIdSecond > 0 {
  314. sql = sql + ` AND classify_id_second=? ORDER BY stage DESC LIMIT 1`
  315. err = o.Raw(sql, classifyIdSecond).QueryRow(&item)
  316. } else {
  317. sql = sql + ` AND classify_id_first=? ORDER BY stage DESC LIMIT 1`
  318. err = o.Raw(sql, classifyIdFirst).QueryRow(&item)
  319. }
  320. return
  321. }
  322. type SendTemplateMsgReq struct {
  323. ReportId int `description:"报告id"`
  324. }
  325. func ModifyReportMsgIsSend(reportId int) (err error) {
  326. o := orm.NewOrmUsingDB("rddp")
  327. report, err := GetReportById(reportId)
  328. if err != nil {
  329. return
  330. }
  331. if report.MsgIsSend == 0 {
  332. sql := `UPDATE report SET msg_is_send = 1, msg_send_time=NOW() WHERE id = ? `
  333. _, err = o.Raw(sql, reportId).Exec()
  334. }
  335. return
  336. }
  337. func ModifyReportVideo(reportId int, videoUrl, videoName, videoSize string, playSeconds float64) (err error) {
  338. o := orm.NewOrmUsingDB("rddp")
  339. sql := `UPDATE report SET video_url=?,video_name=?,video_play_seconds=?,video_size=? WHERE id=? `
  340. _, err = o.Raw(sql, videoUrl, videoName, playSeconds, videoSize, reportId).Exec()
  341. return
  342. }
  343. type ReportItem struct {
  344. Id int `orm:"column(id)" description:"报告Id"`
  345. AddType int `description:"新增方式:1:新增报告,2:继承报告"`
  346. ClassifyIdFirst int `description:"一级分类id"`
  347. ClassifyNameFirst string `description:"一级分类名称"`
  348. ClassifyIdSecond int `description:"二级分类id"`
  349. ClassifyNameSecond string `description:"二级分类名称"`
  350. Title string `description:"标题"`
  351. Abstract string `description:"摘要"`
  352. Author string `description:"作者"`
  353. Frequency string `description:"频度"`
  354. CreateTime time.Time `description:"创建时间"`
  355. ModifyTime time.Time `description:"修改时间"`
  356. State int `description:"1:未发布,2:已发布"`
  357. PublishTime time.Time `description:"发布时间"`
  358. Stage int `description:"期数"`
  359. MsgIsSend int `description:"消息是否已发送,0:否,1:是"`
  360. Content string `description:"内容"`
  361. VideoUrl string `description:"音频文件URL"`
  362. VideoName string `description:"音频文件名称"`
  363. VideoPlaySeconds string `description:"音频播放时长"`
  364. ContentSub string `description:"内容前两个章节"`
  365. }
  366. func GetReportItemById(reportId int) (item *ReportItem, err error) {
  367. o := orm.NewOrmUsingDB("rddp")
  368. sql := `SELECT * FROM report WHERE id=?`
  369. err = o.Raw(sql, reportId).QueryRow(&item)
  370. return
  371. }
  372. type SaveReportContent struct {
  373. Content string `description:"内容"`
  374. ReportId int `description:"报告id"`
  375. NoChange int `description:"内容是否未改变:1:内容未改变"`
  376. }
  377. func EditReportContent(reportId int, content, contentSub string) (err error) {
  378. o := orm.NewOrmUsingDB("rddp")
  379. sql := ` UPDATE report SET content=?,content_sub=?,modify_time=NOW() WHERE id=? `
  380. _, err = o.Raw(sql, content, contentSub, reportId).Exec()
  381. return
  382. }
  383. func AddReportSaveLog(reportId, adminId int, content, contentSub, adminName string) (err error) {
  384. o := orm.NewOrmUsingDB("rddp")
  385. sql := ` INSERT INTO report_save_log(report_id, content,content_sub,admin_id,admin_name) VALUES (?,?,?,?,?) `
  386. _, err = o.Raw(sql, reportId, content, contentSub, adminId, adminName).Exec()
  387. return
  388. }
  389. func MultiAddReportChaptersSaveLog(items []*ReportChapter, adminId int, adminRealName string) (err error) {
  390. o := orm.NewOrmUsingDB("rddp")
  391. p, err := o.Raw(`INSERT INTO report_save_log(report_id, report_chapter_id, content, content_sub, admin_id, admin_name) VALUES (?,?,?,?,?,?)`).Prepare()
  392. if err != nil {
  393. return
  394. }
  395. defer func() {
  396. _ = p.Close()
  397. }()
  398. for _, v := range items {
  399. _, err = p.Exec(v.ReportId, v.ReportChapterId, v.Content, v.ContentSub, adminId, adminRealName)
  400. if err != nil {
  401. return
  402. }
  403. }
  404. return
  405. }
  406. type SaveReportContentResp struct {
  407. ReportId int `description:"报告id"`
  408. }
  409. func ModifyReportCode(reportId int64, reportCode string) (err error) {
  410. o := orm.NewOrmUsingDB("rddp")
  411. sql := `UPDATE report SET report_code=? WHERE id=? `
  412. _, err = o.Raw(sql, reportCode, reportId).Exec()
  413. return
  414. }
  415. func ModifyReportThsMsgIsSend(item *ReportDetail) (err error) {
  416. o := orm.NewOrmUsingDB("rddp")
  417. if item.ThsMsgIsSend == 0 {
  418. sql := `UPDATE report SET ths_msg_is_send = 1 WHERE id = ? `
  419. _, err = o.Raw(sql, item.Id).Exec()
  420. }
  421. return
  422. }
  423. type ThsSendTemplateMsgReq struct {
  424. ReportId []int `description:"报告id"`
  425. }
  426. type PublishDayWeekReportReq struct {
  427. ReportId int `description:"报告ID"`
  428. }
  429. // SaveDayWeekReportReq 新增晨报周报请求体
  430. type SaveDayWeekReportReq struct {
  431. ReportId int `description:"报告ID"`
  432. Title string `description:"标题"`
  433. ReportType string `description:"一级分类ID"`
  434. Author string `description:"作者"`
  435. CreateTime string `description:"创建时间"`
  436. }
  437. // GetDayWeekReportStage 获取晨报周报期数
  438. func GetDayWeekReportStage(classifyIdFirst int, yearStart time.Time) (count int, err error) {
  439. o := orm.NewOrmUsingDB("rddp")
  440. sql := " SELECT MAX(stage) AS max_stage FROM report WHERE classify_id_first = ? AND create_time > ? "
  441. o.Raw(sql, classifyIdFirst, yearStart).QueryRow(&count)
  442. return
  443. }
  444. // AddReportAndChapter 新增报告及章节
  445. func AddReportAndChapter(reportItem *Report, chapterItemList []*ReportChapter) (reportId int64, err error) {
  446. o := orm.NewOrmUsingDB("rddp")
  447. to, err := o.Begin()
  448. if err != nil {
  449. return
  450. }
  451. defer func() {
  452. if err != nil {
  453. _ = to.Rollback()
  454. } else {
  455. _ = to.Commit()
  456. }
  457. }()
  458. if reportId, err = to.Insert(reportItem); err != nil {
  459. return
  460. }
  461. if len(chapterItemList) > 0 {
  462. for _, chapterItem := range chapterItemList {
  463. chapterItem.ReportId = int(reportId)
  464. cpId, tmpErr := to.Insert(chapterItem)
  465. if tmpErr != nil {
  466. return
  467. }
  468. chapterItem.ReportChapterId = int(cpId)
  469. }
  470. }
  471. return
  472. }
  473. // GetReportByReportId 主键获取报告
  474. func GetReportByReportId(reportId int) (item *Report, err error) {
  475. o := orm.NewOrmUsingDB("rddp")
  476. sql := `SELECT * FROM report WHERE id = ?`
  477. err = o.Raw(sql, reportId).QueryRow(&item)
  478. return
  479. }
  480. // GetReportByOldReportId 根据老报告id主键获取报告
  481. func GetReportByOldReportId(reportId int) (item *Report, err error) {
  482. o := orm.NewOrmUsingDB("rddp")
  483. sql := `SELECT * FROM report WHERE old_report_id = ?`
  484. err = o.Raw(sql, reportId).QueryRow(&item)
  485. return
  486. }
  487. // DeleteDayWeekReportAndChapter 删除晨周报及章节
  488. func DeleteDayWeekReportAndChapter(reportId int) (err error) {
  489. o := orm.NewOrmUsingDB("rddp")
  490. to, err := o.Begin()
  491. if err != nil {
  492. return
  493. }
  494. defer func() {
  495. if err != nil {
  496. _ = to.Rollback()
  497. } else {
  498. _ = to.Commit()
  499. }
  500. }()
  501. sql := ` DELETE FROM report WHERE id = ? LIMIT 1 `
  502. if _, err = to.Raw(sql, reportId).Exec(); err != nil {
  503. return
  504. }
  505. sql = ` DELETE FROM report_chapter WHERE report_id = ? `
  506. if _, err = to.Raw(sql, reportId).Exec(); err != nil {
  507. return
  508. }
  509. return
  510. }
  511. // UpdateReport 更新报告
  512. func (reportInfo *Report) UpdateReport(cols []string) (err error) {
  513. o := orm.NewOrmUsingDB("rddp")
  514. _, err = o.Update(reportInfo, cols...)
  515. return
  516. }
  517. // 晨周报详情
  518. type ReportDetailView struct {
  519. *ReportDetail
  520. ChapterList []*ReportChapter
  521. }
  522. func GetUnPublishDayReport(startTime time.Time, endTime time.Time) (item *Report, err error) {
  523. o := orm.NewOrmUsingDB("rddp")
  524. sql := ` SELECT
  525. *
  526. FROM
  527. report AS a
  528. WHERE
  529. a.has_chapter = 1
  530. AND a.chapter_type = "day"
  531. AND a.state = 1
  532. AND (
  533. a.create_time BETWEEN ? AND ?
  534. )
  535. ORDER BY
  536. a.create_time DESC
  537. LIMIT 1 `
  538. err = o.Raw(sql, startTime, endTime).QueryRow(&item)
  539. return
  540. }
  541. type ElasticReportDetail struct {
  542. ReportId int `description:"报告ID"`
  543. ReportChapterId int `description:"报告章节ID"`
  544. Title string `description:"标题"`
  545. Abstract string `description:"摘要"`
  546. BodyContent string `description:"内容"`
  547. PublishTime string `description:"发布时间"`
  548. PublishState int `description:"发布状态 1-未发布 2-已发布"`
  549. Author string `description:"作者"`
  550. ClassifyIdFirst int `description:"一级分类ID"`
  551. ClassifyNameFirst string `description:"一级分类名称"`
  552. ClassifyIdSecond int `description:"二级分类ID"`
  553. ClassifyNameSecond string `description:"二级分类名称"`
  554. Categories string `description:"关联的品种名称(包括品种别名)"`
  555. StageStr string `description:"报告期数"`
  556. BodyMd5 string `description:"MD5加密之后的内容"`
  557. }
  558. // GetLastPublishedDayWeekReport 获取上一篇已发布的晨周报
  559. func GetLastPublishDayWeekReport(chapterType string) (item *Report, err error) {
  560. o := orm.NewOrmUsingDB("rddp")
  561. sql := ` SELECT * FROM report WHERE has_chapter = 1 AND chapter_type = ? AND state = 2 ORDER BY publish_time DESC LIMIT 1 `
  562. err = o.Raw(sql, chapterType).QueryRow(&item)
  563. return
  564. }
  565. // GetNewReportExist
  566. func GetNewReportExist(oldReportId int) (item *Report, err error) {
  567. o := orm.NewOrmUsingDB("rddp")
  568. sql := ` SELECT id FROM report WHERE old_report_id = ? LIMIT 1 `
  569. err = o.Raw(sql, oldReportId).QueryRow(&item)
  570. return
  571. }
  572. // PublishReportAndChapter 发布报告及章节
  573. func PublishReportAndChapter(reportInfo *Report, publishIds, unPublishIds []int, isPublishReport bool, cols []string) (err error) {
  574. o := orm.NewOrmUsingDB("rddp")
  575. to, err := o.Begin()
  576. if err != nil {
  577. return
  578. }
  579. defer func() {
  580. if err != nil {
  581. _ = to.Rollback()
  582. } else {
  583. _ = to.Commit()
  584. }
  585. }()
  586. // 更新报告
  587. if isPublishReport {
  588. if _, err = to.Update(reportInfo, cols...); err != nil {
  589. return
  590. }
  591. }
  592. // 发布章节
  593. if len(publishIds) > 0 {
  594. sql := ` UPDATE report_chapter SET publish_state = 2, publish_time = ? WHERE report_id = ? AND report_chapter_id IN (` + utils.GetOrmInReplace(len(publishIds)) + `) `
  595. _, err = to.Raw(sql, reportInfo.PublishTime, reportInfo.Id, publishIds).Exec()
  596. }
  597. if len(unPublishIds) > 0 {
  598. 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)) + `) `
  599. _, err = to.Raw(sql, reportInfo.Id, unPublishIds).Exec()
  600. }
  601. return
  602. }
  603. func GetSyncEmptyVideoReport() (list []*Report, err error) {
  604. o := orm.NewOrmUsingDB("rddp")
  605. sql := ` SELECT id FROM report WHERE old_report_id > 0 AND state = 2 AND chapter_type = "" AND (video_size = "" OR video_play_seconds = "")
  606. UNION ALL
  607. SELECT DISTINCT report_id FROM report_chapter WHERE publish_state = 2 AND (video_size = "" OR video_play_seconds = "") `
  608. _, err = o.Raw(sql).QueryRows(&list)
  609. return
  610. }
  611. // 发布报告
  612. func PublishReportById(reportId int, publishTime time.Time) (err error) {
  613. o := orm.NewOrmUsingDB("rddp")
  614. sql := `UPDATE report SET state = 2, publish_time = ?, pre_publish_time=null, pre_msg_send=0, modify_time = NOW() WHERE id = ? `
  615. _, err = o.Raw(sql, publishTime, reportId).Exec()
  616. return
  617. }
  618. // GetCommentReportByReportId 查询有留言的报告列表
  619. func GetCommentReportByReportId(condition string, pars []interface{}, startSize, pageSize int) (list []*Report, err error) {
  620. o := orm.NewOrmUsingDB("rddp")
  621. sql := `SELECT
  622. id,
  623. create_time,
  624. title,
  625. classify_name_first,
  626. classify_id_first,
  627. classify_name_second,
  628. classify_id_second,
  629. state,
  630. IF
  631. ( publish_time, publish_time, create_time ) AS publish_time
  632. FROM
  633. report
  634. WHERE
  635. 1=1
  636. `
  637. if condition != "" {
  638. sql += condition
  639. }
  640. sql += ` ORDER BY publish_time DESC , title ASC LIMIT ?,?`
  641. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  642. return
  643. }
  644. // GetCommentReportByReportIdOrderBy 查询有留言的报告列表(指定排序)
  645. func GetCommentReportByReportIdOrderBy(condition string, pars []interface{}, startSize, pageSize int, orderBy string) (list []*Report, err error) {
  646. o := orm.NewOrmUsingDB("rddp")
  647. sql := `SELECT
  648. id,
  649. create_time,
  650. title,
  651. classify_name_first,
  652. classify_id_first,
  653. classify_name_second,
  654. classify_id_second,
  655. state,
  656. IF
  657. ( publish_time, publish_time, create_time ) AS publish_time
  658. FROM
  659. report
  660. WHERE
  661. 1=1
  662. `
  663. if condition != "" {
  664. sql += condition
  665. }
  666. if orderBy == `` {
  667. sql += ` ORDER BY publish_time DESC , title ASC `
  668. } else {
  669. sql += orderBy
  670. }
  671. sql += ` LIMIT ?,? `
  672. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  673. return
  674. }
  675. // GetCommentReportTotalByReportId 查询有留言的报告列表总数
  676. func GetCommentReportTotalByReportId(condition string, pars []interface{}) (total int64, err error) {
  677. o := orm.NewOrmUsingDB("rddp")
  678. sql := `SELECT count(*)
  679. FROM report WHERE 1=1`
  680. if condition != "" {
  681. sql += condition
  682. }
  683. err = o.Raw(sql, pars).QueryRow(&total)
  684. return
  685. }
  686. // 点赞相关的报告列表
  687. type LikeReportItem struct {
  688. ReportId int `description:"报告Id"`
  689. ReportChapterId int `description:"报告章节Id"`
  690. ClassifyIdFirst int `description:"一级分类id"`
  691. ClassifyNameFirst string `description:"一级分类名称"`
  692. ClassifyIdSecond int `description:"二级分类id"`
  693. ClassifyNameSecond string `description:"二级分类名称"`
  694. ReportChapterTypeId int `description:"章节类型"`
  695. ReportChapterTypeName string `description:"品种名称"`
  696. PublishTime time.Time `description:"发布时间" `
  697. Title string `description:"标题"`
  698. }
  699. // GetLikeReportByReportIdReportChapterId 获取有被点赞的报告列表
  700. func GetLikeReportByReportIdReportChapterId(reportIds string, chapterIds string, orderStr string, startSize, pageSize int) (list []*LikeReportItem, err error) {
  701. o := orm.NewOrmUsingDB("rddp")
  702. sql := `( SELECT
  703. id AS report_id,
  704. 0 AS report_chapter_id,
  705. classify_id_first,
  706. classify_id_second,
  707. classify_name_first,
  708. classify_name_second,
  709. 0 as report_chapter_type_id,
  710. "" as report_chapter_type_name,
  711. publish_time,
  712. title
  713. FROM
  714. report
  715. WHERE
  716. classify_name_first != "晨报"
  717. AND classify_name_first != "周报"
  718. AND id in (` + reportIds + `)
  719. )
  720. UNION
  721. ( SELECT
  722. report_id,
  723. report_chapter_id,
  724. classify_id_first,
  725. 0 as classify_id_second,
  726. classify_name_first,
  727. null as classify_name_second,
  728. type_id as report_chapter_type_id,
  729. type_name as report_chapter_type_name,
  730. publish_time,
  731. title
  732. FROM
  733. report_chapter
  734. WHERE
  735. report_chapter_id in (` + chapterIds + `)
  736. )`
  737. if orderStr != "" {
  738. sql += ` ORDER BY FIELD(CONCAT(report_id, "-",report_chapter_id),` + orderStr + `)`
  739. } else {
  740. sql += ` ORDER BY publish_time DESC, report_id Desc`
  741. }
  742. sql += ` LIMIT ?,?`
  743. _, err = o.Raw(sql, startSize, pageSize).QueryRows(&list)
  744. return
  745. }
  746. // GetLikeReportTotalByReportIdReportChapterId 获取有被点赞的报告列表总数
  747. func GetLikeReportTotalByReportIdReportChapterId(reportIds string, chapterIds string) (total int64, err error) {
  748. o := orm.NewOrmUsingDB("rddp")
  749. sql := `select count(*) from (( SELECT
  750. id AS report_id,
  751. 0 AS report_chapter_id,
  752. classify_id_first,
  753. classify_id_second,
  754. classify_name_first,
  755. classify_name_second,
  756. 0 as report_chapter_type_id,
  757. publish_time,
  758. title
  759. FROM
  760. report
  761. WHERE
  762. classify_name_first != "晨报"
  763. AND classify_name_first != "周报"
  764. AND id in (` + reportIds + `)
  765. )
  766. UNION
  767. ( SELECT
  768. report_id,
  769. report_chapter_id,
  770. classify_id_first,
  771. 0 as classify_id_second,
  772. classify_name_first,
  773. null as classify_name_second,
  774. type_id as report_chapter_type_id,
  775. publish_time,
  776. title
  777. FROM
  778. report_chapter
  779. WHERE
  780. report_chapter_id in (` + chapterIds + `)
  781. )) r`
  782. err = o.Raw(sql).QueryRow(&total)
  783. return
  784. }
  785. // GetPageReportList 分页获取报告列表
  786. func GetPageReportList(condition string, pars []interface{}, startSize, pageSize int) (total int, items []*ReportList, err error) {
  787. o := orm.NewOrmUsingDB("rddp")
  788. sql := `SELECT * FROM report WHERE 1=1 `
  789. sql += condition
  790. sql += ` ORDER BY modify_time DESC`
  791. totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z `
  792. err = o.Raw(totalSql, pars).QueryRow(&total)
  793. if err != nil {
  794. return
  795. }
  796. sql += ` LIMIT ?,? `
  797. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  798. return
  799. }
  800. // SunCodeReq 获取太阳码请求体
  801. type SunCodeReq struct {
  802. CodePage string `json:"CodePage" description:"太阳码page"`
  803. CodeScene string `json:"CodeScene" description:"太阳码scene"`
  804. }
  805. // YbPcSuncode 活动海报表
  806. type YbPcSuncode struct {
  807. SuncodeID uint32 `orm:"column(suncode_id);pk" `
  808. Scene string `gorm:"column:scene;type:varchar(255);not null;default:0" json:"scene"` // 微信scene
  809. SceneMd5 string `gorm:"column:scene_md5;type:varchar(255);not null" json:"sceneMd5"`
  810. CodePage string `gorm:"column:code_page;type:varchar(255);not null;default:''" json:"codePage"` // 路径
  811. SuncodeUrl string `gorm:"column:suncode_url;type:varchar(255);not null;default:''" json:"suncodeUrl"` // 太阳码储存地址
  812. CreateTime time.Time `gorm:"column:create_time;type:timestamp;default:CURRENT_TIMESTAMP" json:"createTime"`
  813. }
  814. // GetYbPcSunCode 获取太阳码
  815. func GetYbPcSunCode(scene, page string) (item *YbPcSuncode, err error) {
  816. o := orm.NewOrmUsingDB("weekly")
  817. sql := `SELECT * FROM yb_pc_suncode WHERE scene = ? AND code_page = ? `
  818. err = o.Raw(sql, scene, page).QueryRow(&item)
  819. return
  820. }
  821. func AddYbPcSunCode(item *YbPcSuncode) (err error) {
  822. o := orm.NewOrmUsingDB("weekly")
  823. _, err = o.Insert(item)
  824. return
  825. }
  826. // YbSuncodePars 小程序太阳码scene参数
  827. type YbSuncodePars struct {
  828. ID uint32 `orm:"column(id);pk" `
  829. Scene string `gorm:"column:scene;type:varchar(255);not null;default:''" json:"scene"` // scene参数
  830. SceneKey string `gorm:"column:scene_key;type:varchar(32);not null;default:''" json:"scene_key"` // MD5值
  831. CreateTime time.Time `gorm:"column:create_time;type:datetime;default:CURRENT_TIMESTAMP" json:"createTime"`
  832. }
  833. func AddYbSuncodePars(item *YbSuncodePars) (err error) {
  834. o := orm.NewOrmUsingDB("weekly")
  835. _, err = o.Insert(item)
  836. return
  837. }
  838. // UpdateReportSecondClassifyNameByClassifyId 更新报告分类名称字段
  839. func UpdateReportSecondClassifyNameByClassifyId(classifyId int, classifyName string) (err error) {
  840. o := orm.NewOrmUsingDB("rddp")
  841. sql := " UPDATE report SET classify_name_second = ? WHERE classify_id_second = ? "
  842. _, err = o.Raw(sql, classifyName, classifyId).Exec()
  843. return
  844. }
  845. // UpdateReportFirstClassifyNameByClassifyId 更新报告分类一级名称字段
  846. func UpdateReportFirstClassifyNameByClassifyId(classifyId int, classifyName string) (err error) {
  847. o := orm.NewOrmUsingDB("rddp")
  848. sql := " UPDATE report SET classify_name_first = ? WHERE classify_id_first = ? "
  849. _, err = o.Raw(sql, classifyName, classifyId).Exec()
  850. return
  851. }
  852. // UpdateReportSecondClassifyFirstNameByClassifyId 更新报告二级分类的一级分类名称和id
  853. func UpdateReportSecondClassifyFirstNameByClassifyId(classifyId, newClassifyId int, classifyName string) (err error) {
  854. o := orm.NewOrmUsingDB("rddp")
  855. sql := " UPDATE report SET classify_name_first = ?, classify_id_first = ? WHERE classify_id_second = ? "
  856. _, err = o.Raw(sql, classifyName, newClassifyId, classifyId).Exec()
  857. return
  858. }
  859. // GetEmptyContentSubPPTReport 获取前两章为空的PPT报告
  860. func GetEmptyContentSubPPTReport() (list []*Report, err error) {
  861. sql := `SELECT
  862. r.id,
  863. r.content,
  864. r.content_sub
  865. FROM
  866. report AS r
  867. JOIN ppt_v2 AS p ON r.id = p.report_id
  868. WHERE
  869. p.report_id > 0 AND r.content_sub = ""`
  870. _, err = orm.NewOrmUsingDB("rddp").Raw(sql).QueryRows(&list)
  871. return
  872. }
  873. // ModifyReportAuthor 更改报告作者
  874. func ModifyReportAuthor(condition string, pars []interface{}, authorName string) (count int, err error) {
  875. //产品权限
  876. oRddp := orm.NewOrmUsingDB("rddp")
  877. sql := `UPDATE english_report set author = ? WHERE 1=1 `
  878. if condition != "" {
  879. sql += condition
  880. }
  881. err = oRddp.Raw(sql, authorName, pars).QueryRow(&count)
  882. return
  883. }
  884. func UpdateReportPublishTime(reportId int, videoNameDate string) (err error) {
  885. o := orm.NewOrmUsingDB("rddp")
  886. sql1 := ` UPDATE report SET publish_time = NOW() WHERE id = ? `
  887. _, err = o.Raw(sql1, reportId).Exec()
  888. if err != nil {
  889. return
  890. }
  891. //修改音频标题
  892. sql2 := ` UPDATE report SET video_name=CONCAT(SUBSTRING_INDEX(video_name,"(",1),"` + videoNameDate + `") WHERE id = ? and (video_name !="" and video_name is not null)`
  893. _, err = o.Raw(sql2, reportId).Exec()
  894. return
  895. }
  896. func UpdateReportChapterPublishTime(reportId int, videoNameDate string) (err error) {
  897. o := orm.NewOrmUsingDB("rddp")
  898. sql1 := ` UPDATE report_chapter SET publish_time = NOW() WHERE report_id = ? `
  899. _, err = o.Raw(sql1, reportId).Exec()
  900. if err != nil {
  901. return
  902. }
  903. //修改音频标题
  904. 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)`
  905. _, err = o.Raw(sql2, reportId).Exec()
  906. return
  907. }
  908. // MarkEditReport 标记编辑英文研报的请求数据
  909. type MarkEditReport struct {
  910. ReportId int `description:"研报id"`
  911. Status int `description:"标记状态,1:编辑中,2:编辑完成"`
  912. }
  913. type MarkReportResp struct {
  914. Status int `description:"状态:0:无人编辑, 1:当前有人在编辑"`
  915. Msg string `description:"提示信息"`
  916. Editor string `description:"编辑者姓名"`
  917. }
  918. type MarkReportItem struct {
  919. AdminId int `description:"编辑者ID"`
  920. Editor string `description:"编辑者姓名"`
  921. ReportClassifyNameFirst string
  922. }
  923. // GetReportByCondition 获取报告
  924. func GetReportByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, isPage bool, startSize, pageSize int) (items []*Report, err error) {
  925. o := orm.NewOrmUsingDB("rddp")
  926. fields := `*`
  927. if len(fieldArr) > 0 {
  928. fields = strings.Join(fieldArr, ",")
  929. }
  930. sql := `SELECT ` + fields + ` FROM report WHERE 1=1 `
  931. sql += condition
  932. order := ` ORDER BY modify_time DESC`
  933. if orderRule != `` {
  934. order = orderRule
  935. }
  936. sql += order
  937. if isPage {
  938. sql += ` LIMIT ?,?`
  939. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  940. } else {
  941. _, err = o.Raw(sql, pars).QueryRows(&items)
  942. }
  943. return
  944. }
  945. // ModifyReportMsgIsSendV2 更新报告消息状态
  946. func ModifyReportMsgIsSendV2(reportId int) (err error) {
  947. o := orm.NewOrmUsingDB("rddp")
  948. sql := `UPDATE report SET msg_is_send = 1, ths_msg_is_send = 1, msg_send_time = NOW() WHERE id = ? LIMIT 1`
  949. _, err = o.Raw(sql, reportId).Exec()
  950. return
  951. }
  952. // SetPrePublishReportById 设置定时发布
  953. func SetPrePublishReportById(reportId int, prePublishTime string, preMsgSend int) (err error) {
  954. o := orm.NewOrmUsingDB("rddp")
  955. sql := `UPDATE report SET pre_publish_time=?, pre_msg_send=? WHERE id = ? and state = 1 `
  956. _, err = o.Raw(sql, prePublishTime, preMsgSend, reportId).Exec()
  957. return
  958. }