package data_manage_permission import ( "encoding/json" "eta/eta_api/models" "eta/eta_api/models/data_manage" "eta/eta_api/models/data_manage/data_manage_permission" "eta/eta_api/models/data_manage/excel" "eta/eta_api/models/system" "eta/eta_api/utils" "fmt" "github.com/rdlucklib/rdluck_tools/uuid" "strconv" "strings" "time" ) // GetEdbChartClassifyList // @Description: 获取数据权限那边的分类列表 // @author: Roc // @datetime 2024-04-01 09:46:29 // @param source int // @param subSource int // @return resp data_manage.EdbChartClassifyResp // @return err error func GetEdbChartClassifyList(source, subSource int) (resp data_manage.EdbChartClassifyResp, err error) { switch source { case 1: //手工数据指标 list, e := models.GetEdbdataClassify(0) if e != nil { err = e return } for _, v := range list { item := data_manage.EdbChartClassify{ ClassifyId: v.ClassifyId, ClassifyName: v.ClassifyName, ParentId: v.ParentId, } for _, v2 := range v.Child { child := data_manage.EdbChartClassify{ ClassifyId: v2.ClassifyId, ClassifyName: v2.ClassifyName, ParentId: v2.ParentId, } item.Child = append(item.Child, &child) } resp.List = append(resp.List, &item) } case 2: //上海钢联数据库 rootList, e := data_manage.GetBaseFromMysteelChemicalClassifyByParentId(0) if e != nil && e.Error() != utils.ErrNoRow() { err = e return } classifyAll, e := data_manage.GetAllBaseFromMysteelChemicalClassify() if e != nil && e.Error() != utils.ErrNoRow() { err = e return } rootChildMap := make(map[int][]*data_manage.BaseFromMysteelChemicalClassifyItems) for _, v := range classifyAll { rootChildMap[v.ParentId] = append(rootChildMap[v.ParentId], v) } nodeAll := make([]*data_manage.BaseFromMysteelChemicalClassifyItems, 0) for _, v := range rootList { if existItems, ok := rootChildMap[v.BaseFromMysteelChemicalClassifyId]; ok { v.Children = existItems } else { items := make([]*data_manage.BaseFromMysteelChemicalClassifyItems, 0) v.Children = items } nodeAll = append(nodeAll, v) } for _, v := range nodeAll { item := data_manage.EdbChartClassify{ ClassifyId: v.BaseFromMysteelChemicalClassifyId, ClassifyName: v.ClassifyName, ParentId: v.ParentId, } for _, v2 := range v.Children { child := data_manage.EdbChartClassify{ ClassifyId: v2.BaseFromMysteelChemicalClassifyId, ClassifyName: v2.ClassifyName, ParentId: v2.ParentId, } item.Child = append(item.Child, &child) } resp.List = append(resp.List, &item) } case 3: //ETA指标库 //rootList, e := data_manage.GetEdbClassifyByParentId(0, 0) //if e != nil && e.Error() != utils.ErrNoRow() { // err = e // return //} // 考虑到后面可以会迭代到10层, 这里直接用递归处理 classifyAll, e := data_manage.GetAllEdbClassifyByType(0) if e != nil && e.Error() != utils.ErrNoRow() { err = e return } resp.List = GetEdbMenuTreeRecursive(classifyAll, 0) case 4: //ETA预测指标 // 考虑到后面可以会迭代到10层, 这里直接用递归 classifyAll, e := data_manage.GetAllEdbClassifyByType(1) if e != nil && e.Error() != utils.ErrNoRow() { err = e return } resp.List = GetEdbMenuTreeRecursive(classifyAll, 0) case 5: //图库 // 考虑到后面可以会迭代到10层, 这里直接用递归处理 classifyAll, e := data_manage.GetAllChartClassifyItemsBySource(1) if e != nil && e.Error() != utils.ErrNoRow() { err = e return } resp.List = GetChartMenuTreeRecursive(classifyAll, 0) // 将数据加入缓存 //if utils.Re == nil { // utils.Rc.Delete(key) //} case 6: // ETA表格 // 考虑到后面可以会迭代到10层, 这里直接用递归处理 classifyAll, e := excel.GetExcelClassifyBySourceOrderByLevel(subSource) if e != nil && e.Error() != utils.ErrNoRow() { err = e return } resp.List = GetExcelMenuTreeRecursive(classifyAll, 0) default: return } return } // GetEdbMenuTreeRecursive 递归指标库菜单树 func GetEdbMenuTreeRecursive(list []*data_manage.EdbClassifyItems, parentId int) []*data_manage.EdbChartClassify { res := make([]*data_manage.EdbChartClassify, 0) for _, v := range list { t := new(data_manage.EdbChartClassify) t.ClassifyId = v.ClassifyId t.ClassifyName = v.ClassifyName t.ParentId = v.ParentId t.IsJoinPermission = v.IsJoinPermission if v.ParentId == parentId { t.Child = GetEdbMenuTreeRecursive(list, v.ClassifyId) res = append(res, t) } } return res } // GetChartMenuTreeRecursive 递归指标库菜单树 func GetChartMenuTreeRecursive(list []*data_manage.ChartClassifyItems, parentId int) []*data_manage.EdbChartClassify { res := make([]*data_manage.EdbChartClassify, 0) for _, v := range list { t := new(data_manage.EdbChartClassify) t.ClassifyId = v.ChartClassifyId t.ClassifyName = v.ChartClassifyName t.ParentId = v.ParentId t.IsJoinPermission = v.IsJoinPermission if v.ParentId == parentId { t.Child = GetChartMenuTreeRecursive(list, v.ChartClassifyId) res = append(res, t) } } return res } // GetExcelMenuTreeRecursive // @Description: 递归ETA表格菜单树 // @author: Roc // @datetime 2024-03-27 10:30:32 // @param list []*data_manage.ChartClassifyItems // @param parentId int // @return []*data_manage.EdbChartClassify func GetExcelMenuTreeRecursive(list []*excel.ExcelClassifyItems, parentId int) []*data_manage.EdbChartClassify { res := make([]*data_manage.EdbChartClassify, 0) for _, v := range list { t := new(data_manage.EdbChartClassify) t.ClassifyId = v.ExcelClassifyId t.ClassifyName = v.ExcelClassifyName t.ParentId = v.ParentId t.IsJoinPermission = v.IsJoinPermission if v.ParentId == parentId { t.Child = GetExcelMenuTreeRecursive(list, v.ExcelClassifyId) res = append(res, t) } } return res } // GetMoveEdbChartList 获取待转移的指标/图表列表 // @param source 来源 :1:手工数据指标 2:上海钢联数据库 3:ETA指标库 4:ETA预测指标 5:图库 6:ETA表格 func GetMoveEdbChartList(source, subSource, userId int, keyword, classify string, startSize, pageSize int) (list []data_manage.MoveEdbChartList, total int, err error) { var condition string var pars []interface{} switch source { case 1: //手工数据指标 if keyword != "" { condition += ` AND (a.SEC_NAME LIKE ? OR a.TRADE_CODE like ? ) ` pars = utils.GetLikeKeywordPars(pars, keyword, 2) } if userId > 0 { condition += ` AND a.user_id = ? ` pars = append(pars, userId) } if classify != "" { condition += ` AND a.classify_id IN (` + classify + `) ` } total, err = models.GetEdbinfoListCount(condition, pars, "", 0) if err != nil { return } tmpList, tmpErr := models.GetEdbinfoItemList(condition, pars, startSize, pageSize, "", 0) if tmpErr != nil { err = tmpErr return } if len(tmpList) > 0 { adminIdList := make([]int, 0) for _, v := range tmpList { adminIdList = append(adminIdList, v.UserId) } adminList, tmpErr := system.GetAdminListByIdListWithoutEnable(adminIdList) if tmpErr != nil { err = tmpErr return } adminMap := make(map[int]string) for _, adminInfo := range adminList { adminMap[adminInfo.AdminId] = adminInfo.RealName } for _, v := range tmpList { list = append(list, data_manage.MoveEdbChartList{ DataId: v.TradeCode, Code: v.TradeCode, Name: v.SecName, ClassifyName: v.ClassifyName, NameEn: v.SecName, ClassifyNameEn: v.ClassifyName, CreateUserId: v.UserId, CreateUserName: adminMap[v.UserId], }) } } case 2: //上海钢联数据库 if keyword != `` { condition += " AND (index_name like ? OR index_code like ? OR sys_user_real_name like ? ) " pars = utils.GetLikeKeywordPars(pars, keyword, 3) } if userId > 0 { condition += ` AND sys_user_id = ? ` pars = append(pars, userId) } if classify != "" { condition += ` AND base_from_mysteel_chemical_classify_id IN (` + classify + `) ` } total, err = data_manage.GetMysteelChemicalIndexCount(condition, pars) if err != nil { return } tmpList, tmpErr := data_manage.GetMysteelChemicalIndexList(condition, pars, startSize, pageSize, "DESC") if tmpErr != nil { err = tmpErr return } for _, v := range tmpList { list = append(list, data_manage.MoveEdbChartList{ DataId: strconv.Itoa(v.BaseFromMysteelChemicalIndexId), Code: v.IndexCode, Name: v.IndexName, NameEn: v.IndexName, //ClassifyName: v., CreateUserId: v.SysUserId, CreateUserName: v.SysUserRealName, }) } case 3, 4: //ETA指标库、ETA预测指标 if keyword != `` { condition += " AND (edb_code like ? OR edb_name like ? OR edb_name_en like ? OR sys_user_real_name like ? ) " pars = utils.GetLikeKeywordPars(pars, keyword, 4) } if userId > 0 { condition += ` AND sys_user_id = ? ` pars = append(pars, userId) } if classify != "" { condition += ` AND classify_id IN (` + classify + `) ` } edbInfoType := 0 //ETA指标库 if source == 4 { //ETA预测指标 edbInfoType = 1 } condition += ` AND edb_info_type = ? ` pars = append(pars, edbInfoType) total, err = data_manage.GetEdbInfoByConditionCount(condition, pars) if err != nil { return } tmpList, tmpErr := data_manage.GetEdbInfoListByCondition(condition, pars, startSize, pageSize, "DESC") if tmpErr != nil { err = tmpErr return } for _, v := range tmpList { list = append(list, data_manage.MoveEdbChartList{ DataId: strconv.Itoa(v.EdbInfoId), Code: v.EdbCode, Name: v.EdbName, NameEn: v.EdbNameEn, //ClassifyName: v., CreateUserId: v.SysUserId, CreateUserName: v.SysUserRealName, }) } case 5: //图库 // 只展示eta图库 condition += ` AND source = ? ` pars = append(pars, utils.CHART_SOURCE_DEFAULT) // 关键字搜索 if keyword != `` { condition += " AND (chart_name like ? or chart_name_en like ? OR sys_user_real_name like ? ) " pars = utils.GetLikeKeywordPars(pars, keyword, 3) } if userId > 0 { condition += ` AND sys_user_id = ? ` pars = append(pars, userId) } if classify != "" { condition += ` AND chart_classify_id IN (` + classify + `) ` } total, err = data_manage.GetChartInfoCountByCondition(condition, pars) if err != nil { return } tmpList, tmpErr := data_manage.GetChartInfoListByCondition(condition, pars, startSize, pageSize) if tmpErr != nil { err = tmpErr return } // 获取所有的图表分类 firstClassifyList, tmpErr := data_manage.GetChartClassifyByParentId(0, 1) if tmpErr != nil { err = tmpErr return } firstClassifyNameMap := make(map[int]string) firstClassifyNameEnMap := make(map[int]string) for _, v := range firstClassifyList { firstClassifyNameMap[v.ChartClassifyId] = v.ChartClassifyName firstClassifyNameEnMap[v.ChartClassifyId] = v.ChartClassifyNameEn } secondClassList, tmpErr := data_manage.GetChartClassifyAll(1) if tmpErr != nil { err = tmpErr return } secondClassifyNameMap := make(map[int]string) secondClassifyNameEnMap := make(map[int]string) for _, v := range secondClassList { firstName := firstClassifyNameMap[v.ParentId] firstNameEn := firstClassifyNameEnMap[v.ParentId] if firstName != `` { firstName = firstName + " / " } if firstNameEn != `` { firstNameEn = firstNameEn + " / " } secondClassifyNameMap[v.ChartClassifyId] = firstName + v.ChartClassifyName secondClassifyNameEnMap[v.ChartClassifyId] = firstNameEn + v.ChartClassifyNameEn } for _, v := range tmpList { list = append(list, data_manage.MoveEdbChartList{ DataId: strconv.Itoa(v.ChartInfoId), Name: v.ChartName, ClassifyName: secondClassifyNameMap[v.ChartClassifyId], NameEn: v.ChartNameEn, ClassifyNameEn: secondClassifyNameEnMap[v.ChartClassifyId], CreateUserId: v.SysUserId, CreateUserName: v.SysUserRealName, }) } case 6: //ETA表格 if keyword != `` { condition += " AND (excel_name like ? OR sys_user_real_name like ? ) " pars = utils.GetLikeKeywordPars(pars, keyword, 2) } if userId > 0 { condition += ` AND sys_user_id = ? ` pars = append(pars, userId) } if classify != "" { condition += ` AND excel_classify_id IN (` + classify + `) ` } condition += " AND source = ? " pars = append(pars, subSource) if subSource == utils.BALANCE_TABLE { condition += " AND parent_id = 0 AND balance_type = 0 " } total, err = excel.GetExcelInfoCountByCondition(condition, pars) if err != nil { return } tmpList, tmpErr := excel.GetNoContentExcelInfoListByCondition(condition, pars, startSize, pageSize) if tmpErr != nil { err = tmpErr return } // 获取所有的图表分类 firstClassifyList, tmpErr := excel.GetExcelClassifyByParentId(0, subSource) if tmpErr != nil { err = tmpErr return } firstClassifyNameMap := make(map[int]string) for _, v := range firstClassifyList { firstClassifyNameMap[v.ExcelClassifyId] = v.ExcelClassifyName } secondClassList, tmpErr := excel.GetExcelClassifyBySource(subSource) if tmpErr != nil { err = tmpErr return } secondClassifyNameMap := make(map[int]string) for _, v := range secondClassList { firstName := firstClassifyNameMap[v.ParentId] if firstName != `` { firstName = firstName + " / " } secondClassifyNameMap[v.ExcelClassifyId] = firstName + v.ExcelClassifyName } for _, v := range tmpList { list = append(list, data_manage.MoveEdbChartList{ DataId: strconv.Itoa(v.ExcelInfoId), Name: v.ExcelClassifyName, ClassifyName: secondClassifyNameMap[v.ExcelClassifyId], NameEn: v.ExcelClassifyName, ClassifyNameEn: secondClassifyNameMap[v.ExcelClassifyId], CreateUserId: v.SysUserId, CreateUserName: v.SysUserRealName, }) } default: return } return } // MoveEdbChart 转移指标/图表创建人 // @param source 来源 :1:手工数据指标 2:上海钢联数据库 3:ETA指标库 4:ETA预测指标 5:图库 6:ETA表格 func MoveEdbChart(source, subSource, oldUserId, newUserId int, isSelectAll bool, dataId, noDataId []string, keyword, classify string, opUserId int, opUserName string) (err error, errMsg string) { adminInfo, err := system.GetSysAdminById(newUserId) if err != nil { return } // 如果勾选全部数据,那么 if isSelectAll { // 找出不要的指标ID列表 noDataIdMap := make(map[string]string, 0) for _, v := range noDataId { noDataIdMap[v] = v } // 需要转义的指标/图表ID列表 dataId = make([]string, 0) // 获取所有指标/图表 list, _, tmpErr := GetMoveEdbChartList(source, subSource, oldUserId, keyword, classify, 0, 100000) if tmpErr != nil { err = tmpErr return } for _, v := range list { if _, ok := noDataIdMap[v.DataId]; !ok { dataId = append(dataId, v.DataId) } } } // 操作记录 dataPermissionMoveRecordList := make([]*data_manage_permission.DataPermissionMoveRecord, 0) uniqueCode := uuid.NewUUID().Hex32() title := `资产转移` content := `资产转入` switch source { case 1: //手工数据指标 content += `(手工指标数据)` tmpList, tmpErr := models.GetEdbinfoListByCodeListByCodeIdList(dataId) if tmpErr != nil { err = tmpErr return } if len(tmpList) > 0 { for _, v := range tmpList { //if v.UserId == newUserId { // errMsg = "新创建人不可和原创建人一致" // err = errors.New(errMsg) // return //} // 操作记录 dataPermissionMoveRecordList = append(dataPermissionMoveRecordList, &data_manage_permission.DataPermissionMoveRecord{ DataPermissionMoveRecordId: 0, Source: int32(source), SubSource: int32(subSource), OpUniqueCode: uniqueCode, DataId: v.TradeCode, DataCode: v.TradeCode, DataName: v.SecName, OriginalUserId: int32(v.UserId), OriginalUserName: v.UserName, NewUserId: int32(adminInfo.AdminId), NewUserName: adminInfo.RealName, CreateTime: time.Now(), }) } // 修改创建人 err = models.ModifyEdbinfoUserIdByCodeList(dataId, newUserId, adminInfo.RealName) // 如果移动成功了,需要记录该指标的操作记录 if err == nil && len(tmpList) > 0 { go afterMoveManualEdb(tmpList, adminInfo, opUserId, opUserName) } } case 2: //上海钢联数据库 content += `(上海钢联数据库)` tmpList, tmpErr := data_manage.GetMysteelChemicalIndexListByIndexId(dataId) if tmpErr != nil { err = tmpErr return } if len(tmpList) > 0 { for _, v := range tmpList { //if v.SysUserId == newUserId { // errMsg = "新创建人不可和原创建人一致" // err = errors.New(errMsg) // return //} // 操作记录 dataPermissionMoveRecordList = append(dataPermissionMoveRecordList, &data_manage_permission.DataPermissionMoveRecord{ DataPermissionMoveRecordId: 0, Source: int32(source), SubSource: int32(subSource), OpUniqueCode: uniqueCode, DataId: fmt.Sprint(v.BaseFromMysteelChemicalIndexId), DataCode: v.IndexCode, DataName: v.IndexName, OriginalUserId: int32(v.SysUserId), OriginalUserName: v.SysUserRealName, NewUserId: int32(adminInfo.AdminId), NewUserName: adminInfo.RealName, CreateTime: time.Now(), }) } // 修改创建人 err = data_manage.ModifyMysteelChemicalIndexUserIdByCodeList(dataId, adminInfo.AdminId, adminInfo.RealName) } case 3, 4: //ETA指标库、ETA预测指标 if source == 3 { content += `(ETA指标库)` } else { content += `(ETA预测指标)` } tmpList, tmpErr := data_manage.GetEdbInfoListByEdbInfoId(dataId) if tmpErr != nil { err = tmpErr return } if len(tmpList) > 0 { for _, v := range tmpList { //if v.SysUserId == newUserId { // errMsg = "新创建人不可和原创建人一致" // err = errors.New(errMsg) // return //} // 操作记录 dataPermissionMoveRecordList = append(dataPermissionMoveRecordList, &data_manage_permission.DataPermissionMoveRecord{ DataPermissionMoveRecordId: 0, Source: int32(source), SubSource: int32(subSource), OpUniqueCode: uniqueCode, DataId: fmt.Sprint(v.EdbInfoId), DataCode: v.EdbCode, DataName: v.EdbName, OriginalUserId: int32(v.SysUserId), OriginalUserName: v.SysUserRealName, NewUserId: int32(adminInfo.AdminId), NewUserName: adminInfo.RealName, CreateTime: time.Now(), }) } // 修改创建人 err = data_manage.ModifyEdbInfoUserIdByCodeList(dataId, adminInfo.AdminId, adminInfo.RealName) // 移除当前用户的不可操作权限 { obj := data_manage.EdbInfoNoPermissionAdmin{} for _, v := range dataId { edbInfoId, _ := strconv.Atoi(v) go obj.DeleteByEdbInfoIdAndAdminId(edbInfoId, adminInfo.AdminId) } } } case 5: //图库 content += `(ETA图库)` // 只展示eta图库 subSource = utils.CHART_SOURCE_DEFAULT tmpList, tmpErr := data_manage.GetChartInfoListByChartIdList(dataId) if tmpErr != nil { err = tmpErr return } if len(tmpList) > 0 { for _, v := range tmpList { //if v.SysUserId == newUserId { // errMsg = "新创建人不可和原创建人一致" // err = errors.New(errMsg) // return //} // 操作记录 dataPermissionMoveRecordList = append(dataPermissionMoveRecordList, &data_manage_permission.DataPermissionMoveRecord{ DataPermissionMoveRecordId: 0, Source: int32(source), SubSource: int32(subSource), OpUniqueCode: uniqueCode, DataId: fmt.Sprint(v.ChartInfoId), DataCode: fmt.Sprint(v.ChartInfoId), DataName: v.ChartName, OriginalUserId: int32(v.SysUserId), OriginalUserName: v.SysUserRealName, NewUserId: int32(adminInfo.AdminId), NewUserName: adminInfo.RealName, CreateTime: time.Now(), }) } // 修改创建人 err = data_manage.ModifyChartInfoUserIdByCodeList(dataId, adminInfo.AdminId, adminInfo.RealName) } case 6: // ETA表格 content += `(ETA表格)` tmpList, tmpErr := excel.GetNoContentExcelListByExcelInfoIdList(dataId) if tmpErr != nil { err = tmpErr return } var customAnalysisIds []int if len(tmpList) > 0 { for _, v := range tmpList { //if v.SysUserId == newUserId { // errMsg = "新创建人不可和原创建人一致" // err = errors.New(errMsg) // return //} // 操作记录 dataPermissionMoveRecordList = append(dataPermissionMoveRecordList, &data_manage_permission.DataPermissionMoveRecord{ DataPermissionMoveRecordId: 0, Source: int32(source), SubSource: int32(subSource), OpUniqueCode: uniqueCode, DataId: fmt.Sprint(v.ExcelInfoId), DataCode: fmt.Sprint(v.ExcelInfoId), DataName: v.ExcelName, OriginalUserId: int32(v.SysUserId), OriginalUserName: v.SysUserRealName, NewUserId: int32(adminInfo.AdminId), NewUserName: adminInfo.RealName, CreateTime: time.Now(), }) if v.Source == utils.CUSTOM_ANALYSIS_TABLE { customAnalysisIds = append(customAnalysisIds, v.ExcelInfoId) } } // 修改创建人 err = excel.ModifyExcelInfoUserIdByCodeList(dataId, adminInfo.AdminId, adminInfo.RealName) if err != nil { return } // 由于自定义分析表分类私有化, 如果转移的是自定义分析表, 那么需要将excel转移至未分类下 if len(customAnalysisIds) > 0 { err = MoveCustomAnalysisExcel2DefaultClassify(customAnalysisIds, adminInfo.AdminId, adminInfo.RealName) } } default: return } // 转移失败了那么就直接返回了 if err != nil { return } // 添加资产转移的记录 dataPermissionMessage := &data_manage_permission.DataPermissionMessage{ DataPermissionMessageId: 0, SendUserId: int32(opUserId), ReceiveUserId: int32(adminInfo.AdminId), Content: title, Remark: content, OpType: 1, Source: int32(source), SubSource: int32(subSource), OpUniqueCode: uniqueCode, IsRead: 0, CreateTime: time.Now(), ModifyTime: time.Now(), } tmpErr := data_manage_permission.AddDataPermissionMoveRecord(dataPermissionMoveRecordList, dataPermissionMessage) // 失败的话,日志记录 if tmpErr != nil { // 序列化结构体为 JSON 数据 recordListJsonBytes, tmpErr2 := json.Marshal(dataPermissionMoveRecordList) if tmpErr2 != nil { utils.FileLog.Error(fmt.Sprintf("添加资产转移的记录失败:%s;list转json失败:;操作记录:%+v;消息记录:%+v", tmpErr.Error(), dataPermissionMoveRecordList, dataPermissionMessage)) return } // 序列化结构体为 JSON 数据 messageJsonBytes, tmpErr2 := json.Marshal(dataPermissionMessage) if tmpErr2 != nil { utils.FileLog.Error(fmt.Sprintf("添加资产转移的记录失败:%s;message转json失败:;操作记录:%+v;消息记录:%+v", tmpErr.Error(), dataPermissionMoveRecordList, dataPermissionMessage)) return } utils.FileLog.Error(fmt.Sprintf("添加资产转移的记录失败:%s;操作记录:%s;消息记录:%s", tmpErr.Error(), string(recordListJsonBytes), string(messageJsonBytes))) } return } // MoveAllEdbChartOld // @Description: 通过原创建人转移指标/图表创建人 // @author: Roc // @datetime 2024-03-26 15:11:12 // @param sourceList []int 1:手工数据指标 2:上海钢联数据库 3:ETA指标库 4:ETA预测指标 5:图库 6:ETA表格 // @param oldUserId []int // @param userId int // @param opUserId int // @return err error // @return errMsg string func MoveAllEdbChartOld(sourceList, oldUserIdList []int, userId, opUserId int) (err error, errMsg string) { adminInfo, err := system.GetSysAdminById(userId) if err != nil { return } // 操作记录 dataPermissionMoveRecordList := make([]*data_manage_permission.DataPermissionMoveRecord, 0) uniqueCode := uuid.NewUUID().Hex32() title := `资产转移` content := `资产转入` sourceStrList := make([]string, 0) for _, source := range sourceList { switch source { case 1: //手工数据指标 sourceStrList = append(sourceStrList, "手工指标数据") tmpList, tmpErr := models.GetEdbinfoListByCodeListByUserId(oldUserIdList) if tmpErr != nil { err = tmpErr return } if len(tmpList) > 0 { for _, v := range tmpList { // 操作记录 dataPermissionMoveRecordList = append(dataPermissionMoveRecordList, &data_manage_permission.DataPermissionMoveRecord{ DataPermissionMoveRecordId: 0, Source: int32(source), SubSource: 0, OpUniqueCode: uniqueCode, DataId: v.TradeCode, DataCode: v.TradeCode, DataName: v.SecName, OriginalUserId: int32(v.UserId), OriginalUserName: v.UserName, NewUserId: int32(adminInfo.AdminId), NewUserName: adminInfo.RealName, CreateTime: time.Now(), }) } // 修改创建人 err = models.ModifyEdbinfoUserIdByOldUserId(oldUserIdList, userId) } case 2: //上海钢联数据库 sourceStrList = append(sourceStrList, "上海钢联数据库") tmpList, tmpErr := data_manage.GetMysteelChemicalIndexListByUserId(oldUserIdList) if tmpErr != nil { err = tmpErr return } if len(tmpList) > 0 { for _, v := range tmpList { //if v.SysUserId == newUserId { // errMsg = "新创建人不可和原创建人一致" // err = errors.New(errMsg) // return //} // 操作记录 dataPermissionMoveRecordList = append(dataPermissionMoveRecordList, &data_manage_permission.DataPermissionMoveRecord{ DataPermissionMoveRecordId: 0, Source: int32(source), SubSource: 0, OpUniqueCode: uniqueCode, DataId: fmt.Sprint(v.BaseFromMysteelChemicalIndexId), DataCode: v.IndexCode, DataName: v.IndexName, OriginalUserId: int32(v.SysUserId), OriginalUserName: v.SysUserRealName, NewUserId: int32(adminInfo.AdminId), NewUserName: adminInfo.RealName, CreateTime: time.Now(), }) } // 修改创建人 err = data_manage.ModifyMysteelChemicalIndexUserIdByOldUserId(oldUserIdList, adminInfo.AdminId, adminInfo.RealName) } case 3: //ETA指标库 sourceStrList = append(sourceStrList, "ETA指标库") edbInfoType := 0 tmpList, tmpErr := data_manage.GetEdbInfoListByUserId(oldUserIdList, edbInfoType) if tmpErr != nil { err = tmpErr return } if len(tmpList) > 0 { for _, v := range tmpList { // 操作记录 dataPermissionMoveRecordList = append(dataPermissionMoveRecordList, &data_manage_permission.DataPermissionMoveRecord{ DataPermissionMoveRecordId: 0, Source: int32(source), SubSource: int32(edbInfoType), OpUniqueCode: uniqueCode, DataId: fmt.Sprint(v.EdbInfoId), DataCode: v.EdbCode, DataName: v.EdbName, OriginalUserId: int32(v.SysUserId), OriginalUserName: v.SysUserRealName, NewUserId: int32(adminInfo.AdminId), NewUserName: adminInfo.RealName, CreateTime: time.Now(), }) } // 修改创建人 err = data_manage.ModifyEdbInfoUserIdByOldUserId(oldUserIdList, edbInfoType, adminInfo.AdminId, adminInfo.RealName) } case 4: //ETA预测指标 sourceStrList = append(sourceStrList, "ETA预测指标") edbInfoType := 1 tmpList, tmpErr := data_manage.GetEdbInfoListByUserId(oldUserIdList, edbInfoType) if tmpErr != nil { err = tmpErr return } if len(tmpList) > 0 { for _, v := range tmpList { // 操作记录 dataPermissionMoveRecordList = append(dataPermissionMoveRecordList, &data_manage_permission.DataPermissionMoveRecord{ DataPermissionMoveRecordId: 0, Source: int32(source), SubSource: int32(edbInfoType), OpUniqueCode: uniqueCode, DataId: fmt.Sprint(v.EdbInfoId), DataCode: v.EdbCode, DataName: v.EdbName, OriginalUserId: int32(v.SysUserId), OriginalUserName: v.SysUserRealName, NewUserId: int32(adminInfo.AdminId), NewUserName: adminInfo.RealName, CreateTime: time.Now(), }) } // 修改创建人 err = data_manage.ModifyEdbInfoUserIdByOldUserId(oldUserIdList, edbInfoType, adminInfo.AdminId, adminInfo.RealName) } case 5: //ETA图库 sourceStrList = append(sourceStrList, "ETA图库") // 只展示eta图库 subSource := utils.CHART_SOURCE_DEFAULT tmpList, tmpErr := data_manage.GetChartInfoListByUserId(oldUserIdList, subSource) if tmpErr != nil { err = tmpErr return } if len(tmpList) > 0 { for _, v := range tmpList { // 操作记录 dataPermissionMoveRecordList = append(dataPermissionMoveRecordList, &data_manage_permission.DataPermissionMoveRecord{ DataPermissionMoveRecordId: 0, Source: int32(source), SubSource: int32(subSource), OpUniqueCode: uniqueCode, DataId: fmt.Sprint(v.ChartInfoId), DataCode: fmt.Sprint(v.ChartInfoId), DataName: v.ChartName, OriginalUserId: int32(v.SysUserId), OriginalUserName: v.SysUserRealName, NewUserId: int32(adminInfo.AdminId), NewUserName: adminInfo.RealName, CreateTime: time.Now(), }) } // 修改创建人 err = data_manage.ModifyChartInfoUserIdByOldUserId(oldUserIdList, adminInfo.AdminId, adminInfo.RealName) } case 6: // ETA表格 sourceStrList = append(sourceStrList, "ETA表格") tmpList, tmpErr := excel.GetNoContentExcelListByUserId(oldUserIdList) if tmpErr != nil { err = tmpErr return } if len(tmpList) > 0 { for _, v := range tmpList { // 操作记录 dataPermissionMoveRecordList = append(dataPermissionMoveRecordList, &data_manage_permission.DataPermissionMoveRecord{ DataPermissionMoveRecordId: 0, Source: int32(source), SubSource: int32(v.Source), OpUniqueCode: uniqueCode, DataId: fmt.Sprint(v.ExcelInfoId), DataCode: fmt.Sprint(v.ExcelInfoId), DataName: v.ExcelName, OriginalUserId: int32(v.SysUserId), OriginalUserName: v.SysUserRealName, NewUserId: int32(adminInfo.AdminId), NewUserName: adminInfo.RealName, CreateTime: time.Now(), }) } // 修改创建人 err = excel.ModifyExcelInfoUserIdByOldUserId(oldUserIdList, adminInfo.AdminId, adminInfo.RealName) } default: } // 转移失败了那么就直接返回了 if err != nil { return } } content += fmt.Sprintf("(%s)", strings.Join(sourceStrList, ",")) // 添加资产转移的记录 dataPermissionMessage := &data_manage_permission.DataPermissionMessage{ DataPermissionMessageId: 0, SendUserId: int32(opUserId), ReceiveUserId: int32(adminInfo.AdminId), Content: title, Remark: content, OpType: 2, OpUniqueCode: uniqueCode, IsRead: 0, CreateTime: time.Now(), ModifyTime: time.Now(), } tmpErr := data_manage_permission.AddDataPermissionMoveRecord(dataPermissionMoveRecordList, dataPermissionMessage) // 失败的话,日志记录 if tmpErr != nil { // 序列化结构体为 JSON 数据 recordListJsonBytes, tmpErr2 := json.Marshal(dataPermissionMoveRecordList) if tmpErr2 != nil { utils.FileLog.Error(fmt.Sprintf("添加资产转移的记录失败:%s;list转json失败:;操作记录:%+v;消息记录:%+v", tmpErr.Error(), dataPermissionMoveRecordList, dataPermissionMessage)) return } // 序列化结构体为 JSON 数据 messageJsonBytes, tmpErr2 := json.Marshal(dataPermissionMessage) if tmpErr2 != nil { utils.FileLog.Error(fmt.Sprintf("添加资产转移的记录失败:%s;message转json失败:;操作记录:%+v;消息记录:%+v", tmpErr.Error(), dataPermissionMoveRecordList, dataPermissionMessage)) return } utils.FileLog.Error(fmt.Sprintf("添加资产转移的记录失败:%s;操作记录:%s;消息记录:%s", tmpErr.Error(), string(recordListJsonBytes), string(messageJsonBytes))) } return } // MoveAllEdbChart // @Description: 通过原创建人转移指标/图表创建人 // @author: Roc // @datetime 2024-03-26 15:11:12 // @param sourceList []int 1:手工数据指标 2:上海钢联数据库 3:ETA指标库 4:ETA预测指标 5:图库 6:ETA表格 // @param oldUserId []int // @param userId int // @param opUserId int // @return err error // @return errMsg string func MoveAllEdbChart(sourceList, oldUserIdList []int, userId, opUserId int) (err error, errMsg string) { adminInfo, err := system.GetSysAdminById(userId) if err != nil { return } // 操作记录 dataPermissionMoveRecordList := make([]*data_manage_permission.DataPermissionMoveRecord, 0) uniqueCode := uuid.NewUUID().Hex32() title := `资产转移` content := `资产转入` sourceStrList := make([]string, 0) var isMoveManual, isMoveMysteelChemical, isMoveEdb, isMovePredictEdb, isMoveChart, isMoveExcel bool var customAnalysisIds []int // 遍历需要转移的模块,1:手工数据指标 2:上海钢联数据库 3:ETA指标库 4:ETA预测指标 5:图库 6:ETA表格,并找出当前需要转移的资产 for _, source := range sourceList { switch source { case 1: //手工数据指标 sourceStrList = append(sourceStrList, "手工指标数据") tmpList, tmpErr := models.GetEdbinfoListByCodeListByUserId(oldUserIdList) if tmpErr != nil { err = tmpErr return } if len(tmpList) > 0 { for _, v := range tmpList { // 操作记录 dataPermissionMoveRecordList = append(dataPermissionMoveRecordList, &data_manage_permission.DataPermissionMoveRecord{ DataPermissionMoveRecordId: 0, Source: int32(source), SubSource: 0, OpUniqueCode: uniqueCode, DataId: v.TradeCode, DataCode: v.TradeCode, DataName: v.SecName, OriginalUserId: int32(v.UserId), OriginalUserName: v.UserName, NewUserId: int32(adminInfo.AdminId), NewUserName: adminInfo.RealName, CreateTime: time.Now(), }) } isMoveManual = true } case 2: //上海钢联数据库 sourceStrList = append(sourceStrList, "上海钢联数据库") tmpList, tmpErr := data_manage.GetMysteelChemicalIndexListByUserId(oldUserIdList) if tmpErr != nil { err = tmpErr return } if len(tmpList) > 0 { for _, v := range tmpList { //if v.SysUserId == newUserId { // errMsg = "新创建人不可和原创建人一致" // err = errors.New(errMsg) // return //} // 操作记录 dataPermissionMoveRecordList = append(dataPermissionMoveRecordList, &data_manage_permission.DataPermissionMoveRecord{ DataPermissionMoveRecordId: 0, Source: int32(source), SubSource: 0, OpUniqueCode: uniqueCode, DataId: fmt.Sprint(v.BaseFromMysteelChemicalIndexId), DataCode: v.IndexCode, DataName: v.IndexName, OriginalUserId: int32(v.SysUserId), OriginalUserName: v.SysUserRealName, NewUserId: int32(adminInfo.AdminId), NewUserName: adminInfo.RealName, CreateTime: time.Now(), }) } isMoveMysteelChemical = true } case 3: //ETA指标库 sourceStrList = append(sourceStrList, "ETA指标库") edbInfoType := 0 tmpList, tmpErr := data_manage.GetEdbInfoListByUserId(oldUserIdList, edbInfoType) if tmpErr != nil { err = tmpErr return } if len(tmpList) > 0 { for _, v := range tmpList { // 操作记录 dataPermissionMoveRecordList = append(dataPermissionMoveRecordList, &data_manage_permission.DataPermissionMoveRecord{ DataPermissionMoveRecordId: 0, Source: int32(source), SubSource: int32(edbInfoType), OpUniqueCode: uniqueCode, DataId: fmt.Sprint(v.EdbInfoId), DataCode: v.EdbCode, DataName: v.EdbName, OriginalUserId: int32(v.SysUserId), OriginalUserName: v.SysUserRealName, NewUserId: int32(adminInfo.AdminId), NewUserName: adminInfo.RealName, CreateTime: time.Now(), }) } isMoveEdb = true } case 4: //ETA预测指标 sourceStrList = append(sourceStrList, "ETA预测指标") edbInfoType := 1 tmpList, tmpErr := data_manage.GetEdbInfoListByUserId(oldUserIdList, edbInfoType) if tmpErr != nil { err = tmpErr return } if len(tmpList) > 0 { for _, v := range tmpList { // 操作记录 dataPermissionMoveRecordList = append(dataPermissionMoveRecordList, &data_manage_permission.DataPermissionMoveRecord{ DataPermissionMoveRecordId: 0, Source: int32(source), SubSource: int32(edbInfoType), OpUniqueCode: uniqueCode, DataId: fmt.Sprint(v.EdbInfoId), DataCode: v.EdbCode, DataName: v.EdbName, OriginalUserId: int32(v.SysUserId), OriginalUserName: v.SysUserRealName, NewUserId: int32(adminInfo.AdminId), NewUserName: adminInfo.RealName, CreateTime: time.Now(), }) } isMovePredictEdb = true } case 5: //ETA图库 sourceStrList = append(sourceStrList, "ETA图库") // 只展示eta图库 subSource := utils.CHART_SOURCE_DEFAULT tmpList, tmpErr := data_manage.GetChartInfoListByUserId(oldUserIdList, subSource) if tmpErr != nil { err = tmpErr return } if len(tmpList) > 0 { for _, v := range tmpList { // 操作记录 dataPermissionMoveRecordList = append(dataPermissionMoveRecordList, &data_manage_permission.DataPermissionMoveRecord{ DataPermissionMoveRecordId: 0, Source: int32(source), SubSource: int32(subSource), OpUniqueCode: uniqueCode, DataId: fmt.Sprint(v.ChartInfoId), DataCode: fmt.Sprint(v.ChartInfoId), DataName: v.ChartName, OriginalUserId: int32(v.SysUserId), OriginalUserName: v.SysUserRealName, NewUserId: int32(adminInfo.AdminId), NewUserName: adminInfo.RealName, CreateTime: time.Now(), }) } isMoveChart = true } case 6: // ETA表格 sourceStrList = append(sourceStrList, "ETA表格") tmpList, tmpErr := excel.GetNoContentExcelListByUserId(oldUserIdList) if tmpErr != nil { err = tmpErr return } if len(tmpList) > 0 { for _, v := range tmpList { // 操作记录 dataPermissionMoveRecordList = append(dataPermissionMoveRecordList, &data_manage_permission.DataPermissionMoveRecord{ DataPermissionMoveRecordId: 0, Source: int32(source), SubSource: int32(v.Source), OpUniqueCode: uniqueCode, DataId: fmt.Sprint(v.ExcelInfoId), DataCode: fmt.Sprint(v.ExcelInfoId), DataName: v.ExcelName, OriginalUserId: int32(v.SysUserId), OriginalUserName: v.SysUserRealName, NewUserId: int32(adminInfo.AdminId), NewUserName: adminInfo.RealName, CreateTime: time.Now(), }) if v.Source == utils.CUSTOM_ANALYSIS_TABLE { customAnalysisIds = append(customAnalysisIds, v.ExcelInfoId) } } isMoveExcel = true } default: } } // 修改创建人 err = data_manage_permission.ModifyDataUserIdByOldUserId(oldUserIdList, adminInfo.AdminId, adminInfo.RealName, isMoveManual, isMoveMysteelChemical, isMoveEdb, isMovePredictEdb, isMoveChart, isMoveExcel) // 转移失败了那么就直接返回了 if err != nil { return } // 由于自定义分析表分类私有化, 如果转移的含自定义分析表, 那么需要将excel转移至未分类下 if len(customAnalysisIds) > 0 { err = MoveCustomAnalysisExcel2DefaultClassify(customAnalysisIds, adminInfo.AdminId, adminInfo.RealName) if err != nil { err = fmt.Errorf("MoveCustomAnalysisExcel2DefaultClassify, err: %v", err) return } } // 添加资产转移的记录 content += fmt.Sprintf("(%s)", strings.Join(sourceStrList, ",")) dataPermissionMessage := &data_manage_permission.DataPermissionMessage{ DataPermissionMessageId: 0, SendUserId: int32(opUserId), ReceiveUserId: int32(adminInfo.AdminId), Content: title, Remark: content, OpType: 2, OpUniqueCode: uniqueCode, IsRead: 0, CreateTime: time.Now(), ModifyTime: time.Now(), } tmpErr := data_manage_permission.AddDataPermissionMoveRecord(dataPermissionMoveRecordList, dataPermissionMessage) // 失败的话,日志记录 if tmpErr != nil { // 序列化结构体为 JSON 数据 recordListJsonBytes, tmpErr2 := json.Marshal(dataPermissionMoveRecordList) if tmpErr2 != nil { utils.FileLog.Error(fmt.Sprintf("添加资产转移的记录失败:%s;list转json失败:;操作记录:%+v;消息记录:%+v", tmpErr.Error(), dataPermissionMoveRecordList, dataPermissionMessage)) return } // 序列化结构体为 JSON 数据 messageJsonBytes, tmpErr2 := json.Marshal(dataPermissionMessage) if tmpErr2 != nil { utils.FileLog.Error(fmt.Sprintf("添加资产转移的记录失败:%s;message转json失败:;操作记录:%+v;消息记录:%+v", tmpErr.Error(), dataPermissionMoveRecordList, dataPermissionMessage)) return } utils.FileLog.Error(fmt.Sprintf("添加资产转移的记录失败:%s;操作记录:%s;消息记录:%s", tmpErr.Error(), string(recordListJsonBytes), string(messageJsonBytes))) } return } // GetMoveEdbChartCount 获取待转移的指标/图表列表 // @param userId 用户 // @param countType 来源 :1:设置资产权限;2:设置数据权限 func GetMoveEdbChartCount(userId, countType int) (sourceMap map[int]int, err error) { sourceMap = make(map[int]int) { // 手工数据指标 var condition string var pars []interface{} if userId > 0 { condition += ` AND a.user_id = ? ` pars = append(pars, userId) } total, tmpErr := models.GetEdbinfoListCount(condition, pars, "", 0) if tmpErr != nil { err = tmpErr return } sourceMap[1] = total } { // 上海钢联数据库 var condition string var pars []interface{} if userId > 0 { condition += ` AND sys_user_id = ? ` pars = append(pars, userId) } total, tmpErr := data_manage.GetMysteelChemicalIndexCount(condition, pars) if tmpErr != nil { err = tmpErr return } sourceMap[2] = total } { // ETA指标库 var condition string var pars []interface{} if userId > 0 { condition += ` AND sys_user_id = ? ` pars = append(pars, userId) } //ETA指标库 condition += ` AND edb_info_type = ? ` pars = append(pars, 0) total, tmpErr := data_manage.GetEdbInfoByConditionCount(condition, pars) if tmpErr != nil { err = tmpErr return } sourceMap[3] = total } { // ETA预测指标库 var condition string var pars []interface{} if userId > 0 { condition += ` AND sys_user_id = ? ` pars = append(pars, userId) } //ETA预测指标 condition += ` AND edb_info_type = ? ` pars = append(pars, 1) total, tmpErr := data_manage.GetEdbInfoByConditionCount(condition, pars) if tmpErr != nil { err = tmpErr return } sourceMap[4] = total } { // 图库 var condition string var pars []interface{} if userId > 0 { condition += ` AND sys_user_id = ? ` pars = append(pars, userId) } total, tmpErr := data_manage.GetChartInfoCountByCondition(condition, pars) if tmpErr != nil { err = tmpErr return } sourceMap[5] = total } { // ETA表格 var condition string var pars []interface{} condition += ` AND is_delete = 0 ` if userId > 0 { condition += ` AND sys_user_id = ? ` pars = append(pars, userId) } switch countType { case 2: condition += ` AND source in (2,3) ` default: condition += ` AND source in (2,3,4) ` } total, tmpErr := excel.GetExcelInfoCountByCondition(condition, pars) if tmpErr != nil { err = tmpErr return } sourceMap[6] = total } return } // afterMoveManualEdb // @Description: 手工数据移动后的操作 // @author: Roc // @datetime 2024-08-01 13:55:59 // @param tmpList []*models.Edbinfo // @param adminInfo *system.Admin // @param opUserId int // @param opUserName string func afterMoveManualEdb(tmpList []*models.Edbinfo, adminInfo *system.Admin, opUserId int, opUserName string) { recordList := make([]*models.EdbinfoOpRecord, 0) for _, tmpManualInfo := range tmpList { remark := fmt.Sprintf("数据资产由%s转移给%s", tmpManualInfo.UserName, adminInfo.RealName) recordList = append(recordList, &models.EdbinfoOpRecord{ TradeCode: tmpManualInfo.TradeCode, Remark: remark, UserId: opUserId, UserName: opUserName, CreateTime: time.Now(), }) } obj := models.EdbinfoOpRecord{} _ = obj.MulCreate(recordList) } // MoveCustomAnalysisExcel2DefaultClassify 移动表格至用户未分类目录下 func MoveCustomAnalysisExcel2DefaultClassify(excelIds []int, adminId int, realName string) (err error) { if len(excelIds) == 0 { return } var moveClassifyId int // 查询默认分类 cond := ` AND excel_classify_name = ? AND sys_user_id = ?` pars := make([]interface{}, 0) pars = append(pars, "未分类", adminId) classify, e := excel.GetExcelClassifyByCondition(cond, pars) if e != nil && e.Error() != utils.ErrNoRow() { err = fmt.Errorf("获取默认分类失败, %v", e) return } if classify != nil { moveClassifyId = classify.ExcelClassifyId } // 新增一个新的分类 if classify == nil { classifyNew := &excel.ExcelClassify{ ExcelClassifyName: "未分类", Source: utils.CUSTOM_ANALYSIS_TABLE, SysUserId: adminId, SysUserRealName: realName, Level: 1, UniqueCode: utils.MD5(utils.EXCEL_DATA_PREFIX + "_" + strconv.FormatInt(time.Now().UnixNano(), 10)), CreateTime: time.Now(), ModifyTime: time.Now(), } if _, e = excel.AddExcelClassify(classifyNew); e != nil { err = fmt.Errorf("新增默认分类失败, %v", e) return } moveClassifyId = classifyNew.ExcelClassifyId } if moveClassifyId <= 0 { err = fmt.Errorf("移动分类ID有误") return } // 更新表格分类ID if e = excel.UpdateExcelInfoClassifyIdByIds(moveClassifyId, excelIds); e != nil { err = fmt.Errorf("移动多表格分类失败, %v", e) return } return }