578 lines
20 KiB
C#
578 lines
20 KiB
C#
using Microsoft.AspNetCore.Http;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text.Json;
|
||
using System.Threading.Tasks;
|
||
using WalkingTec.Mvvm.Core;
|
||
using WalkingTec.Mvvm.Mvc;
|
||
using WalkingTec.Mvvm.Core.Extensions;
|
||
using HHWMS.Web;
|
||
using HHWMS.Web.Models;
|
||
|
||
namespace HHWMS.Web.Controllers
|
||
{
|
||
[Area("inventory")]
|
||
[ActionDescription("库存管理")]
|
||
public partial class InventoryController : BaseController
|
||
{
|
||
private readonly IUnitWork _unitWork;
|
||
|
||
public InventoryController(IUnitWork unitWork)
|
||
{
|
||
_unitWork = unitWork;
|
||
}
|
||
|
||
#region Search
|
||
[ActionDescription("Sys.Search")]
|
||
public ActionResult Index()
|
||
{
|
||
var vm = Wtm.CreateVM<InventoryListVM>();
|
||
return PartialView(vm);
|
||
}
|
||
|
||
[ActionDescription("Sys.Search")]
|
||
[HttpPost]
|
||
public string Search(InventorySearcher searcher)
|
||
{
|
||
var vm = Wtm.CreateVM<InventoryListVM>(passInit: true);
|
||
if (ModelState.IsValid)
|
||
{
|
||
vm.Searcher = searcher;
|
||
return vm.GetJson(false);
|
||
}
|
||
else
|
||
{
|
||
return vm.GetError();
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region Create
|
||
[ActionDescription("Sys.Create")]
|
||
public ActionResult Create()
|
||
{
|
||
var vm = Wtm.CreateVM<InventoryVM>();
|
||
return PartialView(vm);
|
||
}
|
||
|
||
[HttpPost]
|
||
[ActionDescription("Sys.Create")]
|
||
public ActionResult Create(InventoryVM vm)
|
||
{
|
||
if (!ModelState.IsValid)
|
||
{
|
||
return PartialView(vm);
|
||
}
|
||
else
|
||
{
|
||
vm.DoAdd();
|
||
if (!ModelState.IsValid)
|
||
{
|
||
vm.DoReInit();
|
||
return PartialView(vm);
|
||
}
|
||
else
|
||
{
|
||
return FFResult().CloseDialog().RefreshGrid();
|
||
}
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region Edit
|
||
[ActionDescription("Sys.Edit")]
|
||
public ActionResult Edit(string id)
|
||
{
|
||
var vm = Wtm.CreateVM<InventoryVM>(id);
|
||
return PartialView(vm);
|
||
}
|
||
|
||
[ActionDescription("Sys.Edit")]
|
||
[HttpPost]
|
||
[ValidateFormItemOnly]
|
||
public ActionResult Edit(InventoryVM vm)
|
||
{
|
||
if (!ModelState.IsValid)
|
||
{
|
||
return PartialView(vm);
|
||
}
|
||
else
|
||
{
|
||
vm.DoEdit();
|
||
if (!ModelState.IsValid)
|
||
{
|
||
vm.DoReInit();
|
||
return PartialView(vm);
|
||
}
|
||
else
|
||
{
|
||
return FFResult().CloseDialog().RefreshGridRow(vm.Entity.ID);
|
||
}
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region Delete
|
||
[ActionDescription("Sys.Delete")]
|
||
public ActionResult Delete(string id)
|
||
{
|
||
var vm = Wtm.CreateVM<InventoryVM>(id);
|
||
return PartialView(vm);
|
||
}
|
||
|
||
[ActionDescription("Sys.Delete")]
|
||
[HttpPost]
|
||
public ActionResult Delete(string id, IFormCollection nouse)
|
||
{
|
||
var vm = Wtm.CreateVM<InventoryVM>(id);
|
||
vm.DoDelete();
|
||
if (!ModelState.IsValid)
|
||
{
|
||
return PartialView(vm);
|
||
}
|
||
else
|
||
{
|
||
return FFResult().CloseDialog().RefreshGrid();
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region Details
|
||
[ActionDescription("Sys.Details")]
|
||
public ActionResult Details(string id)
|
||
{
|
||
var vm = Wtm.CreateVM<InventoryVM>(id);
|
||
return PartialView(vm);
|
||
}
|
||
#endregion
|
||
|
||
#region BatchEdit
|
||
[HttpPost]
|
||
[ActionDescription("Sys.BatchEdit")]
|
||
public ActionResult BatchEdit(string[] IDs)
|
||
{
|
||
var vm = Wtm.CreateVM<InventoryBatchVM>(Ids: IDs);
|
||
return PartialView(vm);
|
||
}
|
||
|
||
[HttpPost]
|
||
[ActionDescription("Sys.BatchEdit")]
|
||
public ActionResult DoBatchEdit(InventoryBatchVM vm, IFormCollection nouse)
|
||
{
|
||
if (!ModelState.IsValid || !vm.DoBatchEdit())
|
||
{
|
||
return PartialView("BatchEdit",vm);
|
||
}
|
||
else
|
||
{
|
||
return FFResult().CloseDialog().RefreshGrid().Alert(Localizer["Sys.BatchEditSuccess", vm.Ids.Length]);
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region BatchDelete
|
||
[HttpPost]
|
||
[ActionDescription("Sys.BatchDelete")]
|
||
public ActionResult BatchDelete(string[] IDs)
|
||
{
|
||
var vm = Wtm.CreateVM<InventoryBatchVM>(Ids: IDs);
|
||
return PartialView(vm);
|
||
}
|
||
|
||
[HttpPost]
|
||
[ActionDescription("Sys.BatchDelete")]
|
||
public ActionResult DoBatchDelete(InventoryBatchVM vm, IFormCollection nouse)
|
||
{
|
||
if (!ModelState.IsValid || !vm.DoBatchDelete())
|
||
{
|
||
return PartialView("BatchDelete",vm);
|
||
}
|
||
else
|
||
{
|
||
return FFResult().CloseDialog().RefreshGrid().Alert(Localizer["Sys.BatchDeleteSuccess", vm.Ids.Length]);
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region Import
|
||
[ActionDescription("Sys.Import")]
|
||
public ActionResult Import()
|
||
{
|
||
var vm = Wtm.CreateVM<InventoryImportVM>();
|
||
return PartialView(vm);
|
||
}
|
||
|
||
[HttpPost]
|
||
[ActionDescription("Sys.Import")]
|
||
public ActionResult Import(InventoryImportVM vm, IFormCollection nouse)
|
||
{
|
||
if (vm.ErrorListVM.EntityList.Count > 0 || !vm.BatchSaveData())
|
||
{
|
||
return PartialView(vm);
|
||
}
|
||
else
|
||
{
|
||
return FFResult().CloseDialog().RefreshGrid().Alert(Localizer["Sys.ImportSuccess", vm.EntityList.Count.ToString()]);
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region Export
|
||
[ActionDescription("Sys.Export")]
|
||
[HttpPost]
|
||
public IActionResult ExportExcel(InventoryListVM vm)
|
||
{
|
||
return vm.GetExportData();
|
||
}
|
||
#endregion
|
||
|
||
#region InvOut 出库
|
||
/*[ActionDescription("TaskType.Out")]
|
||
public ActionResult InvOut(string id)
|
||
{
|
||
var vm = Wtm.CreateVM<InventoryVM>(id);
|
||
if (vm.Entity.TaskStatus == InventoryTaskStatusEnum.Locked)
|
||
{
|
||
return FFResult().Alert("所选库存已被锁定!,请检查后重试!").RefreshGrid();
|
||
}
|
||
// 判断是否存在出口配置
|
||
var processConfig = _unitWork.FindSingle<ProcessConfig>(m =>
|
||
m.Name == vm.Entity.Lot);
|
||
if (processConfig == null)
|
||
{
|
||
return FFResult().Alert("所选库存下道工序出口未配置!,请检查后重试!").RefreshGrid();
|
||
}
|
||
return PartialView(vm);
|
||
}*/
|
||
|
||
[ActionDescription("TaskType.Out")]
|
||
public ActionResult InvOut(string id)
|
||
{
|
||
// var vm = Wtm.CreateVM<InventoryVM>(id);
|
||
// 校验参数
|
||
// if (string.IsNullOrWhiteSpace(vm.OutPort))
|
||
// {
|
||
// return FFResult().Alert("出口必选,请选择后重试!");
|
||
// }
|
||
// 校验库存
|
||
var inv = _unitWork.FindSingle<Inventory>(x => x.ID == int.Parse(id));
|
||
if (inv == null)
|
||
{
|
||
return FFResult().Alert("所选库存不存在,请检查后重试!").RefreshGrid();
|
||
}
|
||
if (inv.TaskStatus == InventoryTaskStatusEnum.Locked)
|
||
{
|
||
return FFResult().Alert("所选库存已被锁定!,请检查后重试!").RefreshGrid();
|
||
}
|
||
|
||
// 校验容器
|
||
var container = _unitWork.FindSingle<Container>(x => x.Code == inv.ContainerCode);
|
||
if (container == null)
|
||
{
|
||
return FFResult().Alert("所选库存的容器不存在,请检查后重试!").RefreshGrid();
|
||
}
|
||
|
||
if (container.IsLock == ContainerLockEnum.任务锁)
|
||
{
|
||
return FFResult().Alert("库存所在容器已被锁定!,请检查后重试!").RefreshGrid();
|
||
}
|
||
|
||
// 校验库位
|
||
var location = _unitWork.FindSingle<Location>(x => x.Code == inv.LocationCode);
|
||
if (location == null)
|
||
{
|
||
return FFResult().Alert("所选库存不在库内,请检查后重试!").RefreshGrid();
|
||
}
|
||
|
||
if (location.Status == LocationStatusEnum.锁定)
|
||
{
|
||
return FFResult().Alert("库存所在仓位已被锁定!,请检查后重试!").RefreshGrid();
|
||
}
|
||
|
||
// 判断是否存在出口配置
|
||
var processConfig = _unitWork.FindSingle<ProcessConfig>(m =>
|
||
m.Name == inv.Lot);
|
||
if (processConfig == null)
|
||
{
|
||
return FFResult().Alert("所选库存下道工序出口未配置!,请检查后重试!").RefreshGrid();
|
||
}
|
||
// 创建任务
|
||
var task = new TaskHeader()
|
||
{
|
||
TaskNo = _unitWork.GetSerialNumber("Out", 6),
|
||
ZoneId = inv.ZoneId,
|
||
TaskType = TaskTypeEnum.出库,
|
||
FromLocation = inv.LocationCode,
|
||
ToLocation = processConfig.Port,
|
||
ContainerCode = inv.ContainerCode,
|
||
RoadWay = 1,
|
||
Status = TaskStatusEnum.新建,
|
||
CreateBy = Wtm.LoginUserInfo.ITCode,
|
||
CreateTime = DateTime.Now
|
||
};
|
||
|
||
// 6. 调用WCS接口下发任务
|
||
var wcsTaskModel = new WCSTaskModel
|
||
{
|
||
TaskNo = task.TaskNo,
|
||
PalletNo = task.ContainerCode,
|
||
FromLocation = task.FromLocation, // 入库任务起始位置为空
|
||
ToLocation = task.ToLocation,
|
||
};
|
||
|
||
//调用WCS任务下发接口
|
||
var wcsResult = Wtm.CallAPI<WCSTaskResponse>("WCSXB", "TaskAssign", HttpMethodEnum.POST, wcsTaskModel).Result.Data;
|
||
|
||
if (wcsResult != null && wcsResult.Success)
|
||
{
|
||
// WCS任务下发成功
|
||
task.Status = TaskStatusEnum.已下发;
|
||
_unitWork.Add(task);
|
||
|
||
// 更新库存
|
||
inv.TaskStatus = InventoryTaskStatusEnum.Locked;
|
||
inv.UpdateTime = DateTime.Now;
|
||
_unitWork.Update(inv);
|
||
|
||
// 更新容器
|
||
// _unitWork.BatchUpdate<Container>(x => x.Code == task.ContainerCode, x => new Container()
|
||
// {
|
||
// IsLock = ContainerLockEnum.任务锁,
|
||
// UpdateTime = DateTime.Now
|
||
// });
|
||
container.IsLock = ContainerLockEnum.任务锁;
|
||
container.UpdateTime = DateTime.Now;
|
||
_unitWork.Update(container);
|
||
|
||
// 更新库位
|
||
location.Status = LocationStatusEnum.锁定;
|
||
location.UpdateTime = DateTime.Now;
|
||
_unitWork.Update(location);
|
||
|
||
return FFResult().Message("出库任务创建成功").RefreshGrid();
|
||
}
|
||
else
|
||
{
|
||
// WCS任务下发失败
|
||
return FFResult().Message(wcsResult?.Msg ?? "WCS任务下发失败").RefreshGrid();
|
||
}
|
||
|
||
}
|
||
#endregion
|
||
|
||
#region InvOut 成品出库
|
||
[ActionDescription("TaskType.Out")]
|
||
public ActionResult InvOut2(string id)
|
||
{
|
||
var vm = Wtm.CreateVM<InventoryVM>(id);
|
||
if (vm.Entity.TaskStatus == InventoryTaskStatusEnum.Locked)
|
||
{
|
||
return FFResult().Alert("所选库存已被锁定!,请检查后重试!").RefreshGrid();
|
||
}
|
||
return PartialView(vm);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 成品出库
|
||
/// </summary>
|
||
/// <param name="vm"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
[ActionDescription("TaskType.Out")]
|
||
public ActionResult InvOut2(InventoryVM vm)
|
||
{
|
||
try
|
||
{
|
||
var id = vm.Entity.ID;
|
||
// 校验参数
|
||
if (string.IsNullOrWhiteSpace(vm.OutPort))
|
||
{
|
||
return FFResult().Alert("出口必选,请选择后重试!");
|
||
}
|
||
|
||
// 校验库存
|
||
var inv = _unitWork.FindSingle<Inventory>(x => x.ID == id);
|
||
if (inv == null)
|
||
{
|
||
return FFResult().CloseDialog().Alert("所选库存不存在,请检查后重试!").RefreshGrid();
|
||
}
|
||
|
||
if (inv.TaskStatus == InventoryTaskStatusEnum.Locked)
|
||
{
|
||
return FFResult().CloseDialog().Alert("所选库存已被锁定!,请检查后重试!").RefreshGrid();
|
||
}
|
||
|
||
// 校验容器
|
||
var container = _unitWork.FindSingle<Container>(x => x.Code == inv.ContainerCode);
|
||
if (container == null)
|
||
{
|
||
return FFResult().CloseDialog().Alert("所选库存的容器不存在,请检查后重试!").RefreshGrid();
|
||
}
|
||
|
||
if (container.IsLock == ContainerLockEnum.任务锁)
|
||
{
|
||
return FFResult().CloseDialog().Alert("库存所在容器已被锁定!,请检查后重试!").RefreshGrid();
|
||
}
|
||
|
||
// 校验库位
|
||
var location = _unitWork.FindSingle<Location>(x => x.Code == inv.LocationCode);
|
||
if (location == null)
|
||
{
|
||
return FFResult().CloseDialog().Alert("所选库存不在库内,请检查后重试!").RefreshGrid();
|
||
}
|
||
|
||
if (location.Status == LocationStatusEnum.锁定)
|
||
{
|
||
return FFResult().CloseDialog().Alert("库存所在仓位已被锁定!,请检查后重试!").RefreshGrid();
|
||
}
|
||
|
||
// 创建任务
|
||
var task = new TaskHeader()
|
||
{
|
||
TaskNo = _unitWork.GetSerialNumber("Out", 6),
|
||
ZoneId = inv.ZoneId,
|
||
TaskType = TaskTypeEnum.出库,
|
||
FromLocation = inv.LocationCode,
|
||
ToLocation = vm.OutPort,
|
||
ContainerCode = inv.ContainerCode,
|
||
RoadWay = 2,
|
||
Status = TaskStatusEnum.新建,
|
||
CreateBy = Wtm.LoginUserInfo.ITCode,
|
||
CreateTime = DateTime.Now
|
||
};
|
||
|
||
var locationVm = Wtm.CreateVM<LocationVM>();
|
||
var result = locationVm.SendWcsTask(task);
|
||
if (result.Status)
|
||
{
|
||
// 更新库存
|
||
inv.TaskStatus = InventoryTaskStatusEnum.Locked;
|
||
inv.UpdateTime = DateTime.Now;
|
||
_unitWork.Update(inv);
|
||
|
||
// 更新容器
|
||
container.IsLock = ContainerLockEnum.任务锁;
|
||
container.UpdateTime = DateTime.Now;
|
||
_unitWork.Update(container);
|
||
|
||
// 更新库位
|
||
location.Status = LocationStatusEnum.锁定;
|
||
location.UpdateTime = DateTime.Now;
|
||
_unitWork.Update(location);
|
||
return FFResult().CloseDialog().Message("出库任务创建成功").RefreshGrid();
|
||
}
|
||
else
|
||
{
|
||
return FFResult().CloseDialog().Message(result.Message ?? "WCS任务下发失败").RefreshGrid();
|
||
}
|
||
// 6. 调用WCS接口下发任务
|
||
|
||
/*
|
||
var wcsTaskModel = new WCSTaskModel
|
||
{
|
||
TaskNo = task.TaskNo,
|
||
PalletNo = task.ContainerCode,
|
||
FromLocation = task.FromLocation, // 入库任务起始位置为空
|
||
ToLocation = task.ToLocation,
|
||
};
|
||
|
||
//调用WCS任务下发接口
|
||
var wcsResult = Wtm.CallAPI<WCSTaskResponse>("WCSCP", "TaskAssign", HttpMethodEnum.POST, wcsTaskModel).Result.Data;
|
||
|
||
if (wcsResult != null && wcsResult.Success)
|
||
{
|
||
// WCS任务下发成功
|
||
task.Status = TaskStatusEnum.已下发;
|
||
_unitWork.Add(task);
|
||
|
||
// 更新库存
|
||
inv.TaskStatus = InventoryTaskStatusEnum.Locked;
|
||
inv.UpdateTime = DateTime.Now;
|
||
_unitWork.Update(inv);
|
||
|
||
// 更新容器
|
||
// _unitWork.BatchUpdate<Container>(x => x.Code == task.ContainerCode, x => new Container()
|
||
// {
|
||
// IsLock = ContainerLockEnum.任务锁,
|
||
// UpdateTime = DateTime.Now
|
||
// });
|
||
container.IsLock = ContainerLockEnum.任务锁;
|
||
container.UpdateTime = DateTime.Now;
|
||
_unitWork.Update(container);
|
||
|
||
// 更新库位
|
||
location.Status = LocationStatusEnum.锁定;
|
||
location.UpdateTime = DateTime.Now;
|
||
_unitWork.Update(location);
|
||
|
||
return FFResult().CloseDialog().Message("出库任务创建成功").RefreshGrid();
|
||
}
|
||
else
|
||
{
|
||
// WCS任务下发失败
|
||
return FFResult().CloseDialog().Message(wcsResult?.Msg ?? "WCS任务下发失败").RefreshGrid();
|
||
}*/
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return FFResult().CloseDialog().Alert(ex.Message).RefreshGrid();
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region FinishIn 回传ERP入库信息
|
||
/// <summary>
|
||
/// 回传ERP入库信息
|
||
/// </summary>
|
||
/// <param name="ids">库存ID数组</param>
|
||
/// <returns></returns>
|
||
[ActionDescription("回传ERP入库")]
|
||
public async Task<ActionResult> FinishIn(string id)
|
||
{
|
||
try
|
||
{
|
||
// 参数校验
|
||
// 校验库存
|
||
var inv = _unitWork.FindSingle<Inventory>(x => x.ID == int.Parse(id));
|
||
if (inv == null)
|
||
{
|
||
return FFResult().Alert("所选库存不存在,请检查后重试!").RefreshGrid();
|
||
}
|
||
if (inv.TaskStatus == InventoryTaskStatusEnum.Locked)
|
||
{
|
||
return FFResult().Alert("所选库存存在任务,请检查后重试!").RefreshGrid();
|
||
}
|
||
// if (string.IsNullOrEmpty(inv.ReceiptDetailId))
|
||
// {
|
||
// return FFResult().Alert("所选库存没关联订单,无需回传!").RefreshGrid();
|
||
// }
|
||
if (inv.UploadErpFlag is 1)
|
||
{
|
||
return FFResult().Alert("所选库存已报工回传,无需再次回传!").RefreshGrid();
|
||
}
|
||
|
||
// 使用通用的ProcessErpFinishIn方法
|
||
var invVM = Wtm.CreateVM<InventoryVM>();
|
||
var success = await invVM.ProcessErpFinishIn(inv, TaskTypeEnum.入库);
|
||
|
||
if (success)
|
||
{
|
||
return FFResult().Alert("回传ERP成功").RefreshGrid();
|
||
}
|
||
else
|
||
{
|
||
return FFResult().Alert("回传ERP失败").RefreshGrid();
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return FFResult().Alert($"回传ERP失败:{ex.Message}" ).RefreshGrid();
|
||
}
|
||
}
|
||
#endregion
|
||
}
|
||
}
|