加事务

This commit is contained in:
zhougf committed 2024-04-17 21:47:06 +08:00
1 parent 9f6f95828c
commit 2453e8405b
4 files changed
+497 -420

No files matched your search

@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Threading.Tasks;
using HHWMS.Web.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@@ -153,50 +154,62 @@ public class ReceiptSLInController : BaseController
var receiptHeaderList = _unitWork.Find<ReceiptHeader>(x =>
data.Select(x => x.ReceiptCode).ToList().Contains(x.ID.ToString()))
.Include(x => x.ReceiptDetails).ToList();
// 查找明细,修改对应状态,并且 绑定物料周转箱
foreach (Binding item in data.OrderBy(x => x.ReceiptCode))
using (var tran = Wtm.DC.Database.BeginTransaction())
{
// 更新入库单明细状态
var header = receiptHeaderList.Find(x => x.ID.ToString().Equals(item.ReceiptCode));
var receipt = header.ReceiptDetails.First(x => x.UniqueCode.Equals(item.UniqueCode));
if (receipt != null)
try
{
if (receipt.Status == ReceiptStatusEnum.收完) continue;
receipt.CompletedQty += item.BindQty;
}
if(receipt.CompletedQty == receipt.Qty)
{
receipt.Status = ReceiptStatusEnum.收完;
if (!header.ReceiptDetails.Any(x => x.Status < ReceiptStatusEnum.收完))
// 查找明细,修改对应状态,并且 绑定物料周转箱
foreach (Binding item in data.OrderBy(x => x.ReceiptCode))
{
header.FirstStatus = ReceiptStatusEnum.收完;
// 更新入库单明细状态
var header = receiptHeaderList.Find(x => x.ID.ToString().Equals(item.ReceiptCode));
var receipt = header.ReceiptDetails.First(x => x.UniqueCode.Equals(item.UniqueCode));
if (receipt != null)
{
if (receipt.Status == ReceiptStatusEnum.收完) continue;
receipt.CompletedQty += item.BindQty;
}
if (receipt.CompletedQty == receipt.Qty)
{
receipt.Status = ReceiptStatusEnum.收完;
if (!header.ReceiptDetails.Any(x => x.Status < ReceiptStatusEnum.收完))
{
header.FirstStatus = ReceiptStatusEnum.收完;
}
}
var station = _unitWork.FindSingle<Location>(x => x.Code == item.BoxCode);
// 新建库存
var inventory = new Inventory
{
ZoneCode = ZoneCodeEnum.散料库,
UniqueCode = item.UniqueCode,
MaterialCode = receipt.MaterialCode,
MaterialType = receipt.MaterialType,
MaterialName = receipt.MaterialName,
MaterialSpec = receipt.MaterialSpec,
Batch = receipt.Batch,
MaterialUnit = receipt.MaterialUnit,
SupplierCode = receipt.SupplierCode,
Qty = item.BindQty,
LocationCode = item.BoxCode,
ContainerCode = station.ContainerCode,
ReceiptCode = item.ReceiptCode,
Status = "0",
TaskStatus = InventoryTaskStatus.任务中,
Flag = item.Size,
};
_unitWork.Add(inventory);
}
_unitWork.BatchUpdate(receiptHeaderList.ToArray());
tran.Commit();
}
var station = _unitWork.FindSingle<Location>(x => x.Code == item.BoxCode);
// 新建库存
var inventory = new Inventory
catch (Exception ex)
{
ZoneCode = ZoneCodeEnum.散料库,
UniqueCode = item.UniqueCode,
MaterialCode = receipt.MaterialCode,
MaterialType = receipt.MaterialType,
MaterialName = receipt.MaterialName,
MaterialSpec = receipt.MaterialSpec,
Batch = receipt.Batch,
MaterialUnit = receipt.MaterialUnit,
SupplierCode = receipt.SupplierCode,
Qty = item.BindQty,
LocationCode = item.BoxCode,
ContainerCode = station.ContainerCode,
ReceiptCode = item.ReceiptCode,
Status = "0",
TaskStatus = InventoryTaskStatus.任务中,
Flag = item.Size,
};
_unitWork.Add(inventory);
tran.Rollback();
return JsonMore(null, 500, ex.Message);
}
}
_unitWork.BatchUpdate(receiptHeaderList.ToArray());
return JsonMore(null);
}
/// <summary>
@@ -224,52 +237,92 @@ public class ReceiptSLInController : BaseController
{
return JsonMore(null, 500, $"{boxCode}站台没有料箱,无法入库!");
}
// 判断箱号是否合理
var box = _unitWork.FindSingle<Location>(x => x.ContainerCode.Equals(station.ContainerCode)
&& x.Type != LocationTypeEnum.散料库站台);
if (box != null)
using (var tran = Wtm.DC.Database.BeginTransaction())
{
//if(box.Type != LocationTypeEnum.散料库站台)
//{
// return JsonMore(null, 500, $"{boxCode}不在站台,无法入库!");
//}
//else
//{
// 查找目标库位,托盘回原库位
var toLocation = _unitWork.FindSingle<Location>(x => x.Code.Equals(box.Code));
try
{
// 判断箱号是否合理
var box = _unitWork.FindSingle<Location>(x => x.ContainerCode.Equals(station.ContainerCode)
&& x.Type != LocationTypeEnum.散料库站台);
if (toLocation != null)
if (box != null)
{
// 创建任务
var task = new TaskHeader
{
ZoneCode = ZoneCodeEnum.散料库,
TaskNo = _unitWork.GetSerialNumber("CIn"),
TaskType = TaskTypeEnum.散料盒入库,
ContainerCode = toLocation.Code,
FromLocation = station.Code,
ToLocation = toLocation.Code,
Status = TaskStatusEnum.待下发,
CreateBy = Wtm.LoginUserInfo.ITCode,
CreateTime = DateTime.Now
};
_unitWork.Add(task);
//if(box.Type != LocationTypeEnum.散料库站台)
//{
// return JsonMore(null, 500, $"{boxCode}不在站台,无法入库!");
//}
//else
//{
// 查找目标库位,托盘回原库位
var toLocation = _unitWork.FindSingle<Location>(x => x.Code.Equals(box.Code));
toLocation.Status = LocationStatus.任务锁;
toLocation.Length = isFull ? 1 : 0; // 是否满盘
_unitWork.Update(toLocation);
if (toLocation != null)
{
// 创建任务
var task = new TaskHeader
{
ZoneCode = ZoneCodeEnum.散料库,
TaskNo = _unitWork.GetSerialNumber("CIn"),
TaskType = TaskTypeEnum.散料盒入库,
ContainerCode = toLocation.Code,
FromLocation = station.Code,
ToLocation = toLocation.Code,
Status = TaskStatusEnum.待下发,
CreateBy = Wtm.LoginUserInfo.ITCode,
CreateTime = DateTime.Now
};
_unitWork.Add(task);
toLocation.Status = LocationStatus.任务锁;
toLocation.Length = isFull ? 1 : 0; // 是否满盘
_unitWork.Update(toLocation);
var listInvtran = new List<InventoryTransaction>();
var invList = _unitWork.Find<Inventory>(x =>
x.ContainerCode.Equals(task.ContainerCode)
&& x.TaskStatus == InventoryTaskStatus.任务中
).ToList();
foreach (var inv in invList)
{
listInvtran.Add(new InventoryTransaction
{
BillType = "in",
ZoneCode = inv.ZoneCode,
Type = inv.UniqueCode,
MaterialCode = inv.MaterialCode,
MaterialName = inv.MaterialName,
MaterialSpec = inv.MaterialSpec,
MaterialUnit = inv.MaterialUnit,
MaterialType = inv.MaterialType,
Batch = inv.Batch,
Lot = inv.Lot,
CompanyCode = inv.CompanyCode,
LocationCode = inv.LocationCode,
SupplierCode = inv.SupplierCode,
Qty = inv.Qty,
CreateTime = DateTime.Now
});
}
_unitWork.BatchAdd(listInvtran.ToArray());
tran.Commit();
}
else
{
return JsonMore(null, 500, $"库内无可用仓位,无法入库!");
}
//}
return JsonMore(data: null);
}
else
{
return JsonMore(null, 500, $"库内无可用仓位,无法入库!");
return JsonMore(null, 500, $"{boxCode}不正确!");
}
//}
return JsonMore(data: null);
}
else
{
return JsonMore(null, 500, $"{boxCode}不正确!");
}
catch(Exception ex)
{
tran.Rollback();
return JsonMore(null, 500, ex.Message);
}
}
}
}