using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WIDESEAWCS_Core; using WIDESEAWCS_Core.BaseRepository; using WIDESEAWCS_Core.BaseServices; using WIDESEAWCS_DTO.BasicInfo; using WIDESEAWCS_Model.Models; namespace WIDESEAWCS_BasicInfoService { public class BoxingDetailService : ServiceBase>, IBoxingDetailService { private readonly IBoxingService _boxingService; private readonly IFormulaService _formulaService; private readonly IFormulaDetailService _formulaDetailService; private readonly IUnitOfWorkManage _unitOfWorkManage; public BoxingDetailService( IRepository BaseDal, IFormulaService formulaService, IBoxingService boxingService, IFormulaDetailService formulaDetailService, IUnitOfWorkManage unitOfWorkManage ) : base(BaseDal) { _formulaService = formulaService; _boxingService = boxingService; _formulaDetailService = formulaDetailService; _unitOfWorkManage = unitOfWorkManage; } public IRepository Repository => BaseDal; /// /// 比较零件是否齐全 /// /// /// /// public bool IsComponentCodesEqual(List boxingDetails, List formulaDetails) { if (boxingDetails == null || formulaDetails == null || boxingDetails.Count != formulaDetails.Count) { return false; } List BoxingIdList = boxingDetails.Select(x => x.ComponentCode).ToList(); List FormulaIdList = formulaDetails.Select(x => x.ComponentCode).ToList(); return !BoxingIdList.Except(FormulaIdList).Any() && !FormulaIdList.Except(BoxingIdList).Any(); } /// /// 检查零件 /// /// /// /// public string IsComponentCodesEqual(List boxingDetails, List formulaDetails) { // 处理null集合,规避空指针异常 var boxList = boxingDetails ?? new List(); var formulaList = formulaDetails ?? new List(); var validBoxCodes = boxList.Where(code => !string.IsNullOrEmpty(code)).ToList(); var validFormulaCodes = formulaList.Where(code => !string.IsNullOrEmpty(code)).ToList(); foreach (var group in validBoxCodes.GroupBy(code => code)) { string componentCode = group.Key; int boxCount = group.Count(); int formulaCount = validFormulaCodes.Count(code => code == componentCode); if (formulaCount == 0 || formulaCount < boxCount) { return componentCode; } } return null; } /// /// 添加/修改组盘信息(一体化接口:传工装板编号存在则修改,不存在则新增) /// /// /// public WebResponseContent SaveToolingBoardNo(ToolingBoardSubmitDto toolingBoardSubmitDto) { using (var uow = _unitOfWorkManage.CreateUnitOfWork()) { try { // 1. 参数校验 if (toolingBoardSubmitDto == null) { return WebResponseContent.Instance.Error("提交参数不能为空"); } string palletCode = toolingBoardSubmitDto.ToolingBoardNo?.Trim(); string productCode = toolingBoardSubmitDto.FinishedProductCode?.Trim(); if (string.IsNullOrWhiteSpace(palletCode)) { return WebResponseContent.Instance.Error("工装板编号不能为空"); } if (string.IsNullOrWhiteSpace(productCode)) { return WebResponseContent.Instance.Error("成品编码不能为空"); } // 2. 查询成品配方信息 Dt_Formula formulaModel = _formulaService.Repository.QueryFirst(x => x.ProductCode == productCode); if (formulaModel == null) { return WebResponseContent.Instance.Error("无成品配方,请核对成品编码"); } List dt_FormulaDetails = _formulaDetailService.Repository.QueryData(x => x.FormulaId == formulaModel.Id && x.IsScanned == 1); List Codelist = dt_FormulaDetails.Select(x => x.ComponentCode).ToList(); string IsCode = IsComponentCodesEqual(toolingBoardSubmitDto.PartsList, Codelist); if (IsCode != null) { return WebResponseContent.Instance.Error($"物料【{IsCode}】错误,请核对物料编号"); } // 3. 核心判断:根据工装板编号查询是否存在【新增/修改】的主表数据 Dt_Boxing existBoxinModel = _boxingService.Repository.QueryFirst(x => x.PalletCode == palletCode); if (existBoxinModel == null) { #region 新增逻辑 - 原逻辑优化版 Dt_Boxing dt_boxin = new Dt_Boxing() { PalletCode = palletCode, ProductCode = productCode, ProductName = formulaModel.ProductName.Trim(), Creater = "admin", CreateDate = DateTime.Now }; int newBoxingId = _boxingService.Repository.AddData(dt_boxin); // 批量添加明细表数据 if (toolingBoardSubmitDto.PartsList != null && toolingBoardSubmitDto.PartsList.Count > 0) { List detailList = new List(); foreach (string ComponentCode in toolingBoardSubmitDto.PartsList) { if (string.IsNullOrWhiteSpace(ComponentCode)) continue; string ComponentCodeTrim = ComponentCode.Trim(); Dt_FormulaDetail formulaDetail = _formulaDetailService.Repository.QueryFirst(x => x.FormulaId == formulaModel.Id && x.ComponentCode == ComponentCodeTrim); if (formulaDetail == null) { return WebResponseContent.Instance.Error($"配方中无此物料【{ComponentCodeTrim}】,请核对物料编号"); } Dt_BoxingDetail detailModel = new Dt_BoxingDetail() { BoxingId = newBoxingId, ComponentCode = ComponentCodeTrim, ComponentName = formulaDetail.ComponentName, Creater = "admin", CreateDate = DateTime.Now }; detailList.Add(detailModel); } if (detailList.Count > 0) { BaseDal.AddData(detailList); } } #endregion } else { #region 修改逻辑 - 新增核心逻辑 // 1. 更新主表数据 existBoxinModel.ProductCode = productCode; existBoxinModel.ProductName = formulaModel.ProductName.Trim(); existBoxinModel.Modifier = "admin"; existBoxinModel.ModifyDate = DateTime.Now; _boxingService.Repository.UpdateData(existBoxinModel); int boxingId = existBoxinModel.Id; // 2. 删除该组盘下的【原有全部明细表数据】 List oldDetailList = BaseDal.QueryData(x => x.BoxingId == boxingId); if (oldDetailList != null && oldDetailList.Count > 0) { BaseDal.DeleteData(oldDetailList); } // 3. 重新插入修改后的明细表数据 if (toolingBoardSubmitDto.PartsList != null && toolingBoardSubmitDto.PartsList.Count > 0) { List detailList = new List(); foreach (string ComponentCode in toolingBoardSubmitDto.PartsList) { if (string.IsNullOrWhiteSpace(ComponentCode)) continue; string ComponentCodeTrim = ComponentCode.Trim(); Dt_FormulaDetail formulaDetail = _formulaDetailService.Repository.QueryFirst(x => x.FormulaId == formulaModel.Id && x.ComponentCode == ComponentCodeTrim); if (formulaDetail == null) { return WebResponseContent.Instance.Error($"配方中无此物料【{ComponentCodeTrim}】,请核对物料名称"); } Dt_BoxingDetail detailModel = new Dt_BoxingDetail() { BoxingId = boxingId, ComponentCode = ComponentCodeTrim, ComponentName = formulaDetail.ComponentName, Creater = "admin", CreateDate = DateTime.Now }; detailList.Add(detailModel); } if (detailList.Count > 0) { BaseDal.AddData(detailList); } } #endregion } uow.Commit(); string msg = existBoxinModel == null ? "组盘信息新增成功!" : "组盘信息修改成功!"; return WebResponseContent.Instance.OK(msg); } catch (Exception ex) { Console.WriteLine($"保存工装板组盘信息异常:{ex.Message},堆栈:{ex.StackTrace}"); return WebResponseContent.Instance.Error($"提交失败:{ex.Message}"); } } } /// /// 获取成品编号和零件编号 /// /// /// public WebResponseContent GetProductAndPartsByBoardNo(string palletCode) { try { if (string.IsNullOrWhiteSpace(palletCode)) { return WebResponseContent.Instance.OK("查询成功", new { finishedProductCode = "", partsList = Enumerable.Repeat("", 10).ToList() }); } Dt_Boxing dt_Boxing = _boxingService.Repository.QueryFirst(x => x.PalletCode == palletCode); if (dt_Boxing == null) { return WebResponseContent.Instance.OK("未查询到该工装板对应的成品信息", new { finishedProductCode = "", partsList = Enumerable.Repeat("", 10).ToList() }); } List dt_BoxingDetails = BaseDal.QueryData(x => x.BoxingId == dt_Boxing.Id)?.ToList() ?? new List(); // 取零件编号 List partsList = dt_BoxingDetails.Select(d => d.ComponentCode).ToList(); List resultPartsList = new List(); for (int i = 0; i < 10; i++) { if (i < partsList.Count) { resultPartsList.Add(partsList[i] ?? ""); } else { resultPartsList.Add(""); } } return WebResponseContent.Instance.OK("零件编号列表查询成功", new { finishedProductCode = dt_Boxing.ProductCode ?? "", partsList = resultPartsList }); } catch (Exception ex) { return WebResponseContent.Instance.Error($"查询零件编号列表异常:{ex.Message}"); } } } }