package controllers import ( "encoding/json" "eta/eta_hub/models" "eta/eta_hub/services" "eta/eta_hub/utils" "fmt" "github.com/rdlucklib/rdluck_tools/paging" "sort" "strings" "time" ) // EnglishReportController 英文研报 type EnglishReportController 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 "状态" // @Success 200 {object} models.EnglishReportListResp // @router /list [get] func (this *EnglishReportController) 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) } admindId, _ := this.GetInt("AdminId") if admindId > 0 { condition += ` AND admin_id = ? ` pars = append(pars, admindId) } classifyId, _ := this.GetInt("ClassifyId") if classifyId > 0 { condition += ` AND (classify_id_first = ? OR classify_id_second = ?) ` pars = append(pars, classifyId, classifyId) } reportOb := new(models.EnglishReport) total, e := reportOb.GetCountByCondition(condition, pars) if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取英文报告列表总数失败, Err: " + e.Error() return } respList := make([]*models.EnglishReportItem, 0) list, e := reportOb.GetPageItemsByCondition(condition, pars, []string{}, "", startSize, pageSize) if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取英文报告列表失败, Err: " + e.Error() return } for _, v := range list { t := models.FormatEnglishReport2Item(v) respList = append(respList, t) } page := paging.GetPaging(currentIndex, pageSize, total) resp := new(models.EnglishReportListResp) resp.Paging = page resp.List = respList br.Ret = 200 br.Msg = "获取成功" br.Data = resp } // Detail // @Title 报告详情 // @Description 报告详情 // @Param ReportId query int true "报告ID" // @Success 200 {object} models.EnglishReportItem // @router /detail [get] func (this *EnglishReportController) 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 } reportOb := new(models.EnglishReport) item, e := reportOb.GetItemById(reportId) if e != nil { if e.Error() == utils.ErrNoRow() { br.Msg = "报告已被删除" return } br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + e.Error() return } detail := models.FormatEnglishReport2Item(item) br.Data = detail br.Ret = 200 br.Msg = "获取成功" } // Approve // @Title 审批 // @Description 审批报告 // @Param request body models.EnglishReportApproveReq true "type json string" // @Success 200 string "操作成功" // @router /approve [post] func (this *EnglishReportController) Approve() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.EnglishReportApproveReq 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("参数有误, EnglishReportId: %d", req.ReportId) return } ob := new(models.EnglishReport) item, e := ob.GetItemById(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.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.Update(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.Update(cols); e != nil { br.Msg = "审批失败" br.ErrMsg = "报告审批通过失败, Err: " + e.Error() return } // 更新报告ES go func() { _ = services.UpdateEnglishReportEs(item.Id, models.ReportStatePublished) }() } // 记录报告变更状态 go func() { recordItem := &models.ReportStateRecord{ ReportId: req.ReportId, ReportType: models.ReportTypeEnglish, State: item.State, CreateTime: time.Now(), } _, _ = models.AddReportStateRecord(recordItem) }() br.Ret = 200 br.Msg = "审批成功" } // ListClassify // @Title 获取分类列表 // @Description 获取分类列表 // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Param KeyWord query string true "检索关键词" // @Param CompanyType query string false "产品类型,枚举值:'ficc','权益';不传默认返回全部" // @Success 200 {object} models.EnglishClassifyListResp // @router /classify/list [get] func (this *EnglishReportController) 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") var startSize int if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } startSize = utils.StartIndex(currentIndex, pageSize) page := paging.GetPaging(currentIndex, pageSize, 0) resp := new(models.EnglishClassifyListResp) // 处理一级分类分页的情况 rootList, err := models.GetEnglishClassifyRootId(startSize, pageSize, keyWord) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } var ids []int var rootIds []int rootMap := make(map[int]struct{}, 0) for _, v := range rootList { rootIds = append(rootIds, v.Id) rootMap[v.Id] = struct{}{} } total, err := models.GetEnglishClassifyListCount(keyWord) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } if total == 0 { resp.List = make([]*models.EnglishClassifyList, 0) resp.Paging = page br.Data = resp br.Ret = 200 br.Msg = "获取成功" return } page = paging.GetPaging(currentIndex, pageSize, total) //获取相关的分类ID idList, err := models.GetEnglishClassifyListByRootId(rootIds, keyWord) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } secondListMap := make(map[int][]*models.EnglishClassifyList) thirdListMap := make(map[int][]*models.EnglishClassifyList) var thirdIds []int var sortChildList models.RSChildClassifyList // 三级分类-品种权限 permissionMap := make(map[int][]int) if len(idList) > 0 { childIdMap := make(map[int]struct{}, 0) for _, v := range idList { if _, ok := childIdMap[v.ParentId]; !ok { ids = append(ids, v.ParentId) childIdMap[v.ParentId] = struct{}{} } if _, ok := childIdMap[v.Id]; !ok { ids = append(ids, v.Id) childIdMap[v.Id] = struct{}{} } } tmpList, err := models.GetEnglishClassifyChildByIds(ids) if err != nil { br.Msg = "获取二级分类失败" br.ErrMsg = "获取二级分类失败,Err:" + err.Error() return } for _, v := range tmpList { if _, ok := rootMap[v.ParentId]; !ok { thirdIds = append(thirdIds, v.Id) } } { classifyIds := thirdIds if len(classifyIds) > 0 { cond := fmt.Sprintf(` AND %s IN (%s)`, models.EnClassifyPermissionColumns.EnClassifyId, utils.GetOrmInReplace(len(classifyIds))) pars := make([]interface{}, 0) pars = append(pars, classifyIds) ob := new(models.EnClassifyPermission) items, e := ob.GetItemsByCondition(cond, pars, []string{}, "") if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取客户权限列表失败, Err: " + e.Error() return } for _, v := range items { if permissionMap[v.EnClassifyId] == nil { permissionMap[v.EnClassifyId] = make([]int, 0) } permissionMap[v.EnClassifyId] = append(permissionMap[v.EnClassifyId], v.EnPermissionId) } } } // 处理三级分类 for _, v := range tmpList { if _, ok := rootMap[v.ParentId]; !ok { if p, ok1 := permissionMap[v.Id]; ok1 { v.EnPermissions = p } thirdListMap[v.ParentId] = append(thirdListMap[v.ParentId], v) } } //处理二级分类 for _, v := range tmpList { if _, ok := rootMap[v.ParentId]; ok { if child, ok1 := thirdListMap[v.Id]; ok1 { sortChildList = child sort.Sort(sortChildList) v.Child = sortChildList } secondListMap[v.ParentId] = append(secondListMap[v.ParentId], v) } } } //处理一级分类 var sortList models.RSClassifyList for _, v := range rootList { if child, ok := secondListMap[v.Id]; ok { sortChildList = child sort.Sort(sortChildList) v.Child = sortChildList } sortList = append(sortList, v) } sort.Sort(sortList) resp.List = sortList resp.Paging = page br.Data = resp br.Ret = 200 br.Msg = "获取成功" }