From b52018589bf6c7ec1d51ce8ad000a7aa993b0ab5 Mon Sep 17 00:00:00 2001
From: dengjunjie <dengjunjie@hnkhzn.com>
Date: 星期一, 13 四月 2026 16:06:15 +0800
Subject: [PATCH] 优化组盘表获取成品编号、流水号、供方代码

---
 代码管理/WIDESEAWCS_Server/WIDESEAWCS_Server/WIDESEAWCS_BasicInfoService/BoxingDetailService.cs |  428 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 409 insertions(+), 19 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..8c3a935 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,55 +1,445 @@
 锘縰sing System;
+using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
+using System.Text.RegularExpressions;
 using System.Threading.Tasks;
+using WIDESEAWCS_Common;
+using WIDESEAWCS_Common.PLCEnum;
+using WIDESEAWCS_Core;
 using WIDESEAWCS_Core.BaseRepository;
 using WIDESEAWCS_Core.BaseServices;
+using WIDESEAWCS_Core.Helper;
+using WIDESEAWCS_DTO.BasicInfo;
+using WIDESEAWCS_IBasicInfoService;
 using WIDESEAWCS_Model.Models;
+using WIDESEAWCS_QuartzJob;
 
 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 IProcessInfoDetailService _processInfoDetailService;
+        private readonly IScanStationService _scanStationService;
+
+
+        private readonly IUnitOfWorkManage _unitOfWorkManage;
+
+        public BoxingDetailService(
+            IRepository<Dt_BoxingDetail> BaseDal,
+            IFormulaService formulaService,
+            IBoxingService boxingService,
+            IFormulaDetailService formulaDetailService,
+            IProcessInfoDetailService processInfoDetailService,
+            IScanStationService scanStationService,
+            IUnitOfWorkManage unitOfWorkManage
+            ) : base(BaseDal)
         {
+            _formulaService = formulaService;
+            _boxingService = boxingService;
+            _formulaDetailService = formulaDetailService;
+            _processInfoDetailService = processInfoDetailService;
+            _scanStationService = scanStationService;
+            _unitOfWorkManage = unitOfWorkManage;
+
         }
 
         public IRepository<Dt_BoxingDetail> Repository => BaseDal;
 
-
         /// <summary>
         /// 姣旇緝闆朵欢鏄惁榻愬叏
         /// </summary>
-        /// <param name="boxingDetails"></param>
-        /// <param name="formulaDetails"></param>
-        /// <returns></returns>
-        public bool IsComponentCodesEqual(List<Dt_BoxingDetail> boxingDetails, List<Dt_FormulaDetail> formulaDetails)
+        /// <param name="boxingDetails">瀹為檯瑁呯鐨勯浂浠堕泦鍚�</param>
+        /// <param name="formulaDetails">閰嶆柟瑕佹眰鐨勯浂浠堕泦鍚�</param>
+        /// <returns>鏄惁瀹屽叏鍖归厤</returns>
+        public bool CheckComponentsMatchExactly(List<string> boxingDetails, List<string> formulaDetails)
         {
+            // 1. 鍩虹鏍¢獙锛氱┖鍊�/鏁伴噺涓嶄竴鑷寸洿鎺ヨ繑鍥炰笉鍖归厤
             if (boxingDetails == null || formulaDetails == null || boxingDetails.Count != formulaDetails.Count)
             {
                 return false;
             }
-
-            List<string> BoxingIdList = new List<string>();
-            List<string> FormulaIdList = new List<string>();
-
-            for (int i = 0; i < boxingDetails.Count; i++)
+            // 2. 涓や釜绌洪泦鍚堢洿鎺ュ垽瀹氫负鍖归厤锛堟棤闆朵欢=闆朵欢榻愬叏锛�
+            if (boxingDetails.Count == 0)
             {
-                BoxingIdList.Add(boxingDetails[i].ComponentCode);
-                FormulaIdList.Add(formulaDetails[i].ComponentCode);
+                return true;
             }
-            BoxingIdList.Sort();
-            FormulaIdList.Sort();
 
-            for (int i = 0; i < BoxingIdList.Count; i++)
+            // 3. 缁熻绗竴涓泦鍚堢殑鍏冪礌鍑虹幇娆℃暟锛堜弗鏍奸敭鍖归厤锛屽鐞嗛噸澶嶅厓绱狅級
+            var elementCountDict = new Dictionary<string, int>();
+            foreach (var item in boxingDetails)
             {
-                if (BoxingIdList[i] != FormulaIdList[i])
+                // 澶勭悊闆嗗悎涓殑null鍏冪礌锛岃浆涓虹┖瀛楃涓查伩鍏嶅瓧鍏搁敭涓簄ull鎶ラ敊
+                var key = item ?? string.Empty;
+                if (elementCountDict.ContainsKey(key))
                 {
-                    return false;
+                    elementCountDict[key]++;
+                }
+                else
+                {
+                    elementCountDict[key] = 1;
                 }
             }
-            return true;
+
+            // 4. 閬嶅巻绗簩涓泦鍚堬紝鎶垫秷璁℃暟锛氭棤姝ゅ厓绱�/鏁伴噺涓嶅尮閰嶇洿鎺ヨ繑鍥瀎alse
+            foreach (var item in formulaDetails)
+            {
+                var key = item ?? string.Empty;
+                if (!elementCountDict.ContainsKey(key))
+                {
+                    return false; // 瀛樺湪閰嶆柟娌℃湁/瑁呯澶氬嚭鏉ョ殑闆朵欢
+                }
+                elementCountDict[key]--;
+                if (elementCountDict[key] == 0)
+                {
+                    elementCountDict.Remove(key); // 璁℃暟娓呴浂鍒欑Щ闄わ紝閬垮厤骞叉壈鏈�缁堝垽鏂�
+                }
+            }
+
+            // 5. 瀛楀吀涓虹┖ = 鎵�鏈夊厓绱犵绫�+鏁伴噺瀹屽叏鍖归厤
+            return elementCountDict.Count == 0;
+        }
+
+
+        /// <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))
+            {
+                string componentCode = group.Key;
+                int boxCount = group.Count();
+                int formulaCount = validFormulaCodes.Count(code => code == componentCode);
+
+                if (formulaCount == 0 || formulaCount < boxCount)
+                {
+                    return componentCode;
+                }
+            }
+            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 productCode = toolingBoardSubmitDto.FinishedProductCode.Trim();
+
+                    string proCode = string.Empty;
+                    string proOther = string.Empty;
+                    string supplierCode = string.Empty;
+
+                    if (!string.IsNullOrWhiteSpace(productCode))
+                    {
+                        string[] parts = productCode.Split(new char[] { ' ', '|' }, StringSplitOptions.RemoveEmptyEntries);
+                        if (parts.Length < 3) return WebResponseContent.Instance.Error("缂栫爜瑙勫垯鏈夎锛�");
+                        proCode = parts[0];
+                        proOther = parts[1];
+                        supplierCode = parts[2];
+
+                        #region 鑾峰彇鎴愬搧缂栧彿銆佹祦姘村彿銆佷緵鏂逛唬鐮�
+                        //var proCodeMatch = Regex.Match(productCode, @"TX(\d+)(?=\s|\||$)", RegexOptions.IgnoreCase);
+                        //if (proCodeMatch.Success)
+                        //{
+                        //    proCode = $"TX{proCodeMatch.Groups[1].Value.Trim()}";
+                        //}
+
+                        //// 姝e垯2锛氬尮閰嶈繛缁殑10浣嶇函鏁板瓧锛堝叏灞�鍖归厤锛屽彇绗竴涓鍚堢殑鍗冲彲锛�
+                        //var proOtherMatch = Regex.Match(productCode, @"\d{10}");
+                        //if (proOtherMatch.Success)
+                        //{
+                        //    proOther = proOtherMatch.Value.Trim();
+                        //}
+                        //var supplierCodeMatch = Regex.Match(productCode, @"[A-Z]+$", RegexOptions.IgnoreCase);
+                        //if (supplierCodeMatch.Success)
+                        //{
+                        //    supplierCode = supplierCodeMatch.Value.Trim();
+                        //} 
+                        #endregion
+                        toolingBoardSubmitDto.PartsList = toolingBoardSubmitDto.PartsList.Where(code => !string.IsNullOrEmpty(code)).ToList();
+                        // 2. 鏌ヨ鎴愬搧閰嶆柟淇℃伅
+                        Dt_Formula formulaModel = _formulaService.Repository.QueryFirst(x => x.ProductCode == proCode);
+                        if (formulaModel == null)
+                        {
+                            return WebResponseContent.Instance.Error("鏃犳垚鍝侀厤鏂癸紝璇锋牳瀵规垚鍝佺紪鐮�");
+                        }
+
+                        Dt_ScanStation dt_ScanStation = _scanStationService.Repository.QueryFirst(x => x.StationCode == "001");
+                        if (dt_ScanStation.StationEndProduct != proCode)
+                        {
+                            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}銆戦敊璇紝璇锋牳瀵圭墿鏂欑紪鍙�");
+                        }
+
+
+
+                        //Dt_Boxing existBoxinModel = _boxingService.Repository.QueryFirst(x => x.PalletCode == PalletCode);
+
+                        //if (existBoxinModel == null)
+                        //{
+                        //    return WebResponseContent.Instance.Error($"鎵樼洏鏈坊鍔狅紝璇风◢鍚庨噸璇�");
+                        //}
+
+                        if (!CheckComponentsMatchExactly(toolingBoardSubmitDto.PartsList, Codelist))
+                        {
+                            throw new InvalidOperationException("闆朵欢涓庨厤鏂硅〃涓笉鍚�");
+                        }
+
+                        var PalletCode = TcpClientExample.Start("192.168.2.120", 2001);
+                        if (!PalletCode.IsNotEmptyOrNull() || PalletCode == "NoRead")
+                        {
+                            throw new InvalidOperationException("鎵樼洏鐮佹湭鎵埌锛岃閲嶈瘯");
+                        }
+                        Dt_Boxing dt_Boxing = _boxingService.Repository.QueryFirst(x => x.PalletCode == PalletCode);
+                        if (dt_Boxing != null)
+                        {
+                            throw new InvalidOperationException("鎵樼洏鐮侀噸澶嶆壂鎻�");
+                        }
+                        Dt_Boxing existBoxinModel = new Dt_Boxing();
+                        existBoxinModel.PalletCode = PalletCode;
+                        existBoxinModel.ProductCode = proCode;
+                        existBoxinModel.ProductName = formulaModel.ProductName;
+                        existBoxinModel.BoxingNo = proOther;
+                        existBoxinModel.SupplierCode = supplierCode;
+
+                        int id = _boxingService.Repository.AddData(existBoxinModel);
+
+                        foreach (var item in dt_FormulaDetails)
+                        {
+                            Dt_BoxingDetail dt_BoxingDetail = new Dt_BoxingDetail()
+                            {
+                                BoxingId = id,
+                                ComponentCode = item.ComponentCode,
+                                ComponentName = item.ComponentName,
+                                Creater = "admin",
+                                CreateDate = new DateTime()
+                            };
+                            BaseDal.AddData(dt_BoxingDetail);
+                        }
+                        uow.Commit();
+                    }
+
+
+
+                    OtherDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceName == "涓绘帶PLC") as OtherDevice;
+                    if (device == null) throw new Exception("鏈壘鍒颁富鎺LC璁惧淇℃伅");
+                    if (!device.IsConnected) throw new Exception($"涓绘帶PLC璁惧閫氳寮傚父");
+
+                    device.SetValue(W_PLCDBName.wboolLocation5ScanDone, true);
+                    Thread.Sleep(500);
+
+                    device.SetValue(W_PLCDBName.wboolLocation5ScanDone, false);
+
+
+                    //    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 = null;
+                    List<Dt_ProcessInfoDetail> dt_ProcessInfoDetail = _processInfoDetailService.Repository
+                            .QueryData(x => x.ProductSn == proOther && x.ProductCode == proCode).ToList();
+
+                    // 3. 鍒ゆ柇鏌ヨ缁撴灉锛屽瓨鍦ㄥ垯璧嬪�兼彁绀轰俊鎭�
+                    if (dt_ProcessInfoDetail.Count <= 3 && dt_ProcessInfoDetail.Count != 0)
+                    {
+                        msg = "宸叉湁娴佹按鍙�";
+                    }
+
+                    // 4. 杩斿洖鍝嶅簲锛堝瓨鍦ㄩ噸澶嶅垯msg鏄�"宸叉湁娴佹按鍙�"锛屼笉瀛樺湪鍒檓sg涓簄ull锛�
+                    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