From ba8aa925e7901381ceb394adb53eca8723d1c4c5 Mon Sep 17 00:00:00 2001
From: leiqunqing <zhengqifeng@hnkhzn.com>
Date: 星期一, 19 一月 2026 11:05:49 +0800
Subject: [PATCH] 完善工位界面

---
 代码管理/WIDESEAWCS_Server/WIDESEAWCS_Server/WIDESEAWCS_BasicInfoService/BoxingDetailService.cs |  271 +++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 256 insertions(+), 15 deletions(-)

diff --git "a/\344\273\243\347\240\201\347\256\241\347\220\206/WIDESEAWCS_Server/WIDESEAWCS_Server/WIDESEAWCS_BasicInfoService/BoxingDetailService.cs" "b/\344\273\243\347\240\201\347\256\241\347\220\206/WIDESEAWCS_Server/WIDESEAWCS_Server/WIDESEAWCS_BasicInfoService/BoxingDetailService.cs"
index dd8c472..7db9778 100644
--- "a/\344\273\243\347\240\201\347\256\241\347\220\206/WIDESEAWCS_Server/WIDESEAWCS_Server/WIDESEAWCS_BasicInfoService/BoxingDetailService.cs"
+++ "b/\344\273\243\347\240\201\347\256\241\347\220\206/WIDESEAWCS_Server/WIDESEAWCS_Server/WIDESEAWCS_BasicInfoService/BoxingDetailService.cs"
@@ -1,22 +1,40 @@
 锘縰sing 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<Dt_BoxingDetail, IRepository<Dt_BoxingDetail>>, IBoxingDetailService
     {
-        public BoxingDetailService(IRepository<Dt_BoxingDetail> BaseDal) : base(BaseDal)
+
+        private readonly IBoxingService _boxingService;
+        private readonly IFormulaService _formulaService;
+        private readonly IFormulaDetailService _formulaDetailService;
+        private readonly IUnitOfWorkManage _unitOfWorkManage;
+
+        public BoxingDetailService(
+            IRepository<Dt_BoxingDetail> BaseDal,
+            IFormulaService formulaService,
+            IBoxingService boxingService,
+            IFormulaDetailService formulaDetailService,
+            IUnitOfWorkManage unitOfWorkManage
+            ) : base(BaseDal)
         {
+            _formulaService = formulaService;
+            _boxingService = boxingService;
+            _formulaDetailService = formulaDetailService;
+            _unitOfWorkManage = unitOfWorkManage;
         }
 
         public IRepository<Dt_BoxingDetail> Repository => BaseDal;
-
 
         /// <summary>
         /// 姣旇緝闆朵欢鏄惁榻愬叏
@@ -30,26 +48,249 @@
             {
                 return false;
             }
+            List<string> BoxingIdList = boxingDetails.Select(x => x.ComponentCode).ToList();
+            List<string> FormulaIdList = formulaDetails.Select(x => x.ComponentCode).ToList();
+            return !BoxingIdList.Except(FormulaIdList).Any() && !FormulaIdList.Except(BoxingIdList).Any();
+        }
 
-            List<string> BoxingIdList = new List<string>();
-            List<string> FormulaIdList = new List<string>();
 
-            for (int i = 0; i < boxingDetails.Count; i++)
+        /// <summary>
+        /// 妫�鏌ラ浂浠�
+        /// </summary>
+        /// <param name="boxingDetails"></param>
+        /// <param name="formulaDetails"></param>
+        /// <returns></returns>
+        public string IsComponentCodesEqual(List<string> boxingDetails, List<string> formulaDetails)
+        {
+            // 澶勭悊null闆嗗悎锛岃閬跨┖鎸囬拡寮傚父
+            var boxList = boxingDetails ?? new List<string>();
+            var formulaList = formulaDetails ?? new List<string>();
+
+            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))
             {
-                BoxingIdList.Add(boxingDetails[i].ComponentCode);
-                FormulaIdList.Add(formulaDetails[i].ComponentCode);
-            }
-            BoxingIdList.Sort();
-            FormulaIdList.Sort();
+                string componentCode = group.Key;
+                int boxCount = group.Count();
+                int formulaCount = validFormulaCodes.Count(code => code == componentCode);
 
-            for (int i = 0; i < BoxingIdList.Count; i++)
-            {
-                if (BoxingIdList[i] != FormulaIdList[i])
+                if (formulaCount == 0 || formulaCount < boxCount)
                 {
-                    return false;
+                    return componentCode;
                 }
             }
-            return true;
+            return null;
+        }
+
+
+        /// <summary>
+        /// 娣诲姞/淇敼缁勭洏淇℃伅锛堜竴浣撳寲鎺ュ彛锛氫紶宸ヨ鏉跨紪鍙峰瓨鍦ㄥ垯淇敼锛屼笉瀛樺湪鍒欐柊澧烇級
+        /// </summary>
+        /// <param name="toolingBoardSubmitDto"></param>
+        /// <returns></returns>
+        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_FormulaDetail> dt_FormulaDetails = _formulaDetailService.Repository.QueryData(x => x.FormulaId == formulaModel.Id && x.IsScanned == 1);
+                    List<string> 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<Dt_BoxingDetail> detailList = new List<Dt_BoxingDetail>();
+                            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<Dt_BoxingDetail> 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<Dt_BoxingDetail> detailList = new List<Dt_BoxingDetail>();
+                            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}");
+                }
+            }
+        }
+
+
+
+        /// <summary>
+        /// 鑾峰彇鎴愬搧缂栧彿鍜岄浂浠剁紪鍙�
+        /// </summary>
+        /// <param name="palletCode"></param>
+        /// <returns></returns>
+        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_BoxingDetail> dt_BoxingDetails = BaseDal.QueryData(x => x.BoxingId == dt_Boxing.Id)?.ToList() ?? new List<Dt_BoxingDetail>();
+                // 鍙栭浂浠剁紪鍙�
+                List<string> partsList = dt_BoxingDetails.Select(d => d.ComponentCode).ToList();
+                List<string> resultPartsList = new List<string>();
+                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}");
+            }
         }
     }
 }

--
Gitblit v1.9.3