using F9WebAiTest.Application.Common; using F9WebAiTest.Application.Dtos; using F9WebAiTest.Application.Dtos.Base; using F9WebAiTest.Domain.Common; using F9WebAiTest.Domain.Entities; using F9WebAiTest.Domain.Entities.Base; using SysDept = F9WebAiTest.Domain.Entities.SYS.Departments; namespace F9WebAiTest.Application.Services.Base; // ==================== 工艺庄口 ==================== public interface ICraftZhuangKouService { Task> GetPagedAsync(int pageIndex, int pageSize, string keyword, int? year); Task CreateAsync(CraftZhuangKouCreateDto dto); Task UpdateAsync(long id, CraftZhuangKouCreateDto dto); Task DeleteAsync(long id); } public class CraftZhuangKouService : ICraftZhuangKouService { private readonly IRepository _repo; public CraftZhuangKouService(IRepository repo) => _repo = repo; private static string BendiWaidiName(int v) => v switch { 1 => "本地", 2 => "外地", _ => "" }; public async Task> GetPagedAsync(int pageIndex, int pageSize, string keyword, int? year) { var all = await _repo.GetAllAsync(); var q = all.AsQueryable(); if (!string.IsNullOrWhiteSpace(keyword)) q = q.Where(x => x.ZhuangKouName.Contains(keyword) || x.ZhuangKouCode.Contains(keyword) || x.ProductionDisplayName.Contains(keyword)); if (year.HasValue) q = q.Where(x => x.BelongingYear == year.Value); var total = q.LongCount(); var items = q.OrderBy(x => x.Sort).ThenBy(x => x.Id) .Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); return new PageResult { Items = items.Select(ToDto).ToList(), Total = total, PageIndex = pageIndex, PageSize = pageSize }; } public async Task CreateAsync(CraftZhuangKouCreateDto dto) { if (string.IsNullOrWhiteSpace(dto.ZhuangKouName)) throw new ServiceException("庄口名称不能为空"); var e = new CraftZhuangKou { BelongingYear = dto.BelongingYear, BendiWaidi = dto.BendiWaidi, JidiWaigou = dto.JidiWaigou, ZhuangKouCode = dto.ZhuangKouCode, ZhuangKouName = dto.ZhuangKouName, ProductionDisplayName = dto.ProductionDisplayName, Chandi = dto.Chandi, Sort = dto.Sort, Memo = dto.Memo, IsJiezhuang = dto.IsJiezhuang }; await _repo.AddAsync(e); await _repo.SaveChangesAsync(); return ToDto(e); } public async Task UpdateAsync(long id, CraftZhuangKouCreateDto dto) { var e = await _repo.GetByIdAsync(id); if (e == null) throw new ServiceException("记录不存在", 404); e.BelongingYear = dto.BelongingYear; e.BendiWaidi = dto.BendiWaidi; e.JidiWaigou = dto.JidiWaigou; e.ZhuangKouCode = dto.ZhuangKouCode; e.ZhuangKouName = dto.ZhuangKouName; e.ProductionDisplayName = dto.ProductionDisplayName; e.Chandi = dto.Chandi; e.Sort = dto.Sort; e.Memo = dto.Memo; e.IsJiezhuang = dto.IsJiezhuang; await _repo.UpdateAsync(e); await _repo.SaveChangesAsync(); return ToDto(e); } public async Task DeleteAsync(long id) { var e = await _repo.GetByIdAsync(id); if (e == null) throw new ServiceException("记录不存在", 404); await _repo.DeleteAsync(e); await _repo.SaveChangesAsync(); } private static CraftZhuangKouDto ToDto(CraftZhuangKou e) => new() { Id = e.Id, BelongingYear = e.BelongingYear, BendiWaidi = e.BendiWaidi, BendiWaidiName = BendiWaidiName(e.BendiWaidi), JidiWaigou = e.JidiWaigou, ZhuangKouCode = e.ZhuangKouCode, ZhuangKouName = e.ZhuangKouName, ProductionDisplayName = e.ProductionDisplayName, Chandi = e.Chandi, Sort = e.Sort, Memo = e.Memo, IsJiezhuang = e.IsJiezhuang }; } // ==================== 生丝规格 ==================== public interface IShengsiGuigeService { Task> GetPagedAsync(int pageIndex, int pageSize, string keyword); Task> GetAllGuigeAsync(); Task CreateAsync(ShengsiGuigeDto dto); Task UpdateAsync(long id, ShengsiGuigeDto dto); Task DeleteAsync(long id); } public class ShengsiGuigeService : IShengsiGuigeService { private readonly IRepository _repo; public ShengsiGuigeService(IRepository repo) => _repo = repo; public async Task> GetPagedAsync(int pageIndex, int pageSize, string keyword) { var all = await _repo.GetAllAsync(); var q = all.AsQueryable(); if (!string.IsNullOrWhiteSpace(keyword)) q = q.Where(x => x.Guige.Contains(keyword)); var total = q.LongCount(); var items = q.OrderBy(x => x.Sort).ThenBy(x => x.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); return new PageResult { Items = items.Select(ToDto).ToList(), Total = total, PageIndex = pageIndex, PageSize = pageSize }; } public async Task> GetAllGuigeAsync() => (await _repo.GetAllAsync()).OrderBy(x => x.Sort).Select(x => x.Guige).Distinct().ToList(); public async Task CreateAsync(ShengsiGuigeDto dto) { if (string.IsNullOrWhiteSpace(dto.Guige)) throw new ServiceException("规格不能为空"); var e = Map(new ShengsiGuige(), dto); await _repo.AddAsync(e); await _repo.SaveChangesAsync(); return ToDto(e); } public async Task UpdateAsync(long id, ShengsiGuigeDto dto) { var e = await _repo.GetByIdAsync(id); if (e == null) throw new ServiceException("记录不存在", 404); Map(e, dto); await _repo.UpdateAsync(e); await _repo.SaveChangesAsync(); return ToDto(e); } public async Task DeleteAsync(long id) { var e = await _repo.GetByIdAsync(id); if (e == null) throw new ServiceException("记录不存在", 404); await _repo.DeleteAsync(e); await _repo.SaveChangesAsync(); } private static ShengsiGuige Map(ShengsiGuige e, ShengsiGuigeDto d) { e.Guige = d.Guige; e.CenterrangeLolimit = d.CenterrangeLolimit; e.CenterrangeUplimit = d.CenterrangeUplimit; e.CenterXiandu = d.CenterXiandu; e.ValidLolimit = d.ValidLolimit; e.ValidUplimit = d.ValidUplimit; e.XiaoyeLolimit = d.XiaoyeLolimit; e.XiaoyeUplimit = d.XiaoyeUplimit; e.DayeLolimit = d.DayeLolimit; e.DayeUplimit = d.DayeUplimit; e.TeyeLolimit = d.TeyeLolimit; e.TeyeUplimit = d.TeyeUplimit; e.YexianNkLolimitLeft = d.YexianNkLolimitLeft; e.YexianNkLolimitRight = d.YexianNkLolimitRight; e.YexianNkUplimitLeft = d.YexianNkUplimitLeft; e.YexianNkUplimitRight = d.YexianNkUplimitRight; e.LaodongJingsaiYexianLolimit = d.LaodongJingsaiYexianLolimit; e.LaodongJingsaiYexianUplimit = d.LaodongJingsaiYexianUplimit; e.Sort = d.Sort; e.Memo = d.Memo; return e; } private static ShengsiGuigeDto ToDto(ShengsiGuige e) => new() { Id = e.Id, Guige = e.Guige, CenterrangeLolimit = e.CenterrangeLolimit, CenterrangeUplimit = e.CenterrangeUplimit, CenterXiandu = e.CenterXiandu, ValidLolimit = e.ValidLolimit, ValidUplimit = e.ValidUplimit, XiaoyeLolimit = e.XiaoyeLolimit, XiaoyeUplimit = e.XiaoyeUplimit, DayeLolimit = e.DayeLolimit, DayeUplimit = e.DayeUplimit, TeyeLolimit = e.TeyeLolimit, TeyeUplimit = e.TeyeUplimit, YexianNkLolimitLeft = e.YexianNkLolimitLeft, YexianNkLolimitRight = e.YexianNkLolimitRight, YexianNkUplimitLeft = e.YexianNkUplimitLeft, YexianNkUplimitRight = e.YexianNkUplimitRight, LaodongJingsaiYexianLolimit = e.LaodongJingsaiYexianLolimit, LaodongJingsaiYexianUplimit = e.LaodongJingsaiYexianUplimit, Sort = e.Sort, Memo = e.Memo }; } // ==================== 生丝定级指标 ==================== public interface IShengsiDingjizhibiaoService { Task> GetPagedAsync(int pageIndex, int pageSize, string keyword, int? standard, string guige); Task CreateAsync(ShengsiDingjizhibiaoDto dto); Task UpdateAsync(long id, ShengsiDingjizhibiaoDto dto); Task DeleteAsync(long id); } public class ShengsiDingjizhibiaoService : IShengsiDingjizhibiaoService { private readonly IRepository _repo; public ShengsiDingjizhibiaoService(IRepository repo) => _repo = repo; private static string StandardName(int v) => v switch { 0 => "厂标", 1 => "国标", 2 => "个人", _ => "" }; public async Task> GetPagedAsync(int pageIndex, int pageSize, string keyword, int? standard, string guige) { var all = await _repo.GetAllAsync(); var q = all.AsQueryable(); if (!string.IsNullOrWhiteSpace(keyword)) q = q.Where(x => x.Grade.Contains(keyword)); if (standard.HasValue) q = q.Where(x => x.Standard == standard.Value); if (!string.IsNullOrWhiteSpace(guige)) q = q.Where(x => x.Guige == guige); var total = q.LongCount(); var items = q.OrderBy(x => x.Standard).ThenBy(x => x.Guige).ThenBy(x => x.GradeScore) .Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); return new PageResult { Items = items.Select(ToDto).ToList(), Total = total, PageIndex = pageIndex, PageSize = pageSize }; } public async Task CreateAsync(ShengsiDingjizhibiaoDto dto) { if (string.IsNullOrWhiteSpace(dto.Grade)) throw new ServiceException("等级不能为空"); var e = Map(new ShengsiDingjizhibiao(), dto); await _repo.AddAsync(e); await _repo.SaveChangesAsync(); return ToDto(e); } public async Task UpdateAsync(long id, ShengsiDingjizhibiaoDto dto) { var e = await _repo.GetByIdAsync(id); if (e == null) throw new ServiceException("记录不存在", 404); Map(e, dto); await _repo.UpdateAsync(e); await _repo.SaveChangesAsync(); return ToDto(e); } public async Task DeleteAsync(long id) { var e = await _repo.GetByIdAsync(id); if (e == null) throw new ServiceException("记录不存在", 404); await _repo.DeleteAsync(e); await _repo.SaveChangesAsync(); } private static ShengsiDingjizhibiao Map(ShengsiDingjizhibiao e, ShengsiDingjizhibiaoDto d) { e.Standard = d.Standard; e.Guige = d.Guige; e.Grade = d.Grade; e.GradeScore = d.GradeScore; e.Piancha = d.Piancha; e.Dacha = d.Dacha; e.Erdu = d.Erdu; e.Sandu = d.Sandu; e.Qingjie = d.Qingjie; e.Jiejing = d.Jiejing; return e; } private static ShengsiDingjizhibiaoDto ToDto(ShengsiDingjizhibiao e) => new() { Id = e.Id, Standard = e.Standard, StandardName = StandardName(e.Standard), Guige = e.Guige, Grade = e.Grade, GradeScore = e.GradeScore, Piancha = e.Piancha, Dacha = e.Dacha, Erdu = e.Erdu, Sandu = e.Sandu, Qingjie = e.Qingjie, Jiejing = e.Jiejing }; } // ==================== 机组车台 ==================== public interface IJizuChetaiService { Task> GetPagedAsync(int pageIndex, int pageSize, string keyword); Task GetByDepIdAndJizuAsync(long deptId, int jizu); Task CreateAsync(JizuChetaiDto dto); Task UpdateAsync(long id, JizuChetaiDto dto); Task DeleteAsync(long id); } public class JizuChetaiService : IJizuChetaiService { private readonly IRepository _repo; private readonly IRepository _deptRepo; public JizuChetaiService(IRepository repo, IRepository deptRepo) { _repo = repo; _deptRepo = deptRepo; } public async Task> GetPagedAsync(int pageIndex, int pageSize, string keyword) { var all = await _repo.GetAllAsync(); var q = all.AsQueryable(); var depts = await _deptRepo.GetAllAsync(); var deptDict = depts.ToDictionary(d => d.Id, d => d.Name); if (!string.IsNullOrWhiteSpace(keyword)) { var kwDeptIds = depts.Where(d => d.Name.Contains(keyword)).Select(d => d.Id).ToHashSet(); q = q.Where(x => kwDeptIds.Contains(x.DepartmentId) || x.Jizu.ToString().Contains(keyword)); } var total = q.LongCount(); var items = q.OrderBy(x => x.DepartmentId).ThenBy(x => x.Jizu).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); return new PageResult { Items = items.Select(x => new JizuChetaiDto { Id = x.Id, DepartmentId = x.DepartmentId, DepartmentName = deptDict.GetValueOrDefault(x.DepartmentId, ""), Jizu = x.Jizu, ChetaiStart = x.ChetaiStart, ChetaiEnd = x.ChetaiEnd }).ToList(), Total = total, PageIndex = pageIndex, PageSize = pageSize }; } public async Task GetByDepIdAndJizuAsync(long deptId, int jizu) { var e = await _repo.FirstOrDefaultAsync(x => x.DepartmentId == deptId && x.Jizu == jizu); if (e == null) return null; return await GetDto(e); } public async Task CreateAsync(JizuChetaiDto dto) { var e = new JizuChetai { DepartmentId = dto.DepartmentId, Jizu = dto.Jizu, ChetaiStart = dto.ChetaiStart, ChetaiEnd = dto.ChetaiEnd }; await _repo.AddAsync(e); await _repo.SaveChangesAsync(); return await GetDto(e); } public async Task UpdateAsync(long id, JizuChetaiDto dto) { var e = await _repo.GetByIdAsync(id); if (e == null) throw new ServiceException("记录不存在", 404); e.DepartmentId = dto.DepartmentId; e.Jizu = dto.Jizu; e.ChetaiStart = dto.ChetaiStart; e.ChetaiEnd = dto.ChetaiEnd; await _repo.UpdateAsync(e); await _repo.SaveChangesAsync(); return await GetDto(e); } public async Task DeleteAsync(long id) { var e = await _repo.GetByIdAsync(id); if (e == null) throw new ServiceException("记录不存在", 404); await _repo.DeleteAsync(e); await _repo.SaveChangesAsync(); } private async Task GetDto(JizuChetai e) { var dept = await _deptRepo.GetByIdAsync(e.DepartmentId); return new JizuChetaiDto { Id = e.Id, DepartmentId = e.DepartmentId, DepartmentName = dept?.Name ?? "", Jizu = e.Jizu, ChetaiStart = e.ChetaiStart, ChetaiEnd = e.ChetaiEnd }; } }