sjz
2025-09-12 0c1a03e880bbf795b771a9c8ee4b7b271c262b58
MES.Service/service/BasicData/DeliveryBarcodeManager.cs
@@ -34,10 +34,7 @@
                case "1":
                    return DeleteBarcode(barcodeEntity.Id);
                default:
                    throw new ArgumentOutOfRangeException(
                        nameof(barcodeDto.Type),
                        barcodeDto.Type,
                        "条码操作类型错误:仅支持 0(新增)、1(删除)");
                    throw new ArgumentOutOfRangeException( nameof(barcodeDto.Type), barcodeDto.Type, "条码操作类型错误:仅支持 0(新增)、1(删除)");
            }
        }
        catch (Exception ex)
@@ -72,12 +69,7 @@
            }
            // 2. 按操作类型Type分组(使用强类型避免类型推断问题)
            var typeGroupDict = entityTypePairs
                .GroupBy(pair => pair.Type)
                .ToDictionary(
                    group => group.Key,
                    group => group.Select(pair => pair.Entity).ToList()
                );
            var typeGroupDict = entityTypePairs.GroupBy(pair => pair.Type).ToDictionary(group => group.Key,group => group.Select(pair => pair.Entity).ToList());
            // 3. 存储各组操作结果
            var groupResultList = new List<bool>();
@@ -93,10 +85,7 @@
                        groupResultList.Add(DeleteBarcodeBatch(entityGroup.Select(e => e.Id).ToArray()));
                        break;
                    default:
                        throw new ArgumentOutOfRangeException(
                            nameof(type),
                            type,
                            "批量操作中存在非法Type:仅支持 0(新增)、1(删除)");
                        throw new ArgumentOutOfRangeException(nameof(type),type,"批量操作中存在非法Type:仅支持 0(新增)、1(删除)");
                }
            }
@@ -123,8 +112,9 @@
    {
        // 1. 校验参数:送货单号不能为空
        if (string.IsNullOrWhiteSpace(deliveryNo))
        {
            throw new ArgumentException("送货单号 DeliveryNo 不能为空或空格", nameof(deliveryNo));
        }
        try
        {
            // 2. 执行删除:根据 DeliveryNo 匹配表中数据(对应实体的 DeliveryNo 字段)
@@ -134,8 +124,9 @@
            // 3. 判断结果:至少删除1条则视为成功,否则抛出“未找到数据”异常
            if (deletedCount > 0)
            {
                return true;
            }
            throw new KeyNotFoundException($"未找到送货单号为「{deliveryNo}」的条码数据,删除操作未执行");
        }
        catch (Exception ex)
@@ -154,8 +145,9 @@
    {
        // 1. 参数校验与处理(从控制器移过来的逻辑)
        if (deliveryNoItems == null || !deliveryNoItems.Any())
        {
            throw new ArgumentException("送货单号列表不能为空", nameof(deliveryNoItems));
        }
        // 提取并验证送货单号列表
        var deliveryNoList = deliveryNoItems
            .Select(item => item.DeliveryNo) // 从对象中提取DeliveryNo字段
@@ -165,21 +157,16 @@
        // 判断是否有有效单号
        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();
            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();
            int totalDeleted = Db.Deleteable<BarcodeInformation>().Where(barcode => deliveryNoList.Contains(barcode.DeliveryNo)).ExecuteCommand();
            // 4. 返回结果
            return new BatchDeleteResult
@@ -192,9 +179,7 @@
        }
        catch (Exception ex)
        {
            throw new ApplicationException(
                $"批量删除失败(共{deliveryNoList.Count}个单号):{ex.Message}",
                ex);
            throw new ApplicationException($"批量删除失败(共{deliveryNoList.Count}个单号):{ex.Message}",ex);
        }
    }
@@ -208,33 +193,31 @@
    {
        // 1. 双参数校验:避免空值/空字符串
        if (string.IsNullOrWhiteSpace(deliveryNo))
        {
            throw new ArgumentException("送货单号 DeliveryNo 不能为空或空格", nameof(deliveryNo));
        }
        if (string.IsNullOrWhiteSpace(lineNo))
        {
            throw new ArgumentException("送货单行内码 LineNo 不能为空或空格", nameof(lineNo));
        }
        try
        {
            // 2. 联合条件删除:匹配 DeliveryNo(送货单号)和 DnLines(行内码,实体字段对应表的 dnLines 列)
            int deletedCount = Db.Deleteable<BarcodeInformation>()
                .Where(barcode =>
                        barcode.DeliveryNo == deliveryNo // 匹配送货单号
                        && barcode.DnLines == lineNo // 匹配行内码(实体 DnLines 对应表 dnLines 字段)
                )
            int deletedCount = Db.Deleteable<BarcodeInformation>().Where(barcode =>barcode.DeliveryNo == deliveryNo && barcode.DnLines == lineNo) // 匹配行内码(实体 DnLines 对应表 dnLines 字段)
                .ExecuteCommand(); // 返回受影响的行数
            // 3. 结果判断:有数据被删除则成功,否则抛“未找到数据”异常
            if (deletedCount > 0)
            {
                return true;
            throw new KeyNotFoundException(
                $"未找到送货单号「{deliveryNo}」且行内码「{lineNo}」对应的条码数据,删除操作未执行");
            }
            throw new KeyNotFoundException($"未找到送货单号「{deliveryNo}」且行内码「{lineNo}」对应的条码数据,删除操作未执行");
        }
        catch (Exception ex)
        {
            // 4. 包装异常:补充联合条件上下文,便于定位问题
            throw new ApplicationException(
                $"按送货单号+行内码删除条码数据失败(DeliveryNo:{deliveryNo},LineNo:{lineNo}):{ex.Message}",
                ex);
            throw new ApplicationException($"按送货单号+行内码删除条码数据失败(DeliveryNo:{deliveryNo},LineNo:{lineNo}):{ex.Message}",ex);
        }
    }
@@ -247,25 +230,21 @@
    {
        // 1. 参数校验与处理
        if (deliveryItems == null || !deliveryItems.Any())
        {
            throw new ArgumentException("送货单行列表不能为空", nameof(deliveryItems));
        }
        // 提取并验证送货单号+行内码组合
        var validItems = deliveryItems
            .Where(item =>
                !string.IsNullOrWhiteSpace(item.DeliveryNo) &&
                !string.IsNullOrWhiteSpace(item.LineNo)
            )
        var validItems = deliveryItems.Where(item =>!string.IsNullOrWhiteSpace(item.DeliveryNo) &&!string.IsNullOrWhiteSpace(item.LineNo))
            .Select(item => new
            {
                DeliveryNo = item.DeliveryNo,
                LineNo = item.LineNo
            })
            .Distinct() // 去重相同的组合
            .ToList();
            }).Distinct().ToList();
        if (!validItems.Any())
        {
            throw new ArgumentException("请求中没有有效的效的送货单行数据(均为空或空格)");
        }
        try
        {
            // 2. 执行批量删除(按DeliveryNo+LineNo组合条件)
@@ -277,16 +256,12 @@
            foreach (var item in validItems)
            {
                // 检查当前组合是否存在
                var exists = Db.Queryable<BarcodeInformation>()
                    .Any(b => b.DeliveryNo == item.DeliveryNo && b.DnLines == item.LineNo);
                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();
                    var deleted = Db.Deleteable<BarcodeInformation>().Where(b => b.DeliveryNo == item.DeliveryNo && b.DnLines == item.LineNo).ExecuteCommand();
                    totalDeleted += deleted;
                    deletedItems.Add($"{item.DeliveryNo}_{item.LineNo}");
                }
@@ -329,9 +304,7 @@
    /// </summary>
    private BarcodeInformation ConvertDtoToEntity(DeliveryBarcodeInfo dto)
    {
        var entityId = dto.Type == "0"
            ? Guid.NewGuid()
            : (string.IsNullOrEmpty(dto.SmallBarcode) ? Guid.Empty : Guid.Parse(dto.SmallBarcode));
        var entityId = dto.Type == "0" ? Guid.NewGuid() : (string.IsNullOrEmpty(dto.SmallBarcode) ? Guid.Empty : Guid.Parse(dto.SmallBarcode));
        return new BarcodeInformation
        {
@@ -345,7 +318,6 @@
            PackLevel = dto.BarcodeType,
            CreateTime = dto.Type == "0" ? DateTime.Now : (DateTime?)null,
            UpdateTime = DateTime.Now,
            // 扩展字段赋默认值
            BigBarcode = null,
            SmallPackageLength = null,
@@ -379,18 +351,16 @@
        var isInsertSuccess = base.Insert(entity);
        return isInsertSuccess ? true : throw new NotImplementedException("条码新增失败:数据库插入操作未执行成功");
    }
    /// <summary>
    /// 单个条码删除(按主键Id)
    /// </summary>
    private bool DeleteBarcode(Guid id)
    {
        if (id == Guid.Empty)
        {
            throw new ArgumentException("删除操作的条码Id不能为空");
        var deleteRowCount = Db.Deleteable<BarcodeInformation>()
            .Where(entity => entity.Id == id)
            .ExecuteCommand();
        }
        var deleteRowCount = Db.Deleteable<BarcodeInformation>().Where(entity => entity.Id == id).ExecuteCommand();
        return deleteRowCount > 0 ? true : throw new NotImplementedException($"条码删除失败:未找到Id为「{id}」的条码记录");
    }
@@ -410,11 +380,10 @@
    private bool DeleteBarcodeBatch(Guid[] ids)
    {
        if (ids == null || ids.Length == 0)
        {
            throw new ArgumentException("批量删除的条码Id数组不能为空");
        var deleteRowCount = Db.Deleteable<BarcodeInformation>()
            .Where(entity => ids.Contains(entity.Id))
            .ExecuteCommand();
        }
        var deleteRowCount = Db.Deleteable<BarcodeInformation>().Where(entity => ids.Contains(entity.Id)).ExecuteCommand();
        return deleteRowCount > 0 ? true : throw new NotImplementedException($"批量条码删除失败:共{ids.Length}个Id");
    }