report.go 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  1. package models
  2. import (
  3. "eta/eta_mobile/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. "strings"
  8. "time"
  9. )
  10. // 报告状态
  11. const (
  12. ReportStateUnpublished = 1 // 未发布
  13. ReportStatePublished = 2 // 已发布
  14. ReportStateWaitSubmit = 3 // 待提交
  15. ReportStateWaitApprove = 4 // 审批中
  16. ReportStateRefused = 5 // 已驳回
  17. ReportStatePass = 6 // 已通过
  18. )
  19. // 报告操作
  20. const (
  21. ReportOperateAdd = 1 // 新增报告
  22. ReportOperateEdit = 2 // 编辑报告
  23. ReportOperatePublish = 3 // 发布报告
  24. ReportOperateCancelPublish = 4 // 取消发布报告
  25. ReportOperateSubmitApprove = 5 // 提交审批
  26. ReportOperateCancelApprove = 6 // 撤回审批
  27. )
  28. type Report struct {
  29. Id int `orm:"column(id)" description:"报告Id"`
  30. AddType int `description:"新增方式:1:新增报告,2:继承报告"`
  31. ClassifyIdFirst int `description:"一级分类id"`
  32. ClassifyNameFirst string `description:"一级分类名称"`
  33. ClassifyIdSecond int `description:"二级分类id"`
  34. ClassifyNameSecond string `description:"二级分类名称"`
  35. Title string `description:"标题"`
  36. Abstract string `description:"摘要"`
  37. Author string `description:"作者"`
  38. Frequency string `description:"频度"`
  39. CreateTime string `description:"创建时间"`
  40. ModifyTime time.Time `description:"修改时间"`
  41. State int `description:"1:未发布;2:已发布;3-待提交;4-待审批;5-已驳回;6-已通过"`
  42. PublishTime time.Time `description:"发布时间"`
  43. Stage int `description:"期数"`
  44. MsgIsSend int `description:"消息是否已发送,0:否,1:是"`
  45. ThsMsgIsSend int `description:"客户群消息是否已发送,0:否,1:是"`
  46. Content string `description:"内容"`
  47. VideoUrl string `description:"音频文件URL"`
  48. VideoName string `description:"音频文件名称"`
  49. VideoPlaySeconds string `description:"音频播放时长"`
  50. VideoSize string `description:"音频文件大小,单位M"`
  51. ContentSub string `description:"内容前两个章节"`
  52. ReportCode string `description:"报告唯一编码"`
  53. ReportVersion int `description:"1:旧版,2:新版"`
  54. HasChapter int `description:"是否有章节 0-否 1-是"`
  55. ChapterType string `description:"章节类型 day-晨报 week-周报"`
  56. OldReportId int `description:"research_report表ID, 大于0则表示该报告为老后台同步过来的"`
  57. MsgSendTime time.Time `description:"模版消息发送时间"`
  58. AdminId int `description:"创建者账号"`
  59. AdminRealName string `description:"创建者姓名"`
  60. ApproveTime time.Time `description:"审批时间"`
  61. ApproveId int `description:"审批ID"`
  62. }
  63. type ReportList struct {
  64. Id int `description:"报告Id"`
  65. AddType int `description:"新增方式:1:新增报告,2:继承报告"`
  66. ClassifyIdFirst int `description:"一级分类id"`
  67. ClassifyNameFirst string `description:"一级分类名称"`
  68. ClassifyIdSecond int `description:"二级分类id"`
  69. ClassifyNameSecond string `description:"二级分类名称"`
  70. Title string `description:"标题"`
  71. Abstract string `description:"摘要"`
  72. Author string `description:"作者"`
  73. Frequency string `description:"频度"`
  74. CreateTime string `description:"创建时间"`
  75. ModifyTime time.Time `description:"修改时间"`
  76. State int `description:"1:未发布;2:已发布;3-待提交;4-待审批;5-已驳回;6-已通过"`
  77. PublishTime string `description:"发布时间"`
  78. PrePublishTime string `description:"预发布时间"`
  79. Stage int `description:"期数"`
  80. MsgIsSend int `description:"模板消息是否已发送,0:否,1:是"`
  81. Content string `description:"内容"`
  82. VideoUrl string `description:"音频文件URL"`
  83. VideoName string `description:"音频文件名称"`
  84. VideoPlaySeconds string `description:"音频播放时长"`
  85. ContentSub string `description:"内容前两个章节"`
  86. Pv int `description:"Pv"`
  87. Uv int `description:"Uv"`
  88. ReportCode string `description:"报告唯一编码"`
  89. ReportVersion int `description:"1:旧版,2:新版"`
  90. ThsMsgIsSend int `description:"客户群消息是否已发送,0:否,1:是"`
  91. NeedThsMsg int `description:"是否需要推送客群消息 0-否 1-是"`
  92. HasChapter int `description:"是否有章节 0-否 1-是"`
  93. ChapterType string `description:"章节类型 day-晨报 week-周报"`
  94. ChapterVideoList []*ReportChapterVideoList `description:"章节音频列表"`
  95. OldReportId int `description:"research_report表ID, 大于0则表示该报告为老后台同步过来的"`
  96. MsgSendTime string `description:"模版消息发送时间"`
  97. CanEdit bool `description:"是否可编辑"`
  98. Editor string `description:"编辑人"`
  99. AdminId int `description:"创建者账号"`
  100. AdminRealName string `description:"创建者姓名"`
  101. ApproveTime string `description:"审批时间"`
  102. DetailImgUrl string `description:"报告详情长图地址"`
  103. DetailPdfUrl string `description:"报告详情PDF地址"`
  104. }
  105. type ReportListResp struct {
  106. List []*ReportList
  107. Paging *paging.PagingItem `description:"分页数据"`
  108. }
  109. func GetReportListCount(condition string, pars []interface{}, companyType string) (count int, err error) {
  110. //产品权限
  111. companyTypeSqlStr := ``
  112. if companyType == "ficc" {
  113. companyTypeSqlStr = " AND classify_id_first != 40 "
  114. } else if companyType == "权益" {
  115. companyTypeSqlStr = " AND classify_id_first = 40 "
  116. }
  117. oRddp := orm.NewOrmUsingDB("rddp")
  118. sql := `SELECT COUNT(1) AS count FROM report WHERE 1=1 ` + companyTypeSqlStr
  119. if condition != "" {
  120. sql += condition
  121. }
  122. err = oRddp.Raw(sql, pars).QueryRow(&count)
  123. return
  124. }
  125. func GetReportList(condition string, pars []interface{}, companyType string, startSize, pageSize int) (items []*ReportList, err error) {
  126. o := orm.NewOrmUsingDB("rddp")
  127. //产品权限
  128. companyTypeSqlStr := ``
  129. if companyType == "ficc" {
  130. companyTypeSqlStr = " AND classify_id_first != 40 "
  131. } else if companyType == "权益" {
  132. companyTypeSqlStr = " AND classify_id_first = 40 "
  133. }
  134. sql := `SELECT *,
  135. (SELECT COUNT(1) FROM report_view_record AS rvr WHERE rvr.report_id=report.id) AS pv,
  136. (SELECT COUNT(DISTINCT user_id) FROM report_view_record AS rvr WHERE rvr.report_id=report.id) AS uv
  137. FROM report WHERE 1=1 ` + companyTypeSqlStr
  138. if condition != "" {
  139. sql += condition
  140. }
  141. // 排序:1:未发布;2:已发布;3-待提交;4-待审批;5-已驳回;6-已通过
  142. sql += `ORDER BY FIELD(state,3,1,4,5,6,2), modify_time DESC LIMIT ?,?`
  143. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  144. return
  145. }
  146. // PublishReport 发布报告
  147. func PublishReport(reportIds []int) (err error) {
  148. if len(reportIds) == 0 {
  149. return
  150. }
  151. o := orm.NewOrmUsingDB("rddp")
  152. sql := `UPDATE report SET state=2,publish_time=now(),modify_time=NOW() WHERE id IN (` + utils.GetOrmInReplace(len(reportIds)) + `)`
  153. _, err = o.Raw(sql).Exec()
  154. return
  155. }
  156. // PublishCancelReport 取消发布报告
  157. func PublishCancelReport(reportId, state int, publishTimeNullFlag bool) (err error) {
  158. o := orm.NewOrmUsingDB("rddp")
  159. var sql string
  160. if publishTimeNullFlag {
  161. sql = ` UPDATE report SET state=?, publish_time=null, pre_publish_time=null, pre_msg_send=0 WHERE id =?`
  162. } else {
  163. sql = ` UPDATE report SET state=?, pre_publish_time=null, pre_msg_send=0 WHERE id =?`
  164. }
  165. _, err = o.Raw(sql, state, reportId).Exec()
  166. return
  167. }
  168. // 取消发布报告
  169. func PublishCancleReport(reportIds int, publishTimeNullFlag bool) (err error) {
  170. o := orm.NewOrmUsingDB("rddp")
  171. var sql string
  172. if publishTimeNullFlag {
  173. sql = ` UPDATE report SET state=1, publish_time=null, pre_publish_time=null, pre_msg_send=0 WHERE id =?`
  174. } else {
  175. sql = ` UPDATE report SET state=1, pre_publish_time=null, pre_msg_send=0 WHERE id =?`
  176. }
  177. _, err = o.Raw(sql, reportIds).Exec()
  178. return
  179. }
  180. // 删除报告
  181. func DeleteReport(reportIds int) (err error) {
  182. o := orm.NewOrmUsingDB("rddp")
  183. sql := ` DELETE FROM report WHERE id =? `
  184. _, err = o.Raw(sql, reportIds).Exec()
  185. return
  186. }
  187. type ReportDetail struct {
  188. Id int `orm:"column(id)" description:"报告Id"`
  189. AddType int `description:"新增方式:1:新增报告,2:继承报告"`
  190. ClassifyIdFirst int `description:"一级分类id"`
  191. ClassifyNameFirst string `description:"一级分类名称"`
  192. ClassifyIdSecond int `description:"二级分类id"`
  193. ClassifyNameSecond string `description:"二级分类名称"`
  194. Title string `description:"标题"`
  195. Abstract string `description:"摘要"`
  196. Author string `description:"作者"`
  197. Frequency string `description:"频度"`
  198. CreateTime string `description:"创建时间"`
  199. ModifyTime string `description:"修改时间"`
  200. State int `description:"1:未发布,2:已发布"`
  201. PublishTime string `description:"发布时间"`
  202. PrePublishTime string `description:"预发布时间"`
  203. Stage int `description:"期数"`
  204. MsgIsSend int `description:"消息是否已发送,0:否,1:是"`
  205. PreMsgSend int `description:"定时发布成功后是否立即推送模版消息:0否,1是"`
  206. Content string `description:"内容"`
  207. VideoUrl string `description:"音频文件URL"`
  208. VideoName string `description:"音频文件名称"`
  209. VideoPlaySeconds string `description:"音频播放时长"`
  210. ContentSub string `description:"内容前两个章节"`
  211. ThsMsgIsSend int `description:"客户群消息是否已发送,0:否,1:是"`
  212. HasChapter int `description:"是否有章节 0-否 1-是"`
  213. ChapterType string `description:"章节类型 day-晨报 week-周报"`
  214. ReportCode string `description:"报告code"`
  215. }
  216. func GetReportById(reportId int) (item *ReportDetail, err error) {
  217. o := orm.NewOrmUsingDB("rddp")
  218. sql := `SELECT * FROM report WHERE id=?`
  219. err = o.Raw(sql, reportId).QueryRow(&item)
  220. return
  221. }
  222. func GetReportByIds(reportIds string) (list []*ReportDetail, err error) {
  223. o := orm.NewOrmUsingDB("rddp")
  224. sql := `SELECT * FROM report WHERE id in ` + reportIds
  225. _, err = o.Raw(sql).QueryRows(&list)
  226. return
  227. }
  228. // GetSimpleReportByIds 根据报告ID查询报告基本信息
  229. func GetSimpleReportByIds(reportIds []int) (list []*ReportDetail, err error) {
  230. if len(reportIds) == 0 {
  231. return
  232. }
  233. o := orm.NewOrmUsingDB("rddp")
  234. sql := `SELECT id, title FROM report WHERE id IN (` + utils.GetOrmInReplace(len(reportIds)) + `)`
  235. _, err = o.Raw(sql, reportIds).QueryRows(&list)
  236. return
  237. }
  238. func GetReportStage(classifyIdFirst, classifyIdSecond int) (count int, err error) {
  239. o := orm.NewOrmUsingDB("rddp")
  240. sql := ``
  241. if classifyIdSecond > 0 {
  242. sql = "SELECT MAX(stage) AS max_stage FROM report WHERE classify_id_second=? "
  243. o.Raw(sql, classifyIdSecond).QueryRow(&count)
  244. } else {
  245. sql = "SELECT MAX(stage) AS max_stage FROM report WHERE classify_id_first=? "
  246. o.Raw(sql, classifyIdFirst).QueryRow(&count)
  247. }
  248. return
  249. }
  250. func GetReportStageEdit(classifyIdFirst, classifyIdSecond, reportId int) (count int, err error) {
  251. o := orm.NewOrmUsingDB("rddp")
  252. sql := ``
  253. if classifyIdSecond > 0 {
  254. sql = "SELECT MAX(stage) AS max_stage FROM report WHERE classify_id_second=? AND id<>? "
  255. o.Raw(sql, classifyIdSecond, reportId).QueryRow(&count)
  256. } else {
  257. sql = "SELECT MAX(stage) AS max_stage FROM report WHERE classify_id_first=? AND id<>? "
  258. o.Raw(sql, classifyIdFirst, reportId).QueryRow(&count)
  259. }
  260. return
  261. }
  262. type PublishReq struct {
  263. ReportIds string `description:"报告id,多个用英文逗号隔开"`
  264. ReportUrl string `description:"报告Url"`
  265. }
  266. type PublishCancelReq struct {
  267. ReportIds int `description:"报告id"`
  268. }
  269. type DeleteReq struct {
  270. ReportIds int `description:"报告id"`
  271. }
  272. type AddReq struct {
  273. AddType int `description:"新增方式:1:新增报告,2:继承报告"`
  274. ClassifyIdFirst int `description:"一级分类id"`
  275. ClassifyNameFirst string `description:"一级分类名称"`
  276. ClassifyIdSecond int `description:"二级分类id"`
  277. ClassifyNameSecond string `description:"二级分类名称"`
  278. Title string `description:"标题"`
  279. Abstract string `description:"摘要"`
  280. Author string `description:"作者"`
  281. Frequency string `description:"频度"`
  282. State int `description:"状态:1:未发布,2:已发布"`
  283. Content string `description:"内容"`
  284. CreateTime string `description:"创建时间"`
  285. ReportVersion int `description:"1:旧版,2:新版"`
  286. }
  287. type PrePublishReq struct {
  288. ReportId int `description:"报告id"`
  289. PrePublishTime string `description:"预发布时间"`
  290. PreMsgSend int `description:"定时发布成功后是否立即推送模版消息:0否,1是"`
  291. ReportUrl string `description:"报告Url"`
  292. }
  293. type AddResp struct {
  294. ReportId int64 `description:"报告id"`
  295. ReportCode string `description:"报告code"`
  296. }
  297. func AddReport(item *Report) (lastId int64, err error) {
  298. o := orm.NewOrmUsingDB("rddp")
  299. lastId, err = o.Insert(item)
  300. return
  301. }
  302. type EditReq struct {
  303. ReportId int64 `description:"报告id"`
  304. ClassifyIdFirst int `description:"一级分类id"`
  305. ClassifyNameFirst string `description:"一级分类名称"`
  306. ClassifyIdSecond int `description:"二级分类id"`
  307. ClassifyNameSecond string `description:"二级分类名称"`
  308. Title string `description:"标题"`
  309. Abstract string `description:"摘要"`
  310. Author string `description:"作者"`
  311. Frequency string `description:"频度"`
  312. State int `description:"状态:1:未发布,2:已发布"`
  313. Content string `description:"内容"`
  314. CreateTime string `description:"创建时间"`
  315. }
  316. type EditResp struct {
  317. ReportId int64 `description:"报告id"`
  318. ReportCode string `description:"报告code"`
  319. }
  320. func EditReport(item *Report, reportId int64) (err error) {
  321. o := orm.NewOrmUsingDB("rddp")
  322. sql := `UPDATE report
  323. SET
  324. classify_id_first =?,
  325. classify_name_first = ?,
  326. classify_id_second = ?,
  327. classify_name_second = ?,
  328. title = ?,
  329. abstract = ?,
  330. author = ?,
  331. frequency = ?,
  332. state = ?,
  333. content = ?,
  334. content_sub = ?,
  335. stage =?,
  336. create_time = ?,
  337. modify_time = ?
  338. WHERE id = ? `
  339. _, err = o.Raw(sql, item.ClassifyIdFirst, item.ClassifyNameFirst, item.ClassifyIdSecond, item.ClassifyNameSecond, item.Title,
  340. item.Abstract, item.Author, item.Frequency, item.State, item.Content, item.ContentSub, item.Stage, item.CreateTime, time.Now(), reportId).Exec()
  341. return
  342. }
  343. type ReportDetailReq struct {
  344. ReportId int `description:"报告id"`
  345. }
  346. type ClassifyIdDetailReq struct {
  347. ClassifyIdFirst int `description:"报告一级分类id"`
  348. ClassifyIdSecond int `description:"报告二级分类id"`
  349. }
  350. func GetReportDetailByClassifyId(classifyIdFirst, classifyIdSecond int) (item *Report, err error) {
  351. o := orm.NewOrmUsingDB("rddp")
  352. sql := ` SELECT * FROM report WHERE 1=1 `
  353. if classifyIdSecond > 0 {
  354. sql = sql + ` AND classify_id_second=? ORDER BY stage DESC LIMIT 1`
  355. err = o.Raw(sql, classifyIdSecond).QueryRow(&item)
  356. } else {
  357. sql = sql + ` AND classify_id_first=? ORDER BY stage DESC LIMIT 1`
  358. err = o.Raw(sql, classifyIdFirst).QueryRow(&item)
  359. }
  360. return
  361. }
  362. type SendTemplateMsgReq struct {
  363. ReportId int `description:"报告id"`
  364. }
  365. func ModifyReportMsgIsSend(reportId int) (err error) {
  366. o := orm.NewOrmUsingDB("rddp")
  367. report, err := GetReportById(reportId)
  368. if err != nil {
  369. return
  370. }
  371. if report.MsgIsSend == 0 {
  372. sql := `UPDATE report SET msg_is_send = 1, msg_send_time=NOW() WHERE id = ? `
  373. _, err = o.Raw(sql, reportId).Exec()
  374. }
  375. return
  376. }
  377. func ModifyReportVideo(reportId int, videoUrl, videoName, videoSize string, playSeconds float64) (err error) {
  378. o := orm.NewOrmUsingDB("rddp")
  379. sql := `UPDATE report SET video_url=?,video_name=?,video_play_seconds=?,video_size=? WHERE id=? `
  380. _, err = o.Raw(sql, videoUrl, videoName, playSeconds, videoSize, reportId).Exec()
  381. return
  382. }
  383. type ReportItem struct {
  384. Id int `orm:"column(id)" description:"报告Id"`
  385. AddType int `description:"新增方式:1:新增报告,2:继承报告"`
  386. ClassifyIdFirst int `description:"一级分类id"`
  387. ClassifyNameFirst string `description:"一级分类名称"`
  388. ClassifyIdSecond int `description:"二级分类id"`
  389. ClassifyNameSecond string `description:"二级分类名称"`
  390. Title string `description:"标题"`
  391. Abstract string `description:"摘要"`
  392. Author string `description:"作者"`
  393. Frequency string `description:"频度"`
  394. CreateTime time.Time `description:"创建时间"`
  395. ModifyTime time.Time `description:"修改时间"`
  396. State int `description:"1:未发布,2:已发布"`
  397. PublishTime time.Time `description:"发布时间"`
  398. Stage int `description:"期数"`
  399. MsgIsSend int `description:"消息是否已发送,0:否,1:是"`
  400. Content string `description:"内容"`
  401. VideoUrl string `description:"音频文件URL"`
  402. VideoName string `description:"音频文件名称"`
  403. VideoPlaySeconds string `description:"音频播放时长"`
  404. ContentSub string `description:"内容前两个章节"`
  405. }
  406. func GetReportItemById(reportId int) (item *ReportItem, err error) {
  407. o := orm.NewOrmUsingDB("rddp")
  408. sql := `SELECT * FROM report WHERE id=?`
  409. err = o.Raw(sql, reportId).QueryRow(&item)
  410. return
  411. }
  412. type SaveReportContent struct {
  413. Content string `description:"内容"`
  414. ReportId int `description:"报告id"`
  415. NoChange int `description:"内容是否未改变:1:内容未改变"`
  416. }
  417. func EditReportContent(reportId int, content, contentSub string) (err error) {
  418. o := orm.NewOrmUsingDB("rddp")
  419. sql := ` UPDATE report SET content=?,content_sub=?,modify_time=NOW() WHERE id=? `
  420. _, err = o.Raw(sql, content, contentSub, reportId).Exec()
  421. return
  422. }
  423. func AddReportSaveLog(reportId, adminId int, content, contentSub, adminName string) (err error) {
  424. o := orm.NewOrmUsingDB("rddp")
  425. sql := ` INSERT INTO report_save_log(report_id, content,content_sub,admin_id,admin_name) VALUES (?,?,?,?,?) `
  426. _, err = o.Raw(sql, reportId, content, contentSub, adminId, adminName).Exec()
  427. return
  428. }
  429. func MultiAddReportChaptersSaveLog(items []*ReportChapter, adminId int, adminRealName string) (err error) {
  430. o := orm.NewOrmUsingDB("rddp")
  431. p, err := o.Raw(`INSERT INTO report_save_log(report_id, report_chapter_id, content, content_sub, admin_id, admin_name) VALUES (?,?,?,?,?,?)`).Prepare()
  432. if err != nil {
  433. return
  434. }
  435. defer func() {
  436. _ = p.Close()
  437. }()
  438. for _, v := range items {
  439. _, err = p.Exec(v.ReportId, v.ReportChapterId, v.Content, v.ContentSub, adminId, adminRealName)
  440. if err != nil {
  441. return
  442. }
  443. }
  444. return
  445. }
  446. type SaveReportContentResp struct {
  447. ReportId int `description:"报告id"`
  448. }
  449. func ModifyReportCode(reportId int64, reportCode string) (err error) {
  450. o := orm.NewOrmUsingDB("rddp")
  451. sql := `UPDATE report SET report_code=? WHERE id=? `
  452. _, err = o.Raw(sql, reportCode, reportId).Exec()
  453. return
  454. }
  455. func ModifyReportThsMsgIsSend(item *ReportDetail) (err error) {
  456. o := orm.NewOrmUsingDB("rddp")
  457. if item.ThsMsgIsSend == 0 {
  458. sql := `UPDATE report SET ths_msg_is_send = 1 WHERE id = ? `
  459. _, err = o.Raw(sql, item.Id).Exec()
  460. }
  461. return
  462. }
  463. type ThsSendTemplateMsgReq struct {
  464. ReportId []int `description:"报告id"`
  465. }
  466. type PublishDayWeekReportReq struct {
  467. ReportId int `description:"报告ID"`
  468. }
  469. // SaveDayWeekReportReq 新增晨报周报请求体
  470. type SaveDayWeekReportReq struct {
  471. ReportId int `description:"报告ID"`
  472. Title string `description:"标题"`
  473. ReportType string `description:"一级分类ID"`
  474. Author string `description:"作者"`
  475. CreateTime string `description:"创建时间"`
  476. }
  477. // GetDayWeekReportStage 获取晨报周报期数
  478. func GetDayWeekReportStage(classifyIdFirst int, yearStart time.Time) (count int, err error) {
  479. o := orm.NewOrmUsingDB("rddp")
  480. sql := " SELECT MAX(stage) AS max_stage FROM report WHERE classify_id_first = ? AND create_time > ? "
  481. o.Raw(sql, classifyIdFirst, yearStart).QueryRow(&count)
  482. return
  483. }
  484. // AddReportAndChapter 新增报告及章节
  485. func AddReportAndChapter(reportItem *Report, chapterItemList []*ReportChapter) (reportId int64, err error) {
  486. o := orm.NewOrmUsingDB("rddp")
  487. to, err := o.Begin()
  488. if err != nil {
  489. return
  490. }
  491. defer func() {
  492. if err != nil {
  493. _ = to.Rollback()
  494. } else {
  495. _ = to.Commit()
  496. }
  497. }()
  498. if reportId, err = to.Insert(reportItem); err != nil {
  499. return
  500. }
  501. if len(chapterItemList) > 0 {
  502. for _, chapterItem := range chapterItemList {
  503. chapterItem.ReportId = int(reportId)
  504. cpId, tmpErr := to.Insert(chapterItem)
  505. if tmpErr != nil {
  506. return
  507. }
  508. chapterItem.ReportChapterId = int(cpId)
  509. }
  510. }
  511. return
  512. }
  513. // GetReportByReportId 主键获取报告
  514. func GetReportByReportId(reportId int) (item *Report, err error) {
  515. o := orm.NewOrmUsingDB("rddp")
  516. sql := `SELECT * FROM report WHERE id = ?`
  517. err = o.Raw(sql, reportId).QueryRow(&item)
  518. return
  519. }
  520. // GetReportByOldReportId 根据老报告id主键获取报告
  521. func GetReportByOldReportId(reportId int) (item *Report, err error) {
  522. o := orm.NewOrmUsingDB("rddp")
  523. sql := `SELECT * FROM report WHERE old_report_id = ?`
  524. err = o.Raw(sql, reportId).QueryRow(&item)
  525. return
  526. }
  527. // DeleteDayWeekReportAndChapter 删除晨周报及章节
  528. func DeleteDayWeekReportAndChapter(reportId int) (err error) {
  529. o := orm.NewOrmUsingDB("rddp")
  530. to, err := o.Begin()
  531. if err != nil {
  532. return
  533. }
  534. defer func() {
  535. if err != nil {
  536. _ = to.Rollback()
  537. } else {
  538. _ = to.Commit()
  539. }
  540. }()
  541. sql := ` DELETE FROM report WHERE id = ? LIMIT 1 `
  542. if _, err = to.Raw(sql, reportId).Exec(); err != nil {
  543. return
  544. }
  545. sql = ` DELETE FROM report_chapter WHERE report_id = ? `
  546. if _, err = to.Raw(sql, reportId).Exec(); err != nil {
  547. return
  548. }
  549. return
  550. }
  551. // UpdateReport 更新报告
  552. func (reportInfo *Report) UpdateReport(cols []string) (err error) {
  553. o := orm.NewOrmUsingDB("rddp")
  554. _, err = o.Update(reportInfo, cols...)
  555. return
  556. }
  557. // 晨周报详情
  558. type ReportDetailView struct {
  559. *ReportDetail
  560. ChapterList []*ReportChapter
  561. }
  562. func GetUnPublishDayReport(startTime time.Time, endTime time.Time) (item *Report, err error) {
  563. o := orm.NewOrmUsingDB("rddp")
  564. sql := ` SELECT
  565. *
  566. FROM
  567. report AS a
  568. WHERE
  569. a.has_chapter = 1
  570. AND a.chapter_type = "day"
  571. AND a.state = 1
  572. AND (
  573. a.create_time BETWEEN ? AND ?
  574. )
  575. ORDER BY
  576. a.create_time DESC
  577. LIMIT 1 `
  578. err = o.Raw(sql, startTime, endTime).QueryRow(&item)
  579. return
  580. }
  581. type ElasticReportDetail struct {
  582. ReportId int `description:"报告ID"`
  583. ReportChapterId int `description:"报告章节ID"`
  584. Title string `description:"标题"`
  585. Abstract string `description:"摘要"`
  586. BodyContent string `description:"内容"`
  587. PublishTime string `description:"发布时间"`
  588. PublishState int `description:"发布状态 1-未发布 2-已发布"`
  589. Author string `description:"作者"`
  590. ClassifyIdFirst int `description:"一级分类ID"`
  591. ClassifyNameFirst string `description:"一级分类名称"`
  592. ClassifyIdSecond int `description:"二级分类ID"`
  593. ClassifyNameSecond string `description:"二级分类名称"`
  594. Categories string `description:"关联的品种名称(包括品种别名)"`
  595. StageStr string `description:"报告期数"`
  596. }
  597. // GetLastPublishedDayWeekReport 获取上一篇已发布的晨周报
  598. func GetLastPublishDayWeekReport(chapterType string) (item *Report, err error) {
  599. o := orm.NewOrmUsingDB("rddp")
  600. sql := ` SELECT * FROM report WHERE has_chapter = 1 AND chapter_type = ? AND state = 2 ORDER BY publish_time DESC LIMIT 1 `
  601. err = o.Raw(sql, chapterType).QueryRow(&item)
  602. return
  603. }
  604. // GetNewReportExist
  605. func GetNewReportExist(oldReportId int) (item *Report, err error) {
  606. o := orm.NewOrmUsingDB("rddp")
  607. sql := ` SELECT id FROM report WHERE old_report_id = ? LIMIT 1 `
  608. err = o.Raw(sql, oldReportId).QueryRow(&item)
  609. return
  610. }
  611. // PublishReportAndChapter 发布报告及章节
  612. func PublishReportAndChapter(reportInfo *Report, publishIds, unPublishIds []int, isPublishReport bool, cols []string) (err error) {
  613. o := orm.NewOrmUsingDB("rddp")
  614. to, err := o.Begin()
  615. if err != nil {
  616. return
  617. }
  618. defer func() {
  619. if err != nil {
  620. _ = to.Rollback()
  621. } else {
  622. _ = to.Commit()
  623. }
  624. }()
  625. // 更新报告
  626. if isPublishReport {
  627. if _, err = to.Update(reportInfo, cols...); err != nil {
  628. return
  629. }
  630. }
  631. // 发布章节
  632. if len(publishIds) > 0 {
  633. sql := ` UPDATE report_chapter SET publish_state = 2, publish_time = ? WHERE report_id = ? AND report_chapter_id IN (` + utils.GetOrmInReplace(len(publishIds)) + `) `
  634. _, err = to.Raw(sql, reportInfo.PublishTime, reportInfo.Id, publishIds).Exec()
  635. }
  636. if len(unPublishIds) > 0 {
  637. 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)) + `) `
  638. _, err = to.Raw(sql, reportInfo.Id, unPublishIds).Exec()
  639. }
  640. return
  641. }
  642. func GetSyncEmptyVideoReport() (list []*Report, err error) {
  643. o := orm.NewOrmUsingDB("rddp")
  644. sql := ` SELECT id FROM report WHERE old_report_id > 0 AND state = 2 AND chapter_type = "" AND (video_size = "" OR video_play_seconds = "")
  645. UNION ALL
  646. SELECT DISTINCT report_id FROM report_chapter WHERE publish_state = 2 AND (video_size = "" OR video_play_seconds = "") `
  647. _, err = o.Raw(sql).QueryRows(&list)
  648. return
  649. }
  650. // PublishReportById 发布报告
  651. func PublishReportById(reportId int, publishTime time.Time) (err error) {
  652. o := orm.NewOrmUsingDB("rddp")
  653. sql := `UPDATE report SET state = 2, publish_time = ?, pre_publish_time=null, pre_msg_send=0, modify_time = NOW() WHERE id = ? `
  654. _, err = o.Raw(sql, publishTime, reportId).Exec()
  655. return
  656. }
  657. // ResetReportById 重置报告状态
  658. func ResetReportById(reportId, state int) (err error) {
  659. o := orm.NewOrmUsingDB("rddp")
  660. sql := `UPDATE report SET state = ?, pre_publish_time = null, pre_msg_send = 0, modify_time = NOW() WHERE id = ?`
  661. _, err = o.Raw(sql, state, reportId).Exec()
  662. return
  663. }
  664. // GetCommentReportByReportId 查询有留言的报告列表
  665. func GetCommentReportByReportId(condition string, pars []interface{}, startSize, pageSize int) (list []*Report, err error) {
  666. o := orm.NewOrmUsingDB("rddp")
  667. sql := `SELECT
  668. id,
  669. create_time,
  670. title,
  671. classify_name_first,
  672. classify_id_first,
  673. classify_name_second,
  674. classify_id_second,
  675. state,
  676. IF
  677. ( publish_time, publish_time, create_time ) AS publish_time
  678. FROM
  679. report
  680. WHERE
  681. 1=1
  682. `
  683. if condition != "" {
  684. sql += condition
  685. }
  686. sql += ` ORDER BY publish_time DESC , title ASC LIMIT ?,?`
  687. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  688. return
  689. }
  690. // GetCommentReportByReportIdOrderBy 查询有留言的报告列表(指定排序)
  691. func GetCommentReportByReportIdOrderBy(condition string, pars []interface{}, startSize, pageSize int, orderBy string) (list []*Report, err error) {
  692. o := orm.NewOrmUsingDB("rddp")
  693. sql := `SELECT
  694. id,
  695. create_time,
  696. title,
  697. classify_name_first,
  698. classify_id_first,
  699. classify_name_second,
  700. classify_id_second,
  701. state,
  702. IF
  703. ( publish_time, publish_time, create_time ) AS publish_time
  704. FROM
  705. report
  706. WHERE
  707. 1=1
  708. `
  709. if condition != "" {
  710. sql += condition
  711. }
  712. if orderBy == `` {
  713. sql += ` ORDER BY publish_time DESC , title ASC `
  714. } else {
  715. sql += orderBy
  716. }
  717. sql += ` LIMIT ?,? `
  718. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  719. return
  720. }
  721. // GetCommentReportTotalByReportId 查询有留言的报告列表总数
  722. func GetCommentReportTotalByReportId(condition string, pars []interface{}) (total int64, err error) {
  723. o := orm.NewOrmUsingDB("rddp")
  724. sql := `SELECT count(*)
  725. FROM report WHERE 1=1`
  726. if condition != "" {
  727. sql += condition
  728. }
  729. err = o.Raw(sql, pars).QueryRow(&total)
  730. return
  731. }
  732. // 点赞相关的报告列表
  733. type LikeReportItem struct {
  734. ReportId int `description:"报告Id"`
  735. ReportChapterId int `description:"报告章节Id"`
  736. ClassifyIdFirst int `description:"一级分类id"`
  737. ClassifyNameFirst string `description:"一级分类名称"`
  738. ClassifyIdSecond int `description:"二级分类id"`
  739. ClassifyNameSecond string `description:"二级分类名称"`
  740. ReportChapterTypeId int `description:"章节类型"`
  741. ReportChapterTypeName string `description:"品种名称"`
  742. PublishTime time.Time `description:"发布时间" `
  743. Title string `description:"标题"`
  744. }
  745. // GetLikeReportByReportIdReportChapterId 获取有被点赞的报告列表
  746. func GetLikeReportByReportIdReportChapterId(reportIds string, chapterIds string, orderStr string, startSize, pageSize int) (list []*LikeReportItem, err error) {
  747. o := orm.NewOrmUsingDB("rddp")
  748. sql := `( 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. "" as report_chapter_type_name,
  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. type_name as report_chapter_type_name,
  776. publish_time,
  777. title
  778. FROM
  779. report_chapter
  780. WHERE
  781. report_chapter_id in (` + chapterIds + `)
  782. )`
  783. if orderStr != "" {
  784. sql += ` ORDER BY FIELD(CONCAT(report_id, "-",report_chapter_id),` + orderStr + `)`
  785. } else {
  786. sql += ` ORDER BY publish_time DESC, report_id Desc`
  787. }
  788. sql += ` LIMIT ?,?`
  789. _, err = o.Raw(sql, startSize, pageSize).QueryRows(&list)
  790. return
  791. }
  792. // GetLikeReportTotalByReportIdReportChapterId 获取有被点赞的报告列表总数
  793. func GetLikeReportTotalByReportIdReportChapterId(reportIds string, chapterIds string) (total int64, err error) {
  794. o := orm.NewOrmUsingDB("rddp")
  795. sql := `select count(*) from (( SELECT
  796. id AS report_id,
  797. 0 AS report_chapter_id,
  798. classify_id_first,
  799. classify_id_second,
  800. classify_name_first,
  801. classify_name_second,
  802. 0 as report_chapter_type_id,
  803. publish_time,
  804. title
  805. FROM
  806. report
  807. WHERE
  808. classify_name_first != "晨报"
  809. AND classify_name_first != "周报"
  810. AND id in (` + reportIds + `)
  811. )
  812. UNION
  813. ( SELECT
  814. report_id,
  815. report_chapter_id,
  816. classify_id_first,
  817. 0 as classify_id_second,
  818. classify_name_first,
  819. null as classify_name_second,
  820. type_id as report_chapter_type_id,
  821. publish_time,
  822. title
  823. FROM
  824. report_chapter
  825. WHERE
  826. report_chapter_id in (` + chapterIds + `)
  827. )) r`
  828. err = o.Raw(sql).QueryRow(&total)
  829. return
  830. }
  831. // GetPageReportList 分页获取报告列表
  832. func GetPageReportList(condition string, pars []interface{}, startSize, pageSize int) (total int, items []*ReportList, err error) {
  833. o := orm.NewOrmUsingDB("rddp")
  834. sql := `SELECT * FROM report WHERE 1=1 `
  835. sql += condition
  836. sql += ` ORDER BY modify_time DESC`
  837. totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z `
  838. err = o.Raw(totalSql, pars).QueryRow(&total)
  839. if err != nil {
  840. return
  841. }
  842. sql += ` LIMIT ?,? `
  843. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  844. return
  845. }
  846. // SunCodeReq 获取太阳码请求体
  847. type SunCodeReq struct {
  848. CodePage string `json:"CodePage" description:"太阳码page"`
  849. CodeScene string `json:"CodeScene" description:"太阳码scene"`
  850. }
  851. // YbPcSuncode 活动海报表
  852. type YbPcSuncode struct {
  853. SuncodeID uint32 `orm:"column(suncode_id);pk" `
  854. Scene string `gorm:"column:scene;type:varchar(255);not null;default:0" json:"scene"` // 微信scene
  855. SceneMd5 string `gorm:"column:scene_md5;type:varchar(255);not null" json:"sceneMd5"`
  856. CodePage string `gorm:"column:code_page;type:varchar(255);not null;default:''" json:"codePage"` // 路径
  857. SuncodeUrl string `gorm:"column:suncode_url;type:varchar(255);not null;default:''" json:"suncodeUrl"` // 太阳码储存地址
  858. CreateTime time.Time `gorm:"column:create_time;type:timestamp;default:CURRENT_TIMESTAMP" json:"createTime"`
  859. }
  860. // GetYbPcSunCode 获取太阳码
  861. func GetYbPcSunCode(scene, page string) (item *YbPcSuncode, err error) {
  862. o := orm.NewOrmUsingDB("weekly")
  863. sql := `SELECT * FROM yb_pc_suncode WHERE scene = ? AND code_page = ? `
  864. err = o.Raw(sql, scene, page).QueryRow(&item)
  865. return
  866. }
  867. func AddYbPcSunCode(item *YbPcSuncode) (err error) {
  868. o := orm.NewOrmUsingDB("weekly")
  869. _, err = o.Insert(item)
  870. return
  871. }
  872. // YbSuncodePars 小程序太阳码scene参数
  873. type YbSuncodePars struct {
  874. ID uint32 `orm:"column(id);pk" `
  875. Scene string `gorm:"column:scene;type:varchar(255);not null;default:''" json:"scene"` // scene参数
  876. SceneKey string `gorm:"column:scene_key;type:varchar(32);not null;default:''" json:"scene_key"` // MD5值
  877. CreateTime time.Time `gorm:"column:create_time;type:datetime;default:CURRENT_TIMESTAMP" json:"createTime"`
  878. }
  879. func AddYbSuncodePars(item *YbSuncodePars) (err error) {
  880. o := orm.NewOrmUsingDB("weekly")
  881. _, err = o.Insert(item)
  882. return
  883. }
  884. // UpdateReportSecondClassifyNameByClassifyId 更新报告分类名称字段
  885. func UpdateReportSecondClassifyNameByClassifyId(classifyId int, classifyName string) (err error) {
  886. o := orm.NewOrmUsingDB("rddp")
  887. sql := " UPDATE report SET classify_name_second = ? WHERE classify_id_second = ? "
  888. _, err = o.Raw(sql, classifyName, classifyId).Exec()
  889. return
  890. }
  891. // UpdateReportFirstClassifyNameByClassifyId 更新报告分类一级名称字段
  892. func UpdateReportFirstClassifyNameByClassifyId(classifyId int, classifyName string) (err error) {
  893. o := orm.NewOrmUsingDB("rddp")
  894. sql := " UPDATE report SET classify_name_first = ? WHERE classify_id_first = ? "
  895. _, err = o.Raw(sql, classifyName, classifyId).Exec()
  896. return
  897. }
  898. // UpdateReportSecondClassifyFirstNameByClassifyId 更新报告二级分类的一级分类名称和id
  899. func UpdateReportSecondClassifyFirstNameByClassifyId(classifyId, newClassifyId int, classifyName string) (err error) {
  900. o := orm.NewOrmUsingDB("rddp")
  901. sql := " UPDATE report SET classify_name_first = ?, classify_id_first = ? WHERE classify_id_second = ? "
  902. _, err = o.Raw(sql, classifyName, newClassifyId, classifyId).Exec()
  903. return
  904. }
  905. // GetEmptyContentSubPPTReport 获取前两章为空的PPT报告
  906. func GetEmptyContentSubPPTReport() (list []*Report, err error) {
  907. sql := `SELECT
  908. r.id,
  909. r.content,
  910. r.content_sub
  911. FROM
  912. report AS r
  913. JOIN ppt_v2 AS p ON r.id = p.report_id
  914. WHERE
  915. p.report_id > 0 AND r.content_sub = ""`
  916. _, err = orm.NewOrmUsingDB("rddp").Raw(sql).QueryRows(&list)
  917. return
  918. }
  919. // ModifyReportAuthor 更改报告作者
  920. func ModifyReportAuthor(condition string, pars []interface{}, authorName string) (count int, err error) {
  921. //产品权限
  922. oRddp := orm.NewOrmUsingDB("rddp")
  923. sql := `UPDATE english_report set author = ? WHERE 1=1 `
  924. if condition != "" {
  925. sql += condition
  926. }
  927. err = oRddp.Raw(sql, authorName, pars).QueryRow(&count)
  928. return
  929. }
  930. func UpdateReportPublishTime(reportId int, videoNameDate string) (err error) {
  931. o := orm.NewOrmUsingDB("rddp")
  932. sql1 := ` UPDATE report SET publish_time = NOW() WHERE id = ? `
  933. _, err = o.Raw(sql1, reportId).Exec()
  934. if err != nil {
  935. return
  936. }
  937. //修改音频标题
  938. sql2 := ` UPDATE report SET video_name=CONCAT(SUBSTRING_INDEX(video_name,"(",1),"` + videoNameDate + `") WHERE id = ? and (video_name !="" and video_name is not null)`
  939. _, err = o.Raw(sql2, reportId).Exec()
  940. return
  941. }
  942. func UpdateReportChapterPublishTime(reportId int, videoNameDate string) (err error) {
  943. o := orm.NewOrmUsingDB("rddp")
  944. sql1 := ` UPDATE report_chapter SET publish_time = NOW() WHERE report_id = ? `
  945. _, err = o.Raw(sql1, reportId).Exec()
  946. if err != nil {
  947. return
  948. }
  949. //修改音频标题
  950. 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)`
  951. _, err = o.Raw(sql2, reportId).Exec()
  952. return
  953. }
  954. // MarkEditReport 标记编辑英文研报的请求数据
  955. type MarkEditReport struct {
  956. ReportId int `description:"研报id"`
  957. Status int `description:"标记状态,1:编辑中,2:编辑完成"`
  958. }
  959. type MarkReportResp struct {
  960. Status int `description:"状态:0:无人编辑, 1:当前有人在编辑"`
  961. Msg string `description:"提示信息"`
  962. Editor string `description:"编辑者姓名"`
  963. }
  964. type MarkReportItem struct {
  965. AdminId int `description:"编辑者ID"`
  966. Editor string `description:"编辑者姓名"`
  967. ReportClassifyNameFirst string
  968. }
  969. // GetReportByCondition 获取报告
  970. func GetReportByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, isPage bool, startSize, pageSize int) (items []*Report, err error) {
  971. o := orm.NewOrmUsingDB("rddp")
  972. fields := `*`
  973. if len(fieldArr) > 0 {
  974. fields = strings.Join(fieldArr, ",")
  975. }
  976. sql := `SELECT ` + fields + ` FROM report WHERE 1=1 `
  977. sql += condition
  978. order := ` ORDER BY modify_time DESC`
  979. if orderRule != `` {
  980. order = orderRule
  981. }
  982. sql += order
  983. if isPage {
  984. sql += ` LIMIT ?,?`
  985. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  986. } else {
  987. _, err = o.Raw(sql, pars).QueryRows(&items)
  988. }
  989. return
  990. }
  991. // ModifyReportMsgIsSendV2 更新报告消息状态
  992. func ModifyReportMsgIsSendV2(reportId int) (err error) {
  993. o := orm.NewOrmUsingDB("rddp")
  994. sql := `UPDATE report SET msg_is_send = 1, ths_msg_is_send = 1, msg_send_time = NOW() WHERE id = ? LIMIT 1`
  995. _, err = o.Raw(sql, reportId).Exec()
  996. return
  997. }
  998. // SetPrePublishReportById 设置定时发布
  999. func SetPrePublishReportById(reportId int, prePublishTime string, preMsgSend int) (err error) {
  1000. o := orm.NewOrmUsingDB("rddp")
  1001. sql := `UPDATE report SET pre_publish_time=?, pre_msg_send=? WHERE id = ? and state = 1 `
  1002. _, err = o.Raw(sql, prePublishTime, preMsgSend, reportId).Exec()
  1003. return
  1004. }
  1005. // ReportSubmitApproveReq 提交审批请求体
  1006. type ReportSubmitApproveReq struct {
  1007. ReportId int `description:"报告ID"`
  1008. }
  1009. // ReportCancelApproveReq 撤回审批请求体
  1010. type ReportCancelApproveReq struct {
  1011. ReportId int `description:"报告ID"`
  1012. }
  1013. func (m *Report) GetItemById(id int) (item *Report, err error) {
  1014. o := orm.NewOrmUsingDB("rddp")
  1015. sql := `SELECT * FROM report WHERE id = ? LIMIT 1`
  1016. err = o.Raw(sql, id).QueryRow(&item)
  1017. return
  1018. }
  1019. // GetReportStateCount 获取指定状态的报告数量
  1020. func GetReportStateCount(state int) (count int, err error) {
  1021. o := orm.NewOrmUsingDB("rddp")
  1022. sql := `SELECT COUNT(1) AS count FROM report WHERE state = ?`
  1023. err = o.Raw(sql, state).QueryRow(&count)
  1024. return
  1025. }
  1026. // UpdateReportsStateByCond 批量更新报告状态
  1027. func UpdateReportsStateByCond(classifyFirstId, classifySecondId, oldState, newState int) (err error) {
  1028. o := orm.NewOrmUsingDB("rddp")
  1029. cond := ``
  1030. if classifyFirstId > 0 {
  1031. cond += fmt.Sprintf(` AND classify_id_first = %d`, classifyFirstId)
  1032. }
  1033. if classifySecondId > 0 {
  1034. cond += fmt.Sprintf(` AND classify_id_second = %d`, classifySecondId)
  1035. }
  1036. sql := fmt.Sprintf(`UPDATE report SET state = ?, pre_publish_time = NULL WHERE state = ? %s`, cond)
  1037. _, err = o.Raw(sql, newState, oldState).Exec()
  1038. return
  1039. }
  1040. // UpdateReportsStateBySecondIds 批量更新二级分类报告状态
  1041. func UpdateReportsStateBySecondIds(oldState, newState int, secondIds []int) (err error) {
  1042. if len(secondIds) <= 0 {
  1043. return
  1044. }
  1045. o := orm.NewOrmUsingDB("rddp")
  1046. // (有审批流的)未发布->待提交
  1047. sql := fmt.Sprintf(`UPDATE report SET state = ?, pre_publish_time = NULL WHERE state = ? AND classify_id_second IN (%s)`, utils.GetOrmInReplace(len(secondIds)))
  1048. _, err = o.Raw(sql, newState, oldState, secondIds).Exec()
  1049. if err != nil {
  1050. return
  1051. }
  1052. // (无审批流的)待提交->未发布
  1053. sql = fmt.Sprintf(`UPDATE report SET state = ?, pre_publish_time = NULL WHERE state = ? AND classify_id_second NOT IN (%s)`, utils.GetOrmInReplace(len(secondIds)))
  1054. _, err = o.Raw(sql, oldState, newState, secondIds).Exec()
  1055. return
  1056. }
  1057. func ModifyReportPdfUrl(reportId int, detailPdfUrl string) (err error) {
  1058. o := orm.NewOrmUsingDB("rddp")
  1059. sql := `UPDATE report SET detail_pdf_url=? WHERE id=? `
  1060. _, err = o.Raw(sql, detailPdfUrl, reportId).Exec()
  1061. return
  1062. }
  1063. func ModifyReportImgUrl(reportId int, detailImgUrl string) (err error) {
  1064. o := orm.NewOrmUsingDB("rddp")
  1065. sql := `UPDATE report SET detail_img_url=? WHERE id=? `
  1066. _, err = o.Raw(sql, detailImgUrl, reportId).Exec()
  1067. return
  1068. }
  1069. // UpdatePdfUrlReportById 清空pdf相关字段
  1070. func UpdatePdfUrlReportById(reportId int) (err error) {
  1071. o := orm.NewOrmUsingDB("rddp")
  1072. sql := `UPDATE report SET detail_img_url = '',detail_pdf_url='',modify_time=NOW() WHERE id = ? `
  1073. _, err = o.Raw(sql, reportId).Exec()
  1074. return
  1075. }
  1076. func GetReportListByCollectCount(classifyIdFirst, classifyIdSecond, classifyIdThird []string, keyword, author string, state int) (count int, err error) {
  1077. o := orm.NewOrmUsingDB("rddp")
  1078. var params []interface{}
  1079. // SQL 主体
  1080. sql := `
  1081. SELECT COUNT(DISTINCT t.id) AS totalCount
  1082. FROM (
  1083. SELECT b.id
  1084. FROM report AS b
  1085. LEFT JOIN user_collect_classify AS a ON a.classify_id = b.classify_id_first
  1086. WHERE 1 = 1
  1087. `
  1088. // 处理 classifyIdFirst
  1089. if len(classifyIdFirst) > 0 {
  1090. sql += " AND a.classify_id IN ( " + utils.GetOrmInReplace(len(classifyIdFirst)) + " ) "
  1091. for _, id := range classifyIdFirst {
  1092. params = append(params, id)
  1093. }
  1094. }
  1095. // 处理关键词
  1096. if keyword != "" {
  1097. sql += " AND (b.title LIKE ? OR b.admin_real_name LIKE ?)"
  1098. params = append(params, "%"+keyword+"%", "%"+keyword+"%")
  1099. }
  1100. if author != "" {
  1101. sql += " AND b.author = ? "
  1102. params = append(params, author)
  1103. }
  1104. if state > 0 {
  1105. sql += " AND b.state = ? "
  1106. params = append(params, state)
  1107. }
  1108. // 第二段 SQL
  1109. sql += `
  1110. UNION ALL
  1111. SELECT b.id
  1112. FROM report AS b
  1113. LEFT JOIN user_collect_classify AS a ON a.classify_id = b.classify_id_second
  1114. WHERE 1 = 1
  1115. `
  1116. // 处理 classifyIdSecond
  1117. if len(classifyIdSecond) > 0 {
  1118. sql += " AND a.classify_id IN ( " + utils.GetOrmInReplace(len(classifyIdSecond)) + " ) "
  1119. for _, id := range classifyIdSecond {
  1120. params = append(params, id)
  1121. }
  1122. }
  1123. // 处理关键词
  1124. if keyword != "" {
  1125. sql += " AND (b.title LIKE ? OR b.admin_real_name LIKE ?)"
  1126. params = append(params, "%"+keyword+"%", "%"+keyword+"%")
  1127. }
  1128. if author != "" {
  1129. sql += " AND b.author = ? "
  1130. params = append(params, author)
  1131. }
  1132. if state > 0 {
  1133. sql += " AND b.state = ? "
  1134. params = append(params, state)
  1135. }
  1136. // 第三段 SQL
  1137. sql += `
  1138. UNION ALL
  1139. SELECT b.id
  1140. FROM report AS b
  1141. LEFT JOIN user_collect_classify AS a ON a.classify_id = b.classify_id_third
  1142. WHERE 1 = 1
  1143. `
  1144. // 处理 classifyIdThird
  1145. if len(classifyIdThird) > 0 {
  1146. sql += " AND a.classify_id IN ( " + utils.GetOrmInReplace(len(classifyIdThird)) + " ) "
  1147. for _, id := range classifyIdThird {
  1148. params = append(params, id)
  1149. }
  1150. }
  1151. // 处理关键词
  1152. if keyword != "" {
  1153. sql += " AND (b.title LIKE ? OR b.admin_real_name LIKE ?)"
  1154. params = append(params, "%"+keyword+"%", "%"+keyword+"%")
  1155. }
  1156. if author != "" {
  1157. sql += " AND b.author = ? "
  1158. params = append(params, author)
  1159. }
  1160. if state > 0 {
  1161. sql += " AND b.state = ? "
  1162. params = append(params, state)
  1163. }
  1164. sql += ") AS t"
  1165. // 执行 SQL 查询获取总数
  1166. err = o.Raw(sql, params...).QueryRow(&count)
  1167. return count, err
  1168. }
  1169. func GetReportListByCollectList(classifyIdFirst, classifyIdSecond, classifyIdThird []string, keyword, orderField, orderType string, startSize, pageSize int, author string, state int) (items []*ReportList, err error) {
  1170. o := orm.NewOrmUsingDB("rddp")
  1171. var params []interface{}
  1172. // SQL 主体
  1173. sql := `
  1174. SELECT DISTINCT t.id, t.title, t.author, t.modify_time, t.publish_time
  1175. FROM (
  1176. SELECT b.id, b.title, b.author, b.modify_time, b.publish_time
  1177. FROM report AS b
  1178. LEFT JOIN user_collect_classify AS a ON a.classify_id = b.classify_id_first
  1179. WHERE 1 = 1
  1180. `
  1181. // 处理 classifyIdFirst
  1182. if len(classifyIdFirst) > 0 {
  1183. sql += " AND a.classify_id IN ( " + utils.GetOrmInReplace(len(classifyIdFirst)) + " ) "
  1184. for _, id := range classifyIdFirst {
  1185. params = append(params, id)
  1186. }
  1187. }
  1188. // 处理关键词
  1189. if keyword != "" {
  1190. sql += " AND (b.title LIKE ? OR b.admin_real_name LIKE ?)"
  1191. params = append(params, "%"+keyword+"%", "%"+keyword+"%")
  1192. }
  1193. if author != "" {
  1194. sql += " AND b.author = ? "
  1195. params = append(params, author)
  1196. }
  1197. if state > 0 {
  1198. sql += " AND b.state = ? "
  1199. params = append(params, state)
  1200. }
  1201. // 第二段 SQL
  1202. sql += `
  1203. UNION ALL
  1204. SELECT b.id, b.title, b.author, b.modify_time, b.publish_time
  1205. FROM report AS b
  1206. LEFT JOIN user_collect_classify AS a ON a.classify_id = b.classify_id_second
  1207. WHERE 1 = 1
  1208. `
  1209. // 处理 classifyIdSecond
  1210. if len(classifyIdSecond) > 0 {
  1211. sql += " AND a.classify_id IN ( " + utils.GetOrmInReplace(len(classifyIdSecond)) + " ) "
  1212. for _, id := range classifyIdSecond {
  1213. params = append(params, id)
  1214. }
  1215. }
  1216. // 处理关键词
  1217. if keyword != "" {
  1218. sql += " AND (b.title LIKE ? OR b.admin_real_name LIKE ?)"
  1219. params = append(params, "%"+keyword+"%", "%"+keyword+"%")
  1220. }
  1221. if author != "" {
  1222. sql += " AND b.author = ? "
  1223. params = append(params, author)
  1224. }
  1225. if state > 0 {
  1226. sql += " AND b.state = ? "
  1227. params = append(params, state)
  1228. }
  1229. // 第三段 SQL
  1230. sql += `
  1231. UNION ALL
  1232. SELECT b.id, b.title, b.author, b.modify_time, b.publish_time
  1233. FROM report AS b
  1234. LEFT JOIN user_collect_classify AS a ON a.classify_id = b.classify_id_third
  1235. WHERE 1 = 1
  1236. `
  1237. // 处理 classifyIdThird
  1238. if len(classifyIdThird) > 0 {
  1239. sql += " AND a.classify_id IN ( " + utils.GetOrmInReplace(len(classifyIdThird)) + " ) "
  1240. for _, id := range classifyIdThird {
  1241. params = append(params, id)
  1242. }
  1243. }
  1244. // 处理关键词
  1245. if keyword != "" {
  1246. sql += " AND (b.title LIKE ? OR b.admin_real_name LIKE ?)"
  1247. params = append(params, "%"+keyword+"%", "%"+keyword+"%")
  1248. }
  1249. if author != "" {
  1250. sql += " AND b.author = ? "
  1251. params = append(params, author)
  1252. }
  1253. if state > 0 {
  1254. sql += " AND b.state = ? "
  1255. params = append(params, state)
  1256. }
  1257. sql += ") AS t "
  1258. // 排序处理
  1259. if orderField != "" && orderType != "" {
  1260. sql += " ORDER BY " + orderField + " " + orderType
  1261. } else {
  1262. sql += " ORDER BY t.modify_time DESC, t.publish_time DESC "
  1263. }
  1264. // 分页
  1265. sql += " LIMIT ?, ?"
  1266. params = append(params, (startSize-1)*pageSize, pageSize)
  1267. // 执行 SQL 查询
  1268. _, err = o.Raw(sql, params...).QueryRows(&items)
  1269. return items, err
  1270. }