MES.Service/Dto/webApi/DeleteByDeliveryItemRequest.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
MES.Service/Dto/webApi/DeleteByDeliveryNoRequest.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
MES.Service/service/BasicData/DeliveryBarcodeManager.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
MES.Service/util/ResponseResult.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
MESApplication/Controllers/BasicData/DeliveryBarcodeController.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
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> { // 继承自List<DeleteByDeliveryItemRequest>,直接接收数组格式 } MES.Service/Dto/webApi/DeleteByDeliveryNoRequest.cs
@@ -1,9 +1,10 @@ using System.ComponentModel.DataAnnotations; using 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> { // 继承自List<DeliveryNoItem>,直接接收数组格式 } 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, "批量操作中存在非法Type:仅支持 0(新增)、1(删除)"); } } @@ -112,7 +113,7 @@ throw new ApplicationException($"批量条码操作失败(总条数:{barcodeDtoList.Count}):{ex.Message}", ex); } } /// <summary> /// 根据送货单号删除条码数据 /// </summary> @@ -143,7 +144,60 @@ throw new ApplicationException($"根据送货单号删除条码数据失败(DeliveryNo:{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},LineNo:{lineNo}):{ex.Message}", $"按送货单号+行内码删除条码数据失败(DeliveryNo:{deliveryNo},LineNo:{lineNo}):{ex.Message}", ex); } } /// <summary> /// 批量按送货单号+行内码删除条码数据 /// </summary> /// <param name="deliveryItems">包含DeliveryNo和LineNo的对象列表</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> /// 内部辅助类:用于关联实体和操作类型(解决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}个Id"); } #endregion } } 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>(); } 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); } } }