| | |
| | | using 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 CheckComponentsMatchExactly(List<string> boxingDetails, List<string> formulaDetails) |
| | | { |
| | | // 1. åºç¡æ ¡éªï¼ç©ºå¼/æ°éä¸ä¸è´ç´æ¥è¿åä¸å¹é
|
| | | if (boxingDetails == null || formulaDetails == null || boxingDetails.Count != formulaDetails.Count) |
| | | { |
| | | return false; |
| | | } |
| | | // 2. 两个空éåç´æ¥å¤å®ä¸ºå¹é
ï¼æ é¶ä»¶=é¶ä»¶é½å
¨ï¼ |
| | | if (boxingDetails.Count == 0) |
| | | { |
| | | return true; |
| | | } |
| | | |
| | | // 3. ç»è®¡ç¬¬ä¸ä¸ªéåçå
ç´ åºç°æ¬¡æ°ï¼ä¸¥æ ¼é®å¹é
ï¼å¤çéå¤å
ç´ ï¼ |
| | | var elementCountDict = new Dictionary<string, int>(); |
| | | foreach (var item in boxingDetails) |
| | | { |
| | | // å¤çéåä¸çnullå
ç´ ï¼è½¬ä¸ºç©ºå符串é¿å
åå
¸é®ä¸ºnullæ¥é |
| | | var key = item ?? string.Empty; |
| | | if (elementCountDict.ContainsKey(key)) |
| | | { |
| | | elementCountDict[key]++; |
| | | } |
| | | else |
| | | { |
| | | elementCountDict[key] = 1; |
| | | } |
| | | } |
| | | |
| | | // 4. éå第äºä¸ªéåï¼æµæ¶è®¡æ°ï¼æ æ¤å
ç´ /æ°éä¸å¹é
ç´æ¥è¿åfalse |
| | | 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()}"; |
| | | //} |
| | | |
| | | //// æ£å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("æªæ¾å°ä¸»æ§PLC设å¤ä¿¡æ¯"); |
| | | 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æ¯"å·²ææµæ°´å·"ï¼ä¸åå¨åmsg为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}"); |
| | | } |
| | | } |
| | | } |
| | | } |