From 37379aa1d6e95c1964419b32031aaccc302888c3 Mon Sep 17 00:00:00 2001 From: kyy <3283105747@qq.com> Date: 星期六, 30 八月 2025 09:43:32 +0800 Subject: [PATCH] 1、新增批量按送货单删除条码、按明细删除条码 --- MES.Service/Dto/webApi/DeleteByDeliveryItemRequest.cs | 7 + MES.Service/service/BasicData/DeliveryBarcodeManager.cs | 160 ++++++++++++++++++++++++-- MESApplication/Controllers/BasicData/DeliveryBarcodeController.cs | 131 +++++++++++++++++++++ MES.Service/Dto/webApi/DeleteByDeliveryNoRequest.cs | 14 ++ MES.Service/util/ResponseResult.cs | 27 ++++ 5 files changed, 319 insertions(+), 20 deletions(-) diff --git a/MES.Service/Dto/webApi/DeleteByDeliveryItemRequest.cs b/MES.Service/Dto/webApi/DeleteByDeliveryItemRequest.cs index ecb4e85..dcaf0ce 100644 --- a/MES.Service/Dto/webApi/DeleteByDeliveryItemRequest.cs +++ b/MES.Service/Dto/webApi/DeleteByDeliveryItemRequest.cs @@ -19,4 +19,11 @@ /// </summary> [Required(ErrorMessage = "閫佽揣鍗曡鍐呯爜 LineNo 涓嶈兘涓虹┖")] public string LineNo { get; set; } +} +/// <summary> +/// 鎵归噺鎸夐�佽揣鍗曞彿+琛屽唴鐮佸垹闄ょ殑璇锋眰妯″瀷 +/// </summary> +public class BatchDeleteByDeliveryItemRequest : List<DeleteByDeliveryItemRequest> +{ + // 缁ф壙鑷狶ist<DeleteByDeliveryItemRequest>锛岀洿鎺ユ帴鏀舵暟缁勬牸寮� } \ No newline at end of file diff --git a/MES.Service/Dto/webApi/DeleteByDeliveryNoRequest.cs b/MES.Service/Dto/webApi/DeleteByDeliveryNoRequest.cs index 4c6a00a..7f0c985 100644 --- a/MES.Service/Dto/webApi/DeleteByDeliveryNoRequest.cs +++ b/MES.Service/Dto/webApi/DeleteByDeliveryNoRequest.cs @@ -1,9 +1,10 @@ -锘縰sing System.ComponentModel.DataAnnotations; +锘縰sing System.Collections.Generic; +using System.ComponentModel.DataAnnotations; namespace MES.Service.Dto.webApi; /// <summary> -/// 鏍规嵁閫佽揣鍗曞彿鍒犻櫎鏉$爜鐨勮姹傛ā鍨� +/// 鏍规嵁閫佽揣鍗曞彿鍒犻櫎鏉$爜鐨勮姹傛ā鍨嬶紙鍗曚釜锛� /// </summary> public class DeleteByDeliveryNoRequest { @@ -12,4 +13,13 @@ /// </summary> [Required(ErrorMessage = "閫佽揣鍗曞彿 DeliveryNo 涓嶈兘涓虹┖")] public string DeliveryNo { get; set; } + + } + +/// <summary> +/// 鎵归噺鏍规嵁閫佽揣鍗曞彿鍒犻櫎鐨勮姹傛ā鍨� +/// </summary> +public class BatchDeleteByDeliveryNoRequest : List<DeleteByDeliveryNoRequest> +{ + // 缁ф壙鑷狶ist<DeliveryNoItem>锛岀洿鎺ユ帴鏀舵暟缁勬牸寮� } \ No newline at end of file diff --git a/MES.Service/service/BasicData/DeliveryBarcodeManager.cs b/MES.Service/service/BasicData/DeliveryBarcodeManager.cs index 515e5fb..6352d5f 100644 --- a/MES.Service/service/BasicData/DeliveryBarcodeManager.cs +++ b/MES.Service/service/BasicData/DeliveryBarcodeManager.cs @@ -2,6 +2,7 @@ using MES.Service.Dto.webApi; using MES.Service.Modes; using System.Linq; +using MES.Service.util; namespace MES.Service.service.BasicData; @@ -34,8 +35,8 @@ return DeleteBarcode(barcodeEntity.Id); default: throw new ArgumentOutOfRangeException( - nameof(barcodeDto.Type), - barcodeDto.Type, + nameof(barcodeDto.Type), + barcodeDto.Type, "鏉$爜鎿嶄綔绫诲瀷閿欒锛氫粎鏀寔 0锛堟柊澧烇級銆�1锛堝垹闄わ級"); } } @@ -74,7 +75,7 @@ var typeGroupDict = entityTypePairs .GroupBy(pair => pair.Type) .ToDictionary( - group => group.Key, + group => group.Key, group => group.Select(pair => pair.Entity).ToList() ); @@ -93,8 +94,8 @@ break; default: throw new ArgumentOutOfRangeException( - nameof(type), - type, + nameof(type), + type, "鎵归噺鎿嶄綔涓瓨鍦ㄩ潪娉昑ype锛氫粎鏀寔 0锛堟柊澧烇級銆�1锛堝垹闄わ級"); } } @@ -112,7 +113,7 @@ throw new ApplicationException($"鎵归噺鏉$爜鎿嶄綔澶辫触锛堟�绘潯鏁帮細{barcodeDtoList.Count}锛夛細{ex.Message}", ex); } } - + /// <summary> /// 鏍规嵁閫佽揣鍗曞彿鍒犻櫎鏉$爜鏁版嵁 /// </summary> @@ -143,7 +144,60 @@ throw new ApplicationException($"鏍规嵁閫佽揣鍗曞彿鍒犻櫎鏉$爜鏁版嵁澶辫触锛圖eliveryNo锛歿deliveryNo}锛夛細{ex.Message}", ex); } } - + + /// <summary> + /// 鎵归噺鎸夐�佽揣鍗曞彿鍒犻櫎鏉$爜鏁版嵁锛堟帴鏀舵暟缁勯」鍒楄〃锛� + /// </summary> + /// <param name="deliveryNoItems">鍖呭惈DeliveryNo鐨勫璞″垪琛�</param> + /// <returns>鎵归噺鍒犻櫎缁撴灉</returns> + public BatchDeleteResult DeleteListByDeliveryNo(List<DeleteByDeliveryNoRequest> deliveryNoItems) + { + // 1. 鍙傛暟鏍¢獙涓庡鐞嗭紙浠庢帶鍒跺櫒绉昏繃鏉ョ殑閫昏緫锛� + if (deliveryNoItems == null || !deliveryNoItems.Any()) + throw new ArgumentException("閫佽揣鍗曞彿鍒楄〃涓嶈兘涓虹┖", nameof(deliveryNoItems)); + + // 鎻愬彇骞堕獙璇侀�佽揣鍗曞彿鍒楄〃 + var deliveryNoList = deliveryNoItems + .Select(item => item.DeliveryNo) // 浠庡璞′腑鎻愬彇DeliveryNo瀛楁 + .Where(no => !string.IsNullOrWhiteSpace(no)) // 杩囨护绌哄��/绌烘牸 + .Distinct() // 鍘婚噸锛岄伩鍏嶉噸澶嶅垹闄� + .ToList(); + + // 鍒ゆ柇鏄惁鏈夋湁鏁堝崟鍙� + if (!deliveryNoList.Any()) + throw new ArgumentException("璇锋眰涓病鏈夋湁鏁堢殑閫佽揣鍗曞彿锛堝潎涓虹┖鎴栫┖鏍硷級"); + + try + { + // 2. 鏌ヨ鏁版嵁搴撲腑瀛樺湪鐨勯�佽揣鍗曞彿 + var existingNos = Db.Queryable<BarcodeInformation>() + .Where(barcode => deliveryNoList.Contains(barcode.DeliveryNo)) + .Select(barcode => barcode.DeliveryNo) + .Distinct() + .ToList(); + + // 3. 鎵ц鎵归噺鍒犻櫎 + int totalDeleted = Db.Deleteable<BarcodeInformation>() + .Where(barcode => deliveryNoList.Contains(barcode.DeliveryNo)) + .ExecuteCommand(); + + // 4. 杩斿洖缁撴灉 + return new BatchDeleteResult + { + TotalRequested = deliveryNoList.Count, + TotalDeleted = totalDeleted, + DeletedNos = existingNos, + NotFoundNos = deliveryNoList.Except(existingNos).ToList() + }; + } + catch (Exception ex) + { + throw new ApplicationException( + $"鎵归噺鍒犻櫎澶辫触锛堝叡{deliveryNoList.Count}涓崟鍙凤級锛歿ex.Message}", + ex); + } + } + /// <summary> /// 鎸夐�佽揣鍗曞彿+琛屽唴鐮佽仈鍚堝垹闄ゆ潯鐮佹暟鎹� /// </summary> @@ -162,9 +216,9 @@ { // 2. 鑱斿悎鏉′欢鍒犻櫎锛氬尮閰� DeliveryNo锛堥�佽揣鍗曞彿锛夊拰 DnLines锛堣鍐呯爜锛屽疄浣撳瓧娈靛搴旇〃鐨� dnLines 鍒楋級 int deletedCount = Db.Deleteable<BarcodeInformation>() - .Where(barcode => + .Where(barcode => barcode.DeliveryNo == deliveryNo // 鍖归厤閫佽揣鍗曞彿 - && barcode.DnLines == lineNo // 鍖归厤琛屽唴鐮侊紙瀹炰綋 DnLines 瀵瑰簲琛� dnLines 瀛楁锛� + && barcode.DnLines == lineNo // 鍖归厤琛屽唴鐮侊紙瀹炰綋 DnLines 瀵瑰簲琛� dnLines 瀛楁锛� ) .ExecuteCommand(); // 杩斿洖鍙楀奖鍝嶇殑琛屾暟 @@ -179,11 +233,88 @@ { // 4. 鍖呰寮傚父锛氳ˉ鍏呰仈鍚堟潯浠朵笂涓嬫枃锛屼究浜庡畾浣嶉棶棰� throw new ApplicationException( - $"鎸夐�佽揣鍗曞彿+琛屽唴鐮佸垹闄ゆ潯鐮佹暟鎹け璐ワ紙DeliveryNo锛歿deliveryNo}锛孡ineNo锛歿lineNo}锛夛細{ex.Message}", + $"鎸夐�佽揣鍗曞彿+琛屽唴鐮佸垹闄ゆ潯鐮佹暟鎹け璐ワ紙DeliveryNo锛歿deliveryNo}锛孡ineNo锛歿lineNo}锛夛細{ex.Message}", ex); } } + + /// <summary> + /// 鎵归噺鎸夐�佽揣鍗曞彿+琛屽唴鐮佸垹闄ゆ潯鐮佹暟鎹� + /// </summary> + /// <param name="deliveryItems">鍖呭惈DeliveryNo鍜孡ineNo鐨勫璞″垪琛�</param> + /// <returns>鎵归噺鍒犻櫎缁撴灉</returns> + public BatchDeleteResult DeleteListByDeliveryItem(List<DeleteByDeliveryItemRequest> deliveryItems) + { + // 1. 鍙傛暟鏍¢獙涓庡鐞� + if (deliveryItems == null || !deliveryItems.Any()) + throw new ArgumentException("閫佽揣鍗曡鍒楄〃涓嶈兘涓虹┖", nameof(deliveryItems)); + + // 鎻愬彇骞堕獙璇侀�佽揣鍗曞彿+琛屽唴鐮佺粍鍚� + var validItems = deliveryItems + .Where(item => + !string.IsNullOrWhiteSpace(item.DeliveryNo) && + !string.IsNullOrWhiteSpace(item.LineNo) + ) + .Select(item => new + { + DeliveryNo = item.DeliveryNo, + LineNo = item.LineNo + }) + .Distinct() // 鍘婚噸鐩稿悓鐨勭粍鍚� + .ToList(); + + if (!validItems.Any()) + throw new ArgumentException("璇锋眰涓病鏈夋湁鏁堢殑鏁堢殑閫佽揣鍗曡鏁版嵁锛堝潎涓虹┖鎴栫┖鏍硷級"); + + try + { + // 2. 鎵ц鎵归噺鍒犻櫎锛堟寜DeliveryNo+LineNo缁勫悎鏉′欢锛� + int totalDeleted = 0; + var deletedItems = new List<string>(); + var notFoundItems = new List<string>(); + + // 閬嶅巻姣忎釜缁勫悎鎵ц鍒犻櫎锛堟垨浣跨敤鎵归噺鏉′欢鍒犻櫎锛� + foreach (var item in validItems) + { + // 妫�鏌ュ綋鍓嶇粍鍚堟槸鍚﹀瓨鍦� + var exists = Db.Queryable<BarcodeInformation>() + .Any(b => b.DeliveryNo == item.DeliveryNo && b.DnLines == item.LineNo); + + if (exists) + { + // 鎵ц鍒犻櫎 + var deleted = Db.Deleteable<BarcodeInformation>() + .Where(b => b.DeliveryNo == item.DeliveryNo && b.DnLines == item.LineNo) + .ExecuteCommand(); + + totalDeleted += deleted; + deletedItems.Add($"{item.DeliveryNo}_{item.LineNo}"); + } + else + { + notFoundItems.Add($"{item.DeliveryNo}_{item.LineNo}"); + } + } + + // 3. 杩斿洖缁撴灉 + return new BatchDeleteResult + { + TotalRequested = validItems.Count, + TotalDeleted = totalDeleted, + DeletedNos = deletedItems, // 瀛樺偍鏍煎紡锛�"DeliveryNo_LineNo" + NotFoundNos = notFoundItems // 瀛樺偍鏍煎紡锛�"DeliveryNo_LineNo" + }; + } + catch (Exception ex) + { + throw new ApplicationException( + $"鎵归噺鍒犻櫎閫佽揣鍗曡澶辫触锛堝叡{validItems.Count}鏉★級锛歿ex.Message}", + ex); + } + } + #region 绉佹湁杈呭姪鏂规硶鍜屽唴閮ㄧ被 + /// <summary> /// 鍐呴儴杈呭姪绫伙細鐢ㄤ簬鍏宠仈瀹炰綋鍜屾搷浣滅被鍨嬶紙瑙e喅dynamic绫诲瀷杞崲闂锛� /// </summary> @@ -198,8 +329,8 @@ /// </summary> private BarcodeInformation ConvertDtoToEntity(DeliveryBarcodeInfo dto) { - var entityId = dto.Type == "0" - ? Guid.NewGuid() + var entityId = dto.Type == "0" + ? Guid.NewGuid() : (string.IsNullOrEmpty(dto.SmallBarcode) ? Guid.Empty : Guid.Parse(dto.SmallBarcode)); return new BarcodeInformation @@ -214,7 +345,7 @@ PackLevel = dto.BarcodeType, CreateTime = dto.Type == "0" ? DateTime.Now : (DateTime?)null, UpdateTime = DateTime.Now, - + // 鎵╁睍瀛楁璧嬮粯璁ゅ�� BigBarcode = null, SmallPackageLength = null, @@ -287,5 +418,6 @@ return deleteRowCount > 0 ? true : throw new NotImplementedException($"鎵归噺鏉$爜鍒犻櫎澶辫触锛氬叡{ids.Length}涓狪d"); } + #endregion -} +} \ No newline at end of file diff --git a/MES.Service/util/ResponseResult.cs b/MES.Service/util/ResponseResult.cs index 830da6b..4d3ef07 100644 --- a/MES.Service/util/ResponseResult.cs +++ b/MES.Service/util/ResponseResult.cs @@ -25,4 +25,31 @@ data = e.Message }; } +} + +/// <summary> +/// 鎵归噺鍒犻櫎鎿嶄綔鐨勭粨鏋滀俊鎭� +/// 鐢ㄤ簬杩斿洖鎵归噺鍒犻櫎鐨勮缁嗙粺璁℃暟鎹� +/// </summary> +public class BatchDeleteResult +{ + /// <summary> + /// 璇锋眰鍒犻櫎鐨勯�佽揣鍗曞彿鎬绘暟锛堝幓閲嶅悗锛� + /// </summary> + public int TotalRequested { get; set; } + + /// <summary> + /// 瀹為檯鍒犻櫎鐨勮褰曟�绘暟 + /// </summary> + public int TotalDeleted { get; set; } + + /// <summary> + /// 鎴愬姛鍒犻櫎鐨勯�佽揣鍗曞彿鍒楄〃 + /// </summary> + public List<string> DeletedNos { get; set; } = new List<string>(); + + /// <summary> + /// 鏈壘鍒扮殑閫佽揣鍗曞彿鍒楄〃 + /// </summary> + public List<string> NotFoundNos { get; set; } = new List<string>(); } \ No newline at end of file diff --git a/MESApplication/Controllers/BasicData/DeliveryBarcodeController.cs b/MESApplication/Controllers/BasicData/DeliveryBarcodeController.cs index e7a0768..d6b35e3 100644 --- a/MESApplication/Controllers/BasicData/DeliveryBarcodeController.cs +++ b/MESApplication/Controllers/BasicData/DeliveryBarcodeController.cs @@ -137,7 +137,7 @@ return ResponseResult.ResponseError(ex); } } - + /// <summary> /// 鏍规嵁閫佽揣鍗曞彿鍒犻櫎鏉$爜鏁版嵁 /// </summary> @@ -194,6 +194,68 @@ } /// <summary> + /// 鎵归噺鏍规嵁閫佽揣鍗曞彿鍒犻櫎鏉$爜鏁版嵁 + /// </summary> + /// <param name="request">鍖呭惈鎵归噺閫佽揣鍗曞彿鐨勮姹傛ā鍨嬶紙鏁扮粍鏍煎紡锛�</param> + /// <returns>缁熶竴鍝嶅簲缁撴灉</returns> + [HttpPost("DeleteListByNo")] + public ResponseResult DeleteListByNo([FromBody] BatchDeleteByDeliveryNoRequest request) + { + // 1. 鍒濆鍖栨秷鎭腑蹇冨疄浣� + var messageEntity = new MessageCenter + { + TableName = TARGET_TABLE, + Url = BASE_API_URL + "DeleteListByNo", + Method = REQUEST_METHOD, + Data = JsonConvert.SerializeObject(request), + Status = 1, // 澶勭悊涓� + CreateBy = "PL017", + // 璺敱鏍囪瘑锛氭樉绀哄墠3涓崟鍙� + Route = + $"Batch_{string.Join(",", request.Take(3).Select(item => item.DeliveryNo))}{(request.Count > 3 ? "..." : "")}", + DealWith = 0 // 鏈鐞� + }; + + try + { + // 2. 鐩存帴璋冪敤涓氬姟灞傦紙涓嶅啀鍦ㄦ帶鍒跺櫒澶勭悊鍒楄〃锛岄�昏緫绉昏嚦涓氬姟灞傦級 + dynamic resultData = new ExpandoObject(); + BatchDeleteResult deleteResult = _deliveryBarcodeManager.DeleteListByDeliveryNo(request); + + // 3. 缁勮杩斿洖鏁版嵁 + resultData.totalRequested = deleteResult.TotalRequested; + resultData.totalDeleted = deleteResult.TotalDeleted; + resultData.deletedNos = deleteResult.DeletedNos; + resultData.notFoundNos = deleteResult.NotFoundNos; + resultData.isSuccess = deleteResult.TotalDeleted > 0; + + // 4. 鏇存柊娑堟伅涓績鐘舵�侊紙鎴愬姛锛� + messageEntity.Result = 1; + messageEntity.DealWith = 1; + messageEntity.ResultData = $"鎵归噺鍒犻櫎瀹屾垚锛屽叡澶勭悊{deleteResult.TotalRequested}涓崟鍙�"; + _messageCenterManager.save(messageEntity); + + // 5. 杩斿洖鎴愬姛鍝嶅簲 + return new ResponseResult + { + status = 0, + message = $"鎴愬姛鍒犻櫎{deleteResult.DeletedNos.Count}涓�佽揣鍗曞彿瀵瑰簲鐨勬潯鐮佹暟鎹紝鍏眥deleteResult.TotalDeleted}鏉¤褰�", + data = resultData + }; + } + catch (Exception ex) + { + // 6. 寮傚父澶勭悊 + messageEntity.Result = 0; + messageEntity.DealWith = 0; + messageEntity.ResultData = ex.Message; + _messageCenterManager.save(messageEntity); + + return ResponseResult.ResponseError(ex); + } + } + + /// <summary> /// 鎸夐�佽揣鍗曞彿+琛屽唴鐮佽仈鍚堝垹闄ゆ潯鐮佹暟鎹� /// </summary> /// <param name="request">鍖呭惈閫佽揣鍗曞彿鍜岃鍐呯爜鐨勮姹傛ā鍨�</param> @@ -219,7 +281,7 @@ // 2. 璋冪敤涓氬姟灞傝仈鍚堝垹闄ゆ柟娉� dynamic resultData = new ExpandoObject(); bool deleteSuccess = _deliveryBarcodeManager.DeleteByDeliveryItem( - request.DeliveryNo, + request.DeliveryNo, request.LineNo ); // 鍝嶅簲涓繑鍥炲叧閿潯浠讹紝渚夸簬鍓嶇纭鍒犻櫎鑼冨洿 @@ -251,6 +313,67 @@ // 6. 杩斿洖缁熶竴寮傚父鍝嶅簲 return ResponseResult.ResponseError(ex); } - } -} + } + /// <summary> + /// 鎵归噺鎸夐�佽揣鍗曞彿+琛屽唴鐮佸垹闄ゆ潯鐮佹暟鎹� + /// </summary> + /// <param name="request">鍖呭惈鎵归噺閫佽揣鍗曡鐨勮姹傛ā鍨嬶紙鏁扮粍鏍煎紡锛�</param> + /// <returns>缁熶竴鍝嶅簲缁撴灉</returns> + [HttpPost("DeleteListByItem")] + public ResponseResult DeleteListByItem([FromBody] BatchDeleteByDeliveryItemRequest request) + { + // 1. 鍒濆鍖栨秷鎭腑蹇冨疄浣� + var messageEntity = new MessageCenter + { + TableName = TARGET_TABLE, + Url = BASE_API_URL + "DeleteListByItem", + Method = REQUEST_METHOD, + Data = JsonConvert.SerializeObject(request), + Status = 1, // 澶勭悊涓� + CreateBy = "PL017", + // 璺敱鏍囪瘑锛氭樉绀哄墠3涓�"DeliveryNo_LineNo"缁勫悎 + Route = + $"BatchItem_{string.Join(",", request.Take(3).Select(item => $"{item.DeliveryNo}_{item.LineNo}"))}{(request.Count > 3 ? "..." : "")}", + DealWith = 0 // 鏈鐞� + }; + + try + { + // 2. 璋冪敤涓氬姟灞傛墽琛屾壒閲忓垹闄� + dynamic resultData = new ExpandoObject(); + BatchDeleteResult deleteResult = _deliveryBarcodeManager.DeleteListByDeliveryItem(request); + + // 3. 缁勮杩斿洖鏁版嵁 + resultData.totalRequested = deleteResult.TotalRequested; + resultData.totalDeleted = deleteResult.TotalDeleted; + resultData.deletedItems = deleteResult.DeletedNos; // 鏍煎紡锛歔"DN1_1", "DN1_2"] + resultData.notFoundItems = deleteResult.NotFoundNos; + resultData.isSuccess = deleteResult.TotalDeleted > 0; + + // 4. 鏇存柊娑堟伅涓績鐘舵�侊紙鎴愬姛锛� + messageEntity.Result = 1; + messageEntity.DealWith = 1; + messageEntity.ResultData = $"鎵归噺鍒犻櫎瀹屾垚锛屽叡澶勭悊{deleteResult.TotalRequested}鏉¢�佽揣鍗曡"; + _messageCenterManager.save(messageEntity); + + // 5. 杩斿洖鎴愬姛鍝嶅簲 + return new ResponseResult + { + status = 0, + message = $"鎴愬姛鍒犻櫎{deleteResult.DeletedNos.Count}鏉¢�佽揣鍗曡鏁版嵁锛屽叡{deleteResult.TotalDeleted}鏉¤褰�", + data = resultData + }; + } + catch (Exception ex) + { + // 6. 寮傚父澶勭悊 + messageEntity.Result = 0; + messageEntity.DealWith = 0; + messageEntity.ResultData = ex.Message; + _messageCenterManager.save(messageEntity); + + return ResponseResult.ResponseError(ex); + } + } +} \ No newline at end of file -- Gitblit v1.9.3