| | |
| | | // 按Type区分操作类型(0=新增,1=删除) |
| | | switch (barcodeDto.Type) |
| | | { |
| | | case "0": |
| | | return InsertBarcode(barcodeEntity); |
| | | case "1": |
| | | return InsertBarcode(barcodeEntity); |
| | | case "2": |
| | | return DeleteBarcode(barcodeEntity.Id); |
| | | default: |
| | | throw new ArgumentOutOfRangeException( nameof(barcodeDto.Type), barcodeDto.Type, "条码操作类型错误:仅支持 0(新增)、1(删除)"); |
| | |
| | | { |
| | | switch (type) |
| | | { |
| | | case "0": |
| | | case "1": |
| | | groupResultList.Add(InsertBarcodeBatch(entityGroup)); |
| | | break; |
| | | case "1": |
| | | case "2": |
| | | 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:仅支持 1(新增)、2(删除)"); |
| | | } |
| | | } |
| | | |
| | |
| | | /// </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 == "1" ? Guid.NewGuid() : (string.IsNullOrEmpty(dto.SmallBarcode) ? Guid.Empty : Guid.NewGuid()); |
| | | |
| | | return new BarcodeInformation |
| | | { |
| | |
| | | DeliveryNo = dto.DeliveryNo, |
| | | DnLines = dto.LineNo, |
| | | PackLevel = dto.BarcodeType, |
| | | CreateTime = dto.Type == "0" ? DateTime.Now : (DateTime?)null, |
| | | CreateTime = dto.Type == "1" ? DateTime.Now : (DateTime?)null, |
| | | UpdateTime = DateTime.Now, |
| | | // 扩展字段赋默认值 |
| | | BigBarcode = null, |
| | |
| | | /// </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("条码新增失败:数据库插入操作未执行成功"); |
| | | } |
| | |
| | | /// </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}条记录"); |
| | | } |
| | |
| | | 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 |
| | | } |