report.go 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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:已发布"`
  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 state ASC, 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. }
  557. // GetLastPublishedDayWeekReport 获取上一篇已发布的晨周报
  558. func GetLastPublishDayWeekReport(chapterType string) (item *Report, err error) {
  559. o := orm.NewOrmUsingDB("rddp")
  560. sql := ` SELECT * FROM report WHERE has_chapter = 1 AND chapter_type = ? AND state = 2 ORDER BY publish_time DESC LIMIT 1 `
  561. err = o.Raw(sql, chapterType).QueryRow(&item)
  562. return
  563. }
  564. // GetNewReportExist
  565. func GetNewReportExist(oldReportId int) (item *Report, err error) {
  566. o := orm.NewOrmUsingDB("rddp")
  567. sql := ` SELECT id FROM report WHERE old_report_id = ? LIMIT 1 `
  568. err = o.Raw(sql, oldReportId).QueryRow(&item)
  569. return
  570. }
  571. // PublishReportAndChapter 发布报告及章节
  572. func PublishReportAndChapter(reportInfo *Report, publishIds, unPublishIds []int, isPublishReport bool, cols []string) (err error) {
  573. o := orm.NewOrmUsingDB("rddp")
  574. to, err := o.Begin()
  575. if err != nil {
  576. return
  577. }
  578. defer func() {
  579. if err != nil {
  580. _ = to.Rollback()
  581. } else {
  582. _ = to.Commit()
  583. }
  584. }()
  585. // 更新报告
  586. if isPublishReport {
  587. if _, err = to.Update(reportInfo, cols...); err != nil {
  588. return
  589. }
  590. }
  591. // 发布章节
  592. if len(publishIds) > 0 {
  593. sql := ` UPDATE report_chapter SET publish_state = 2, publish_time = ? WHERE report_id = ? AND report_chapter_id IN (` + utils.GetOrmInReplace(len(publishIds)) + `) `
  594. _, err = to.Raw(sql, reportInfo.PublishTime, reportInfo.Id, publishIds).Exec()
  595. }
  596. if len(unPublishIds) > 0 {
  597. 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)) + `) `
  598. _, err = to.Raw(sql, reportInfo.Id, unPublishIds).Exec()
  599. }
  600. return
  601. }
  602. func GetSyncEmptyVideoReport() (list []*Report, err error) {
  603. o := orm.NewOrmUsingDB("rddp")
  604. sql := ` SELECT id FROM report WHERE old_report_id > 0 AND state = 2 AND chapter_type = "" AND (video_size = "" OR video_play_seconds = "")
  605. UNION ALL
  606. SELECT DISTINCT report_id FROM report_chapter WHERE publish_state = 2 AND (video_size = "" OR video_play_seconds = "") `
  607. _, err = o.Raw(sql).QueryRows(&list)
  608. return
  609. }
  610. // 发布报告
  611. func PublishReportById(reportId int, publishTime time.Time) (err error) {
  612. o := orm.NewOrmUsingDB("rddp")
  613. sql := `UPDATE report SET state = 2, publish_time = ?, pre_publish_time=null, pre_msg_send=0, modify_time = NOW() WHERE id = ? `
  614. _, err = o.Raw(sql, publishTime, reportId).Exec()
  615. return
  616. }
  617. // GetCommentReportByReportId 查询有留言的报告列表
  618. func GetCommentReportByReportId(condition string, pars []interface{}, startSize, pageSize int) (list []*Report, err error) {
  619. o := orm.NewOrmUsingDB("rddp")
  620. sql := `SELECT
  621. id,
  622. create_time,
  623. title,
  624. classify_name_first,
  625. classify_id_first,
  626. classify_name_second,
  627. classify_id_second,
  628. state,
  629. IF
  630. ( publish_time, publish_time, create_time ) AS publish_time
  631. FROM
  632. report
  633. WHERE
  634. 1=1
  635. `
  636. if condition != "" {
  637. sql += condition
  638. }
  639. sql += ` ORDER BY publish_time DESC , title ASC LIMIT ?,?`
  640. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  641. return
  642. }
  643. // GetCommentReportByReportIdOrderBy 查询有留言的报告列表(指定排序)
  644. func GetCommentReportByReportIdOrderBy(condition string, pars []interface{}, startSize, pageSize int, orderBy string) (list []*Report, err error) {
  645. o := orm.NewOrmUsingDB("rddp")
  646. sql := `SELECT
  647. id,
  648. create_time,
  649. title,
  650. classify_name_first,
  651. classify_id_first,
  652. classify_name_second,
  653. classify_id_second,
  654. state,
  655. IF
  656. ( publish_time, publish_time, create_time ) AS publish_time
  657. FROM
  658. report
  659. WHERE
  660. 1=1
  661. `
  662. if condition != "" {
  663. sql += condition
  664. }
  665. if orderBy == `` {
  666. sql += ` ORDER BY publish_time DESC , title ASC `
  667. } else {
  668. sql += orderBy
  669. }
  670. sql += ` LIMIT ?,? `
  671. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  672. return
  673. }
  674. // GetCommentReportTotalByReportId 查询有留言的报告列表总数
  675. func GetCommentReportTotalByReportId(condition string, pars []interface{}) (total int64, err error) {
  676. o := orm.NewOrmUsingDB("rddp")
  677. sql := `SELECT count(*)
  678. FROM report WHERE 1=1`
  679. if condition != "" {
  680. sql += condition
  681. }
  682. err = o.Raw(sql, pars).QueryRow(&total)
  683. return
  684. }
  685. // 点赞相关的报告列表
  686. type LikeReportItem struct {
  687. ReportId int `description:"报告Id"`
  688. ReportChapterId int `description:"报告章节Id"`
  689. ClassifyIdFirst int `description:"一级分类id"`
  690. ClassifyNameFirst string `description:"一级分类名称"`
  691. ClassifyIdSecond int `description:"二级分类id"`
  692. ClassifyNameSecond string `description:"二级分类名称"`
  693. ReportChapterTypeId int `description:"章节类型"`
  694. ReportChapterTypeName string `description:"品种名称"`
  695. PublishTime time.Time `description:"发布时间" `
  696. Title string `description:"标题"`
  697. }
  698. // GetLikeReportByReportIdReportChapterId 获取有被点赞的报告列表
  699. func GetLikeReportByReportIdReportChapterId(reportIds string, chapterIds string, orderStr string, startSize, pageSize int) (list []*LikeReportItem, err error) {
  700. o := orm.NewOrmUsingDB("rddp")
  701. sql := `( SELECT
  702. id AS report_id,
  703. 0 AS report_chapter_id,
  704. classify_id_first,
  705. classify_id_second,
  706. classify_name_first,
  707. classify_name_second,
  708. 0 as report_chapter_type_id,
  709. "" as report_chapter_type_name,
  710. publish_time,
  711. title
  712. FROM
  713. report
  714. WHERE
  715. classify_name_first != "晨报"
  716. AND classify_name_first != "周报"
  717. AND id in (` + reportIds + `)
  718. )
  719. UNION
  720. ( SELECT
  721. report_id,
  722. report_chapter_id,
  723. classify_id_first,
  724. 0 as classify_id_second,
  725. classify_name_first,
  726. null as classify_name_second,
  727. type_id as report_chapter_type_id,
  728. type_name as report_chapter_type_name,
  729. publish_time,
  730. title
  731. FROM
  732. report_chapter
  733. WHERE
  734. report_chapter_id in (` + chapterIds + `)
  735. )`
  736. if orderStr != "" {
  737. sql += ` ORDER BY FIELD(CONCAT(report_id, "-",report_chapter_id),` + orderStr + `)`
  738. } else {
  739. sql += ` ORDER BY publish_time DESC, report_id Desc`
  740. }
  741. sql += ` LIMIT ?,?`
  742. _, err = o.Raw(sql, startSize, pageSize).QueryRows(&list)
  743. return
  744. }
  745. // GetLikeReportTotalByReportIdReportChapterId 获取有被点赞的报告列表总数
  746. func GetLikeReportTotalByReportIdReportChapterId(reportIds string, chapterIds string) (total int64, err error) {
  747. o := orm.NewOrmUsingDB("rddp")
  748. sql := `select count(*) from (( SELECT
  749. id AS report_id,
  750. 0 AS report_chapter_id,
  751. classify_id_first,
  752. classify_id_second,
  753. classify_name_first,
  754. classify_name_second,
  755. 0 as report_chapter_type_id,
  756. publish_time,
  757. title
  758. FROM
  759. report
  760. WHERE
  761. classify_name_first != "晨报"
  762. AND classify_name_first != "周报"
  763. AND id in (` + reportIds + `)
  764. )
  765. UNION
  766. ( SELECT
  767. report_id,
  768. report_chapter_id,
  769. classify_id_first,
  770. 0 as classify_id_second,
  771. classify_name_first,
  772. null as classify_name_second,
  773. type_id as report_chapter_type_id,
  774. publish_time,
  775. title
  776. FROM
  777. report_chapter
  778. WHERE
  779. report_chapter_id in (` + chapterIds + `)
  780. )) r`
  781. err = o.Raw(sql).QueryRow(&total)
  782. return
  783. }
  784. // GetPageReportList 分页获取报告列表
  785. func GetPageReportList(condition string, pars []interface{}, startSize, pageSize int) (total int, items []*ReportList, err error) {
  786. o := orm.NewOrmUsingDB("rddp")
  787. sql := `SELECT * FROM report WHERE 1=1 `
  788. sql += condition
  789. sql += ` ORDER BY modify_time DESC`
  790. totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z `
  791. err = o.Raw(totalSql, pars).QueryRow(&total)
  792. if err != nil {
  793. return
  794. }
  795. sql += ` LIMIT ?,? `
  796. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  797. return
  798. }
  799. // SunCodeReq 获取太阳码请求体
  800. type SunCodeReq struct {
  801. CodePage string `json:"CodePage" description:"太阳码page"`
  802. CodeScene string `json:"CodeScene" description:"太阳码scene"`
  803. }
  804. // YbPcSuncode 活动海报表
  805. type YbPcSuncode struct {
  806. SuncodeID uint32 `orm:"column(suncode_id);pk" `
  807. Scene string `gorm:"column:scene;type:varchar(255);not null;default:0" json:"scene"` // 微信scene
  808. SceneMd5 string `gorm:"column:scene_md5;type:varchar(255);not null" json:"sceneMd5"`
  809. CodePage string `gorm:"column:code_page;type:varchar(255);not null;default:''" json:"codePage"` // 路径
  810. SuncodeUrl string `gorm:"column:suncode_url;type:varchar(255);not null;default:''" json:"suncodeUrl"` // 太阳码储存地址
  811. CreateTime time.Time `gorm:"column:create_time;type:timestamp;default:CURRENT_TIMESTAMP" json:"createTime"`
  812. }
  813. // GetYbPcSunCode 获取太阳码
  814. func GetYbPcSunCode(scene, page string) (item *YbPcSuncode, err error) {
  815. o := orm.NewOrmUsingDB("weekly")
  816. sql := `SELECT * FROM yb_pc_suncode WHERE scene = ? AND code_page = ? `
  817. err = o.Raw(sql, scene, page).QueryRow(&item)
  818. return
  819. }
  820. func AddYbPcSunCode(item *YbPcSuncode) (err error) {
  821. o := orm.NewOrmUsingDB("weekly")
  822. _, err = o.Insert(item)
  823. return
  824. }
  825. // YbSuncodePars 小程序太阳码scene参数
  826. type YbSuncodePars struct {
  827. ID uint32 `orm:"column(id);pk" `
  828. Scene string `gorm:"column:scene;type:varchar(255);not null;default:''" json:"scene"` // scene参数
  829. SceneKey string `gorm:"column:scene_key;type:varchar(32);not null;default:''" json:"scene_key"` // MD5值
  830. CreateTime time.Time `gorm:"column:create_time;type:datetime;default:CURRENT_TIMESTAMP" json:"createTime"`
  831. }
  832. func AddYbSuncodePars(item *YbSuncodePars) (err error) {
  833. o := orm.NewOrmUsingDB("weekly")
  834. _, err = o.Insert(item)
  835. return
  836. }
  837. // UpdateReportSecondClassifyNameByClassifyId 更新报告分类名称字段
  838. func UpdateReportSecondClassifyNameByClassifyId(classifyId int, classifyName string) (err error) {
  839. o := orm.NewOrmUsingDB("rddp")
  840. sql := " UPDATE report SET classify_name_second = ? WHERE classify_id_second = ? "
  841. _, err = o.Raw(sql, classifyName, classifyId).Exec()
  842. return
  843. }
  844. // UpdateReportFirstClassifyNameByClassifyId 更新报告分类一级名称字段
  845. func UpdateReportFirstClassifyNameByClassifyId(classifyId int, classifyName string) (err error) {
  846. o := orm.NewOrmUsingDB("rddp")
  847. sql := " UPDATE report SET classify_name_first = ? WHERE classify_id_first = ? "
  848. _, err = o.Raw(sql, classifyName, classifyId).Exec()
  849. return
  850. }
  851. // UpdateReportSecondClassifyFirstNameByClassifyId 更新报告二级分类的一级分类名称和id
  852. func UpdateReportSecondClassifyFirstNameByClassifyId(classifyId, newClassifyId int, classifyName string) (err error) {
  853. o := orm.NewOrmUsingDB("rddp")
  854. sql := " UPDATE report SET classify_name_first = ?, classify_id_first = ? WHERE classify_id_second = ? "
  855. _, err = o.Raw(sql, classifyName, newClassifyId, classifyId).Exec()
  856. return
  857. }
  858. // GetEmptyContentSubPPTReport 获取前两章为空的PPT报告
  859. func GetEmptyContentSubPPTReport() (list []*Report, err error) {
  860. sql := `SELECT
  861. r.id,
  862. r.content,
  863. r.content_sub
  864. FROM
  865. report AS r
  866. JOIN ppt_v2 AS p ON r.id = p.report_id
  867. WHERE
  868. p.report_id > 0 AND r.content_sub = ""`
  869. _, err = orm.NewOrmUsingDB("rddp").Raw(sql).QueryRows(&list)
  870. return
  871. }
  872. // ModifyReportAuthor 更改报告作者
  873. func ModifyReportAuthor(condition string, pars []interface{}, authorName string) (count int, err error) {
  874. //产品权限
  875. oRddp := orm.NewOrmUsingDB("rddp")
  876. sql := `UPDATE english_report set author = ? WHERE 1=1 `
  877. if condition != "" {
  878. sql += condition
  879. }
  880. err = oRddp.Raw(sql, authorName, pars).QueryRow(&count)
  881. return
  882. }
  883. func UpdateReportPublishTime(reportId int, videoNameDate string) (err error) {
  884. o := orm.NewOrmUsingDB("rddp")
  885. sql1 := ` UPDATE report SET publish_time = NOW() WHERE id = ? `
  886. _, err = o.Raw(sql1, reportId).Exec()
  887. if err != nil {
  888. return
  889. }
  890. //修改音频标题
  891. sql2 := ` UPDATE report SET video_name=CONCAT(SUBSTRING_INDEX(video_name,"(",1),"` + videoNameDate + `") WHERE id = ? and (video_name !="" and video_name is not null)`
  892. _, err = o.Raw(sql2, reportId).Exec()
  893. return
  894. }
  895. func UpdateReportChapterPublishTime(reportId int, videoNameDate string) (err error) {
  896. o := orm.NewOrmUsingDB("rddp")
  897. sql1 := ` UPDATE report_chapter SET publish_time = NOW() WHERE report_id = ? `
  898. _, err = o.Raw(sql1, reportId).Exec()
  899. if err != nil {
  900. return
  901. }
  902. //修改音频标题
  903. 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)`
  904. _, err = o.Raw(sql2, reportId).Exec()
  905. return
  906. }
  907. // MarkEditReport 标记编辑英文研报的请求数据
  908. type MarkEditReport struct {
  909. ReportId int `description:"研报id"`
  910. Status int `description:"标记状态,1:编辑中,2:编辑完成"`
  911. }
  912. type MarkReportResp struct {
  913. Status int `description:"状态:0:无人编辑, 1:当前有人在编辑"`
  914. Msg string `description:"提示信息"`
  915. Editor string `description:"编辑者姓名"`
  916. }
  917. type MarkReportItem struct {
  918. AdminId int `description:"编辑者ID"`
  919. Editor string `description:"编辑者姓名"`
  920. ReportClassifyNameFirst string
  921. }
  922. // GetReportByCondition 获取报告
  923. func GetReportByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, isPage bool, startSize, pageSize int) (items []*Report, err error) {
  924. o := orm.NewOrmUsingDB("rddp")
  925. fields := `*`
  926. if len(fieldArr) > 0 {
  927. fields = strings.Join(fieldArr, ",")
  928. }
  929. sql := `SELECT ` + fields + ` FROM report WHERE 1=1 `
  930. sql += condition
  931. order := ` ORDER BY modify_time DESC`
  932. if orderRule != `` {
  933. order = orderRule
  934. }
  935. sql += order
  936. if isPage {
  937. sql += ` LIMIT ?,?`
  938. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  939. } else {
  940. _, err = o.Raw(sql, pars).QueryRows(&items)
  941. }
  942. return
  943. }
  944. // ModifyReportMsgIsSendV2 更新报告消息状态
  945. func ModifyReportMsgIsSendV2(reportId int) (err error) {
  946. o := orm.NewOrmUsingDB("rddp")
  947. sql := `UPDATE report SET msg_is_send = 1, ths_msg_is_send = 1, msg_send_time = NOW() WHERE id = ? LIMIT 1`
  948. _, err = o.Raw(sql, reportId).Exec()
  949. return
  950. }
  951. // SetPrePublishReportById 设置定时发布
  952. func SetPrePublishReportById(reportId int, prePublishTime string, preMsgSend int) (err error) {
  953. o := orm.NewOrmUsingDB("rddp")
  954. sql := `UPDATE report SET pre_publish_time=?, pre_msg_send=? WHERE id = ? and state = 1 `
  955. _, err = o.Raw(sql, prePublishTime, preMsgSend, reportId).Exec()
  956. return
  957. }