package controllers import ( "encoding/json" "eta/eta_hub/models" "eta/eta_hub/services" "eta/eta_hub/utils" "fmt" "github.com/rdlucklib/rdluck_tools/paging" "strings" "time" ) // ReportController 报告 type ReportController struct { BaseAuthController } // List // @Title 获取报告列表接口 // @Description 获取报告列表 // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Param Keyword query string false "搜索关键词" // @Param State query int false "状态" // @Param IsPublicPublish query int false "是否公开发布,1:是,2:否" // @Success 200 {object} models.ReportListResp // @router /list [get] func (this *ReportController) List() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") 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{} keyword := this.GetString("Keyword") keyword = strings.TrimSpace(keyword) if keyword != "" { kw := fmt.Sprint("%", keyword, "%") condition += ` AND (title LIKE ? OR author LIKE ? OR admin_real_name LIKE ?) ` pars = append(pars, kw, kw, kw) } state, _ := this.GetInt("State") if state > 0 { condition += ` AND state = ? ` pars = append(pars, state) } isPublicPublish, _ := this.GetInt("IsPublicPublish") if isPublicPublish > 0 { condition += ` AND is_public_publish = ? ` pars = append(pars, isPublicPublish) } adminId, _ := this.GetInt("AdminId") if adminId > 0 { condition += ` AND admin_id = ? ` pars = append(pars, adminId) } classifyId, _ := this.GetInt("ClassifyId") if classifyId > 0 { condition += ` AND (classify_id_first = ? OR classify_id_second = ? OR classify_id_third = ?) ` pars = append(pars, classifyId, classifyId, classifyId) } total, err := models.GetReportListCount(condition, pars, "") if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } list, err := models.GetReportList(condition, pars, startSize, pageSize) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } results := make([]*models.ReportItem, 0) chapterList := make([]*models.ReportChapterItem, 0) for _, v := range list { t := models.FormatReport2Item(v, chapterList) results = append(results, t) } page := paging.GetPaging(currentIndex, pageSize, total) resp := new(models.ReportListResp) resp.Paging = page resp.List = results br.Ret = 200 br.Msg = "获取成功" br.Data = resp } // Detail // @Title 获取报告详情接口 // @Description 获取报告详情 // @Param ReportId query int true "报告ID" // @Success 200 {object} models.ReportItem // @router /detail [get] func (this *ReportController) Detail() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() reportId, err := this.GetInt("ReportId") if err != nil { br.Msg = "获取参数失败!" br.ErrMsg = "获取参数失败,Err:" + err.Error() return } if reportId <= 0 { br.Msg = "参数错误" return } item, err := models.GetReportById(reportId) if err != nil { if err.Error() == utils.ErrNoRow() { br.Msg = "报告已被删除" return } br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } chapterList := make([]*models.ReportChapterItem, 0) if item.HasChapter == 1 { // 获取章节内容 tmpChapterList, err := models.GetChapterListByReportId(item.Id) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取晨/周报章节列表失败, Err: " + err.Error() return } if len(tmpChapterList) > 0 { // 章节类型的字段赋值 for _, item := range tmpChapterList { chapterList = append(chapterList, models.FormatReportChapterItem(item)) } } } if item.HeadResourceId > 0 { headResource, err := models.GetResourceItemById(item.HeadResourceId) if err != nil { br.Msg = "操作失败" br.ErrMsg = "获取资源库版头失败, Err: " + err.Error() return } item.HeadImg = headResource.ImgUrl item.HeadStyle = headResource.Style } if item.EndResourceId > 0 { endResource, err := models.GetResourceItemById(item.EndResourceId) if err != nil { br.Msg = "操作失败" br.ErrMsg = "获取资源库版头失败, Err: " + err.Error() return } item.EndImg = endResource.ImgUrl item.EndStyle = endResource.Style } resp := models.FormatReport2Item(item, chapterList) br.Data = resp br.Ret = 200 br.Msg = "获取成功" } // Approve // @Title 审批 // @Description 报告审批 // @Param request body models.PublishReq true "type json string" // @Success 200 Ret=200 发布成功 // @router /approve [post] func (this *ReportController) Approve() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.ReportApproveReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ReportId <= 0 { br.Msg = "参数错误" br.ErrMsg = fmt.Sprintf("参数有误, ReportId: %d", req.ReportId) return } item, e := models.GetReportById(req.ReportId) if e != nil { if e.Error() == utils.ErrNoRow() { br.Msg = "报告已被删除" return } br.Msg = "操作失败" br.ErrMsg = "获取报告信息失败, Err:" + e.Error() return } if item.State != models.ReportStateWaitApprove { br.Msg = "报告状态有误" br.ErrMsg = fmt.Sprintf("报告状态有误, State: %d", item.State) return } if item.HasChapter != 1 && item.Content == "" { br.Msg = "报告内容为空,请检查内容" return } // 校验审批配置 confMap, e := models.GetBusinessConf() if e != nil { br.Msg = "操作失败" br.ErrMsg = "获取审批配置失败, Err: " + e.Error() return } if confMap[models.BusinessConfIsReportApprove] != "true" { br.Msg = "未开启审批, 请开启后重新操作" return } if confMap[models.BusinessConfReportApproveType] != models.BusinessConfReportApproveTypeOther { br.Msg = "未开启第三方审批, 请开启后重新操作" return } stateMap := map[bool]int{false: models.ReportStateRefused, true: models.ReportStatePass} // 驳回 now := time.Now().Local() if !req.Pass { cols := []string{"State", "ModifyTime", "ApproveTime"} item.State = stateMap[req.Pass] item.ModifyTime = now item.ApproveTime = now if e = item.UpdateReport(cols); e != nil { br.Msg = "审批失败" br.ErrMsg = "报告审批驳回失败, Err: " + e.Error() return } } // 通过审批 if req.Pass { cols := []string{"State", "ModifyTime", "ApproveTime", "PublishTime"} item.State = stateMap[req.Pass] item.ModifyTime = now item.ApproveTime = now item.PublishTime = now if e = item.UpdateReport(cols); e != nil { br.Msg = "审批失败" br.ErrMsg = "报告审批通过失败, Err: " + e.Error() return } // 更新报告Es go func() { _ = services.UpdateReportEs(req.ReportId, models.ReportStatePublished) // 注:状态用已发布的, 兼容以前的代码 }() } // 记录报告变更状态 go func() { recordItem := &models.ReportStateRecord{ ReportId: req.ReportId, ReportType: models.ReportTypeChinese, State: item.State, CreateTime: time.Now(), } _, _ = models.AddReportStateRecord(recordItem) }() br.Ret = 200 br.Msg = "审批成功" } // @Title 获取分类列表 // @Description 获取分类列表 // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Param KeyWord query string true "检索关键词" // @Param CompanyType query string false "产品类型,枚举值:'ficc','权益';不传默认返回全部" // @Param HideDayWeek query int false "是否隐藏晨周报" // @Success 200 {object} models.Classify // @router /classify/list/bak [get] func (this *ReportController) ListClassify() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") keyWord := this.GetString("KeyWord") //companyType := this.GetString("CompanyType") companyType := "" br.Msg = "该接口已启用,请调用V2接口" return var startSize int if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } startSize = utils.StartIndex(currentIndex, pageSize) list, err := models.GetClassifyList(startSize, pageSize, keyWord, companyType) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } total, err := models.GetClassifyListCount(keyWord, companyType) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } parentIds := make([]int, 0) for i := range list { parentIds = append(parentIds, list[i].Id) } parentIdLen := len(parentIds) if parentIdLen == 0 { resp := &models.ClassifyListResp{ List: list, Paging: paging.GetPaging(currentIndex, pageSize, 0), } br.Data = resp br.Ret = 200 br.Msg = "获取成功" return } // 获取一级分类-子目录列表 menuListMap := make(map[int][]*models.ClassifyMenu, 0) var menuCond string var menuPars []interface{} menuCond += ` AND classify_id IN (` + utils.GetOrmInReplace(parentIdLen) + `)` menuPars = append(menuPars, parentIds) parentMenus, e := models.GetClassifyMenuList(menuCond, menuPars) if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取一级分类子目录列表失败" return } for i := range parentMenus { if menuListMap[parentMenus[i].ClassifyId] == nil { menuListMap[parentMenus[i].ClassifyId] = make([]*models.ClassifyMenu, 0) } menuListMap[parentMenus[i].ClassifyId] = append(menuListMap[parentMenus[i].ClassifyId], parentMenus[i]) } // 获取子分类 children, e := models.GetClassifyChildByParentIds(parentIds, keyWord) if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取子分类失败" return } childrenIds := make([]int, 0) for i := range children { childrenIds = append(childrenIds, children[i].Id) } childrenIdsLen := len(childrenIds) // 获取二级分类-子目录关联 relateMap := make(map[int]int, 0) if childrenIdsLen > 0 { var relateCond string var relatePars []interface{} relateCond += ` AND classify_id IN (` + utils.GetOrmInReplace(childrenIdsLen) + `)` relatePars = append(relatePars, childrenIds) relates, e := models.GetClassifyMenuRelationList(relateCond, relatePars) if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取二级分类子目录关联失败, Err: " + e.Error() return } for i := range relates { relateMap[relates[i].ClassifyId] = relates[i].MenuId } } // 二级分类 childrenMap := make(map[int][]*models.ClassifyItem, 0) for i := range children { if childrenMap[children[i].ParentId] == nil { childrenMap[children[i].ParentId] = make([]*models.ClassifyItem, 0) } childrenMap[children[i].ParentId] = append(childrenMap[children[i].ParentId], &models.ClassifyItem{ Classify: *children[i], //ClassifyMenuId: relateMap[children[i].Id], }) } // 一级分类 for i := range list { //list[i].ClassifyMenuList = menuListMap[list[i].Id] list[i].Child = childrenMap[list[i].Id] } page := paging.GetPaging(currentIndex, pageSize, total) resp := new(models.ClassifyListResp) resp.List = list resp.Paging = page br.Data = resp br.Ret = 200 br.Msg = "获取成功" } // ListClassifyV2 // @Title 获取所有分类列表(2024-6-26 13:49:03) // @Description 获取所有分类列表(2024-6-26 13:49:03) // @Param KeyWord query string true "检索关键词" // @Success 200 {object} models.ClassifyListV2Resp // @router /classify/list [get] func (this *ReportController) ListClassifyV2() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() resp := new(models.ClassifyListV2Resp) keyWord := this.GetString("KeyWord") list, err := models.GetClassifyListByKeywordV2(keyWord, -1) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } if keyWord != `` { idMap := make(map[int]bool) currParentClassifyIdList := make([]int, 0) for _, v := range list { idMap[v.Id] = true if v.ParentId > 0 { currParentClassifyIdList = append(currParentClassifyIdList, v.ParentId) } } findList := list list = make([]*models.ClassifyListV2, 0) tmpList, tmpErr := services.GetParentClassifyListByParentIdList(currParentClassifyIdList) if tmpErr != nil { br.Msg = "获取失败" br.ErrMsg = "获取上级分类信息失败,Err:" + err.Error() return } for _, v := range tmpList { if _, ok := idMap[v.Id]; !ok { list = append(list, v) } } list = append(list, findList...) } classifyIdList := make([]int, 0) for i := range list { classifyIdList = append(classifyIdList, list[i].Id) } parentIdLen := len(classifyIdList) if parentIdLen == 0 { resp.List = list br.Data = resp br.Ret = 200 br.Msg = "获取成功" return } // 先将分类列表排序 services.SortClassifyListBySortAndCreateTime(list) // 接着转换结构 list = services.GetClassifyListTreeRecursive(list, 0) resp.List = list br.Data = resp br.Ret = 200 br.Msg = "获取成功" }