| | |
| | | } |
| | | 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; |
| | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | // 4. 包装业务异常,补充上下文信息 |
| | | // 6. 包装业务异常,补充上下文信息 |
| | | throw new ApplicationException($"根据送货单号删除条码数据失败(DeliveryNo:{deliveryNo}):{ex.Message}", ex); |
| | | } |
| | | } |
| | |
| | | } |
| | | 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, |
| | |
| | | |
| | | 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; |
| | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | // 4. 包装异常:补充联合条件上下文,便于定位问题 |
| | | // 6. 包装异常:补充联合条件上下文,便于定位问题 |
| | | throw new ApplicationException($"按送货单号+行内码删除条码数据失败(DeliveryNo:{deliveryNo},LineNo:{lineNo}):{ex.Message}",ex); |
| | | } |
| | | } |
| | |
| | | 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; |
| | |
| | | { |
| | | 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}」的条码记录"); |
| | |
| | | { |
| | | 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"); |
| | |
| | | (!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 |
| | | } |