Selaa lähdekoodia

fix:指标共享时,添加逻辑校验

Roc 4 kuukautta sitten
vanhempi
commit
9dfeb22e1e

+ 182 - 11
controllers/data_manage/edb_info_share.go

@@ -17,6 +17,8 @@ import (
 	"strings"
 )
 
+// TODO 取消共享的时候,需要清除对应用户的收藏状态
+
 // EdbInfoShareController 数据管理
 type EdbInfoShareController struct {
 	controllers.BaseAuthController
@@ -131,6 +133,54 @@ func (c *EdbInfoShareController) Save() {
 		return
 	}
 
+	// 如果指标未公开,则判断是否被其他用户使用
+	if edbItem.PublicStatus != utils.DataPublicSuccess {
+		checkUserIdList := []int{sysUser.AdminId}
+		if len(req.UserIdList) > 0 {
+			checkUserIdList = append(checkUserIdList, req.UserIdList...)
+		}
+		noReferenced, otherUserIdList, err, tips := data.CheckEdbReferencedByOthers(edbItem.EdbInfoType, []int{edbItem.EdbInfoId}, checkUserIdList)
+		if err != nil {
+			br.Msg = "保存失败"
+			if tips != "" {
+				br.Msg = tips
+			}
+			br.ErrMsg = "设置指标共享失败,查找指标的关联用户失败,Err:" + err.Error()
+			return
+		}
+
+		if !noReferenced {
+			userNameStr := ``
+			if len(otherUserIdList) > 0 {
+				userNameList := make([]string, 0)
+				userList, err := system.GetAdminListByIdList(otherUserIdList)
+				if err != nil {
+					br.Msg = "保存失败"
+					if tips != "" {
+						br.Msg = tips
+					}
+					br.ErrMsg = "设置指标共享失败,查找用户信息失败,Err:" + err.Error()
+					return
+				}
+				for _, userInfo := range userList {
+					userNameList = append(userNameList, userInfo.RealName)
+				}
+				userNameStr = strings.Join(userNameList, ",")
+			}
+			msg := `指标被其他用户使用,无法设置共享`
+			if userNameStr != "" {
+				msg = fmt.Sprintf(`指标被用户%s使用,无法设置共享`, userNameStr)
+			}
+			if tips != "" {
+				msg = fmt.Sprintf(`%s,用户为:%s,无法设置共享`, tips, userNameStr)
+			}
+			br.Msg = msg
+			br.ErrMsg = "指标被其他用户共享,无法设置共享,EdbInfoId:" + strconv.Itoa(req.EdbInfoId)
+			br.IsSendEmail = false
+			return
+		}
+	}
+
 	obj := data_manage.EdbInfoShare{}
 	err = obj.SaveEdbInfoShare([]int{req.EdbInfoId}, req.UserIdList, req.ShareType)
 	if err != nil {
@@ -602,26 +652,147 @@ func (c *EdbInfoShareController) BatchSave() {
 	}
 
 	num := len(selectEdbInfoIdList)
+	if num <= 0 {
+		br.Msg = `请选择指标`
+		br.IsSendEmail = false
+		return
+	}
 	if num > 30 {
 		br.Msg = `指标数量不能超过30个`
 		br.IsSendEmail = false
 		return
 	}
 
-	// 设置
-	if num > 0 {
-		obj := data_manage.EdbInfoShare{}
-		err = obj.SaveEdbInfoShare(selectEdbInfoIdList, req.UserIdList, req.ShareType)
-		if err != nil {
-			br.Msg = `保存失败`
-			br.ErrMsg = `保存失败:` + err.Error()
-			return
+	edbInfoList, err := data_manage.GetEdbInfoListByEdbInfoId(selectEdbInfoIdList)
+	if err != nil {
+		br.Msg = "设置失败!"
+		br.ErrMsg = "设置失败,Err:" + err.Error()
+		return
+	}
+
+	{
+		checkBaseEdbInfoIdList := make([]int, 0)
+		checkCalculateEdbInfoIdList := make([]int, 0)
+
+		for _, edbItem := range edbInfoList {
+			// 设置公开的可以忽略
+			if edbItem.PublicStatus == utils.DataPublicSuccess {
+				continue
+			}
+
+			if edbItem.EdbInfoType == utils.EDB_INFO_TYPE {
+				checkBaseEdbInfoIdList = append(checkBaseEdbInfoIdList, edbItem.EdbInfoId)
+			} else if edbItem.EdbInfoType == utils.PREDICT_EDB_INFO_TYPE {
+				checkCalculateEdbInfoIdList = append(checkCalculateEdbInfoIdList, edbItem.EdbInfoId)
+			}
 		}
 
-		// 更新es
-		for _, edbInfoId := range selectEdbInfoIdList {
-			data.AddOrEditEdbInfoToEs(edbInfoId)
+		checkUserIdList := []int{sysUser.AdminId}
+		if len(req.UserIdList) > 0 {
+			checkUserIdList = append(checkUserIdList, req.UserIdList...)
 		}
+
+		// 普通指标
+		{
+			noReferenced, otherUserIdList, err, tips := data.CheckEdbReferencedByOthers(utils.EDB_INFO_TYPE, checkBaseEdbInfoIdList, checkUserIdList)
+			if err != nil {
+				br.Msg = "保存失败"
+				if tips != "" {
+					br.Msg = tips
+				}
+				br.ErrMsg = "设置指标共享失败,查找指标的关联用户失败,Err:" + err.Error()
+				return
+			}
+
+			if !noReferenced {
+				userNameStr := ``
+				if len(otherUserIdList) > 0 {
+					userNameList := make([]string, 0)
+					userList, err := system.GetAdminListByIdList(otherUserIdList)
+					if err != nil {
+						br.Msg = "保存失败"
+						if tips != "" {
+							br.Msg = tips
+						}
+						br.ErrMsg = "设置指标共享失败,查找用户信息失败,Err:" + err.Error()
+						return
+					}
+					for _, userInfo := range userList {
+						userNameList = append(userNameList, userInfo.RealName)
+					}
+					userNameStr = strings.Join(userNameList, ",")
+				}
+				msg := `指标被其他用户使用,无法设置共享`
+				if userNameStr != "" {
+					msg = fmt.Sprintf(`指标被用户%s使用,无法设置共享`, userNameStr)
+				}
+				if tips != "" {
+					msg = fmt.Sprintf(`%s,用户为:%s,无法设置共享`, tips, userNameStr)
+				}
+				br.Msg = msg
+				br.ErrMsg = "指标被其他用户共享,无法设置共享"
+				br.IsSendEmail = false
+				return
+			}
+		}
+
+		// 预测指标
+		{
+			noReferenced, otherUserIdList, err, tips := data.CheckEdbReferencedByOthers(utils.PREDICT_EDB_INFO_TYPE, checkCalculateEdbInfoIdList, checkUserIdList)
+			if err != nil {
+				br.Msg = "保存失败"
+				if tips != "" {
+					br.Msg = tips
+				}
+				br.ErrMsg = "设置指标共享失败,查找指标的关联用户失败,Err:" + err.Error()
+				return
+			}
+
+			if !noReferenced {
+				userNameStr := ``
+				if len(otherUserIdList) > 0 {
+					userNameList := make([]string, 0)
+					userList, err := system.GetAdminListByIdList(otherUserIdList)
+					if err != nil {
+						br.Msg = "保存失败"
+						if tips != "" {
+							br.Msg = tips
+						}
+						br.ErrMsg = "设置指标共享失败,查找用户信息失败,Err:" + err.Error()
+						return
+					}
+					for _, userInfo := range userList {
+						userNameList = append(userNameList, userInfo.RealName)
+					}
+					userNameStr = strings.Join(userNameList, ",")
+				}
+				msg := `指标被其他用户使用,无法设置共享`
+				if userNameStr != "" {
+					msg = fmt.Sprintf(`指标被用户%s使用,无法设置共享`, userNameStr)
+				}
+				if tips != "" {
+					msg = fmt.Sprintf(`%s,用户为:%s,无法设置共享`, tips, userNameStr)
+				}
+				br.Msg = msg
+				br.ErrMsg = "指标被其他用户共享,无法设置共享"
+				br.IsSendEmail = false
+				return
+			}
+		}
+	}
+
+	// 设置
+	obj := data_manage.EdbInfoShare{}
+	err = obj.SaveEdbInfoShare(selectEdbInfoIdList, req.UserIdList, req.ShareType)
+	if err != nil {
+		br.Msg = `保存失败`
+		br.ErrMsg = `保存失败:` + err.Error()
+		return
+	}
+
+	// 更新es
+	for _, edbInfoId := range selectEdbInfoIdList {
+		data.AddOrEditEdbInfoToEs(edbInfoId)
 	}
 
 	br.Ret = 200

+ 48 - 0
models/data_manage/chart_edb_mapping.go

@@ -1,6 +1,7 @@
 package data_manage
 
 import (
+	sql2 "database/sql"
 	"eta_gn/eta_api/global"
 	"eta_gn/eta_api/utils"
 	"fmt"
@@ -483,3 +484,50 @@ func GetRelationEdbInfoListMappingByCondition(condition string, pars []interface
 
 	return
 }
+
+// GetCountRelationChartEdbInfoListMappingByCondition
+// @Description: 根据条件获取关联图数量
+// @author: Roc
+// @datetime 2024-12-26 10:13:47
+// @param condition string
+// @param pars []interface{}
+// @return total int
+// @return err error
+func GetCountRelationChartEdbInfoListMappingByCondition(condition string, pars []interface{}) (total int, err error) {
+	o := global.DmSQL["data"]
+	sql := ` SELECT COUNT(1) total FROM chart_edb_mapping AS a 
+	JOIN chart_info AS b on a.chart_info_id = b.chart_info_id WHERE 1=1 `
+	if condition != "" {
+		sql += condition
+	}
+	var totalNull sql2.NullInt64
+	err = o.Raw(sql, pars...).Scan(&totalNull).Error
+	if err != nil {
+		return
+	}
+	total = int(totalNull.Int64)
+
+	return
+}
+
+// GetUserIdListRelationChartEdbInfoListMappingByCondition
+// @Description: 根据条件获取关联图的用户id列表
+// @author: Roc
+// @datetime 2024-12-26 10:13:47
+// @param condition string
+// @param pars []interface{}
+// @return total int
+// @return err error
+func GetUserIdListRelationChartEdbInfoListMappingByCondition(condition string, pars []interface{}) (userIdList []int, err error) {
+	o := global.DmSQL["data"]
+	sql := ` SELECT b.sys_user_id FROM chart_edb_mapping AS a 
+	JOIN chart_info AS b on a.chart_info_id = b.chart_info_id WHERE 1=1 `
+	if condition != "" {
+		sql += condition
+	}
+	sql += ` GROUP BY b.sys_user_id `
+
+	err = o.Raw(sql, pars...).Scan(&userIdList).Error
+
+	return
+}

+ 48 - 0
models/data_manage/edb_info_calculate_mapping.go

@@ -1,6 +1,7 @@
 package data_manage
 
 import (
+	sql2 "database/sql"
 	"eta_gn/eta_api/global"
 	"eta_gn/eta_api/utils"
 	"time"
@@ -110,3 +111,50 @@ func GetEdbInfoCalculateMappingListByEdbInfoIds(edbInfoIds []int) (items []*EdbI
 
 	return
 }
+
+// GetCountRelationEdbInfoListMappingByCondition
+// @Description: 根据条件获取关联指标数量
+// @author: Roc
+// @datetime 2024-12-26 10:37:48
+// @param condition string
+// @param pars []interface{}
+// @return total int
+// @return err error
+func GetCountRelationEdbInfoListMappingByCondition(condition string, pars []interface{}) (total int, err error) {
+	o := global.DmSQL["data"]
+	sql := ` SELECT COUNT(1) total FROM edb_info_calculate_mapping AS a 
+	JOIN edb_info AS b on a.edb_info_id = b.edb_info_id WHERE 1=1 `
+	if condition != "" {
+		sql += condition
+	}
+	var totalNull sql2.NullInt64
+	err = o.Raw(sql, pars...).Scan(&totalNull).Error
+	if err != nil {
+		return
+	}
+	total = int(totalNull.Int64)
+
+	return
+}
+
+// GetCountRelationEdbInfoListMappingByCondition
+// @Description: 根据条件获取关联指标的用户id列表
+// @author: Roc
+// @datetime 2024-12-26 10:37:48
+// @param condition string
+// @param pars []interface{}
+// @return total int
+// @return err error
+func GetUserIdListRelationEdbInfoListMappingByCondition(condition string, pars []interface{}) (userIdList []int, err error) {
+	o := global.DmSQL["data"]
+	sql := ` SELECT b.sys_user_id FROM edb_info_calculate_mapping AS a 
+	JOIN edb_info AS b on a.edb_info_id = b.edb_info_id WHERE 1=1 `
+	if condition != "" {
+		sql += condition
+	}
+	sql += ` GROUP BY b.sys_user_id `
+
+	err = o.Raw(sql, pars...).Scan(&userIdList).Error
+
+	return
+}

+ 1 - 1
models/data_manage/edb_info_share.go

@@ -197,7 +197,7 @@ func (m EdbInfoShare) GetAllEditSharedEdbInfoIdListByReceivedUserId(sysUserId in
 	}
 	sql += ` GROUP BY a.edb_info_id `
 
-	err = global.DmSQL["data"].Raw(sql, sysUserId, utils.ShareTypeEdit).Scan(&list).Error
+	err = global.DmSQL["data"].Raw(sql, pars...).Scan(&list).Error
 	return
 }
 

+ 49 - 0
models/data_manage/excel/excel_edb_mapping.go

@@ -1,6 +1,7 @@
 package excel
 
 import (
+	sql2 "database/sql"
 	"eta_gn/eta_api/global"
 	"eta_gn/eta_api/utils"
 	"fmt"
@@ -152,3 +153,51 @@ func GetExcelEdbMappingItemByExcelInfoIdOrKeyword(excelInfoId int, keyword strin
 	err = global.DmSQL["data"].Raw(sql, pars...).Find(&items).Error
 	return
 }
+
+// GetCountRelationExcelEdbInfoListMappingByCondition
+// @Description: 根据条件获取表格关联指标的数量
+// @author: Roc
+// @datetime 2024-12-26 10:37:48
+// @param condition string
+// @param pars []interface{}
+// @return total int
+// @return err error
+func GetCountRelationExcelEdbInfoListMappingByCondition(condition string, pars []interface{}) (total int, err error) {
+	o := global.DmSQL["data"]
+	sql := ` SELECT COUNT(1) total FROM excel_edb_mapping AS a 
+	JOIN excel_info AS b on a.excel_info_id = b.excel_info_id WHERE 1=1 `
+	if condition != "" {
+		sql += condition
+	}
+	var totalNull sql2.NullInt64
+	err = o.Raw(sql, pars...).Scan(&totalNull).Error
+	if err != nil {
+		return
+	}
+
+	total = int(totalNull.Int64)
+
+	return
+}
+
+// GetUserIdListRelationExcelEdbInfoListMappingByCondition
+// @Description: 根据条件获取表格关联指标的用户id列表
+// @author: Roc
+// @datetime 2024-12-26 10:37:48
+// @param condition string
+// @param pars []interface{}
+// @return total int
+// @return err error
+func GetUserIdListRelationExcelEdbInfoListMappingByCondition(condition string, pars []interface{}) (userIdList []int, err error) {
+	o := global.DmSQL["data"]
+	sql := ` SELECT b.sys_user_id FROM excel_edb_mapping AS a 
+	JOIN excel_info AS b on a.excel_info_id = b.excel_info_id WHERE 1=1 `
+	if condition != "" {
+		sql += condition
+	}
+	sql += ` GROUP BY b.sys_user_id `
+
+	err = o.Raw(sql, pars...).Scan(&userIdList).Error
+
+	return
+}

+ 48 - 0
models/data_manage/predict_edb_conf.go

@@ -1,6 +1,7 @@
 package data_manage
 
 import (
+	sql2 "database/sql"
 	"eta_gn/eta_api/global"
 	"eta_gn/eta_api/utils"
 	"time"
@@ -169,3 +170,50 @@ func GetGroupPredictEdbInfoIdListBySourceEdbInfoId(sourceEdbInfoId int) (items [
 	err = global.DmSQL["data"].Raw(sql, sourceEdbInfoId).Scan(&items).Error
 	return
 }
+
+// GetCountRelationPredictEdbInfoListMappingByCondition
+// @Description: 根据条件获取关联预测指标数量
+// @author: Roc
+// @datetime 2024-12-26 10:13:47
+// @param condition string
+// @param pars []interface{}
+// @return total int
+// @return err error
+func GetCountRelationPredictEdbInfoListMappingByCondition(condition string, pars []interface{}) (total int, err error) {
+	o := global.DmSQL["data"]
+	sql := ` SELECT COUNT(1) total FROM predict_edb_conf AS a 
+	JOIN edb_info AS b on a.predict_edb_info_id = b.edb_info_id WHERE 1=1 `
+	if condition != "" {
+		sql += condition
+	}
+	var totalNull sql2.NullInt64
+	err = o.Raw(sql, pars...).Scan(&totalNull).Error
+	if err != nil {
+		return
+	}
+	total = int(totalNull.Int64)
+
+	return
+}
+
+// GetUserIdListRelationPredictEdbInfoListMappingByCondition
+// @Description: 根据条件获取关联预测指标的用户id列表
+// @author: Roc
+// @datetime 2024-12-26 10:13:47
+// @param condition string
+// @param pars []interface{}
+// @return total int
+// @return err error
+func GetUserIdListRelationPredictEdbInfoListMappingByCondition(condition string, pars []interface{}) (userIdList []int, err error) {
+	o := global.DmSQL["data"]
+	sql := ` SELECT b.sys_user_id FROM predict_edb_conf AS a 
+	JOIN edb_info AS b on a.predict_edb_info_id = b.edb_info_id WHERE 1=1 `
+	if condition != "" {
+		sql += condition
+	}
+	sql += ` GROUP BY b.sys_user_id `
+
+	err = o.Raw(sql, pars...).Scan(&userIdList).Error
+
+	return
+}

+ 6 - 0
services/data/data_approve/approve.go

@@ -985,6 +985,12 @@ func DataApproveCancel(DataApproveId, adminId int, adminName string) (msg string
 		return
 	}
 
+	if approveItem.State != DataApproveStatePass {
+		msg = "审批单已通过,不允许撤回"
+		err = fmt.Errorf("审批单已通过,不允许撤回")
+		return
+	}
+
 	// 撤销审批
 	e = cancelDataApprove(approveItem, approveItem.DataType, approveItem.DataApproveId, adminId, adminName)
 	if e != nil {

+ 74 - 0
services/data/edb_info.go

@@ -2052,3 +2052,77 @@ func GetIsSupplierStop(source int, edbCode string) (isSupplierStop int) {
 }
 
 //IsSupplierStop  int    `description:"是否供应商停更:1:停更,0:未停更"`
+
+func CheckEdbReferencedByOthers(edbInfoType int, edbInfoIdList, belongUserIdList []int) (noReferenced bool, otherUserIdList []int, err error, tips string) {
+	otherUserIdList = []int{}
+	//判断指标是否用于作图,如果用于作图,则不可删除
+	{
+		var condition string
+		var pars []interface{}
+		// 关联指标
+		condition += ` AND a.edb_info_id in (?) AND b.sys_user_id not in (?) `
+		pars = append(pars, edbInfoIdList, belongUserIdList)
+		otherUserIdList, err = data_manage.GetUserIdListRelationChartEdbInfoListMappingByCondition(condition, pars)
+		if err != nil {
+			err = errors.New("判断指标是否被用于作图失败,Err:" + err.Error())
+			return
+		}
+		if len(otherUserIdList) > 0 {
+			tips = "当前指标已被其他用户用作画图"
+			return
+		}
+	}
+
+	//如果是普通指标,那么还需要判断是否被预测指标作为源指标
+	if edbInfoType == 0 {
+		condition := ` AND a.source_edb_info_id  in (?) AND b.sys_user_id not in (?) `
+		pars := []interface{}{edbInfoIdList, belongUserIdList}
+		otherUserIdList, err = data_manage.GetUserIdListRelationPredictEdbInfoListMappingByCondition(condition, pars)
+		if err != nil {
+			err = errors.New("判断指标是否被用于预测指标失败,Err:" + err.Error())
+			return
+		}
+		if len(otherUserIdList) > 0 {
+			tips = "当前指标已被其他用户用作预测指标"
+			return
+		}
+	}
+
+	//判断指标是否用作其他指标的计算
+	{
+		condition := ` AND a.from_edb_info_id in (?) AND b.sys_user_id  not in (?) `
+		pars := []interface{}{edbInfoIdList, belongUserIdList}
+		otherUserIdList, err = data_manage.GetUserIdListRelationEdbInfoListMappingByCondition(condition, pars)
+		if err != nil {
+			err = errors.New("判断指标是否被用于计算失败,GetCountRelationEdbInfoListMappingByCondition Err:" + err.Error())
+			return
+		}
+		if len(otherUserIdList) > 0 {
+			tips = "当前指标已被其他用户用作指标运算"
+			return
+		}
+	}
+
+	// 判断指标是否用作表格引用
+	{
+		condition := ` AND a.edb_info_id in (?) AND b.sys_user_id not in (?) `
+		pars := []interface{}{edbInfoIdList, belongUserIdList}
+		otherUserIdList, err = excel.GetUserIdListRelationExcelEdbInfoListMappingByCondition(condition, pars)
+		if err != nil {
+			err = errors.New("判断指标是否用作表格引用,GetCountRelationExcelEdbInfoListMappingByCondition Err:" + err.Error())
+			return
+		}
+		if len(otherUserIdList) > 0 {
+			tips = "当前指标已被其他用户用作表格引用"
+			return
+		}
+	}
+	// 判断指标是否用作跨品种图表使用
+	{
+		// todo 跨品种分析图表就是可以使用其他人配置的指标,理论上是不需要去校验是否被他人引用的,后面如果有涉及到这块的校验再去进行补充
+
+	}
+
+	noReferenced = true
+	return
+}