package controllers import ( "encoding/json" "eta_gn/eta_api/models" "eta_gn/eta_api/models/report" "eta_gn/eta_api/models/smart_report" "eta_gn/eta_api/services" "eta_gn/eta_api/utils" "github.com/rdlucklib/rdluck_tools/paging" "html" "time" ) // ReportHistoryController 报告 type ReportHistoryController struct { BaseAuthController } // List // @Title 获取报告列表接口 // @Description 获取报告列表 // @Param ReportId query int true "报告ID" // @Param ReportChapterId query int true "报告ID" // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Param IsShowMe query bool true "是否只看我的,true、false" // @Success 200 {object} models.ReportListResp // @router /list [get] func (this *ReportHistoryController) List() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") reportId, _ := this.GetInt("ReportId") reportChapterId, _ := this.GetInt("ReportChapterId") isShowMe, _ := this.GetBool("IsShowMe") if reportId <= 0 && reportChapterId <= 0 { br.Msg = "请选择报告" br.ErrMsg = "请选择报告" return } var startSize int if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } startSize = utils.StartIndex(currentIndex, pageSize) var condition string var pars []interface{} if reportId > 0 { condition += ` AND report_id = ? ` pars = append(pars, reportId) } if reportChapterId > 0 { condition += ` AND report_chapter_id = ? ` pars = append(pars, reportChapterId) } if isShowMe { condition += ` AND admin_id = ? ` pars = append(pars, this.SysUser.AdminId) } var err error var total int var list []*models.ReportHistoryListItem historyObj := new(models.ReportHistory) total, err = historyObj.GetPageListCount(condition, pars) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } listTmp, err := historyObj.GetNoContentPageList(condition, pars, startSize, pageSize) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } for _, item := range listTmp { tmp := new(models.ReportHistoryListItem) tmp.Id = item.Id tmp.ReportId = item.ReportId tmp.ReportChapterId = item.ReportChapterId tmp.CreateTime = item.CreateTime.Format(utils.FormatDateTime) tmp.AdminId = item.AdminId tmp.AdminName = item.AdminName tmp.Title = item.Title list = append(list, tmp) } page := paging.GetPaging(currentIndex, pageSize, total) resp := new(models.ReportHistoryListResp) resp.Paging = page resp.List = list br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // Delete // @Title 删除版本 // @Description 删除版本 // @Param Id query int true "版本ID" // @Success 200 {object} models.ReportListResp // @router /del [post] func (this *ReportHistoryController) Delete() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.DeleteReportHistoryReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.Id <= 0 { br.Msg = "请选择报告版本" return } historyObj := new(models.ReportHistory) item, err := historyObj.GetById(req.Id) if err != nil { br.Msg = "该版本已删除" return } err = item.Delete() if err != nil { br.Msg = "删除失败" br.ErrMsg = "删除失败,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "操作成功" } // Detail // @Title 获取报告历史版本详情接口 // @Description 获取报告详情 // @Param request body models.ReportDetailReq true "type json string" // @Success 200 {object} models.Report // @router /detail [get] func (this *ReportHistoryController) Detail() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() id, _ := this.GetInt("Id") if id <= 0 { br.Msg = "请选择报告版本" return } historyObj := new(models.ReportHistory) history, err := historyObj.GetById(id) if err != nil { br.Msg = "该版本已删除" return } reportId := history.ReportId reportChapterId := history.ReportChapterId reportInfo, err := models.GetReportById(reportId) if err != nil { if utils.IsErrNoRow(err) { br.Msg = "报告已被删除" return } br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } chapterList := make([]*models.ReportChapter, 0) if reportInfo.HasChapter == 1 && reportChapterId > 0 { chapter, e := models.GetReportChapterInfoById(reportChapterId) if e != nil { if e.Error() == utils.ErrNoRow() { br.Msg = "章节已删除" return } br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + e.Error() return } chapter.Content = html.UnescapeString(history.Content) chapter.ContentSub = html.UnescapeString(history.ContentSub) chapter.ContentStruct = html.UnescapeString(history.ContentStruct) chapter.LastModifyAdminName = history.AdminName chapter.ContentModifyTime = history.CreateTime chapter.LastModifyAdminId = history.AdminId chapterList = append(chapterList, chapter) } else { reportInfo.Title = history.Title reportInfo.Content = html.UnescapeString(history.Content) reportInfo.ContentSub = html.UnescapeString(history.ContentSub) reportInfo.ContentStruct = html.UnescapeString(history.ContentStruct) reportInfo.CanvasColor = history.CanvasColor reportInfo.LastModifyAdminName = history.AdminName reportInfo.LastModifyAdminId = history.AdminId reportInfo.ContentModifyTime = history.CreateTime.Format(utils.FormatDate) } if history.HeadResourceId > 0 { headResource, err := smart_report.GetResourceItemById(history.HeadResourceId) if err != nil { br.Msg = "操作失败" br.ErrMsg = "获取资源库版头失败, Err: " + err.Error() return } reportInfo.HeadImg = headResource.ImgUrl reportInfo.HeadStyle = headResource.Style } else { reportInfo.HeadImg = "" reportInfo.HeadStyle = "" } if history.EndResourceId > 0 { endResource, err := smart_report.GetResourceItemById(history.EndResourceId) if err != nil { br.Msg = "操作失败" br.ErrMsg = "获取资源库版头失败, Err: " + err.Error() return } reportInfo.EndImg = endResource.ImgUrl reportInfo.EndStyle = endResource.Style } else { reportInfo.EndImg = "" reportInfo.EndStyle = "" } resp := &models.ReportDetailView{ ReportDetail: reportInfo, ChapterList: chapterList, } br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // Revert // @Title 恢复报告内容 // @Description 恢复报告内容 // @Param Id query int true "版本ID" // @Success 200 {object} models.ReportListResp // @router /revert [post] func (this *ReportHistoryController) Revert() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.DeleteReportHistoryReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.Id <= 0 { br.Msg = "请选择报告版本" return } historyObj := new(models.ReportHistory) history, err := historyObj.GetById(req.Id) if err != nil { br.Msg = "该版本已删除" return } // 获取报告详情 reportInfo, err := models.GetReportByReportId(history.ReportId) if err != nil { if utils.IsErrNoRow(err) { br.Msg = "报告已删除" return } br.Msg = "获取报告失败" br.ErrMsg = "获取报告失败,Err:" + err.Error() return } if reportInfo.Id > 0 && reportInfo.State == 2 { br.Msg = "该报告已发布,不允许编辑" br.ErrMsg = "该报告已发布,不允许编辑" br.IsSendEmail = false return } // 恢复章节 if history.ReportChapterId > 0 { // 如果不是创建人,那么就要去查看是否授权 reportChapterInfo, e := models.GetReportChapterInfoById(history.ReportChapterId) if e != nil { if utils.IsErrNoRow(e) { br.Msg = "章节已删除" return } br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + e.Error() return } if reportInfo.AdminId != this.SysUser.AdminId { // 授权用户权限校验 chapterGrantObj := report.ReportChapterGrant{} _, tmpErr := chapterGrantObj.GetGrantByIdAndAdmin(reportChapterInfo.ReportChapterId, this.SysUser.AdminId) if tmpErr != nil { if utils.IsErrNoRow(tmpErr) { br.Msg = "没有权限" br.ErrMsg = "没有权限" br.IsSendEmail = false return } br.Msg = "获取章节id授权用户失败" br.ErrMsg = "获取章节id授权用户失败, Err: " + tmpErr.Error() return } } // 标记更新中 { markStatus, err := services.UpdateReportEditMark(reportChapterInfo.ReportId, reportChapterInfo.ReportChapterId, this.SysUser.AdminId, 1, this.SysUser.RealName, this.Lang) if err != nil { br.Msg = err.Error() return } if markStatus.Status == 1 { br.Msg = markStatus.Msg br.IsSendEmail = false return } } reportChapterInfo.Title = history.Title reportChapterInfo.Content = history.Content reportChapterInfo.ContentSub = history.ContentSub reportChapterInfo.IsEdit = 1 reportChapterInfo.ModifyTime = time.Now() reportChapterInfo.LastModifyAdminId = this.SysUser.AdminId reportChapterInfo.LastModifyAdminName = this.SysUser.RealName reportChapterInfo.ContentModifyTime = time.Now() reportChapterInfo.ContentStruct = history.ContentStruct updateCols := make([]string, 0) updateCols = append(updateCols, "Title", "Content", "ContentSub", "IsEdit", "ModifyTime") updateCols = append(updateCols, "LastModifyAdminId", "LastModifyAdminName", "ContentModifyTime", "ContentStruct") err = reportChapterInfo.Update(updateCols) if err != nil { br.Msg = "操作失败" br.ErrMsg = "操作失败,Err:" + err.Error() return } } else { // 标记更新中 { markStatus, err := services.UpdateReportEditMark(history.ReportId, 0, this.SysUser.AdminId, 1, this.SysUser.RealName, this.Lang) if err != nil { br.Msg = err.Error() return } if markStatus.Status == 1 { br.Msg = markStatus.Msg return } } if history.HeadResourceId > 0 { headResource, err := smart_report.GetResourceItemById(history.HeadResourceId) if err != nil { br.Msg = "操作失败" br.ErrMsg = "获取资源库版头失败, Err: " + err.Error() return } reportInfo.HeadImg = headResource.ImgUrl } else { reportInfo.HeadImg = "" } if history.EndResourceId > 0 { endResource, err := smart_report.GetResourceItemById(history.EndResourceId) if err != nil { br.Msg = "操作失败" br.ErrMsg = "获取资源库版头失败, Err: " + err.Error() return } reportInfo.EndImg = endResource.ImgUrl } else { reportInfo.EndImg = "" } // 恢复报告 // todo 标题是否需要恢复 reportInfo.Title = history.Title reportInfo.Content = history.Content reportInfo.ContentSub = history.ContentSub reportInfo.ContentStruct = history.ContentStruct reportInfo.CanvasColor = history.CanvasColor reportInfo.HeadResourceId = history.HeadResourceId reportInfo.EndResourceId = history.EndResourceId reportInfo.ModifyTime = time.Now() reportInfo.ContentModifyTime = time.Now() updateCols := []string{"Title", "Content", "ContentSub", "ContentStruct", "HeadImg", "EndImg", "CanvasColor", "HeadResourceId", "EndResourceId", "ModifyTime", "ContentModifyTime"} err = reportInfo.UpdateReport(updateCols) if err != nil { br.Msg = "操作失败" br.ErrMsg = "操作失败,Err:" + err.Error() return } } // 报告的最后编辑人 reportInfo.LastModifyAdminId = this.SysUser.AdminId reportInfo.LastModifyAdminName = this.SysUser.RealName reportInfo.ModifyTime = time.Now() err = reportInfo.UpdateReport([]string{"LastModifyAdminId", "LastModifyAdminName", "ModifyTime"}) if err != nil { br.Msg = "操作失败" br.ErrMsg = "操作失败,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "操作成功" }