| | |
| | | using MES.Service.Dto.webApi; |
| | | using MES.Service.Modes; |
| | | using System.Linq; |
| | | using MES.Service.util; |
| | | |
| | | namespace MES.Service.service.BasicData; |
| | | |
| | |
| | | } |
| | | |
| | | /// <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> |
| | | /// <param name="deliveryNo">送货单号</param> |
| | |
| | | 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> |
| | |
| | | |
| | | return deleteRowCount > 0 ? true : throw new NotImplementedException($"批量条码删除失败:共{ids.Length}个Id"); |
| | | } |
| | | |
| | | #endregion |
| | | } |