using Dlhis.Basic.BusinessShift.Request; using Dlhis.Basic.BusinessShift.Response; using Dlhis.Entity.Canteen.Entities; using Kurisu.AspNetCore.Utils.Extensions; using Kurisu.Core.CustomClass; using Kurisu.Core.DataAccess.Dto; using Kurisu.SqlSugar.Extensions; using Kurisu.SqlSugar.Services; using Mapster; namespace Dlhis.Basic.BusinessShift.Services.Implements; public class BusinessPlaceShiftService : IBusinessPlaceShiftService { private readonly IDbContext _dbContext; public BusinessPlaceShiftService(IDbContext dbContext) { _dbContext = dbContext; } public async Task> GetPageAsync(BusinessPlaceShiftPageRequest request) { var page = await _dbContext.Queryable() .LeftJoin((shift, place) => shift.BusinessPlaceCode == place.Code) .WhereIF(request.Search.IsPresent(), (shift, place) => shift.CustomCode.Contains(request.Search) || shift.Name.Contains(request.Search)) .Select((shift, place) => new BusinessPlaceShiftResponse { Id = shift.Id, BusinessPlaceCode = place.Code, BusinessPlaceCustomCode = place.CustomCode, Code = shift.Code, CustomCode = shift.CustomCode, Name = shift.Name, Start = shift.Start, End = shift.End }) .ToPageListAsync(request); return page; } public async Task SaveAsync(BusinessPlaceShiftSaveRequest request) { await CheckExistsCustomCodeAsync(request); await DuplicateTimePeriodAsync(request); await CheckPlaceExistsAsync(request.BusinessPlaceCode); if (!request.Id.HasValue) { var entity = request.Adapt(); entity.Code = Guid.NewGuid(); entity.Start = request.TimePeriod.Start.Value; entity.End = request.TimePeriod.End.Value; await _dbContext.InsertAsync(entity); } else { var entity = await _dbContext.Queryable().FirstAsync(x => x.Id == request.Id); entity.Adapt(request); await _dbContext.UpdateAsync(entity); } } public async Task> GetShiftsByPlaceCustomCodeAsync(string placeCustomCode) { return await _dbContext.Queryable() .LeftJoin((shift, place) => shift.BusinessPlaceCode == place.Code) .Where((shift, place) => place.CustomCode == placeCustomCode) .OrderBy((shift, place) => shift.Start) .Select((shift, place) => new BusinessPlaceShiftResponse { Id = shift.Id, BusinessPlaceCode = place.Code, BusinessPlaceCustomCode = place.CustomCode, Code = shift.Code, CustomCode = shift.CustomCode, Name = shift.Name, Start = shift.Start, End = shift.End }) .ToListAsync(); } //*********************************************** help methods ***************************************************// private async Task CheckPlaceExistsAsync(Guid code) { var existsPlace = await _dbContext.Queryable().AnyAsync(x => x.Code == code); existsPlace.ThrowIfFalse("营业点不存在,请检查."); } /// /// 时间段重合检查 /// /// /// private async Task DuplicateTimePeriodAsync(BusinessPlaceShiftSaveRequest request) { var periods = await _dbContext.Queryable() .Where(x => x.BusinessPlaceCode == request.BusinessPlaceCode) .WhereIF(request.Id.HasValue, x => x.Id != request.Id) .Select(x => new TimePeriod { Start = x.Start, End = x.End, }) .ToListAsync(); foreach (var period in periods) { period.IsDuplicate(request.TimePeriod).ThrowIfTrue($"存在时间段重合[{period.Start}-{period.End}]"); } } /// /// 检查重复手工号 /// /// /// private async Task CheckExistsCustomCodeAsync(BusinessPlaceShiftSaveRequest request) { var existsSameCustomCode = await _dbContext.Queryable() .Where(x => x.CustomCode == request.CustomCode) .WhereIF(request.Id.HasValue, x => x.Id != request.Id) .AnyAsync(); existsSameCustomCode.ThrowIfTrue($"自定义手工号[{request.CustomCode}]存在重复,请检查."); } }