kyy
2025-09-17 80b5db679d95b5eaf0da83c1a078e7a105bf327d
1、条码删除增加校验
已修改1个文件
已添加1个文件
149 ■■■■■ 文件已修改
MES.Service/Modes/MesInvItemBarcodes.cs 16 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
MES.Service/service/BasicData/DeliveryBarcodeManager.cs 133 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
MES.Service/Modes/MesInvItemBarcodes.cs
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,16 @@
using SqlSugar;
namespace MES.Service.Modes;
/// <summary>
///     MES库存物料条码表
/// </summary>
[SugarTable("dbo.MES_INV_ITEM_BARCODES")]
public class MesInvItemBarcodes
{
    /// <summary>
    ///     ç‰©æ–™æ¡ç 
    /// </summary>
    [SugarColumn(ColumnName = "ITEM_BARCODE", IsNullable = false, Length = 150)]
    public string ItemBarcode { get; set; }
}
MES.Service/service/BasicData/DeliveryBarcodeManager.cs
@@ -117,12 +117,28 @@
        }
        try
        {
            // 2. æ‰§è¡Œåˆ é™¤ï¼šæ ¹æ® DeliveryNo åŒ¹é…è¡¨ä¸­æ•°æ®ï¼ˆå¯¹åº”实体的 DeliveryNo å­—段)
            // 2. æŸ¥è¯¢è¦åˆ é™¤çš„æ¡ç ä¿¡æ¯
            var barcodesToDelete = Db.Queryable<BarcodeInformation>()
                .Where(barcode => barcode.DeliveryNo == deliveryNo)
                .ToList();
            if (!barcodesToDelete.Any())
            {
                throw new KeyNotFoundException($"未找到送货单号为「{deliveryNo}」的条码数据,删除操作未执行");
            }
            // 3. æ ¡éªŒæ¡ç æ˜¯å¦åœ¨åº“存表中存在
            foreach (var barcode in barcodesToDelete)
            {
                CheckBarcodeInInventory(barcode.SmallBarcode, barcode.OuterBarcode);
            }
            // 4. æ‰§è¡Œåˆ é™¤ï¼šæ ¹æ® DeliveryNo åŒ¹é…è¡¨ä¸­æ•°æ®ï¼ˆå¯¹åº”实体的 DeliveryNo å­—段)
            int deletedCount = Db.Deleteable<BarcodeInformation>()
                .Where(barcode => barcode.DeliveryNo == deliveryNo) // è¡¨å­—段与送货单号匹配
                .ExecuteCommand(); // è¿”回受影响的行数
            // 3. åˆ¤æ–­ç»“果:至少删除1条则视为成功,否则抛出“未找到数据”异常
            // 5. åˆ¤æ–­ç»“果:至少删除1条则视为成功,否则抛出"未找到数据"异常
            if (deletedCount > 0)
            {
                return true;
@@ -131,7 +147,7 @@
        }
        catch (Exception ex)
        {
            // 4. åŒ…装业务异常,补充上下文信息
            // 6. åŒ…装业务异常,补充上下文信息
            throw new ApplicationException($"根据送货单号删除条码数据失败(DeliveryNo:{deliveryNo}):{ex.Message}", ex);
        }
    }
@@ -162,13 +178,20 @@
        }
        try
        {
            // 2. æŸ¥è¯¢æ•°æ®åº“中存在的送货单号
            // 2. æŸ¥è¯¢æ•°æ®åº“中存在的送货单号和对应的条码信息
            var existingNos = Db.Queryable<BarcodeInformation>().Where(barcode => deliveryNoList.Contains(barcode.DeliveryNo)).Select(barcode => barcode.DeliveryNo).Distinct().ToList();
            var barcodesToDelete = Db.Queryable<BarcodeInformation>().Where(barcode => deliveryNoList.Contains(barcode.DeliveryNo)).ToList();
            // 3. æ‰§è¡Œæ‰¹é‡åˆ é™¤
            // 3. æ ¡éªŒæ¡ç æ˜¯å¦åœ¨åº“存表中存在
            foreach (var barcode in barcodesToDelete)
            {
                CheckBarcodeInInventory(barcode.SmallBarcode, barcode.OuterBarcode);
            }
            // 4. æ‰§è¡Œæ‰¹é‡åˆ é™¤
            int totalDeleted = Db.Deleteable<BarcodeInformation>().Where(barcode => deliveryNoList.Contains(barcode.DeliveryNo)).ExecuteCommand();
            // 4. è¿”回结果
            // 5. è¿”回结果
            return new BatchDeleteResult
            {
                TotalRequested = deliveryNoList.Count,
@@ -203,11 +226,27 @@
        try
        {
            // 2. è”合条件删除:匹配 DeliveryNo(送货单号)和 DnLines(行内码,实体字段对应表的 dnLines åˆ—)
            // 2. æŸ¥è¯¢è¦åˆ é™¤çš„æ¡ç ä¿¡æ¯
            var barcodesToDelete = Db.Queryable<BarcodeInformation>()
                .Where(barcode => barcode.DeliveryNo == deliveryNo && barcode.DnLines == lineNo)
                .ToList();
            if (!barcodesToDelete.Any())
            {
                throw new KeyNotFoundException($"未找到送货单号「{deliveryNo}」且行内码「{lineNo}」对应的条码数据,删除操作未执行");
            }
            // 3. æ ¡éªŒæ¡ç æ˜¯å¦åœ¨åº“存表中存在
            foreach (var barcode in barcodesToDelete)
            {
                CheckBarcodeInInventory(barcode.SmallBarcode, barcode.OuterBarcode);
            }
            // 4. è”合条件删除:匹配 DeliveryNo(送货单号)和 DnLines(行内码,实体字段对应表的 dnLines åˆ—)
            int deletedCount = Db.Deleteable<BarcodeInformation>().Where(barcode =>barcode.DeliveryNo == deliveryNo && barcode.DnLines == lineNo) // åŒ¹é…è¡Œå†…码(实体 DnLines å¯¹åº”表 dnLines å­—段)
                .ExecuteCommand(); // è¿”回受影响的行数
            // 3. ç»“果判断:有数据被删除则成功,否则抛“未找到数据”异常
            // 5. ç»“果判断:有数据被删除则成功,否则抛"未找到数据"异常
            if (deletedCount > 0)
            {
                return true;
@@ -216,7 +255,7 @@
        }
        catch (Exception ex)
        {
            // 4. åŒ…装异常:补充联合条件上下文,便于定位问题
            // 6. åŒ…装异常:补充联合条件上下文,便于定位问题
            throw new ApplicationException($"按送货单号+行内码删除条码数据失败(DeliveryNo:{deliveryNo},LineNo:{lineNo}):{ex.Message}",ex);
        }
    }
@@ -256,10 +295,18 @@
            foreach (var item in validItems)
            {
                // æ£€æŸ¥å½“前组合是否存在
                var exists = Db.Queryable<BarcodeInformation>().Any(b => b.DeliveryNo == item.DeliveryNo && b.DnLines == item.LineNo);
                var barcodesToDelete = Db.Queryable<BarcodeInformation>()
                    .Where(b => b.DeliveryNo == item.DeliveryNo && b.DnLines == item.LineNo)
                    .ToList();
                if (exists)
                if (barcodesToDelete.Any())
                {
                    // æ ¡éªŒæ¡ç æ˜¯å¦åœ¨åº“存表中存在
                    foreach (var barcode in barcodesToDelete)
                    {
                        CheckBarcodeInInventory(barcode.SmallBarcode, barcode.OuterBarcode);
                    }
                    // æ‰§è¡Œåˆ é™¤
                    var deleted = Db.Deleteable<BarcodeInformation>().Where(b => b.DeliveryNo == item.DeliveryNo && b.DnLines == item.LineNo).ExecuteCommand();
                    totalDeleted += deleted;
@@ -366,6 +413,17 @@
        {
            throw new ArgumentException("删除操作的条码Id不能为空");
        }
        // æŸ¥è¯¢è¦åˆ é™¤çš„æ¡ç ä¿¡æ¯
        var barcodeToDelete = Db.Queryable<BarcodeInformation>().Where(entity => entity.Id == id).First();
        if (barcodeToDelete == null)
        {
            throw new NotImplementedException($"条码删除失败:未找到Id为「{id}」的条码记录");
        }
        // æ ¡éªŒæ¡ç æ˜¯å¦åœ¨MesInvItemBarcode表中存在
        CheckBarcodeInInventory(barcodeToDelete.SmallBarcode, barcodeToDelete.OuterBarcode);
        var deleteRowCount = Db.Deleteable<BarcodeInformation>().Where(entity => entity.Id == id).ExecuteCommand();
        return deleteRowCount > 0 ? true : throw new NotImplementedException($"条码删除失败:未找到Id为「{id}」的条码记录");
@@ -404,6 +462,20 @@
        {
            throw new ArgumentException("批量删除的条码Id数组不能为空");
        }
        // æŸ¥è¯¢è¦åˆ é™¤çš„æ¡ç ä¿¡æ¯
        var barcodesToDelete = Db.Queryable<BarcodeInformation>().Where(entity => ids.Contains(entity.Id)).ToList();
        if (!barcodesToDelete.Any())
        {
            throw new NotImplementedException($"批量条码删除失败:未找到任何匹配的条码记录");
        }
        // æ‰¹é‡æ ¡éªŒæ¡ç æ˜¯å¦åœ¨åº“存表中存在
        foreach (var barcode in barcodesToDelete)
        {
            CheckBarcodeInInventory(barcode.SmallBarcode, barcode.OuterBarcode);
        }
        var deleteRowCount = Db.Deleteable<BarcodeInformation>().Where(entity => ids.Contains(entity.Id)).ExecuteCommand();
        return deleteRowCount > 0 ? true : throw new NotImplementedException($"批量条码删除失败:共{ids.Length}个Id");
@@ -422,5 +494,44 @@
                     (!string.IsNullOrEmpty(entity.OuterBarcode) && x.OuterBarcode == entity.OuterBarcode));
    }
    /// <summary>
    /// æ ¡éªŒæ¡ç æ˜¯å¦åœ¨åº“存表中存在,如果存在则抛出异常不允许删除
    /// </summary>
    /// <param name="smallBarcode">小包装条码</param>
    /// <param name="outerBarcode">外包装条码</param>
    private void CheckBarcodeInInventory(string smallBarcode, string outerBarcode)
    {
        var existsInInventory = false;
        var existingBarcode = string.Empty;
        // æ£€æŸ¥å°åŒ…装条码是否在库存表中存在
        if (!string.IsNullOrEmpty(smallBarcode))
        {
            existsInInventory = Db.Queryable<MesInvItemBarcodes>()
                .Any(x => x.ItemBarcode == smallBarcode);
            if (existsInInventory)
            {
                existingBarcode = smallBarcode;
            }
        }
        // æ£€æŸ¥å¤–包装条码是否在库存表中存在(如果小包装条码不存在的话)
        if (!existsInInventory && !string.IsNullOrEmpty(outerBarcode))
        {
            existsInInventory = Db.Queryable<MesInvItemBarcodes>()
                .Any(x => x.ItemBarcode == outerBarcode);
            if (existsInInventory)
            {
                existingBarcode = outerBarcode;
            }
        }
        // å¦‚果条码在库存表中存在,则抛出异常不允许删除
        if (existsInInventory)
        {
            throw new InvalidOperationException($"条码「{existingBarcode}」已在库存中,不允许删除");
        }
    }
    #endregion
}