1. 在IUnitWork接口和实现中添加Save方法用于保存更改 2. 完善WCS任务状态处理逻辑,区分线边库和成品库的不同处理流程 3. 实现入库控制器SubmitReceipt方法,包含库位分配、任务创建、库存记录等完整流程 4. 修改LocationTypeEnum枚举值定义,将L改为C表示成品库位 5. 优化库位查询逻辑,增加高度和类型过滤条件 6. 修改容器类型从枚举改为静态类定义 7. 修复LocationController中库位编码生成格式 8. 在WcsRequest中添加LocationType字段用于库位类型区分
268 lines
9.7 KiB
C#
268 lines
9.7 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 Microsoft.EntityFrameworkCore;
|
||
using WalkingTec.Mvvm.Core.Extensions;
|
||
|
||
namespace HHWMS.Web.Controllers
|
||
{
|
||
[Area("receipt")]
|
||
[ActionDescription("入库操作")]
|
||
public partial class ReceiptInController : BaseController
|
||
{
|
||
private readonly IUnitWork _unitWork;
|
||
|
||
public ReceiptInController(IUnitWork unitwork)
|
||
{
|
||
_unitWork = unitwork;
|
||
}
|
||
|
||
#region Index
|
||
|
||
[ActionDescription("Sys.Search")]
|
||
public ActionResult Index()
|
||
{
|
||
return PartialView("Index");
|
||
}
|
||
|
||
#endregion Index
|
||
|
||
#region API Methods
|
||
|
||
[HttpPost]
|
||
public IActionResult GetOperators()
|
||
{
|
||
// TODO: 从数据库获取操作员列表
|
||
var operators = new List<object>
|
||
{
|
||
new { value = "1", label = "张三" },
|
||
new { value = "2", label = "李四" }
|
||
};
|
||
return Json(new { Code = 200, Data = operators });
|
||
}
|
||
|
||
[HttpPost]
|
||
public IActionResult GetProcesses()
|
||
{
|
||
// TODO: 从数据库获取工序列表
|
||
var processes = new List<object>
|
||
{
|
||
new { value = "1", label = "工序1" },
|
||
new { value = "2", label = "工序2" }
|
||
};
|
||
return Json(new { Code = 200, Data = processes });
|
||
}
|
||
|
||
[HttpPost]
|
||
public IActionResult GetOperatorInfo()
|
||
{
|
||
// TODO: 获取当前登录用户信息
|
||
return Json(new { Code = 200, Data = "当前操作员:张三" });
|
||
}
|
||
|
||
[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)
|
||
{
|
||
// 1. 查找可用库位信息 - 使用LocationVM的GetLocation方法
|
||
var locationVM = Wtm.CreateVM<LocationVM>();
|
||
|
||
// 创建WcsRequest对象,设置巷道号为1
|
||
var wcsRequest = new WcsRequest { RoadWay = 1 };
|
||
|
||
// 调用GetLocation方法获取可用库位
|
||
var locationResult = locationVM.GetLocation(wcsRequest);
|
||
|
||
if (!locationResult.Status)
|
||
{
|
||
return Json(new { Code = 500, Msg = locationResult.Message });
|
||
}
|
||
|
||
// 获取返回的库位编码
|
||
string locationCode = locationResult.Data.LocationCode;
|
||
|
||
// 根据库位编码获取完整的库位信息,使用AsNoTracking()避免实体跟踪冲突
|
||
// var availableLocation = DC.Set<Location>().AsNoTracking().FirstOrDefault(l => l.Code == locationCode);
|
||
//
|
||
// if (availableLocation == null)
|
||
// {
|
||
// return Json(new { Code = 500, Msg = "没有可用库位" });
|
||
// }
|
||
|
||
// 获取物料信息
|
||
var material = _unitWork.FindSingle<Material>(m => m.MaterialCode == model.MaterialCode);
|
||
if (material == null)
|
||
{
|
||
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 = "容器不存在" });
|
||
}
|
||
|
||
// 使用事务
|
||
using (var tran = DC.BeginTransaction())
|
||
{
|
||
try
|
||
{
|
||
// 2. 生成入库任务
|
||
var taskNo = _unitWork.GetSerialNumber("In", 6);
|
||
var taskHeader = new TaskHeader
|
||
{
|
||
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
|
||
{
|
||
ContainerCode = model.PalletCode,
|
||
LocationCode = locationCode,
|
||
MaterialCode = model.MaterialCode,
|
||
MaterialName = material.MaterialName,
|
||
MaterialSpec = material.MaterialSpec,
|
||
MaterialUnit = material.MaterialUnit,
|
||
Qty = model.Quantity,
|
||
TaskStatus = InventoryTaskStatusEnum.Locked,
|
||
CreateBy = model.OperatorId,
|
||
CreateTime = DateTime.Now
|
||
};
|
||
|
||
_unitWork.Add(inventory);
|
||
|
||
// 4. 创建库存交易记录
|
||
var transaction = new InventoryTransaction
|
||
{
|
||
Type = InventoryTranType.入库,
|
||
ContainerCode = model.PalletCode,
|
||
LocationCode = locationCode,
|
||
MaterialCode = model.MaterialCode,
|
||
MaterialName = material.MaterialName,
|
||
MaterialSpec = material.MaterialSpec,
|
||
MaterialUnit = material.MaterialUnit,
|
||
Qty = model.Quantity,
|
||
CreateBy = model.OperatorId,
|
||
CreateTime = DateTime.Now
|
||
};
|
||
|
||
_unitWork.Add(transaction);
|
||
|
||
// 5. 锁定容器和库位
|
||
container.IsLock = ContainerLockEnum.任务锁;
|
||
container.LocationCode = locationCode;
|
||
_unitWork.Update(container);
|
||
|
||
// 从数据库中重新获取库位实体,避免实体跟踪冲突
|
||
var locationToUpdate = _unitWork.FindSingle<Location>(l => l.Code == locationCode);
|
||
locationToUpdate.Status = LocationStatusEnum.Locked;
|
||
locationToUpdate.ContainerCode = model.PalletCode;
|
||
_unitWork.Update(locationToUpdate);
|
||
|
||
// 6. 调用WCS接口下发任务
|
||
var wcsTaskModel = new WCSTaskModel
|
||
{
|
||
TaskNo = taskNo,
|
||
PalletNo = model.PalletCode,
|
||
FromLocation = "", // 入库任务起始位置为空
|
||
ToLocation = locationCode,
|
||
};
|
||
|
||
// 调用WCS任务下发接口
|
||
var wcsResult = Wtm.CallAPI<WCSTaskResponse>("WCS", "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,
|
||
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 string ProcessId { get; set; }
|
||
public int Quantity { get; set; }
|
||
public bool IsQualified { get; set; }
|
||
public string OperatorInfo { get; set; }
|
||
}
|
||
} |