using Dlhis.Basic.WorkGrids.Request; using Dlhis.Basic.WorkGrids.Response; using Dlhis.Entity.Basic.Entity; using Kurisu.AspNetCore.DataAccess.SqlSugar.Services; using Kurisu.AspNetCore.Utils.Extensions; namespace Dlhis.Basic.WorkGrids.Services.Implements; public class WorkGridScheduleService : IWorkGridScheduleService { private readonly IDbContext _dbContext; public WorkGridScheduleService(IDbContext dbContext) { _dbContext = dbContext; } public async Task SaveAsync(WorkGridScheduleSaveRequest request) { var exists = await _dbContext.Queryable() .Where(x => x.Date == request.Date) .Where(x => x.PeriodType != request.PeriodType) .Where(x => request.UserCodes.Contains(x.UserCode)) .AnyAsync(); exists.ThrowIfTrue($"存在已经分配的人员"); var data = await _dbContext.Queryable() .Where(x => x.WorkGridCode == request.WorkGridCode) .Where(x => x.Date == request.Date) .Where(x => x.PeriodType == request.PeriodType) .ToListAsync(); await _dbContext.DeleteReallyAsync(data); var toInserts = request.UserCodes.Select(x => new WorkGridScheduleEntity { WorkGridCode = request.WorkGridCode, Date = request.Date, PeriodType = request.PeriodType, UserCode = x }).ToList(); await _dbContext.InsertAsync(toInserts); } /// public async Task> GetListAsync(Guid workGridCode, DateOnly date) { return await _dbContext.Queryable() .Where(x => x.WorkGridCode == workGridCode && x.Date == date) .Select(x => new WorkGridScheduleResponse { Date = x.Date, PeriodType = x.PeriodType, UserCode = x.UserCode }) .ToListAsync(); } /// public async Task> GetListSummaryAsync(Guid workGridCode, DateOnly start, DateOnly end) { var workGridSchedules = await _dbContext.Queryable() .Where(x => x.WorkGridCode == workGridCode && x.Date >= start && x.Date <= end) .ToListAsync(); return workGridSchedules.GroupBy(x => x.Date).Select(x => { return new WorkGridScheduleSummaryResponse { Date = x.Key, Details = x.GroupBy(p => p.PeriodType).Select(p => new WorkGridScheduleSummaryDetailResponse { PeriodType = p.Key, Count = p.Count() }) }; }); } }