739 lines
31 KiB
C#
739 lines
31 KiB
C#
using System;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using WalkingTec.Mvvm.Core;
|
||
using WalkingTec.Mvvm.Mvc;
|
||
using System.Collections.Generic;
|
||
using HHWMS.Web.Models;
|
||
using System.Threading.Tasks;
|
||
using System.Linq;
|
||
using Aliyun.OSS;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using WalkingTec.Mvvm.Core.Extensions;
|
||
|
||
namespace HHWMS.Web.Controllers
|
||
{
|
||
[Area("receipt")]
|
||
[ActionDescription("入库操作")]
|
||
public partial class ReceiptInController : BaseController
|
||
{
|
||
private readonly IUnitWork _unitWork;
|
||
private readonly Dictionary<string, LineRange> _materialZones;
|
||
|
||
public ReceiptInController(IUnitWork unitwork)
|
||
{
|
||
_unitWork = unitwork;
|
||
_materialZones = new Dictionary<string, LineRange>
|
||
{
|
||
{ "Zone1", new LineRange { BeginLine = 1, EndLine = 25 } },
|
||
{ "Zone2", new LineRange { BeginLine = 26, EndLine = 57 } },
|
||
{ "Zone3", new LineRange { BeginLine = 58, EndLine = 96 } },
|
||
};
|
||
}
|
||
|
||
#region Index
|
||
|
||
[ActionDescription("Sys.Search")]
|
||
public ActionResult Index()
|
||
{
|
||
return PartialView("Index");
|
||
}
|
||
|
||
#endregion Index
|
||
|
||
#region API Methods
|
||
|
||
/// <summary>
|
||
/// 搜索物料
|
||
/// </summary>
|
||
[HttpGet]
|
||
public async Task<IActionResult> SearchMaterials(string keyword)
|
||
{
|
||
try
|
||
{
|
||
if (string.IsNullOrWhiteSpace(keyword))
|
||
{
|
||
return Json(new { Code = 200, Data = new object[] { } });
|
||
}
|
||
|
||
keyword = keyword.Trim();
|
||
|
||
// 先查本地物料表:物料编码或图号模糊匹配(名称也一起匹配),和标准入库页面一致
|
||
var dbMaterials = _unitWork.Find<Material>(m => m.MaterialCode.Contains(keyword)
|
||
|| m.MaterialType.Contains(keyword)
|
||
|| m.MaterialName.Contains(keyword))
|
||
.Select(m => new
|
||
{
|
||
MaterialCode = m.MaterialCode,
|
||
MaterialName = m.MaterialName,
|
||
MaterialSpec = m.MaterialSpec,
|
||
MaterialUnit = m.MaterialUnit,
|
||
MaterialType = m.MaterialType,
|
||
SupplierCode = m.SupplierCode
|
||
})
|
||
.Take(15)
|
||
.ToList();
|
||
// 本地查到就返回,不再因为图号为空丢弃记录
|
||
if (dbMaterials.Count > 0)
|
||
{
|
||
return Json(new { Code = 200, Data = dbMaterials });
|
||
}
|
||
|
||
// 本地没有则调 ERP LoadInventory(按物料编码 InvCode 查询)并自动建档
|
||
try
|
||
{
|
||
var request = new LoadInventoryRequest
|
||
{
|
||
ErpCode = "003",
|
||
InvCode = keyword,
|
||
Size = 15,
|
||
Page = 1
|
||
};
|
||
|
||
Wtm.DoLog($"ERP物料查询: 入库页面搜索触发", ActionLogTypesEnum.Normal,
|
||
"物料", $"keyword={keyword}, ErpCode=003");
|
||
var apiResult =
|
||
await Wtm.CallAPI<LoadInventoryResponse>("ERP", "LoadInventory", HttpMethodEnum.POST, request,3);
|
||
|
||
// ERP 有数据才用 ERP 结果;返回成功但 0 条时继续往下走本地兜底,避免页面变成空列表
|
||
if (apiResult?.Data != null && apiResult.Data.Code == 0
|
||
&& apiResult.Data.Data != null && apiResult.Data.Data.Count > 0)
|
||
{
|
||
var materialsList = new List<object>();
|
||
var oldMaterials = new List<object>();
|
||
var newMaterials = new List<Material>();
|
||
|
||
// 获取接口返回的所有物料编码
|
||
var invCodes = apiResult.Data.Data
|
||
.Where(x => !string.IsNullOrWhiteSpace(x.InvCode))
|
||
.Select(x => x.InvCode)
|
||
.ToList();
|
||
|
||
Wtm.DoLog($"ERP物料查询: ERP返回{apiResult.Data.Data.Count}条", ActionLogTypesEnum.Normal,
|
||
"物料", $"keyword={keyword}, codes=[{string.Join(",", invCodes)}]");
|
||
|
||
// 一次性查询本地数据库中已存在的物料(优化性能,避免循环中多次查询)
|
||
var existingMaterials = _unitWork.Find<Material>(m => invCodes.Contains(m.MaterialCode))
|
||
.ToList();
|
||
|
||
// 遍历接口返回的物料数据
|
||
foreach (var item in apiResult.Data.Data)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(item.InvCode))
|
||
continue;
|
||
|
||
// 在已查询的列表中查找物料是否存在
|
||
var existingMaterial =
|
||
existingMaterials.FirstOrDefault(m => m.MaterialCode == item.InvCode);
|
||
|
||
if (existingMaterial == null)
|
||
{
|
||
// 物料不存在,新增到数据库
|
||
var newMaterial = new Material
|
||
{
|
||
MaterialCode = item.InvCode,
|
||
MaterialName = item.InvName,
|
||
MaterialSpec = item.InvStd,
|
||
MaterialUnit = item.UnitName,
|
||
MaterialType = item.InvDefine1, // 永丰图号
|
||
SupplierCode = item.InvDefine4, // 客户图号
|
||
Qty = 0, // 默认数量为0
|
||
CreateTime = DateTime.Now,
|
||
CreateBy = Wtm.LoginUserInfo?.ITCode ?? "System"
|
||
};
|
||
|
||
newMaterials.Add(newMaterial);
|
||
|
||
// 添加到返回列表
|
||
materialsList.Add(new
|
||
{
|
||
MaterialCode = newMaterial.MaterialCode,
|
||
MaterialName = newMaterial.MaterialName,
|
||
MaterialSpec = newMaterial.MaterialSpec,
|
||
MaterialUnit = newMaterial.MaterialUnit,
|
||
MaterialType = newMaterial.MaterialType,
|
||
SupplierCode = newMaterial.SupplierCode
|
||
});
|
||
}
|
||
else
|
||
{
|
||
if (existingMaterial.MaterialName != item.InvName
|
||
|| existingMaterial.MaterialType != item.InvDefine1)
|
||
{
|
||
existingMaterial.MaterialName = item.InvName;
|
||
existingMaterial.MaterialSpec = item.InvStd;
|
||
existingMaterial.MaterialUnit = item.UnitName;
|
||
existingMaterial.MaterialType = item.InvDefine1;
|
||
existingMaterial.SupplierCode = item.InvDefine4;
|
||
existingMaterial.UpdateTime = DateTime.Now;
|
||
existingMaterial.UpdateBy = "System";
|
||
oldMaterials.Add(existingMaterial);
|
||
}
|
||
// 物料已存在,添加到返回列表
|
||
materialsList.Add(new
|
||
{
|
||
MaterialCode = item.InvCode,
|
||
MaterialName = item.InvName,
|
||
MaterialSpec = item.InvStd,
|
||
MaterialUnit = item.UnitName,
|
||
MaterialType = item.InvDefine1, // 永丰图号
|
||
SupplierCode = item.InvDefine4, // 客户图号
|
||
});
|
||
}
|
||
}
|
||
|
||
// 保存新增的物料
|
||
_unitWork.BatchAdd(newMaterials);
|
||
_unitWork.BatchUpdate(oldMaterials);
|
||
|
||
Wtm.DoLog($"ERP物料查询: 本地同步完成", ActionLogTypesEnum.Normal,
|
||
"物料", $"keyword={keyword}, 新增={newMaterials.Count}, 更新={oldMaterials.Count}");
|
||
|
||
// 返回物料列表到前端
|
||
return Json(new { Code = 200, Data = materialsList });
|
||
}
|
||
else
|
||
{
|
||
Wtm.DoLog($"ERP物料查询: ERP返回异常", ActionLogTypesEnum.Normal,
|
||
"物料", $"keyword={keyword}, Code={apiResult?.Data?.Code}, Msg={apiResult?.Data?.Msg}");
|
||
}
|
||
}
|
||
catch (System.Exception apiEx)
|
||
{
|
||
// 接口调用失败,记录日志
|
||
Wtm.DoLog($"ERP物料查询: 接口调用失败", ActionLogTypesEnum.Normal, "物料",
|
||
request: $"keyword={keyword}, 改用本地数据库查询",
|
||
result: apiEx.Message);
|
||
}
|
||
|
||
// ERP 返回成功但没有数据,或调用失败:直接返回本地已查到的记录(本地有数据时不能给页面返回空)
|
||
return Json(new { Code = 200, Data = dbMaterials });
|
||
}
|
||
catch (System.Exception ex)
|
||
{
|
||
return Json(new { Code = 500, Msg = $"搜索物料失败:{ex.Message}" });
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取用户
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
public IActionResult GetOperators()
|
||
{
|
||
var users = _unitWork.Find<FrameworkUser>(x => x.ITCode != "Admin"
|
||
&& x.ITCode.Length > 5)
|
||
.Select(x => new
|
||
{
|
||
value = x.ITCode,
|
||
label = x.Name
|
||
})
|
||
.ToList();
|
||
return Json(new { Code = 200, Data = users });
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取工序列表:默认展示全部工序配置
|
||
/// 具体使用哪道工序由生产订单决定,物料工序表在查到订单后再按订单内容更新
|
||
/// </summary>
|
||
[HttpPost]
|
||
public IActionResult GetProcessesByMaterial()
|
||
{
|
||
try
|
||
{
|
||
var configs = LoadAllProcessConfigs();
|
||
return Json(new
|
||
{
|
||
Code = 200,
|
||
Data = configs.Select(x => new
|
||
{
|
||
value = x.ID.ToString(),
|
||
label = x.Name,
|
||
processCode = x.Code,
|
||
processName = x.Name
|
||
}).ToList()
|
||
});
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return Json(new { Code = 500, Msg = $"获取工序列表失败:{ex.Message}" });
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 查询全部工序配置
|
||
/// </summary>
|
||
private List<ProcessConfig> LoadAllProcessConfigs()
|
||
{
|
||
return _unitWork.Find<ProcessConfig>(x => x.ID > 0)
|
||
.OrderBy(x => x.Code)
|
||
.ToList();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取物料的单据信息
|
||
/// </summary>
|
||
/// <param name="materialCode"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
public async Task<IActionResult> GetOrdersByMaterial(string materialCode,string batch)
|
||
{
|
||
var receiptHeaderVM = Wtm.CreateVM<ReceiptHeaderVM>();
|
||
var vm = new ReceiptDetailVM()
|
||
{
|
||
Entity = new ReceiptDetail()
|
||
{
|
||
MaterialCode = materialCode,
|
||
Batch = batch ?? "32"
|
||
}
|
||
};
|
||
var result = await receiptHeaderVM.GetReceiptDetailsFromERP(vm);
|
||
if (result.Data != null)
|
||
{
|
||
var orders = result.Data.Select(x => new
|
||
{
|
||
value = x.MoDetailId,
|
||
label = string.IsNullOrEmpty(x.MoCode) ? x.MoDetailId : x.MoCode,
|
||
})
|
||
.ToList();
|
||
|
||
|
||
return Json(new { Code = 200, Data = orders });
|
||
}
|
||
return Json(new { Code = 200, Data = new List<MoDetailItem>() });
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 获取托盘类型信息
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[Public]
|
||
[HttpGet]
|
||
public IActionResult GetTrayTypes()
|
||
{
|
||
var containerconfig = _unitWork.Find<SysDictData>(x => x.DictType == "ContainerConfig"
|
||
&& x.DictLabelUs.ToLower() != "container")
|
||
.Select(x => new
|
||
{
|
||
value = x.ID,
|
||
label = x.DictLabel,
|
||
})
|
||
.ToList();
|
||
return Json(new { Code = 200, Data = containerconfig });
|
||
}
|
||
|
||
/// <summary>
|
||
/// 上架入库
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
public IActionResult SubmitReceipt([FromBody] ReceiptInModel model)
|
||
{
|
||
if (_unitWork.IsExit<TaskHeader>(x => x.ContainerCode == model.PalletCode))
|
||
{
|
||
return JsonMore(null, 500, $"库内已存在托盘编码为 {model.PalletCode} 的任务!");
|
||
}
|
||
|
||
// 获取物料信息
|
||
var material = _unitWork.FindSingle<Material>(m => m.MaterialCode == model.MaterialCode);
|
||
if (material == null)
|
||
{
|
||
return Json(new { Code = 500, Msg = "物料不存在" });
|
||
}
|
||
|
||
// 生产单号必填:入库时必须选择由订单列表带出的生产单号
|
||
if (string.IsNullOrWhiteSpace(model.SourceCode))
|
||
{
|
||
return Json(new { Code = 500, Msg = "请选择生产单号" });
|
||
}
|
||
|
||
if (material.Qty == null || material.Qty <= 0)
|
||
{
|
||
return Json(new { Code = 500, Msg = "物料毛重没有设置,请设置后再执行入库" });
|
||
}
|
||
|
||
// 获取容器信息
|
||
var container = _unitWork.FindSingle<Container>(c => c.Code == model.PalletCode);
|
||
if (container == null)
|
||
{
|
||
return Json(new { Code = 500, Msg = "容器不存在" });
|
||
}
|
||
|
||
if (!string.IsNullOrWhiteSpace(container.LocationCode))
|
||
{
|
||
return Json(new { Code = 400, Msg = $"{container.Code} 容器状态不正确!已存在任务或者已在库内" });
|
||
}
|
||
|
||
var materialProcessList = _unitWork.Find<MaterialProcess>(m => m.MaterialCode == material.MaterialCode);
|
||
|
||
// 页面工序下拉来自工序配置表(ProcessConfig),优先按工序编码取,编码为空时退回按 ID 取
|
||
ProcessConfig currentProcessConfig = null;
|
||
if (!string.IsNullOrWhiteSpace(model.ProcessCode))
|
||
{
|
||
currentProcessConfig = _unitWork.FindSingle<ProcessConfig>(x => x.Code == model.ProcessCode);
|
||
}
|
||
if (currentProcessConfig == null)
|
||
{
|
||
currentProcessConfig = _unitWork.FindSingle<ProcessConfig>(x => x.ID == model.ProcessId);
|
||
}
|
||
if (currentProcessConfig == null)
|
||
{
|
||
return Json(new { Code = 500, Msg = "未找到所选工序配置,请重新选择工序" });
|
||
}
|
||
|
||
// 当前工序在物料工序表中的记录(用于定位下一道工序),没有则视为最后一道
|
||
var currentMaterialProcess = materialProcessList
|
||
.FirstOrDefault(p => p.ProcessCode == currentProcessConfig.Code);
|
||
|
||
// 下一道工序:物料工序表中顺序大于当前工序的最小一条;不存在则默认 99(工序结束)
|
||
var nextProcess = currentMaterialProcess == null
|
||
? null
|
||
: materialProcessList
|
||
// 筛选顺序大于当前工序的工序
|
||
.Where(p => p.ProcessOrder > currentMaterialProcess.ProcessOrder)
|
||
// 按工序顺序升序排列(取最小的一个,即下一道)
|
||
.OrderBy(p => p.ProcessOrder)
|
||
// 取第一个(下一道工序)
|
||
.FirstOrDefault();
|
||
|
||
// 从字典查询下一道工序对应的区域
|
||
var nextProcessConfig = _unitWork.FindSingle<ProcessConfig>(x =>
|
||
x.Code == (nextProcess == null ? "99" : nextProcess.ProcessCode));
|
||
var zoneName = nextProcessConfig?.Zone;
|
||
|
||
if (string.IsNullOrEmpty(zoneName))
|
||
{
|
||
return Json(new { Code = 500, Msg = "未找到此工序对应区域配置" });
|
||
}
|
||
|
||
// 1. 查找可用库位信息
|
||
var locationVm = Wtm.CreateVM<LocationVM>();
|
||
var locationCode =
|
||
locationVm.GetEmptyLocationFromXb(_materialZones[zoneName].BeginLine, _materialZones[zoneName].EndLine);
|
||
|
||
if (string.IsNullOrWhiteSpace(locationCode))
|
||
{
|
||
return Json(new { Code = 500, Msg = "未找到可用的空仓位" });
|
||
}
|
||
|
||
// 使用事务
|
||
using (var tran = DC.BeginTransaction())
|
||
{
|
||
try
|
||
{
|
||
var zone = _unitWork.FindSingle<LocationZone>(x => x.Name == "线边库");
|
||
// 2. 生成入库任务
|
||
var taskNo = _unitWork.GetSerialNumber("In", 6);
|
||
var taskHeader = new TaskHeader
|
||
{
|
||
ZoneId = zone.ID,
|
||
TaskNo = taskNo,
|
||
TaskType = TaskTypeEnum.入库,
|
||
FromLocation = "",
|
||
ToLocation = locationCode,
|
||
ContainerCode = container.Code,
|
||
RoadWay = 1,
|
||
Status = TaskStatusEnum.新建,
|
||
CreateBy = model.OperatorId,
|
||
CreateTime = DateTime.Now
|
||
};
|
||
|
||
_unitWork.Add(taskHeader);
|
||
|
||
// 3. 创建库存信息
|
||
var inventory = new Inventory
|
||
{
|
||
ZoneId = zone.ID,
|
||
ContainerCode = taskHeader.ContainerCode,
|
||
LocationCode = locationCode,
|
||
MaterialCode = model.MaterialCode,
|
||
MaterialType = material.MaterialType,
|
||
MaterialName = material.MaterialName,
|
||
MaterialSpec = material.MaterialSpec,
|
||
MaterialUnit = material.MaterialUnit,
|
||
SupplierCode = material.SupplierCode,
|
||
SourceCode = model.BillCode,//客户订单号
|
||
ProduceCode = model.OrderId, // 单号
|
||
ReceiptDetailId = model.SourceCode, // erpdetailid 回传使用
|
||
BatchCode = currentProcessConfig.Code, //erp回传使用 批次编码
|
||
Batch = currentProcessConfig.Name,
|
||
Lot = nextProcessConfig?.Name,
|
||
Status = model.IsQualified ? "合格" : "不合格",
|
||
TaskStatus = InventoryTaskStatusEnum.Locked,
|
||
TaskQty = model.Qty, //小托盘数量
|
||
Flag = model.Type, //小托盘类型
|
||
CreateBy = model.OperatorId,
|
||
ReceiptCode = model.OperatorInfo, //校验员
|
||
CreateTime = DateTime.Now
|
||
};
|
||
|
||
_unitWork.Add(inventory);
|
||
|
||
// 5. 锁定容器和库位
|
||
container.IsLock = ContainerLockEnum.任务锁;
|
||
container.Status = ContainerStatus.有;
|
||
container.LocationCode = locationCode;
|
||
_unitWork.Update(container);
|
||
|
||
// 从数据库中重新获取库位实体,避免实体跟踪冲突
|
||
var locationToUpdate = _unitWork.FindSingle<Location>(l => l.Code == locationCode);
|
||
|
||
var resultNum = _unitWork.BatchUpdate<Location>(
|
||
x => x.Code == locationCode && x.Version == locationToUpdate.Version,
|
||
x => new Location()
|
||
{
|
||
Status = LocationStatusEnum.锁定,
|
||
ContainerStatus = LocationStatus.有货,
|
||
ContainerCode = taskHeader.ContainerCode,
|
||
UpdateTime = DateTime.Now,
|
||
UpdateBy = "WMS",
|
||
Version = Guid.NewGuid()
|
||
});
|
||
|
||
if (resultNum == 0)
|
||
{
|
||
//失败 回滚
|
||
tran.Rollback();
|
||
return Json(new { Code = 500, Msg = "更新仓位失败!请重新提交!" });
|
||
}
|
||
|
||
// 6. 调用WCS接口下发任务
|
||
var wcsTaskModel = new WCSTaskModel
|
||
{
|
||
TaskNo = taskNo,
|
||
PalletNo = taskHeader.ContainerCode,
|
||
FromLocation = "", // 入库任务起始位置为空
|
||
ToLocation = locationCode,
|
||
};
|
||
|
||
// 调用WCS任务下发接口
|
||
// tran.Commit();
|
||
// return Json(new { Code = 200, Msg = "入库成功", Data = new { TaskNo = taskNo } });
|
||
var wcsResult = Wtm
|
||
.CallAPI<WCSTaskResponse>("WCSXB", "TaskAssign", HttpMethodEnum.POST, wcsTaskModel).Result.Data;
|
||
// var wcsResult = new WCSTaskResponse()
|
||
// {
|
||
// Success = true,
|
||
// Msg = "Success"
|
||
// };
|
||
if (wcsResult != null && wcsResult.Success)
|
||
{
|
||
// WCS任务下发成功,提交事务
|
||
taskHeader.Status = TaskStatusEnum.已下发;
|
||
_unitWork.Update(taskHeader);
|
||
|
||
tran.Commit();
|
||
return Json(new { Code = 200, Msg = "入库成功", Data = new { TaskNo = taskNo } });
|
||
}
|
||
else
|
||
{
|
||
// WCS任务下发失败,回滚事务
|
||
tran.Rollback();
|
||
return Json(new { Code = 500, Msg = wcsResult?.Msg ?? "WCS任务下发失败" });
|
||
}
|
||
}
|
||
catch (System.Exception ex)
|
||
{
|
||
// 发生异常,回滚事务
|
||
tran.Rollback();
|
||
return Json(new { Code = 500, Msg = ex.Message });
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 空托盘入库
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
public IActionResult SubmitEmptyPallet([FromBody] ReceiptInModel model)
|
||
{
|
||
// 获取容器信息
|
||
var container = _unitWork.FindSingle<Container>(c => c.Code == model.PalletCode);
|
||
if (container == null)
|
||
{
|
||
return Json(new { Code = 500, Msg = "容器不存在" });
|
||
}
|
||
|
||
// 1. 查找可用库位信息
|
||
var locationVm = Wtm.CreateVM<LocationVM>();
|
||
var locationCode =
|
||
locationVm.GetEmptyLocationFromXb(_materialZones["Zone1"].BeginLine, _materialZones["Zone1"].EndLine);
|
||
|
||
if (string.IsNullOrWhiteSpace(locationCode))
|
||
{
|
||
return Json(new { Code = 500, Msg = "未找到可用的空仓位" });
|
||
}
|
||
|
||
// 使用事务
|
||
using (var tran = DC.BeginTransaction())
|
||
{
|
||
try
|
||
{
|
||
var zone = _unitWork.FindSingle<LocationZone>(x => x.Name == "线边库");
|
||
// 2. 生成入库任务
|
||
var taskNo = _unitWork.GetSerialNumber("In", 6);
|
||
var taskHeader = new TaskHeader
|
||
{
|
||
ZoneId = zone.ID,
|
||
TaskNo = taskNo,
|
||
TaskType = TaskTypeEnum.入库,
|
||
FromLocation = "",
|
||
ToLocation = locationCode,
|
||
ContainerCode = container.Code,
|
||
RoadWay = 1,
|
||
Status = TaskStatusEnum.新建,
|
||
CreateBy = model.OperatorId,
|
||
CreateTime = DateTime.Now
|
||
};
|
||
|
||
_unitWork.Add(taskHeader);
|
||
|
||
|
||
// 5. 锁定容器和库位
|
||
container.IsLock = ContainerLockEnum.任务锁;
|
||
container.LocationCode = locationCode;
|
||
container.Status = ContainerStatus.空;
|
||
_unitWork.Update(container);
|
||
|
||
// 从数据库中重新获取库位实体,避免实体跟踪冲突
|
||
var locationToUpdate = _unitWork.FindSingle<Location>(l => l.Code == locationCode);
|
||
|
||
var resultNum = _unitWork.BatchUpdate<Location>(
|
||
x => x.Code == locationCode && x.Version == locationToUpdate.Version,
|
||
x => new Location()
|
||
{
|
||
Status = LocationStatusEnum.锁定,
|
||
ContainerStatus = LocationStatus.空仓位,
|
||
ContainerCode = model.PalletCode,
|
||
UpdateTime = DateTime.Now,
|
||
UpdateBy = "WMS",
|
||
Version = Guid.NewGuid()
|
||
});
|
||
|
||
if (resultNum == 0)
|
||
{
|
||
//失败 回滚
|
||
tran.Rollback();
|
||
return Json(new { Code = 500, Msg = "更新仓位失败!请重新提交!" });
|
||
}
|
||
|
||
// 6. 调用WCS接口下发任务
|
||
var wcsTaskModel = new WCSTaskModel
|
||
{
|
||
TaskNo = taskNo,
|
||
PalletNo = taskHeader.ContainerCode,
|
||
FromLocation = "", // 入库任务起始位置为空
|
||
ToLocation = locationCode,
|
||
};
|
||
|
||
// 调用WCS任务下发接口
|
||
var wcsResult = Wtm
|
||
.CallAPI<WCSTaskResponse>("WCSXB", "TaskAssign", HttpMethodEnum.POST, wcsTaskModel).Result.Data;
|
||
|
||
if (wcsResult != null && wcsResult.Success)
|
||
{
|
||
// WCS任务下发成功,提交事务
|
||
tran.Commit();
|
||
return Json(new { Code = 200, Msg = "入库成功", Data = new { TaskNo = taskNo } });
|
||
}
|
||
else
|
||
{
|
||
// WCS任务下发失败,回滚事务
|
||
tran.Rollback();
|
||
return Json(new { Code = 500, Msg = wcsResult?.Msg ?? "WCS任务下发失败" });
|
||
}
|
||
}
|
||
catch (System.Exception ex)
|
||
{
|
||
// 发生异常,回滚事务
|
||
tran.Rollback();
|
||
return Json(new { Code = 500, Msg = ex.Message });
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 空托盘出库
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
public IActionResult EmptyPalletOut()
|
||
{
|
||
var container = _unitWork.FindSingle<Container>(x => x.Status == ContainerStatus.空
|
||
&& x.IsLock == ContainerLockEnum.未锁
|
||
&& !string.IsNullOrEmpty(x.LocationCode));
|
||
if (container == null)
|
||
{
|
||
return Json(new { Code = 500, Msg = "库内不存在可用空容器!" });
|
||
}
|
||
|
||
//创建出库任务
|
||
var location = _unitWork.FindSingle<Location>(x => x.Code == container.LocationCode);
|
||
|
||
var outPort = _unitWork.FindSingle<ProcessConfig>(x => x.Code == "00")?.Port;
|
||
if (string.IsNullOrEmpty(outPort))
|
||
{
|
||
return Json(new { Code = 500, Msg = "未配置空托盘出库口,请配置后重新呼叫出库任务!" });
|
||
}
|
||
|
||
var task = new TaskHeader()
|
||
{
|
||
ZoneId = location.ZoneId,
|
||
TaskNo = _unitWork.GetSerialNumber("Out", 6),
|
||
TaskType = TaskTypeEnum.出库,
|
||
ContainerCode = container.Code,
|
||
FromLocation = container.LocationCode,
|
||
ToLocation = outPort,
|
||
Status = TaskStatusEnum.新建
|
||
};
|
||
|
||
var vm = Wtm.CreateVM<LocationVM>();
|
||
var result = vm.SendWcsTask(task);
|
||
if (!result.Status)
|
||
{
|
||
return Json(new { Code = 500, Msg = result.Message });
|
||
}
|
||
|
||
location.Status = LocationStatusEnum.锁定;
|
||
location.UpdateTime = DateTime.Now;
|
||
_unitWork.Update(location);
|
||
|
||
container.IsLock = ContainerLockEnum.任务锁;
|
||
container.UpdateTime = DateTime.Now;
|
||
_unitWork.Update(container);
|
||
return Json(new { Code = 200, Msg = "提交成功" });
|
||
}
|
||
|
||
#endregion API Methods
|
||
}
|
||
|
||
public class ReceiptInModel
|
||
{
|
||
public string PalletCode { get; set; }
|
||
public string SourceCode { get; set; }
|
||
public string MaterialCode { get; set; }
|
||
public string OrderId { get; set; }
|
||
public string OperatorId { get; set; }
|
||
public int ProcessId { get; set; }
|
||
/// <summary>工序编码(页面工序下拉来自工序配置表 ProcessConfig)</summary>
|
||
public string ProcessCode { get; set; }
|
||
public string ProcessName { get; set; }
|
||
public int Type { get; set; }
|
||
public int Qty { get; set; }
|
||
public bool IsQualified { get; set; }
|
||
public string OperatorInfo { get; set; }
|
||
|
||
public string BillCode { get; set; }
|
||
}
|
||
|
||
public class LineRange
|
||
{
|
||
public int BeginLine { get; set; }
|
||
public int EndLine { get; set; }
|
||
}
|
||
}
|