From 15b3879cd259108e7ebb755fe02c190f28f1e20c Mon Sep 17 00:00:00 2001
From: leiqunqing <zhengqifeng@hnkhzn.com>
Date: 星期五, 06 二月 2026 14:46:23 +0800
Subject: [PATCH] 完善项目

---
 代码管理/WIDESEAWCS_Server/WIDESEAWCS_Server/WIDESEAWCS_BasicInfoService/BoxingDetailService.cs |  354 +++++++++++++++++++++++++++++++++++++---------------------
 1 files changed, 227 insertions(+), 127 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 7db9778..03b6dce 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"
@@ -4,11 +4,15 @@
 using System.Linq;
 using System.Text;
 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_Model.Models;
+using WIDESEAWCS_QuartzJob;
 
 namespace WIDESEAWCS_BasicInfoService
 {
@@ -39,18 +43,55 @@
         /// <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 = boxingDetails.Select(x => x.ComponentCode).ToList();
-            List<string> FormulaIdList = formulaDetails.Select(x => x.ComponentCode).ToList();
-            return !BoxingIdList.Except(FormulaIdList).Any() && !FormulaIdList.Except(BoxingIdList).Any();
+            // 2. 涓や釜绌洪泦鍚堢洿鎺ュ垽瀹氫负鍖归厤锛堟棤闆朵欢=闆朵欢榻愬叏锛�
+            if (boxingDetails.Count == 0)
+            {
+                return true;
+            }
+
+            // 3. 缁熻绗竴涓泦鍚堢殑鍏冪礌鍑虹幇娆℃暟锛堜弗鏍奸敭鍖归厤锛屽鐞嗛噸澶嶅厓绱狅級
+            var elementCountDict = new Dictionary<string, int>();
+            foreach (var item in boxingDetails)
+            {
+                // 澶勭悊闆嗗悎涓殑null鍏冪礌锛岃浆涓虹┖瀛楃涓查伩鍏嶅瓧鍏搁敭涓簄ull鎶ラ敊
+                var key = item ?? string.Empty;
+                if (elementCountDict.ContainsKey(key))
+                {
+                    elementCountDict[key]++;
+                }
+                else
+                {
+                    elementCountDict[key] = 1;
+                }
+            }
+
+            // 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;
         }
 
 
@@ -100,135 +141,194 @@
                     {
                         return WebResponseContent.Instance.Error("鎻愪氦鍙傛暟涓嶈兘涓虹┖");
                     }
-                    string palletCode = toolingBoardSubmitDto.ToolingBoardNo?.Trim();
                     string productCode = toolingBoardSubmitDto.FinishedProductCode?.Trim();
-                    if (string.IsNullOrWhiteSpace(palletCode))
+                    if (!string.IsNullOrWhiteSpace(productCode))
                     {
-                        return WebResponseContent.Instance.Error("宸ヨ鏉跨紪鍙蜂笉鑳戒负绌�");
-                    }
-                    if (string.IsNullOrWhiteSpace(productCode))
-                    {
-                        return WebResponseContent.Instance.Error("鎴愬搧缂栫爜涓嶈兘涓虹┖");
-                    }
+                        string proCode = string.Empty;
+                        string proOther = string.Empty;
 
-                    // 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()
+                        var proCodeMatch = System.Text.RegularExpressions.Regex.Match(productCode, @"TX(\d+)(?=\s|\||$)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
+                        if (proCodeMatch.Success)
                         {
-                            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);
+                            proCode = $"TX{proCodeMatch.Groups[1].Value.Trim()}"; // 鎷兼帴TX+鏁板瓧锛屼繚鎸佹牸寮忕粺涓�
                         }
 
-                        // 3. 閲嶆柊鎻掑叆淇敼鍚庣殑鏄庣粏琛ㄦ暟鎹�
-                        if (toolingBoardSubmitDto.PartsList != null && toolingBoardSubmitDto.PartsList.Count > 0)
+                        // 姝e垯2锛氬尮閰嶈繛缁殑10浣嶇函鏁板瓧锛堝叏灞�鍖归厤锛屽彇绗竴涓鍚堢殑鍗冲彲锛�
+                        var proOtherMatch = System.Text.RegularExpressions.Regex.Match(productCode, @"\d{10}");
+                        if (proOtherMatch.Success)
                         {
-                            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);
-                            }
+                            proOther = proOtherMatch.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("鏃犳垚鍝侀厤鏂癸紝璇锋牳瀵规垚鍝佺紪鐮�");
+                        }
+
+                        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 existBoxinModel = new Dt_Boxing();
+                        existBoxinModel.PalletCode = PalletCode;
+                        existBoxinModel.ProductCode = proCode;
+                        existBoxinModel.BoxingNo = proOther;
+
+                        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();
                     }
-                    uow.Commit();
-                    string msg = existBoxinModel == null ? "缁勭洏淇℃伅鏂板鎴愬姛锛�" : "缁勭洏淇℃伅淇敼鎴愬姛锛�";
-                    return WebResponseContent.Instance.OK(msg);
+
+                    
+
+                    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 = existBoxinModel == null ? "缁勭洏淇℃伅鏂板鎴愬姛锛�" : "缁勭洏淇℃伅淇敼鎴愬姛锛�";
+
+                    return WebResponseContent.Instance.OK();
                 }
                 catch (Exception ex)
                 {

--
Gitblit v1.9.3