package excel import ( "encoding/json" "eta/eta_api/controllers" "eta/eta_api/models" "eta/eta_api/models/data_manage/excel" "eta/eta_api/models/data_manage/excel/request" response2 "eta/eta_api/models/data_manage/excel/response" "eta/eta_api/services/data/data_manage_permission" excel2 "eta/eta_api/services/data/excel" "eta/eta_api/utils" "fmt" "sort" "strconv" "time" ) // ExcelClassifyController ETA表格分类 type ExcelClassifyController struct { controllers.BaseAuthController } // List // @Title excel表格分类列表 // @Description excel表格分类列表接口 // @Param Source query int true "格来源,1:excel插件的表格,2:自定义表格,3:混合表格,默认:1" // @Param IsShowMe query bool true "是否只看我的,true、false" // @Success 200 {object} response.ExcelClassifyListResp // @router /excel_classify/list [get] func (this *ExcelClassifyController) List() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() source, _ := this.GetInt("Source") if source <= 0 { source = utils.EXCEL_DEFAULT } //只看我的 isShowMe, _ := this.GetBool("IsShowMe") // 自定义分析只看自己的 //if source == utils.CUSTOM_ANALYSIS_TABLE { // br = AnalysisClassifyList(this) // return //} showUserId := 0 if isShowMe { showUserId = this.SysUser.AdminId } classifyList, err := excel.GetExcelClassifyBySource(source) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取数据失败,Err:" + err.Error() return } // 分类map classifyMap := make(map[int]*excel.ExcelClassifyItems) for _, v := range classifyList { classifyMap[v.ExcelClassifyId] = v } // 获取二级分类 // 获取三级分类 // 根据来源获取所有excel表格(无内容) allExcelInfo := make([]*excel.ExcelClassifyItems, 0) if source == utils.BALANCE_TABLE { //找到当前协作人相关的表格ID var excelIds []int if isShowMe { obj := new(excel.ExcelWorker) existList, err := obj.GetBySysUserId(this.SysUser.AdminId) if err != nil { br.Msg = "获取表格协作人失败!" br.ErrMsg = "获取表格协作人失败,Err:" + err.Error() return } if len(existList) > 0 { for _, v := range existList { excelIds = append(excelIds, v.ExcelInfoId) } } } allExcelInfo, err = excel.GetBalanceNoContentExcelInfoAll(source, excelIds, showUserId) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "获取失败" br.ErrMsg = "获取表格信息失败,Err:" + err.Error() return } } else { allExcelInfo, err = excel.GetNoContentExcelInfoAll(source, showUserId) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "获取失败" br.ErrMsg = "获取表格信息失败,Err:" + err.Error() return } } // 获取所有有权限的指标和分类 permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserExcelAndClassifyPermissionList(this.SysUser.AdminId, 0, 0) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error() return } ExcelInfoMap := make(map[int][]*excel.ExcelClassifyItems) for _, v := range allExcelInfo { // 数据权限 if classifyInfo, ok := classifyMap[v.ExcelClassifyId]; ok { v.HaveOperaAuth = data_manage_permission.CheckExcelPermissionByPermissionIdList(v.IsJoinPermission, classifyInfo.IsJoinPermission, v.ExcelInfoId, v.ExcelClassifyId, permissionEdbIdList, permissionClassifyIdList) } if source == utils.BALANCE_TABLE && isShowMe && !v.HaveOperaAuth { // 过滤我不可编辑的表格 continue } ExcelInfoMap[v.ExcelClassifyId] = append(ExcelInfoMap[v.ExcelClassifyId], v) } classifyListMap := make(map[int][]*excel.ExcelClassifyItems) for _, v := range classifyList { // 数据权限 v.HaveOperaAuth = data_manage_permission.CheckExcelClassifyPermissionByPermissionIdList(v.IsJoinPermission, v.ExcelClassifyId, permissionClassifyIdList) if existItems, ok := ExcelInfoMap[v.ExcelClassifyId]; ok { v.Children = existItems } } for _, v := range classifyList { if v.ParentId > 0 { classifyListMap[v.ParentId] = append(classifyListMap[v.ParentId], v) } } //组装三级分类 for key, classify := range classifyList { subList, ok := classifyListMap[classify.ExcelClassifyId] if ok { classifyList[key].Children = append(classifyList[key].Children, subList...) sort.Slice(classifyList[key].Children, func(i, j int) bool { return excel.ExcelClassifyItemBySort(classifyList[key].Children[i], classifyList[key].Children[j]) }) } } nodeAll := make([]*excel.ExcelClassifyItems, 0) for _, v := range classifyList { if v.ParentId == 0 { sort.Slice(v.Children, func(i, j int) bool { return excel.ExcelClassifyItemBySort(v.Children[i], v.Children[j]) }) nodeAll = append(nodeAll, v) } } resp := response2.ExcelClassifyListResp{ AllNodes: nodeAll, } br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } func AnalysisClassifyList(this *ExcelClassifyController) (br *models.BaseResponse) { br = new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() source, _ := this.GetInt("Source") if source <= 0 { source = utils.EXCEL_DEFAULT } //只看我的 showUserId := this.SysUser.AdminId classifyList, err := excel.GetExcelClassifyBySource(source) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取数据失败,Err:" + err.Error() return } // 分类map classifyMap := make(map[int]*excel.ExcelClassifyItems) for _, v := range classifyList { classifyMap[v.ExcelClassifyId] = v } // 获取二级分类 // 获取三级分类 // 根据来源获取所有excel表格(无内容) allExcelInfo, err := excel.GetNoContentExcelInfoAll(source, showUserId) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "获取失败" br.ErrMsg = "获取表格信息失败,Err:" + err.Error() return } // 获取所有有权限的指标和分类 permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserExcelAndClassifyPermissionList(this.SysUser.AdminId, 0, 0) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error() return } ExcelInfoMap := make(map[int][]*excel.ExcelClassifyItems) for _, v := range allExcelInfo { // 数据权限 if classifyInfo, ok := classifyMap[v.ExcelClassifyId]; ok { v.HaveOperaAuth = data_manage_permission.CheckExcelPermissionByPermissionIdList(v.IsJoinPermission, classifyInfo.IsJoinPermission, v.ExcelInfoId, v.ExcelClassifyId, permissionEdbIdList, permissionClassifyIdList) } ExcelInfoMap[v.ExcelClassifyId] = append(ExcelInfoMap[v.ExcelClassifyId], v) } classifyListMap := make(map[int][]*excel.ExcelClassifyItems) for _, v := range classifyList { // 数据权限 v.HaveOperaAuth = data_manage_permission.CheckExcelClassifyPermissionByPermissionIdList(v.IsJoinPermission, v.ExcelClassifyId, permissionClassifyIdList) if existItems, ok := ExcelInfoMap[v.ExcelClassifyId]; ok { v.Children = existItems } } for _, v := range classifyList { if v.ParentId > 0 { classifyListMap[v.ParentId] = append(classifyListMap[v.ParentId], v) } } //组装三级分类 for key, classify := range classifyList { subList, ok := classifyListMap[classify.ExcelClassifyId] if ok { classifyList[key].Children = append(classifyList[key].Children, subList...) sort.Slice(classifyList[key].Children, func(i, j int) bool { return excel.ExcelClassifyItemBySort(classifyList[key].Children[i], classifyList[key].Children[j]) }) } } nodeAll := make([]*excel.ExcelClassifyItems, 0) for _, v := range classifyList { if v.ParentId == 0 { sort.Slice(v.Children, func(i, j int) bool { return excel.ExcelClassifyItemBySort(v.Children[i], v.Children[j]) }) nodeAll = append(nodeAll, v) } } // 自定义分析只看自己的分类 nodeAll = removeNodesWithAnalysisClassify(nodeAll, this.SysUser.AdminId) resp := response2.ExcelClassifyListResp{ AllNodes: nodeAll, } br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp return } // removeNodesWithAnalysisClassify // @Description: 自定义分析只看自己的分类,所以需要移除分类 // @author: Roc // @datetime 2024-04-09 13:46:51 // @param nodes []*excel.ExcelClassifyItems // @param adminId int // @return []*excel.ExcelClassifyItems func removeNodesWithAnalysisClassify(nodes []*excel.ExcelClassifyItems, adminId int) []*excel.ExcelClassifyItems { var newListNode []*excel.ExcelClassifyItems for _, node := range nodes { if node.Children != nil { node.Children = removeNodesWithAnalysisClassify(node.Children, adminId) } // 如果是ETA表格,那么就保留 if node.ExcelInfoId > 0 { newListNode = append(newListNode, node) continue } // 如果是公共分类,且名下的ETA表格不为空,那么就保留 if node.SysUserId == 0 && node.Children != nil && len(node.Children) > 0 { newListNode = append(newListNode, node) continue } // 如果是自己的分类,那么就保留 if node.SysUserId == adminId { newListNode = append(newListNode, node) continue } } return newListNode } // ExcelClassifyItems // @Title 获取所有excel表格分类接口-不包含表格 // @Description 获取所有excel表格分类接口-不包含表格 // @Param Source query int true "格来源,1:excel插件的表格,2:自定义表格,3:混合表格,默认:1" // @Success 200 {object} response.ExcelClassifyListResp // @router /excel_classify/items [get] func (this *ExcelClassifyController) ExcelClassifyItems() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() source, _ := this.GetInt("Source") if source <= 0 { source = utils.EXCEL_DEFAULT } classifyList, err := excel.GetExcelClassifyBySource(source) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取数据失败,Err:" + err.Error() return } // 获取二级分类 // 获取三级分类 classifyMap := make(map[int][]*excel.ExcelClassifyItems) for _, v := range classifyList { if v.ParentId > 0 { classifyMap[v.ParentId] = append(classifyMap[v.ParentId], v) } } //组装三级分类 for key, classify := range classifyList { subList, ok := classifyMap[classify.ExcelClassifyId] if ok { classifyList[key].Children = append(classifyList[key].Children, subList...) } } nodeAll := make([]*excel.ExcelClassifyItems, 0) for _, v := range classifyList { if v.ParentId == 0 { nodeAll = append(nodeAll, v) } } resp := response2.ExcelClassifyListResp{ AllNodes: nodeAll, } br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // AddExcelClassify // @Title 新增excel表格分类 // @Description 新增excel表格分类接口 // @Param request body request.AddExcelClassifyReq true "type json string" // @Success 200 Ret=200 保存成功 // @router /excel_classify/add [post] func (this *ExcelClassifyController) AddExcelClassify() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req request.AddExcelClassifyReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ExcelClassifyName == "" { br.Msg = "请输入分类名称" br.IsSendEmail = false return } if req.ParentId < 0 { br.Msg = "参数错误" br.IsSendEmail = false return } source := req.Source if source <= 0 { source = utils.EXCEL_DEFAULT } // 获取同级分类下存在同名分类的数量 var count int if source == utils.CUSTOM_ANALYSIS_TABLE { // 自定义分析分类私有化了 ct, e := excel.GetCustomAnalysisExcelClassifyCount(req.ExcelClassifyName, req.ParentId, source, this.SysUser.AdminId) if e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("获取自定义分析同级分类下同名分类失败, %v", e) return } count = ct } else { ct, e := excel.GetExcelClassifyCount(req.ExcelClassifyName, req.ParentId, source) if e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("获取同级分类下同名分类失败, %v", e) return } count = ct } if count > 0 { br.Msg = "分类名称已存在,请重新输入" br.IsSendEmail = false return } //获取该层级下最大的排序数 maxSort, err := excel2.GetExcelClassifyMaxSort(req.ParentId, req.Source) if err != nil { br.Msg = "获取失败" br.ErrMsg = "查询排序信息失败,Err:" + err.Error() return } level := 1 // 查询父级分类是否存在 if req.ParentId > 0 { var parent *excel.ExcelClassify parent, err = excel.GetExcelClassifyById(req.ParentId) if err != nil { if err.Error() == utils.ErrNoRow() { br.Msg = "父级分类不存在" return } br.Msg = "获取失败" br.ErrMsg = "查询父级分类信息失败,Err:" + err.Error() return } level = parent.Level + 1 } // 入库 timestamp := strconv.FormatInt(time.Now().UnixNano(), 10) classify := &excel.ExcelClassify{ //ExcelClassifyId: 0, ExcelClassifyName: req.ExcelClassifyName, ParentId: req.ParentId, Source: source, SysUserId: this.SysUser.AdminId, SysUserRealName: this.SysUser.RealName, Level: level, UniqueCode: utils.MD5(utils.EXCEL_DATA_PREFIX + "_" + timestamp), Sort: maxSort + 1, CreateTime: time.Now(), ModifyTime: time.Now(), } _, err = excel.AddExcelClassify(classify) if err != nil { br.Msg = "保存分类失败" br.ErrMsg = "保存分类失败,Err:" + err.Error() return } // 继承分类权限 { go data_manage_permission.InheritParentClassify(6, source, classify.ExcelClassifyId, classify.ParentId, classify.ExcelClassifyName) } br.Ret = 200 br.Msg = "保存成功" br.Success = true } // EditExcelClassify // @Title 修改excel表格分类 // @Description 修改excel表格分类 // @Param request body request.EditExcelClassifyReq true "type json string" // @Success 200 Ret=200 修改成功 // @router /excel_classify/edit [post] func (this *ExcelClassifyController) EditExcelClassify() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req request.EditExcelClassifyReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ExcelClassifyName == "" { br.Msg = "请输入分类名称" br.IsSendEmail = false return } if req.ExcelClassifyId <= 0 { br.Msg = "参数错误" br.IsSendEmail = false return } item, err := excel.GetExcelClassifyById(req.ExcelClassifyId) if err != nil { br.Msg = "保存失败" br.Msg = "获取分类信息失败,Err:" + err.Error() return } // 名字一致的话,直接返回成功 if item.ExcelClassifyName == req.ExcelClassifyName { br.Ret = 200 br.Msg = "保存成功" br.Success = true return } // 获取同级分类下存在同名分类的数量 count, err := excel.GetExcelClassifyCount(req.ExcelClassifyName, item.ParentId, item.Source) if err != nil { br.Msg = "判断名称是否已存在失败" br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error() return } if count > 0 { br.Msg = "分类名称已存在,请重新输入" br.IsSendEmail = false return } // 修改分类名称 item.ExcelClassifyName = req.ExcelClassifyName err = item.Update([]string{"ExcelClassifyName"}) if err != nil { br.Msg = "保存失败" br.ErrMsg = "保存失败,Err:" + err.Error() return } br.Ret = 200 br.Msg = "修改成功" br.Success = true br.IsAddLog = true } // DeleteExcelClassifyCheck // @Title 删除表格检测接口 // @Description 删除表格检测接口 // @Param request body request.ExcelClassifyDeleteCheckReq true "type json string" // @Success 200 {object} response.ExcelClassifyDeleteCheckResp // @router /excel_classify/delete/check [post] func (this *ExcelClassifyController) DeleteExcelClassifyCheck() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req request.ExcelClassifyDeleteCheckReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ExcelClassifyId < 0 && req.ExcelInfoId <= 0 { br.Msg = "参数错误" br.IsSendEmail = false return } var deleteStatus int var tipsMsg string // 校验是否存在该分类 ExcelClassifyInfo, err := excel.GetExcelClassifyById(req.ExcelClassifyId) if err != nil { if err.Error() == utils.ErrNoRow() { br.Msg = "该分类不存在" br.ErrMsg = "该分类不存在" return } br.Msg = "删除失败" br.ErrMsg = "查询该分类失败,Err:" + err.Error() return } if ExcelClassifyInfo == nil { br.Msg = "该分类不存在" br.ErrMsg = "该分类不存在" br.IsSendEmail = false return } //删除分类 if req.ExcelClassifyId > 0 && req.ExcelInfoId == 0 { //判断表格分类下,是否含有表格 count, err := excel.GetExcelInfoCountByClassifyId(req.ExcelClassifyId) if err != nil { br.Msg = "删除失败" br.ErrMsg = "分类下是否含有指标失败,Err:" + err.Error() return } if count > 0 { deleteStatus = 1 tipsMsg = "该分类下关联表格不可删除" } else { childClassify, e := excel.GetChildClassifyById(req.ExcelClassifyId) if e != nil && e.Error() != utils.ErrNoRow() { br.Msg = "获取失败" br.ErrMsg = "获取分类信息失败, GetEdbClassify,Err:" + e.Error() return } if len(childClassify) > 0 { var classifyIds []int for _, v := range childClassify { classifyIds = append(classifyIds, v.ExcelClassifyId) } condition := fmt.Sprintf(` AND excel_classify_id IN (%s) `, utils.GetOrmInReplace(len(classifyIds))) var pars []interface{} pars = append(pars, classifyIds) childCount, err := excel.GetExcelInfoCountByCondition(condition, pars) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "删除失败" br.ErrMsg = "查询分类下表格数量失败,Err:" + err.Error() return } if childCount > 0 { deleteStatus = 1 tipsMsg = "该分类下关联表格不可删除" } } } } //if deleteStatus != 1 && req.ExcelInfoId == 0 { // classifyCount, err := data_manage.GetExcelClassifyCountByClassifyId(req.ExcelClassifyId) // if err != nil && err.Error() != utils.ErrNoRow() { // br.Msg = "删除失败" // br.ErrMsg = "分类下是否含有表格失败,Err:" + err.Error() // return // } // if classifyCount > 0 { // deleteStatus = 2 // tipsMsg = "确认删除当前目录及包含的子目录吗?" // } //} if deleteStatus == 0 { tipsMsg = "可删除,进行删除操作" } resp := response2.ExcelClassifyDeleteCheckResp{ DeleteStatus: deleteStatus, TipsMsg: tipsMsg, } br.Ret = 200 br.Msg = "检测成功" br.Success = true br.Data = resp } // DeleteExcelClassify // @Title 删除表格分类/表格 // @Description 删除表格分类/表格接口 // @Param request body request.DeleteExcelClassifyReq true "type json string" // @Success 200 Ret=200 删除成功 // @router /excel_classify/delete [post] func (this *ExcelClassifyController) DeleteExcelClassify() { 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 } var req request.DeleteExcelClassifyReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ExcelClassifyId < 0 && req.ExcelInfoId <= 0 { br.Msg = "参数错误" br.IsSendEmail = false return } //删除分类 if req.ExcelClassifyId > 0 && req.ExcelInfoId == 0 { //判断是否含有指标 count, err := excel.GetExcelInfoCountByClassifyId(req.ExcelClassifyId) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "删除失败" br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error() return } if count > 0 { br.Msg = "该目录下存在关联ETA表格,不可删除" br.IsSendEmail = false return } else { childClassify, e := excel.GetChildClassifyById(req.ExcelClassifyId) if e != nil && e.Error() != utils.ErrNoRow() { br.Msg = "获取失败" br.ErrMsg = "获取分类信息失败, GetEdbClassify,Err:" + e.Error() return } if len(childClassify) > 0 { var classifyIds []int for _, v := range childClassify { classifyIds = append(classifyIds, v.ExcelClassifyId) } condition := fmt.Sprintf(` AND excel_classify_id IN (%s) `, utils.GetOrmInReplace(len(classifyIds))) var pars []interface{} pars = append(pars, classifyIds) childCount, err := excel.GetExcelInfoCountByCondition(condition, pars) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "删除失败" br.ErrMsg = "查询分类下表格数量失败,Err:" + err.Error() return } if childCount > 0 { br.Msg = "该目录下存在关联ETA表格,不可删除" br.IsSendEmail = false return } } } classifyItem, err := excel.GetExcelClassifyById(req.ExcelClassifyId) if err != nil { br.Msg = "删除失败" br.ErrMsg = "获取分类失败,Err:" + err.Error() if err.Error() == utils.ErrNoRow() { br.Msg = "分类不存在" br.ErrMsg = "分类不存在" } return } classifyItem.IsDelete = 1 err = classifyItem.Update([]string{"IsDelete"}) if err != nil { br.Msg = "删除失败" br.ErrMsg = "删除失败,Err:" + err.Error() return } } resp := response2.AddExcelInfoResp{} //删除表格 if req.ExcelInfoId > 0 { excelInfo, err := excel.GetExcelInfoById(req.ExcelInfoId) if err != nil { if err.Error() == utils.ErrNoRow() { br.Msg = "表格已删除,请刷新页面" br.ErrMsg = "表格已删除,请刷新页面" return } else { br.Msg = "删除失败" br.ErrMsg = "删除失败,获取指标信息失败,Err:" + err.Error() return } } if excelInfo == nil { br.Msg = "表格已删除,请刷新页面" br.IsSendEmail = false return } // 删除excel err, errMsg, isSendEmail := excel2.Delete(excelInfo, sysUser) if err != nil { br.Msg = "删除失败" if errMsg != `` { br.Msg = errMsg } br.ErrMsg = "删除失败,Err:" + err.Error() br.IsSendEmail = isSendEmail return } // 返回下一个表格的信息 { var nextItem *excel.ExcelInfo var condition string var pars []interface{} condition += " AND excel_classify_id=? " pars = append(pars, excelInfo.ExcelClassifyId) if excelInfo.Source == utils.BALANCE_TABLE { condition += " AND parent_id=0 AND balance_type=0 " } condition += " AND (sort>? OR (sort=? AND excel_info_id