Files
jinko.yikatong/Src/Dlhis.Basic/Device/Services/Implements/DeviceTimePeriodService.cs
2024-07-15 11:00:30 +08:00

76 lines
3.0 KiB
C#

using Dlhis.Basic.Device.Request;
using Dlhis.Basic.Device.Response;
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Device.Entities;
using Dlhis.Entity.Device.Enums;
using Kurisu.AspNetCore.DataAccess.Dto;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using Kurisu.AspNetCore.Utils.Extensions;
using Mapster;
namespace Dlhis.Basic.Device.Services.Implements;
public class DeviceTimePeriodService : IDeviceTimePeriodService
{
private readonly IDbContext _dbContext;
public DeviceTimePeriodService(IDbContext dbContext)
{
_dbContext = dbContext;
}
/// <inheritdoc />
public async Task<Pagination<DeviceTimePeriodPageResponse>> GetPageAsync(DeviceTimePeriodPageRequest request)
{
var page = await _dbContext.Queryable<DeviceTimePeriodEntity>()
.Where(x => x.AssignType == request.AssignType)
.Select<DeviceTimePeriodPageResponse>()
.ToPageListAsync(request);
var values = page.Data.Select(x => x.Value);
var data = request.AssignType == DeviceTimePeriodAssignEnumType.Device
? await _dbContext.Queryable<DeviceEntity>().Where(x => values.Contains(x.Code)).Select(x => new { x.Code, Name = x.DeployPlace }).ToListAsync()
: await _dbContext.Queryable<DeviceTemplateEntity>().Where(x => values.Contains(x.Code)).Select(x => new { x.Code, x.Name }).ToListAsync();
page.Data.ForEach(item => { item.Name = data.FirstOrDefault(x => x.Code == item.Value)?.Name; });
return page;
}
/// <inheritdoc />
public async Task SaveAsync(DeviceTimePeriodSaveRequest request)
{
(await _dbContext.Queryable<BasicTypeEntity>().Where(x => x.Type == "timeperiod").AnyAsync(x => x.Code == request.CardType.ToString()))
.ThrowIfFalse($"卡类型{request.CardType}不支持");
var existsData = request.AssignType == DeviceTimePeriodAssignEnumType.Device
? await _dbContext.Queryable<DeviceEntity>().Where(x => x.Code == request.Value).AnyAsync()
: await _dbContext.Queryable<DeviceTemplateEntity>().Where(x => x.Code == request.Value).AnyAsync();
existsData.ThrowIfFalse("数据不匹配");
(await _dbContext.Queryable<DeviceTimePeriodEntity>()
.Where(x => x.CardType == request.CardType)
.Where(x => x.AssignType == request.AssignType)
.Where(x => x.Value == request.Value)
.AnyAsync()).ThrowIfTrue("请勿重复添加");
if (!request.Id.HasValue)
{
var entity = request.Adapt<DeviceTimePeriodEntity>();
entity.CardType = request.CardType;
entity.AssignType = request.AssignType;
entity.Value = request.Value;
await _dbContext.InsertAsync(entity);
}
else
{
var entity = await _dbContext.Queryable<DeviceTimePeriodEntity>().Where(x => x.Id == request.Id).SingleAsync();
request.Adapt(entity);
await _dbContext.UpdateAsync(entity);
}
}
}