55 lines
1.7 KiB
C#
55 lines
1.7 KiB
C#
using Dlhis.Basic.Common.Holiday.Requests;
|
|
using Dlhis.Basic.Common.Holiday.Responses;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
using Mapster;
|
|
|
|
namespace Dlhis.Basic.Common.Holiday.Services.Implements;
|
|
|
|
public class HolidayService : IHolidayService
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
|
|
public HolidayService(IDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
public async Task<List<HolidayResponse>> GetAllHolidaysAsync()
|
|
{
|
|
return await _dbContext.Queryable<HolidayEntity>()
|
|
.Select<HolidayResponse>()
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task SaveAsync(HolidayRequest req)
|
|
{
|
|
var exists = await _dbContext.Queryable<HolidayEntity>()
|
|
.WhereIF(req.Id.HasValue, x => x.Id != req.Id)
|
|
.Where(x => x.Year == req.Year)
|
|
.Where(x => x.Start < req.End && req.Start < x.End)
|
|
.Where(x => x.Type != req.Type)
|
|
.AnyAsync();
|
|
|
|
exists.ThrowIfTrue("节假日时间冲突,请检查开始和结束时间,或节假日类型是否正确");
|
|
|
|
if (!req.Id.HasValue)
|
|
{
|
|
await _dbContext.InsertAsync(req.Adapt<HolidayEntity>());
|
|
}
|
|
else
|
|
{
|
|
var entity = await _dbContext.Queryable<HolidayEntity>().SingleAsync(x => x.Id == req.Id);
|
|
entity.ThrowIfNull("节假日数据不存在");
|
|
|
|
req.Adapt(entity);
|
|
await _dbContext.UpdateAsync(entity);
|
|
}
|
|
}
|
|
|
|
public async Task DeleteAsync(long id)
|
|
{
|
|
await _dbContext.Deleteable<HolidayEntity>().Where(x => x.Id == id).ExecuteCommandAsync();
|
|
}
|
|
} |