fix 设备保存全局校验
This commit is contained in:
@@ -5,10 +5,12 @@ using Dlhis.Entity.Canteen.Entities;
|
||||
using Dlhis.Entity.Canteen.Enums;
|
||||
using Dlhis.Entity.Device.Entities;
|
||||
using Dlhis.Entity.Device.Enums;
|
||||
using Kurisu.AspNetCore.UnifyResultAndValidation;
|
||||
using Dlhis.Sys.Api;
|
||||
using Kurisu.AspNetCore.Utils.Extensions;
|
||||
using Kurisu.Core.CustomClass;
|
||||
using Kurisu.Core.DataAccess.Dto;
|
||||
using Kurisu.Core.DataAccess.Entity;
|
||||
using Kurisu.Core.User.Abstractions;
|
||||
using Kurisu.SqlSugar.Extensions;
|
||||
using Kurisu.SqlSugar.Services;
|
||||
using Mapster;
|
||||
@@ -21,10 +23,14 @@ namespace Dlhis.Basic.Device.Services.Implements;
|
||||
public class DeviceService : IDeviceService
|
||||
{
|
||||
private readonly IDbContext _dbContext;
|
||||
private readonly ICurrentUser _currentUser;
|
||||
private readonly ITenantApi _tenantApi;
|
||||
|
||||
public DeviceService(IDbContext dbContext)
|
||||
public DeviceService(IDbContext dbContext, ICurrentUser currentUser, ITenantApi tenantApi)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_currentUser = currentUser;
|
||||
_tenantApi = tenantApi;
|
||||
}
|
||||
|
||||
public async Task<Pagination<DevicePageResponse>> GetPageAsync(DevicePageRequest request)
|
||||
@@ -299,20 +305,54 @@ public class DeviceService : IDeviceService
|
||||
/// <returns></returns>
|
||||
private async Task CheckUniqueNoExistsAsync(DeviceSaveRequest request)
|
||||
{
|
||||
var exists = await _dbContext.Queryable<DeviceEntity>()
|
||||
.Where(x => x.UniqueNo == request.UniqueNo)
|
||||
.WhereIF(request.Id.HasValue, x => x.Id != request.Id)
|
||||
.AnyAsync();
|
||||
var currentTenantId = _currentUser.GetStringTenantId();
|
||||
_dbContext.Client.QueryFilter.ClearAndBackup<ITenantId>();
|
||||
try
|
||||
{
|
||||
//查询设备是否存在
|
||||
var device = await _dbContext.Queryable<DeviceEntity>()
|
||||
.Where(x => x.UniqueNo == request.UniqueNo)
|
||||
.WhereIF(request.Id.HasValue, x => x.Id != request.Id)
|
||||
.FirstAsync();
|
||||
|
||||
exists.ThrowIfTrue("唯一号已经存在");
|
||||
if (device != null)
|
||||
{
|
||||
(device.TenantId == currentTenantId).ThrowIfTrue("设备已经存在,请检查");
|
||||
|
||||
var factory = device.TenantId;
|
||||
try
|
||||
{
|
||||
var tenantResult = await _tenantApi.GetTenantByCodeAsync(device.TenantId);
|
||||
factory = tenantResult?.Data?.Name ?? string.Empty;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
true.ThrowIfTrue($"设备已添加{factory}厂区");
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_dbContext.Client.QueryFilter.Restore();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task<bool> CheckExistsAsync(string deviceUniqueNo)
|
||||
{
|
||||
return await _dbContext.Queryable<DeviceEntity>()
|
||||
_dbContext.Client.QueryFilter.ClearAndBackup<ITenantId>();
|
||||
try
|
||||
{
|
||||
return await _dbContext.Queryable<DeviceEntity>()
|
||||
.Where(x => x.UniqueNo == deviceUniqueNo && x.Enable)
|
||||
.AnyAsync();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_dbContext.Client.QueryFilter.Restore();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user