kyy
2025-09-17 1e270a1329bbb77b594df2290f15caa34c56668e
1、条码增加唯一判断
已修改1个文件
34 ■■■■■ 文件已修改
MES.Service/service/BasicData/DeliveryBarcodeManager.cs 34 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
MES.Service/service/BasicData/DeliveryBarcodeManager.cs
@@ -348,6 +348,12 @@
    /// </summary>
    private bool InsertBarcode(BarcodeInformation entity)
    {
        // 检查条码是否已存在
        if (IsBarcodeExists(entity))
        {
            throw new InvalidOperationException($"条码已存在,无法重复插入:SmallBarcode={entity.SmallBarcode}, OuterBarcode={entity.OuterBarcode}");
        }
        var isInsertSuccess = base.Insert(entity);
        return isInsertSuccess ? true : throw new NotImplementedException("条码新增失败:数据库插入操作未执行成功");
    }
@@ -370,6 +376,21 @@
    /// </summary>
    private bool InsertBarcodeBatch(List<BarcodeInformation> entityList)
    {
        // 检查批量条码中是否有重复
        var duplicateBarcodes = new List<string>();
        foreach (var entity in entityList)
        {
            if (IsBarcodeExists(entity))
            {
                duplicateBarcodes.Add($"SmallBarcode={entity.SmallBarcode}, OuterBarcode={entity.OuterBarcode}");
            }
        }
        if (duplicateBarcodes.Any())
        {
            throw new InvalidOperationException($"发现重复条码,无法批量插入:{string.Join("; ", duplicateBarcodes)}");
        }
        var isBatchInsertSuccess = base.InsertRange(entityList);
        return isBatchInsertSuccess ? true : throw new NotImplementedException($"批量条码新增失败:共{entityList.Count}条记录");
    }
@@ -388,5 +409,18 @@
        return deleteRowCount > 0 ? true : throw new NotImplementedException($"批量条码删除失败:共{ids.Length}个Id");
    }
    /// <summary>
    /// 检查条码是否已存在
    /// </summary>
    /// <param name="entity">条码实体</param>
    /// <returns>true=已存在,false=不存在</returns>
    private bool IsBarcodeExists(BarcodeInformation entity)
    {
        // 根据SmallBarcode或OuterBarcode检查是否存在重复条码
        return Db.Queryable<BarcodeInformation>()
            .Any(x => (!string.IsNullOrEmpty(entity.SmallBarcode) && x.SmallBarcode == entity.SmallBarcode) ||
                     (!string.IsNullOrEmpty(entity.OuterBarcode) && x.OuterBarcode == entity.OuterBarcode));
    }
    #endregion
}