增加变量批量删除功能
This commit is contained in:
@@ -14,4 +14,19 @@ namespace YLErp.Modules.RiskEngine.Dto
|
||||
/// <summary>错误信息</summary>
|
||||
public string ErrorMessage { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量删除变量结果
|
||||
/// </summary>
|
||||
public class BatchDeleteVariablesResult : BatchOperationResult
|
||||
{
|
||||
/// <summary>成功删除的变量 ID</summary>
|
||||
public List<long> DeletedIds { get; set; }
|
||||
/// <summary>不存在的变量 ID</summary>
|
||||
public List<long> MissingIds { get; set; }
|
||||
/// <summary>因被生效规则引用而跳过的变量 ID</summary>
|
||||
public List<long> BlockedIds { get; set; }
|
||||
/// <summary>被跳过变量的原因,Key 为变量 ID</summary>
|
||||
public Dictionary<long, string> BlockedReasons { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2383,6 +2383,58 @@ namespace YLErp.Modules.RiskEngine
|
||||
InvalidateVariableCache();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量删除变量(不存在或被生效规则引用的变量将被跳过)
|
||||
/// </summary>
|
||||
public BatchDeleteVariablesResult BatchDeleteVariables(List<long> variableIds)
|
||||
{
|
||||
if (variableIds == null || variableIds.Count == 0)
|
||||
throw new ServiceException("变量ID列表不能为空");
|
||||
|
||||
var distinctIds = variableIds.Distinct().ToList();
|
||||
var variables = DbContext.glms_risk_variable
|
||||
.Where(v => distinctIds.Contains(v.id))
|
||||
.ToList();
|
||||
var foundIds = variables.Select(v => (long)v.id).ToList();
|
||||
var missingIds = distinctIds.Except(foundIds).ToList();
|
||||
var blockedIds = new List<long>();
|
||||
var blockedReasons = new Dictionary<long, string>();
|
||||
var deletedIds = new List<long>();
|
||||
|
||||
foreach (var variable in variables)
|
||||
{
|
||||
var activeRefCount = GetRulesByVariableId(variable.id)
|
||||
.Count(r => r.Status == RiskRuleStatus.Active);
|
||||
if (activeRefCount > 0)
|
||||
{
|
||||
blockedIds.Add(variable.id);
|
||||
blockedReasons[variable.id] = $"该变量被 {activeRefCount} 个生效中的规则引用,无法删除";
|
||||
continue;
|
||||
}
|
||||
|
||||
DbContext.glms_risk_variable.Remove(variable);
|
||||
WriteAuditLog("VAR_BATCH_DELETE", "VARIABLE", variable.id, variable.VariableName, $"批量删除变量:{variable.VariableName}");
|
||||
deletedIds.Add(variable.id);
|
||||
}
|
||||
|
||||
if (deletedIds.Count > 0)
|
||||
{
|
||||
DbContext.SaveChanges();
|
||||
InvalidateVariableCache();
|
||||
}
|
||||
|
||||
return new BatchDeleteVariablesResult
|
||||
{
|
||||
Success = true,
|
||||
TotalCount = distinctIds.Count,
|
||||
SuccessCount = deletedIds.Count,
|
||||
DeletedIds = deletedIds,
|
||||
MissingIds = missingIds,
|
||||
BlockedIds = blockedIds,
|
||||
BlockedReasons = blockedReasons
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有变量列表(供下拉选择)
|
||||
/// </summary>
|
||||
|
||||
@@ -685,6 +685,28 @@ namespace YLErp.Web.Controllers
|
||||
}
|
||||
}
|
||||
|
||||
[HttpDelete("risk-variables/batch")]
|
||||
[MyAuthorize("风险控制-风控变量删除")]
|
||||
/// <summary>批量删除变量</summary>
|
||||
public async Task<JsonResult> BatchDeleteRiskVariables([FromBody] BatchIdsReq req)
|
||||
{
|
||||
try
|
||||
{
|
||||
var service = GetRiskRuleService();
|
||||
var result = await Task.Run(() => service.BatchDeleteVariables(req?.Ids));
|
||||
return Json(new { success = true, data = result });
|
||||
}
|
||||
catch (ServiceException ex)
|
||||
{
|
||||
return Json(new { success = false, message = ex.Message });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error(ex, "批量删除变量");
|
||||
return Json(new { success = false, message = "系统异常,请联系管理员" });
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Trade Types
|
||||
|
||||
Reference in New Issue
Block a user