using F9WebAiTest.Application.Common; using F9WebAiTest.Application.Dtos.Prod; using F9WebAiTest.Domain.Common; using F9WebAiTest.Domain.Entities; using F9WebAiTest.Domain.Entities.Base; using F9WebAiTest.Domain.Entities.Prod; using F9WebAiTest.Domain.Enums.SYS.Departments; using SysDept = F9WebAiTest.Domain.Entities.SYS.Departments; namespace F9WebAiTest.Application.Services.Prod; public interface IChejianBanciService { Task> GetPagedAsync(int pageIndex, int pageSize, string keyword); Task GetByIdForEditAsync(long id); Task CreateAsync(ChejianbanciReadDto_Edit dto); Task UpdateAsync(long id, ChejianbanciReadDto_Edit dto); Task DeleteAsync(long id); /// 车间下拉:只取职责类型为「前缫」的部门 Task> GetQianxianDepsAsync(); } public class ChejianBanciService : IChejianBanciService { private readonly IRepository _repo; private readonly IRepository _deptRepo; public ChejianBanciService(IRepository repo, IRepository deptRepo) { _repo = repo; _deptRepo = deptRepo; } public async Task> GetPagedAsync(int pageIndex, int pageSize, string keyword) { if (pageIndex <= 0) pageIndex = 1; if (pageSize <= 0) pageSize = 20; var depts = await _deptRepo.GetAllAsync(); var deptDict = depts.ToDictionary(d => d.Id, d => d.Name); var all = await _repo.GetAllAsync(); var q = all.AsQueryable(); if (!string.IsNullOrWhiteSpace(keyword)) { var kw = keyword.Trim(); var kwDeptIds = depts.Where(d => d.Name.Contains(kw)).Select(d => d.Id).ToHashSet(); q = q.Where(x => kwDeptIds.Contains(x.DeptId) || x.Banci.ToString().Contains(kw)); } var total = q.LongCount(); var items = q.OrderBy(x => x.DeptId).ThenBy(x => x.Banci) .Skip((pageIndex - 1) * pageSize).Take(pageSize) .Select(x => new ChejianbanciReadDto_View { Id = x.Id, DepId = x.DeptId, Deptname = deptDict.GetValueOrDefault(x.DeptId, ""), Banci = x.Banci, BanciName = x.Banci.ToString(), KaoqinDayhours = x.KaoqinDayhours }).ToList(); return new PageResult { Items = items, Total = total, PageIndex = pageIndex, PageSize = pageSize }; } public async Task GetByIdForEditAsync(long id) { var e = await _repo.GetByIdAsync(id) ?? throw new ServiceException("记录不存在", 404); return new ChejianbanciReadDto_Edit { Id = e.Id, DepId = e.DeptId, Banci = e.Banci, KaoqinDayhours = e.KaoqinDayhours }; } public async Task CreateAsync(ChejianbanciReadDto_Edit dto) { await ValidateAsync(dto, 0); await _repo.AddAsync(new ChejianBanci { DeptId = dto.DepId, Banci = dto.Banci, KaoqinDayhours = dto.KaoqinDayhours }); await _repo.SaveChangesAsync(); } public async Task UpdateAsync(long id, ChejianbanciReadDto_Edit dto) { var e = await _repo.GetByIdAsync(id) ?? throw new ServiceException("记录不存在", 404); await ValidateAsync(dto, id); e.DeptId = dto.DepId; e.Banci = dto.Banci; e.KaoqinDayhours = dto.KaoqinDayhours; await _repo.UpdateAsync(e); await _repo.SaveChangesAsync(); } public async Task DeleteAsync(long id) { var e = await _repo.GetByIdAsync(id) ?? throw new ServiceException("记录不存在", 404); await _repo.DeleteAsync(e); await _repo.SaveChangesAsync(); } public async Task> GetQianxianDepsAsync() { var depts = await _deptRepo.GetAllAsync(); return depts .Where(d => d.DutyType == Department_DutyType_Enum.前缫) .OrderBy(d => d.SortOrder).ThenBy(d => d.Id) .Select(d => new ChejianbanciDepOptionDto { Id = d.Id, Name = d.Name }) .ToList(); } /// 校验:车间、班次、工时必填,且同一车间的同一班次不可重复 private async Task ValidateAsync(ChejianbanciReadDto_Edit dto, long excludeId) { if (dto.DepId <= 0) throw new ServiceException("请选择车间"); if (dto.KaoqinDayhours <= 0) throw new ServiceException("日工作小时数必须大于 0"); var all = await _repo.GetAllAsync(); if (all.Any(x => x.Id != excludeId && x.DeptId == dto.DepId && x.Banci == dto.Banci)) throw new ServiceException("该车间的此班次已存在,请勿重复配置"); } }