report.go 35 KB

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