package controllers import ( "encoding/json" "eta_gn/eta_api/models" "eta_gn/eta_api/models/report_approve" "eta_gn/eta_api/services" "eta_gn/eta_api/utils" "fmt" ) type ClassifyController struct { BaseAuthController } // @router /add [post] func (this *ClassifyController) Add() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.ClassifyAddReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ClassifyName == "" { br.Msg = "分类名称不可为空" return } err, errMsg, isSentEmail := services.AddReportClassify(req.ClassifyName, req.ParentId, req.ChartPermissionIdList) if err != nil { br.Msg = "添加失败" if errMsg != "" { br.Msg = errMsg } br.ErrMsg = "添加失败,Err:" + err.Error() br.IsSendEmail = isSentEmail return } br.Ret = 200 br.Success = true br.Msg = "新增成功" } // @router /checkDeleteClassify [get] func (this *ClassifyController) CheckDeleteClassify() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() classifyId, err := this.GetInt("ClassifyId") if classifyId <= 0 { br.Msg = "参数错误" return } classify, err := models.GetClassifyById(classifyId) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取信息失败,Err:" + err.Error() return } resp := new(models.CheckDeleteClassifyResp) if classify == nil { br.Msg = "分类不存在" resp.Code = 1 resp.Msg = "分类不存在" br.Data = resp br.Ret = 200 br.Success = true return } if classify.ParentId > 0 { count, err := models.GetReportCountByClassifyId(classifyId) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取信息失败,Err:" + err.Error() return } if count > 0 { resp.Code = 2 resp.Msg = "该分类有关联报告,不允许删除" br.Data = resp br.Ret = 200 br.Msg = "该分类有关联报告,不允许删除" br.Success = true return } } else { subCount, err := models.GetClassifySubCountByClassifyId(classifyId) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取信息失败,Err:" + err.Error() return } if subCount > 0 { resp.Code = 3 resp.Msg = "二级分类有关联报告,不允许删除" br.Data = resp br.Ret = 200 br.Msg = "二级分类有关联报告,不允许删除" br.Success = true return } subTotal, err := models.GetClassifySubCountByParentId(classifyId) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取信息失败,Err:" + err.Error() return } if subTotal > 0 { resp.Code = 4 resp.Msg = "请先删除该分类下关联分类" br.Data = resp br.Ret = 200 br.Msg = "请先删除该分类下关联分类" br.Success = true return } } flowOb := new(report_approve.ReportApproveFlow) flowCond := fmt.Sprintf(` AND %s = ? AND (%s = ? OR %s = ?)`, report_approve.ReportApproveFlowCols.ReportType, report_approve.ReportApproveFlowCols.ClassifyFirstId, report_approve.ReportApproveFlowCols.ClassifySecondId) flowPars := make([]interface{}, 0) flowPars = append(flowPars, report_approve.FlowReportTypeChinese, classifyId, classifyId) flowCount, e := flowOb.GetCountByCondition(flowCond, flowPars) if e != nil { br.Msg = "检测失败" br.ErrMsg = "获取关联审批流失败, Err: " + e.Error() return } if flowCount > 0 { resp.Code = 5 resp.Msg = "该分类关联审批流,不允许删除" br.Data = resp br.Ret = 200 br.Msg = resp.Msg br.Success = true return } resp.Code = 0 resp.Msg = "检测完成,可进行删除操作" br.Ret = 200 br.Data = resp br.Success = true br.Msg = "检测成功" } // @router /delete [post] func (this *ClassifyController) Delete() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.DeleteClassifyReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ClassifyId <= 0 { br.Msg = "参数错误" return } br.Msg = "报告分类不允许删除" br.IsSendEmail = false return item, err := models.GetClassifyById(req.ClassifyId) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取信息失败,Err:" + err.Error() return } if item == nil { br.Msg = "分类不存在" return } err = models.DeleteClassify(req.ClassifyId) if err != nil { br.Msg = "删除失败" br.ErrMsg = "删除失败,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "删除成功" } // @router /edit [post] func (this *ClassifyController) Edit() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.EditClassifyReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ClassifyId <= 0 { br.Msg = "参数错误" return } if req.ClassifyName == "" { br.Msg = "分类名称不可为空" return } err, errMsg, isSentEmail := services.EditReportClassify(req.ClassifyId, req.ClassifyName, req.ChartPermissionIdList) if err != nil { br.Msg = "修改失败" if errMsg != "" { br.Msg = errMsg } br.ErrMsg = "修改失败,Err:" + err.Error() br.IsSendEmail = isSentEmail return } br.Ret = 200 br.Success = true br.Msg = "修改成功" } // @router /parent [get] func (this *ClassifyController) ParentClassify() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() items, err := models.GetAllClassify() if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } classifyIds := make([]int, 0) for i := range items { classifyIds = append(classifyIds, items[i].Id) } classifyLen := len(classifyIds) if classifyLen == 0 { br.Ret = 200 br.Success = true br.Msg = "获取成功" return } menuListMap := make(map[int][]*models.ClassifyMenu, 0) var menuCond string var menuPars []interface{} menuCond += ` AND classify_id IN (` + utils.GetOrmInReplace(classifyLen) + `)` menuPars = append(menuPars, classifyIds) 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]) } resp := make([]*models.ClassifyItem, 0) for i := range items { resp = append(resp, &models.ClassifyItem{ Classify: *items[i], ClassifyMenuList: menuListMap[items[i].Id], }) } resp = services.GetClassifyTreeRecursive(resp, 0) br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" } // @router /findById [get] func (this *ClassifyController) FindByIdClassify() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.FindByIdClassifyReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } items, err := models.FindByIdClassify(req.ClassifyId) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } br.Data = items br.Ret = 200 br.Success = true br.Msg = "获取成功" } // @router /list [get] func (this *ClassifyController) ListClassify() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() keyWord := this.GetString("KeyWord") reqEnabled, _ := this.GetInt("Enabled", -1) enabled := -1 if reqEnabled == 1 { enabled = reqEnabled } list, err := models.GetClassifyListByKeyword(keyWord, enabled) 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.ClassifyList, 0) tmpList, tmpErr := services.GetParentClassifyListByParentIdList(currParentClassifyIdList) if tmpErr != nil { br.Msg = "获取失败" br.ErrMsg = "获取上级分类信息失败,Err:" + tmpErr.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 := &models.ClassifyListResp{ List: list, } br.Data = resp br.Ret = 200 br.Success = true 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, classifyIdList) 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]) } relateMap := make(map[int]int, 0) { var relateCond string var relatePars []interface{} relateCond += ` AND classify_id IN (` + utils.GetOrmInReplace(parentIdLen) + `)` relatePars = append(relatePars, classifyIdList) 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 } } permissionList, _ := models.GetAllPermissionMapping() classifyPermissionMap := make(map[int][]int) if len(permissionList) > 0 { for _, v := range permissionList { classifyPermissionMap[v.ClassifyId] = append(classifyPermissionMap[v.ClassifyId], v.ChartPermissionId) } } for i, v := range list { list[i].ClassifyMenuList = menuListMap[v.Id] list[i].ClassifyMenuId = relateMap[v.Id] if permissionIds, ok := classifyPermissionMap[v.Id]; ok { list[i].ChartPermissionIdList = permissionIds } } services.SortClassifyListBySortAndCreateTime(list) list = services.GetClassifyListTreeRecursive(list, 0) resp := new(models.ClassifyListResp) resp.List = list br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" } // @router /permission_list [get] func (this *ClassifyController) ClassifyPermission() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() classifyId, _ := this.GetInt("ClassifyId") if classifyId <= 0 { br.Msg = "参数错误" return } classifyInfo, err := models.GetClassifyById(classifyId) if err != nil { br.Msg = "获取分类信息失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } list, err := models.GetPermissionByClassifyId(classifyInfo.Id) if err != nil { br.Msg = "获取分类信息失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } br.Data = list br.Ret = 200 br.Success = true br.Msg = "获取成功" } // @router /permission/edit [post] func (this *ClassifyController) EditClassifyPermission() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.EditClassifyPermissionReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ClassifyId <= 0 { br.Msg = "参数错误" return } if len(req.ChartPermissionIdList) <= 0 { br.Msg = "请选择权限" return } classifyInfo, err := models.GetClassifyById(req.ClassifyId) if err != nil { br.Msg = "获取分类信息失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } err = models.EditChartPermissionSearchKeyWordMappingMulti(classifyInfo.ClassifyName, req.ChartPermissionIdList, req.ClassifyId) if err != nil { br.Msg = "修改分类权限失败" br.ErrMsg = "修改分类权限失败,Err:" + err.Error() return } go func() { _ = services.EditClassifyChartPermissionSync(classifyInfo.ClassifyName, req.ClassifyId) }() br.Ret = 200 br.Success = true br.Msg = "修改成功" } // @router /tel_list [get] func (this *ClassifyController) TelListClassify() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() resp := make([]*models.SimpleClassifyList, 0) var condition, orderRule string var pars []interface{} telList, e := models.GetClassifyByCondition(condition, orderRule, pars) if e != nil { return } firstList := make([]*models.SimpleClassifyList, 0) secondList := make([]*models.SimpleClassifyList, 0) secondMap := make(map[int][]*models.SimpleClassifyList, 0) for i := range telList { if telList[i].ParentId == 0 { firstList = append(firstList, telList[i]) } else { secondList = append(secondList, telList[i]) secondMap[telList[i].ParentId] = append(secondMap[telList[i].ParentId], telList[i]) } } for i := range firstList { firstList[i].Child = secondMap[firstList[i].Id] } resp = firstList br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" } // @router /move [post] func (this *ClassifyController) Move() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.ClassifyMoveReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ClassifyId <= 0 { br.Msg = "请选择分类" return } e, msg := services.MoveReportClassify(req) if e != nil { br.Msg = msg br.ErrMsg = "移动分类失败, Err: " + e.Error() return } br.Ret = 200 br.Success = true br.Msg = "操作成功" } // @router /enabled/set [post] func (this *ClassifyController) SetEnabled() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.ClassifySetEnabledReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ClassifyId <= 0 { br.Msg = "请选择分类" return } if req.Enabled != 0 && req.Enabled != 1 { br.Msg = "请选择正确的启用禁用状态" return } item, err := models.GetClassifyById(req.ClassifyId) if err != nil { if utils.IsErrNoRow(err) { br.Msg = "分类不存在" return } br.Msg = "获取信息失败" br.ErrMsg = "获取信息失败,Err:" + err.Error() return } if item == nil { br.Msg = "分类不存在" return } ob := new(models.Classify) err = ob.SetEnabled(req.ClassifyId, req.Enabled) if err != nil { br.Msg = "操作失败" br.ErrMsg = "操作失败,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "操作成功" } // @router /permission/list [get] func (this *ClassifyController) ClassifyPermissionV2() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } classifyId, _ := this.GetInt("ClassifyId") if classifyId <= 0 { br.Msg = "参数错误" return } resp := make([]*models.ChartPermissionItem, 0) permissionIdMap := make(map[int]bool) { classifyInfo, err := models.GetClassifyById(classifyId) if err != nil { br.Msg = "获取分类信息失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } list, err := models.GetPermission(classifyInfo.Id) if err != nil { br.Msg = "获取分类信息失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } for _, v := range list { permissionIdMap[v.ChartPermissionId] = true } } num := len(permissionIdMap) if num <= 0 { br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp return } cond := ` and product_id = ?` pars := make([]interface{}, 0) pars = append(pars, 1) list, e := services.GetChartPermissionList(cond, pars) if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取品种列表失败, Err: " + e.Error() return } childMap := make(map[int][]*models.ChartPermissionItem) tmpResp := make([]*models.ChartPermissionItem, 0) for _, v := range list { t := new(models.ChartPermissionItem) t.PermissionId = v.ChartPermissionId t.PermissionName = v.PermissionName t.ParentId = v.ParentId t.IsPublic = v.IsPublic t.Enabled = v.Enabled t.Sort = v.Sort t.CreateTime = v.CreatedTime.Format(utils.FormatDateTime) t.Child = make([]*models.ChartPermissionItem, 0) if v.ParentId == 0 { tmpResp = append(tmpResp, t) continue } if v.ParentId > 0 { if _, ok := permissionIdMap[v.ChartPermissionId]; ok { if childMap[v.ParentId] == nil { childMap[v.ParentId] = make([]*models.ChartPermissionItem, 0) } childMap[v.ParentId] = append(childMap[v.ParentId], t) } } } for _, r := range tmpResp { childList, ok := childMap[r.PermissionId] if !ok { continue } r.Child = childList resp = append(resp, r) } br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp }