using F9WebAiTest.Application.Common; using F9WebAiTest.Application.Dtos.Prod; using F9WebAiTest.Domain.Common; using F9WebAiTest.Domain.Entities.Base; using F9WebAiTest.Domain.Entities.Prod; using SysDept = F9WebAiTest.Domain.Entities.SYS.Departments; namespace F9WebAiTest.Application.Services.Prod; /// 公量纤度:以落丝记录为主表,取纤度检验的平均纤度,由质检录入公量纤度(按日期 + 车间整日批量保存) public interface IGlxdService { Task> GetListAsync(DateTime? date, long? deptId); Task SaveAsync(GlxdSaveDto dto); } public class GlxdService : IGlxdService { private readonly IRepository _lsjlRepo; private readonly IRepository _glxdRepo; private readonly IRepository _xdjyLsjlRepo; private readonly IRepository _deptRepo; private readonly IRepository _zkRepo; public GlxdService( IRepository lsjlRepo, IRepository glxdRepo, IRepository xdjyLsjlRepo, IRepository deptRepo, IRepository zkRepo) { _lsjlRepo = lsjlRepo; _glxdRepo = glxdRepo; _xdjyLsjlRepo = xdjyLsjlRepo; _deptRepo = deptRepo; _zkRepo = zkRepo; } public async Task> GetListAsync(DateTime? date, long? deptId) { var lsjls = await _lsjlRepo.GetAllAsync(); var q = lsjls.AsQueryable(); if (date.HasValue) q = q.Where(x => x.WorkDate.Date == date.Value.Date); if (deptId.HasValue) q = q.Where(x => x.DeptId == deptId.Value); var items = q.OrderBy(x => x.DeptId).ThenBy(x => x.Jizu).ThenBy(x => x.Huishu).ToList(); if (items.Count == 0) return new List(); var depts = await _deptRepo.GetAllAsync(); var deptDict = depts.ToDictionary(d => d.Id, d => d.Name); var zks = await _zkRepo.GetAllAsync(); var zkDict = zks.ToDictionary(z => z.Id, z => z.ZhuangKouName); var glxds = (await _glxdRepo.GetAllAsync()).ToDictionary(x => x.LsjlId); var xdjySums = (await _xdjyLsjlRepo.GetAllAsync()).ToDictionary(x => x.LsjlId); return items.Select(x => { glxds.TryGetValue(x.Id, out var g); xdjySums.TryGetValue(x.Id, out var xs); return new GlxdListItemDto { LsjlId = x.Id, WorkDate = x.WorkDate, DeptId = x.DeptId, DeptName = deptDict.GetValueOrDefault(x.DeptId, ""), Jizu = x.Jizu, Huishu = x.Huishu, Guige = x.Guige, ZhuangKouName = zkDict.GetValueOrDefault(x.GyzkId, ""), Yanse = x.Yanse, Isbanche = x.Isbanche, IsbancheName = x.Isbanche ? "半车" : "整车", Chetaishu = x.Chetaishu, Avgxiandu = xs?.Avgxiandu ?? 0, Gongliangxiandu = g?.Gongliangxiandu ?? 0 }; }).ToList(); } public async Task SaveAsync(GlxdSaveDto dto) { if (dto.Items == null || dto.Items.Count == 0) throw new ServiceException("没有需要保存的公量纤度"); // 校验落丝记录存在,并按落丝记录 Id 去重 var dict = new Dictionary(); foreach (var it in dto.Items) { if (it.LsjlId == 0) throw new ServiceException("存在无效的落丝记录"); var lsjl = await _lsjlRepo.GetByIdAsync(it.LsjlId); if (lsjl == null) throw new ServiceException($"落丝记录 {it.LsjlId} 不存在", 404); dict[it.LsjlId] = it.Gongliangxiandu; } foreach (var (lsjlId, value) in dict) { var e = await _glxdRepo.FirstOrDefaultAsync(x => x.LsjlId == lsjlId); if (e == null) { e = new Glxd { LsjlId = lsjlId, Gongliangxiandu = value }; await _glxdRepo.AddAsync(e); } else { e.Gongliangxiandu = value; await _glxdRepo.UpdateAsync(e); } } } }