443 lines
17 KiB
C#
443 lines
17 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
|
||
|
||
[HttpPost]
|
||
public IActionResult GetOperators()
|
||
{
|
||
var users = _unitWork.Find<FrameworkUser>(x => x.ITCode != "Admin")
|
||
.Select(x => new {x.ITCode,x.Name})
|
||
.ToList();
|
||
return Json(new { Code = 200, Data = users});
|
||
}
|
||
|
||
[HttpPost]
|
||
public IActionResult GetProcesses()
|
||
{
|
||
try
|
||
{
|
||
var processes = _unitWork.Find<MaterialProcess>(x => x.IsActive)
|
||
.OrderBy(x => x.ProcessOrder)
|
||
.Select(x => new {
|
||
value = x.ID.ToString(),
|
||
label = x.ProcessName,
|
||
processCode = x.ProcessCode,
|
||
processName = x.ProcessName
|
||
})
|
||
.ToList();
|
||
return Json(new { Code = 200, Data = processes });
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return Json(new { Code = 500, Msg = $"获取工序列表失败:{ex.Message}" });
|
||
}
|
||
}
|
||
|
||
[HttpPost]
|
||
public IActionResult GetProcessesByMaterial(string materialCode)
|
||
{
|
||
try
|
||
{
|
||
if (string.IsNullOrWhiteSpace(materialCode))
|
||
{
|
||
return Json(new { Code = 200, Data = new object[] { } });
|
||
}
|
||
|
||
var processes = _unitWork.Find<MaterialProcess>(x => x.MaterialCode == materialCode && x.IsActive)
|
||
.OrderBy(x => x.ProcessOrder)
|
||
.Select(x => new {
|
||
value = x.ID.ToString(),
|
||
label = x.ProcessName,
|
||
processCode = x.ProcessCode,
|
||
processName = x.ProcessName
|
||
})
|
||
.ToList();
|
||
return Json(new { Code = 200, Data = processes });
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return Json(new { Code = 500, Msg = $"获取物料工序失败:{ex.Message}" });
|
||
}
|
||
}
|
||
|
||
[HttpPost]
|
||
public IActionResult GetOperatorInfo()
|
||
{
|
||
var users = _unitWork.Find<FrameworkUser>(x => x.ITCode != "Admin")
|
||
.Select(x => new {x.ITCode,x.Name})
|
||
.ToList();
|
||
return Json(new { Code = 200, Data = users});
|
||
}
|
||
|
||
[HttpPost]
|
||
public IActionResult GetOrdersByMaterial(string materialCode)
|
||
{
|
||
// TODO: 根据物料码查询相关生产订单
|
||
var orders = new List<object>
|
||
{
|
||
new { value = "1", label = "生产订单1" },
|
||
new { value = "2", label = "生产订单2" }
|
||
};
|
||
return Json(new { Code = 200, Data = orders });
|
||
}
|
||
|
||
[HttpPost]
|
||
public IActionResult SubmitReceipt([FromBody] ReceiptInModel model)
|
||
{
|
||
// 获取物料信息
|
||
var material = _unitWork.FindSingle<Material>(m => m.MaterialCode == model.MaterialCode);
|
||
if (material == null)
|
||
{
|
||
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 = "容器不存在" });
|
||
}
|
||
var materialProcess = _unitWork.FindSingle<MaterialProcess>(m => m.ID == model.ProcessId);
|
||
if (materialProcess == null)
|
||
{
|
||
return Json(new { Code = 500, Msg = "未找到此工序配置" });
|
||
}
|
||
|
||
// 从字典查询工序对应的区域
|
||
var zoneName = _unitWork.FindSingle<SysDictData>(x => x.DictType == "ProcessZone"
|
||
&& x.DictLabel == materialProcess.ProcessCode)?.DictValue;
|
||
|
||
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 = model.PalletCode,
|
||
RoadWay = 1,
|
||
Status = TaskStatusEnum.新建,
|
||
CreateBy = model.OperatorId,
|
||
CreateTime = DateTime.Now
|
||
};
|
||
|
||
_unitWork.Add(taskHeader);
|
||
|
||
// 3. 创建库存信息
|
||
var inventory = new Inventory
|
||
{
|
||
ZoneId = zone.ID,
|
||
ContainerCode = model.PalletCode,
|
||
LocationCode = locationCode,
|
||
MaterialCode = model.MaterialCode,
|
||
MaterialType = material.MaterialType,
|
||
MaterialName = material.MaterialName,
|
||
MaterialSpec = material.MaterialSpec,
|
||
MaterialUnit = material.MaterialUnit,
|
||
Batch = materialProcess.ProcessName,
|
||
Lot = materialProcess.ProcessCode,
|
||
Status = model.IsQualified ? "合格" : "不合格",
|
||
TaskStatus = InventoryTaskStatusEnum.Locked,
|
||
TaskQty = model.Qty,
|
||
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 = 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 = model.PalletCode,
|
||
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;
|
||
|
||
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 = model.PalletCode,
|
||
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 = model.PalletCode,
|
||
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>
|
||
[HttpPost]
|
||
public IActionResult SearchMaterials(string keyword)
|
||
{
|
||
try
|
||
{
|
||
if (string.IsNullOrWhiteSpace(keyword))
|
||
{
|
||
return Json(new { Code = 200, Data = new object[] { } });
|
||
}
|
||
|
||
var materials = _unitWork.Find<Material>
|
||
(m => m.MaterialCode.Contains(keyword) || m.MaterialName.Contains(keyword))
|
||
.Select(m => new
|
||
{
|
||
MaterialCode = m.MaterialCode,
|
||
m.MaterialType,
|
||
MaterialName = m.MaterialName
|
||
})
|
||
.Take(10)
|
||
.ToList();
|
||
|
||
return Json(new { Code = 200, Data = materials });
|
||
}
|
||
catch (System.Exception ex)
|
||
{
|
||
return Json(new { Code = 500, Msg = $"搜索物料失败:{ex.Message}" });
|
||
}
|
||
}
|
||
|
||
#endregion API Methods
|
||
}
|
||
|
||
public class ReceiptInModel
|
||
{
|
||
public string PalletCode { get; set; }
|
||
public string MaterialCode { get; set; }
|
||
public string OrderId { get; set; }
|
||
public string OperatorId { get; set; }
|
||
public int ProcessId { get; set; }
|
||
public string ProcessName { get; set; }
|
||
public int Qty { get; set; }
|
||
public bool IsQualified { get; set; }
|
||
public string OperatorInfo { get; set; }
|
||
}
|
||
|
||
public class LineRange
|
||
{
|
||
public int BeginLine { get; set; }
|
||
public int EndLine { get; set; }
|
||
}
|
||
} |