refactor 优化命名和结构,注释
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
using Dlhis.Basic.WorkGrids.Request;
|
||||
using Dlhis.Entity.Basic.Entity;
|
||||
using Kurisu.AspNetCore.Authentication.Abstractions;
|
||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
||||
using Kurisu.AspNetCore.Utils.Extensions;
|
||||
|
||||
namespace Dlhis.Basic.WorkGrids.Validations.Implements
|
||||
{
|
||||
public class WorkGridValidateService : IWorkGridValidateService
|
||||
{
|
||||
private readonly ICurrentUser _currentUser;
|
||||
private readonly IDbContext _dbContext;
|
||||
|
||||
public WorkGridValidateService(ICurrentUser currentUser, IDbContext dbContext)
|
||||
{
|
||||
_currentUser = currentUser;
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证保存请求
|
||||
/// </summary>
|
||||
/// <param name="requests"></param>
|
||||
public async Task ValidateSaveAsync(List<WorkGridSaveRequest> requests)
|
||||
{
|
||||
var duplicateUserCodes = requests.GroupBy(x => x.LeaderUserCode)
|
||||
.Where(g => g.Count() > 1)
|
||||
.Select(g => g.Key)
|
||||
.ToList();
|
||||
duplicateUserCodes.ThrowIfExists($"请求中存在重复工号:{duplicateUserCodes.Join(",")}");
|
||||
|
||||
var duplicateNames = requests.GroupBy(x => x.Name)
|
||||
.Where(g => g.Count() > 1)
|
||||
.Select(g => g.Key)
|
||||
.ToList();
|
||||
duplicateNames.ThrowIfExists($"请求中存在重复网格名称:{duplicateNames.Join(",")}");
|
||||
|
||||
var factory = _currentUser.GetStringTenantId();
|
||||
|
||||
var leaderUserCodes = requests.Select(x => x.LeaderUserCode).Distinct().ToList();
|
||||
var userCodeWithFactories = await _dbContext.Queryable<GainfoEntity>()
|
||||
.Where(x => leaderUserCodes.Contains(x.Code))
|
||||
.Select(x => new { x.Code, x.Factory })
|
||||
.ToListAsync();
|
||||
|
||||
//检查工号是否存在
|
||||
var existsUserCodes = userCodeWithFactories.Select(x => x.Code).ToList();
|
||||
var notExistsUserCodes = leaderUserCodes.Except(existsUserCodes).ToList();
|
||||
notExistsUserCodes.ThrowIfExists($"工号:{notExistsUserCodes.Join(",")}不存在");
|
||||
|
||||
//检查负责人工号是否存在
|
||||
var excludeCodes = userCodeWithFactories
|
||||
.Where(x => x.Factory != factory)
|
||||
.Select(x => x.Code)
|
||||
.ToList();
|
||||
excludeCodes.ThrowIfExists($"工号:{excludeCodes.Join(",")}不属于当前厂区");
|
||||
|
||||
//检查网格名称是否重复
|
||||
var where = LinqExpressionExtensions.False<WorkGridEntity>();
|
||||
foreach (var req in requests)
|
||||
{
|
||||
where = req.Id.HasValue
|
||||
? where.Or(x => x.Name == req.Name && x.Id != req.Id.Value)
|
||||
: where.Or(x => x.Name == req.Name);
|
||||
}
|
||||
|
||||
var existsNames = await _dbContext.Queryable<WorkGridEntity>().Where(where).Select(x => x.Name).ToListAsync();
|
||||
if (existsNames.Any())
|
||||
{
|
||||
true.ThrowIfTrue($"网格区域名称[{existsNames.Join(",")}]重复");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user