report_history.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/models/report"
  6. "eta_gn/eta_api/models/smart_report"
  7. "eta_gn/eta_api/services"
  8. "eta_gn/eta_api/utils"
  9. "github.com/rdlucklib/rdluck_tools/paging"
  10. "html"
  11. "time"
  12. )
  13. // ReportHistoryController 报告
  14. type ReportHistoryController struct {
  15. BaseAuthController
  16. }
  17. // List
  18. // @Title 获取报告列表接口
  19. // @Description 获取报告列表
  20. // @Param ReportId query int true "报告ID"
  21. // @Param ReportChapterId query int true "报告ID"
  22. // @Param PageSize query int true "每页数据条数"
  23. // @Param CurrentIndex query int true "当前页页码,从1开始"
  24. // @Param IsShowMe query bool true "是否只看我的,true、false"
  25. // @Success 200 {object} models.ReportListResp
  26. // @router /list [get]
  27. func (this *ReportHistoryController) List() {
  28. br := new(models.BaseResponse).Init()
  29. defer func() {
  30. this.Data["json"] = br
  31. this.ServeJSON()
  32. }()
  33. pageSize, _ := this.GetInt("PageSize")
  34. currentIndex, _ := this.GetInt("CurrentIndex")
  35. reportId, _ := this.GetInt("ReportId")
  36. reportChapterId, _ := this.GetInt("ReportChapterId")
  37. isShowMe, _ := this.GetBool("IsShowMe")
  38. if reportId <= 0 && reportChapterId <= 0 {
  39. br.Msg = "请选择报告"
  40. br.ErrMsg = "请选择报告"
  41. return
  42. }
  43. var startSize int
  44. if pageSize <= 0 {
  45. pageSize = utils.PageSize20
  46. }
  47. if currentIndex <= 0 {
  48. currentIndex = 1
  49. }
  50. startSize = utils.StartIndex(currentIndex, pageSize)
  51. var condition string
  52. var pars []interface{}
  53. if reportId > 0 {
  54. condition += ` AND report_id = ? `
  55. pars = append(pars, reportId)
  56. }
  57. if reportChapterId > 0 {
  58. condition += ` AND report_chapter_id = ? `
  59. pars = append(pars, reportChapterId)
  60. }
  61. if isShowMe {
  62. condition += ` AND admin_id = ? `
  63. pars = append(pars, this.SysUser.AdminId)
  64. }
  65. var err error
  66. var total int
  67. var list []*models.ReportHistoryListItem
  68. historyObj := new(models.ReportHistory)
  69. total, err = historyObj.GetPageListCount(condition, pars)
  70. if err != nil {
  71. br.Msg = "获取失败"
  72. br.ErrMsg = "获取失败,Err:" + err.Error()
  73. return
  74. }
  75. listTmp, err := historyObj.GetNoContentPageList(condition, pars, startSize, pageSize)
  76. if err != nil {
  77. br.Msg = "获取失败"
  78. br.ErrMsg = "获取失败,Err:" + err.Error()
  79. return
  80. }
  81. for _, item := range listTmp {
  82. tmp := new(models.ReportHistoryListItem)
  83. tmp.Id = item.Id
  84. tmp.ReportId = item.ReportId
  85. tmp.ReportChapterId = item.ReportChapterId
  86. tmp.CreateTime = item.CreateTime.Format(utils.FormatDateTime)
  87. tmp.AdminId = item.AdminId
  88. tmp.AdminName = item.AdminName
  89. tmp.Title = item.Title
  90. list = append(list, tmp)
  91. }
  92. page := paging.GetPaging(currentIndex, pageSize, total)
  93. resp := new(models.ReportHistoryListResp)
  94. resp.Paging = page
  95. resp.List = list
  96. br.Ret = 200
  97. br.Success = true
  98. br.Msg = "获取成功"
  99. br.Data = resp
  100. }
  101. // Delete
  102. // @Title 删除版本
  103. // @Description 删除版本
  104. // @Param Id query int true "版本ID"
  105. // @Success 200 {object} models.ReportListResp
  106. // @router /del [post]
  107. func (this *ReportHistoryController) Delete() {
  108. br := new(models.BaseResponse).Init()
  109. defer func() {
  110. this.Data["json"] = br
  111. this.ServeJSON()
  112. }()
  113. var req models.DeleteReportHistoryReq
  114. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  115. if err != nil {
  116. br.Msg = "参数解析异常!"
  117. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  118. return
  119. }
  120. if req.Id <= 0 {
  121. br.Msg = "请选择报告版本"
  122. return
  123. }
  124. historyObj := new(models.ReportHistory)
  125. item, err := historyObj.GetById(req.Id)
  126. if err != nil {
  127. br.Msg = "该版本已删除"
  128. return
  129. }
  130. err = item.Delete()
  131. if err != nil {
  132. br.Msg = "删除失败"
  133. br.ErrMsg = "删除失败,Err:" + err.Error()
  134. return
  135. }
  136. br.Ret = 200
  137. br.Success = true
  138. br.Msg = "操作成功"
  139. }
  140. // Detail
  141. // @Title 获取报告历史版本详情接口
  142. // @Description 获取报告详情
  143. // @Param request body models.ReportDetailReq true "type json string"
  144. // @Success 200 {object} models.Report
  145. // @router /detail [get]
  146. func (this *ReportHistoryController) Detail() {
  147. br := new(models.BaseResponse).Init()
  148. defer func() {
  149. this.Data["json"] = br
  150. this.ServeJSON()
  151. }()
  152. id, _ := this.GetInt("Id")
  153. if id <= 0 {
  154. br.Msg = "请选择报告版本"
  155. return
  156. }
  157. historyObj := new(models.ReportHistory)
  158. history, err := historyObj.GetById(id)
  159. if err != nil {
  160. br.Msg = "该版本已删除"
  161. return
  162. }
  163. reportId := history.ReportId
  164. reportChapterId := history.ReportChapterId
  165. reportInfo, err := models.GetReportById(reportId)
  166. if err != nil {
  167. if utils.IsErrNoRow(err) {
  168. br.Msg = "报告已被删除"
  169. return
  170. }
  171. br.Msg = "获取失败"
  172. br.ErrMsg = "获取失败,Err:" + err.Error()
  173. return
  174. }
  175. chapterList := make([]*models.ReportChapter, 0)
  176. if reportInfo.HasChapter == 1 && reportChapterId > 0 {
  177. chapter, e := models.GetReportChapterInfoById(reportChapterId)
  178. if e != nil {
  179. if e.Error() == utils.ErrNoRow() {
  180. br.Msg = "章节已删除"
  181. return
  182. }
  183. br.Msg = "获取失败"
  184. br.ErrMsg = "获取失败,Err:" + e.Error()
  185. return
  186. }
  187. chapter.Content = html.UnescapeString(history.Content)
  188. chapter.ContentSub = html.UnescapeString(history.ContentSub)
  189. chapter.ContentStruct = html.UnescapeString(history.ContentStruct)
  190. chapter.LastModifyAdminName = history.AdminName
  191. chapter.ContentModifyTime = history.CreateTime
  192. chapter.LastModifyAdminId = history.AdminId
  193. chapterList = append(chapterList, chapter)
  194. } else {
  195. reportInfo.Title = history.Title
  196. reportInfo.Content = html.UnescapeString(history.Content)
  197. reportInfo.ContentSub = html.UnescapeString(history.ContentSub)
  198. reportInfo.ContentStruct = html.UnescapeString(history.ContentStruct)
  199. reportInfo.CanvasColor = history.CanvasColor
  200. reportInfo.LastModifyAdminName = history.AdminName
  201. reportInfo.LastModifyAdminId = history.AdminId
  202. reportInfo.ContentModifyTime = history.CreateTime.Format(utils.FormatDate)
  203. }
  204. if history.HeadResourceId > 0 {
  205. headResource, err := smart_report.GetResourceItemById(history.HeadResourceId)
  206. if err != nil {
  207. br.Msg = "操作失败"
  208. br.ErrMsg = "获取资源库版头失败, Err: " + err.Error()
  209. return
  210. }
  211. reportInfo.HeadImg = headResource.ImgUrl
  212. reportInfo.HeadStyle = headResource.Style
  213. } else {
  214. reportInfo.HeadImg = ""
  215. reportInfo.HeadStyle = ""
  216. }
  217. if history.EndResourceId > 0 {
  218. endResource, err := smart_report.GetResourceItemById(history.EndResourceId)
  219. if err != nil {
  220. br.Msg = "操作失败"
  221. br.ErrMsg = "获取资源库版头失败, Err: " + err.Error()
  222. return
  223. }
  224. reportInfo.EndImg = endResource.ImgUrl
  225. reportInfo.EndStyle = endResource.Style
  226. } else {
  227. reportInfo.EndImg = ""
  228. reportInfo.EndStyle = ""
  229. }
  230. resp := &models.ReportDetailView{
  231. ReportDetail: reportInfo,
  232. ChapterList: chapterList,
  233. }
  234. br.Ret = 200
  235. br.Success = true
  236. br.Msg = "获取成功"
  237. br.Data = resp
  238. }
  239. // Revert
  240. // @Title 恢复报告内容
  241. // @Description 恢复报告内容
  242. // @Param Id query int true "版本ID"
  243. // @Success 200 {object} models.ReportListResp
  244. // @router /revert [post]
  245. func (this *ReportHistoryController) Revert() {
  246. br := new(models.BaseResponse).Init()
  247. defer func() {
  248. this.Data["json"] = br
  249. this.ServeJSON()
  250. }()
  251. var req models.DeleteReportHistoryReq
  252. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  253. if err != nil {
  254. br.Msg = "参数解析异常!"
  255. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  256. return
  257. }
  258. if req.Id <= 0 {
  259. br.Msg = "请选择报告版本"
  260. return
  261. }
  262. historyObj := new(models.ReportHistory)
  263. history, err := historyObj.GetById(req.Id)
  264. if err != nil {
  265. br.Msg = "该版本已删除"
  266. return
  267. }
  268. // 获取报告详情
  269. reportInfo, err := models.GetReportByReportId(history.ReportId)
  270. if err != nil {
  271. if utils.IsErrNoRow(err) {
  272. br.Msg = "报告已删除"
  273. return
  274. }
  275. br.Msg = "获取报告失败"
  276. br.ErrMsg = "获取报告失败,Err:" + err.Error()
  277. return
  278. }
  279. if reportInfo.Id > 0 && reportInfo.State == 2 {
  280. br.Msg = "该报告已发布,不允许编辑"
  281. br.ErrMsg = "该报告已发布,不允许编辑"
  282. br.IsSendEmail = false
  283. return
  284. }
  285. // 恢复章节
  286. if history.ReportChapterId > 0 {
  287. // 如果不是创建人,那么就要去查看是否授权
  288. reportChapterInfo, e := models.GetReportChapterInfoById(history.ReportChapterId)
  289. if e != nil {
  290. if utils.IsErrNoRow(e) {
  291. br.Msg = "章节已删除"
  292. return
  293. }
  294. br.Msg = "获取失败"
  295. br.ErrMsg = "获取失败,Err:" + e.Error()
  296. return
  297. }
  298. if reportInfo.AdminId != this.SysUser.AdminId {
  299. // 授权用户权限校验
  300. chapterGrantObj := report.ReportChapterGrant{}
  301. _, tmpErr := chapterGrantObj.GetGrantByIdAndAdmin(reportChapterInfo.ReportChapterId, this.SysUser.AdminId)
  302. if tmpErr != nil {
  303. if utils.IsErrNoRow(tmpErr) {
  304. br.Msg = "没有权限"
  305. br.ErrMsg = "没有权限"
  306. br.IsSendEmail = false
  307. return
  308. }
  309. br.Msg = "获取章节id授权用户失败"
  310. br.ErrMsg = "获取章节id授权用户失败, Err: " + tmpErr.Error()
  311. return
  312. }
  313. }
  314. // 标记更新中
  315. {
  316. markStatus, err := services.UpdateReportEditMark(reportChapterInfo.ReportId, reportChapterInfo.ReportChapterId, this.SysUser.AdminId, 1, this.SysUser.RealName, this.Lang)
  317. if err != nil {
  318. br.Msg = err.Error()
  319. return
  320. }
  321. if markStatus.Status == 1 {
  322. br.Msg = markStatus.Msg
  323. br.IsSendEmail = false
  324. return
  325. }
  326. }
  327. reportChapterInfo.Title = history.Title
  328. reportChapterInfo.Content = history.Content
  329. reportChapterInfo.ContentSub = history.ContentSub
  330. reportChapterInfo.IsEdit = 1
  331. reportChapterInfo.ModifyTime = time.Now()
  332. reportChapterInfo.LastModifyAdminId = this.SysUser.AdminId
  333. reportChapterInfo.LastModifyAdminName = this.SysUser.RealName
  334. reportChapterInfo.ContentModifyTime = time.Now()
  335. reportChapterInfo.ContentStruct = history.ContentStruct
  336. updateCols := make([]string, 0)
  337. updateCols = append(updateCols, "Title", "Content", "ContentSub", "IsEdit", "ModifyTime")
  338. updateCols = append(updateCols, "LastModifyAdminId", "LastModifyAdminName", "ContentModifyTime", "ContentStruct")
  339. err = reportChapterInfo.Update(updateCols)
  340. if err != nil {
  341. br.Msg = "操作失败"
  342. br.ErrMsg = "操作失败,Err:" + err.Error()
  343. return
  344. }
  345. } else {
  346. // 标记更新中
  347. {
  348. markStatus, err := services.UpdateReportEditMark(history.ReportId, 0, this.SysUser.AdminId, 1, this.SysUser.RealName, this.Lang)
  349. if err != nil {
  350. br.Msg = err.Error()
  351. return
  352. }
  353. if markStatus.Status == 1 {
  354. br.Msg = markStatus.Msg
  355. return
  356. }
  357. }
  358. if history.HeadResourceId > 0 {
  359. headResource, err := smart_report.GetResourceItemById(history.HeadResourceId)
  360. if err != nil {
  361. br.Msg = "操作失败"
  362. br.ErrMsg = "获取资源库版头失败, Err: " + err.Error()
  363. return
  364. }
  365. reportInfo.HeadImg = headResource.ImgUrl
  366. } else {
  367. reportInfo.HeadImg = ""
  368. }
  369. if history.EndResourceId > 0 {
  370. endResource, err := smart_report.GetResourceItemById(history.EndResourceId)
  371. if err != nil {
  372. br.Msg = "操作失败"
  373. br.ErrMsg = "获取资源库版头失败, Err: " + err.Error()
  374. return
  375. }
  376. reportInfo.EndImg = endResource.ImgUrl
  377. } else {
  378. reportInfo.EndImg = ""
  379. }
  380. // 恢复报告
  381. // todo 标题是否需要恢复
  382. reportInfo.Title = history.Title
  383. reportInfo.Content = history.Content
  384. reportInfo.ContentSub = history.ContentSub
  385. reportInfo.ContentStruct = history.ContentStruct
  386. reportInfo.CanvasColor = history.CanvasColor
  387. reportInfo.HeadResourceId = history.HeadResourceId
  388. reportInfo.EndResourceId = history.EndResourceId
  389. reportInfo.ModifyTime = time.Now()
  390. reportInfo.ContentModifyTime = time.Now()
  391. updateCols := []string{"Title", "Content", "ContentSub", "ContentStruct", "HeadImg", "EndImg", "CanvasColor", "HeadResourceId", "EndResourceId", "ModifyTime", "ContentModifyTime"}
  392. err = reportInfo.UpdateReport(updateCols)
  393. if err != nil {
  394. br.Msg = "操作失败"
  395. br.ErrMsg = "操作失败,Err:" + err.Error()
  396. return
  397. }
  398. }
  399. // 报告的最后编辑人
  400. reportInfo.LastModifyAdminId = this.SysUser.AdminId
  401. reportInfo.LastModifyAdminName = this.SysUser.RealName
  402. reportInfo.ModifyTime = time.Now()
  403. err = reportInfo.UpdateReport([]string{"LastModifyAdminId", "LastModifyAdminName", "ModifyTime"})
  404. if err != nil {
  405. br.Msg = "操作失败"
  406. br.ErrMsg = "操作失败,Err:" + err.Error()
  407. return
  408. }
  409. br.Ret = 200
  410. br.Success = true
  411. br.Msg = "操作成功"
  412. }