93 lines
2.9 KiB
C#
93 lines
2.9 KiB
C#
using Dlhis.Basic.Device.Request;
|
|
using Dlhis.Basic.Device.Response;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
using Kurisu.AspNetCore.UnifyResultAndValidation;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
using Kurisu.Core.DataAccess.Dto;
|
|
using Kurisu.SqlSugar.Extensions;
|
|
using Kurisu.SqlSugar.Services;
|
|
using Mapster;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace Dlhis.Basic.Device.Services.Implements;
|
|
|
|
public class DeviceTemplateService : IDeviceTemplateService
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
|
|
public DeviceTemplateService(IDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
|
|
public async Task<Pagination<DeviceTemplateResponse>> GetPageAsync(DeviceTemplatePageRequest request)
|
|
{
|
|
var page = await _dbContext.Queryable<DeviceTemplateEntity>()
|
|
.Select<DeviceTemplateResponse>()
|
|
.ToPageListAsync(request);
|
|
|
|
return page;
|
|
}
|
|
|
|
public async Task SaveTemplateAsync(DeviceTemplateSaveRequest request)
|
|
{
|
|
CheckJsonFormat(request.Template);
|
|
await CheckDuplicateAsync(request);
|
|
|
|
if (!request.Id.HasValue)
|
|
{
|
|
var entity = request.Adapt<DeviceTemplateEntity>();
|
|
entity.Code = Guid.NewGuid();
|
|
await _dbContext.InsertAsync(entity);
|
|
}
|
|
else
|
|
{
|
|
var entity = await _dbContext.Queryable<DeviceTemplateEntity>().Where(x => x.Id == request.Id).FirstAsync();
|
|
await ExistsDeviceUsingTemplateAsync(entity.Code);
|
|
await _dbContext.UpdateAsync(entity);
|
|
}
|
|
}
|
|
|
|
//**********************************************************************************************************/
|
|
|
|
|
|
/// <summary>
|
|
/// 模板被引用,不能修改
|
|
/// </summary>
|
|
/// <param name="templateCode"></param>
|
|
/// <returns></returns>
|
|
private async Task ExistsDeviceUsingTemplateAsync(Guid templateCode)
|
|
{
|
|
var existsUsing = await _dbContext.Queryable<DeviceEntity>().Where(x => x.TemplateCode == templateCode).AnyAsync();
|
|
existsUsing.ThrowIfTrue("存在使用中的设备信息,无法修改");
|
|
}
|
|
|
|
private async Task CheckDuplicateAsync(DeviceTemplateSaveRequest request)
|
|
{
|
|
var duplicate = await _dbContext.Queryable<DeviceTemplateEntity>().Where(x => x.Brand == request.Brand && x.Name == request.Name)
|
|
.WhereIF(request.Id.HasValue, x => x.Id != request.Id)
|
|
.AnyAsync();
|
|
|
|
duplicate.ThrowIfTrue("品牌和型号重复");
|
|
}
|
|
|
|
private void CheckJsonFormat(string template)
|
|
{
|
|
try
|
|
{
|
|
var jobj = JObject.Parse(template);
|
|
(jobj.Count == 0).ThrowIfTrue("模板至少需要一个参数");
|
|
}
|
|
catch (Exception ex) when (ex is JsonReaderException)
|
|
{
|
|
throw new UserFriendlyException("模板格式有误");
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
}
|