629 lines
24 KiB
C#
629 lines
24 KiB
C#
using Microsoft.AspNetCore.Http;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Threading.Tasks;
|
||
using WalkingTec.Mvvm.Core;
|
||
using WalkingTec.Mvvm.Mvc;
|
||
using WalkingTec.Mvvm.Core.Extensions;
|
||
using HHWMS.Web;
|
||
using HHWMS.Web.Models;
|
||
using Microsoft.EntityFrameworkCore;
|
||
|
||
namespace HHWMS.Web.Controllers
|
||
{
|
||
[Area("receipt")]
|
||
[ActionDescription("入库单")]
|
||
public partial class ReceiptHeaderController : BaseController
|
||
{
|
||
private readonly IUnitWork _unitwork;
|
||
public ReceiptHeaderController(IUnitWork unitwork)
|
||
{
|
||
_unitwork = unitwork;
|
||
}
|
||
#region Search
|
||
[ActionDescription("Sys.Search")]
|
||
public ActionResult Index()
|
||
{
|
||
var vm = Wtm.CreateVM<ReceiptHeaderListVM>();
|
||
return PartialView(vm);
|
||
}
|
||
|
||
[ActionDescription("Sys.Search")]
|
||
[HttpPost]
|
||
public string Search(ReceiptHeaderSearcher searcher)
|
||
{
|
||
var vm = Wtm.CreateVM<ReceiptHeaderListVM>(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<ReceiptHeaderVM>();
|
||
vm.Entity.Code = _unitwork.GetSerialNumber("R");
|
||
vm.Entity.BillType = "In";
|
||
vm.Entity.AppointmentTime = DateTime.Now;
|
||
return PartialView(vm);
|
||
}
|
||
|
||
[HttpPost]
|
||
[ActionDescription("Sys.Create")]
|
||
public ActionResult Create(ReceiptHeaderVM vm)
|
||
{
|
||
if (!ModelState.IsValid)
|
||
{
|
||
return PartialView(vm);
|
||
}
|
||
else
|
||
{
|
||
if (_unitwork.IsExit<ReceiptHeader>(x => x.Code == vm.Entity.Code))
|
||
{
|
||
return FFResult().Alert($"入库单号 {vm.Entity.Code} 重复!");
|
||
}
|
||
vm.Entity.FirstStatus = ReceiptStatusEnum.新建;
|
||
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<ReceiptHeaderVM>(id);
|
||
return PartialView(vm);
|
||
}
|
||
|
||
[ActionDescription("Sys.Edit")]
|
||
[HttpPost]
|
||
[ValidateFormItemOnly]
|
||
public ActionResult Edit(ReceiptHeaderVM 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<ReceiptHeaderVM>(id);
|
||
return PartialView(vm);
|
||
}
|
||
|
||
[ActionDescription("Sys.Delete")]
|
||
[HttpPost]
|
||
public ActionResult Delete(string id, IFormCollection nouse)
|
||
{
|
||
var vm = Wtm.CreateVM<ReceiptHeaderVM>(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<ReceiptHeaderVM>(id);
|
||
vm.Details.SearcherMode = ListVMSearchModeEnum.MasterDetail;
|
||
return PartialView(vm);
|
||
}
|
||
#endregion
|
||
|
||
#region BatchEdit
|
||
[HttpPost]
|
||
[ActionDescription("Sys.BatchEdit")]
|
||
public ActionResult BatchEdit(string[] IDs)
|
||
{
|
||
var vm = Wtm.CreateVM<ReceiptHeaderBatchVM>(Ids: IDs);
|
||
return PartialView(vm);
|
||
}
|
||
|
||
[HttpPost]
|
||
[ActionDescription("Sys.BatchEdit")]
|
||
public ActionResult DoBatchEdit(ReceiptHeaderBatchVM 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<ReceiptHeaderBatchVM>(Ids: IDs);
|
||
return PartialView(vm);
|
||
}
|
||
|
||
[HttpPost]
|
||
[ActionDescription("Sys.BatchDelete")]
|
||
public ActionResult DoBatchDelete(ReceiptHeaderBatchVM 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<ReceiptHeaderImportVM>();
|
||
return PartialView(vm);
|
||
}
|
||
|
||
[HttpPost]
|
||
[ActionDescription("Sys.Import")]
|
||
public ActionResult Import(ReceiptHeaderImportVM 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(ReceiptHeaderListVM vm)
|
||
{
|
||
return vm.GetExportData();
|
||
}
|
||
#endregion
|
||
|
||
[ActionDescription("GetReceiptDetails")]
|
||
public ActionResult GetReceiptDetails()
|
||
{
|
||
var vm = Wtm.CreateVM<ReceiptHeaderVM>();
|
||
return PartialView(vm);
|
||
}
|
||
/// <summary>
|
||
/// 根据入库单单号获取ERP生产订单数据并更新
|
||
/// </summary>
|
||
[HttpPost]
|
||
[ActionDescription("GetReceiptDetails")]
|
||
public async Task<IActionResult> GetReceiptDetails(ReceiptHeaderVM vm)
|
||
{
|
||
string receiptCode = vm.Entity.SourceCode;
|
||
try
|
||
{
|
||
if (string.IsNullOrWhiteSpace(receiptCode))
|
||
{
|
||
return FFResult().Alert($"入库单单号不能为空");
|
||
}
|
||
|
||
// 根据入库单单号查找对应的 SourceCode(生产订单号)
|
||
var header = _unitwork.FindSingle<ReceiptHeader>(h => h.SourceCode == receiptCode);
|
||
if (header != null)
|
||
{
|
||
return FFResult().Alert($"已存在此入库单,单号:{receiptCode}");
|
||
}
|
||
|
||
// 调用 ERP 接口获取该订单的最新数据
|
||
var request = new LoadMoDetailListRequest
|
||
{
|
||
ErpCode = "002",
|
||
MoCode = receiptCode, // 使用生产订单号查询
|
||
Size = 100,
|
||
Page = 1
|
||
};
|
||
|
||
var apiResult = await Wtm.CallAPI<LoadMoDetailListResponse>("ERP", "LoadMoDetailList", HttpMethodEnum.POST, request);
|
||
|
||
if (apiResult?.Data != null && apiResult.Data.Code == 0 && apiResult.Data.Data?.Count > 0)
|
||
{
|
||
Wtm.DoLog("获取ERP订单数据", ActionLogTypesEnum.Normal, "GetReceiptDetails",
|
||
request: $"订单号:{receiptCode},获取到 {apiResult.Data.Data.Count} 条明细",
|
||
result: "开始处理订单数据");
|
||
|
||
// 使用相同的逻辑处理订单数据
|
||
await ProcessOrderData(_unitwork, apiResult.Data.Data);
|
||
|
||
Wtm.DoLog("获取ERP订单数据", ActionLogTypesEnum.Normal, "GetReceiptDetails",
|
||
request: $"订单号:{receiptCode}",
|
||
result: "订单数据处理完成");
|
||
|
||
return FFResult().CloseDialog().RefreshGrid().Message("订单数据获取成功");
|
||
}
|
||
else
|
||
{
|
||
return FFResult().Alert(apiResult?.Data?.Msg ?? apiResult?.ErrorMsg ?? "未获取到订单数据");
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Wtm.DoLog("获取ERP订单数据", ActionLogTypesEnum.Normal, "GetReceiptDetails",
|
||
request: $"入库单号:{receiptCode}",
|
||
result: $"错误:{ex.Message}");
|
||
return FFResult().Alert($"获取订单数据失败:{ex.Message}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 处理订单数据并保存到本地数据库(照搬 LoadErpOrderJob)
|
||
/// </summary>
|
||
private async Task ProcessOrderData(IUnitWork unitWork, List<MoDetailItem> orderList)
|
||
{
|
||
// 按订单号分组
|
||
var orderGroups = orderList.GroupBy(x => x.MoCode);
|
||
|
||
foreach (var group in orderGroups)
|
||
{
|
||
var moCode = group.Key;
|
||
var orderDetails = group.ToList();
|
||
|
||
// 检查订单是否已存在
|
||
var existingHeader = unitWork.FindSingle<ReceiptHeader>(h => h.SourceCode == moCode);
|
||
|
||
if (existingHeader == null)
|
||
{
|
||
// 创建新的入库单头
|
||
var header = new ReceiptHeader
|
||
{
|
||
SourceCode = moCode, // 生产订单号
|
||
Code = unitWork.GetSerialNumber("REC", 8), // 生成入库单号
|
||
BillType = "生产订单",
|
||
TotalQty = orderDetails.Sum(x => x.Qty),
|
||
TotalLines = orderDetails.Count,
|
||
FirstStatus = ReceiptStatusEnum.新建,
|
||
LastStatus = ReceiptStatusEnum.新建,
|
||
Remark = orderDetails.First().Memo,
|
||
CreateTime = DateTime.Now,
|
||
CreateBy = Wtm.LoginUserInfo?.ITCode ?? "系统",
|
||
UpdateVersion = 1
|
||
};
|
||
|
||
unitWork.Add(header); // 保存以获取 HeaderId
|
||
|
||
// 获取订单明细中的所有物料编码
|
||
var invCodes = orderDetails
|
||
.Where(x => !string.IsNullOrWhiteSpace(x.InvCode))
|
||
.Select(x => x.InvCode)
|
||
.Distinct()
|
||
.ToList();
|
||
|
||
// 一次性查询本地数据库中已存在的物料(优化性能)
|
||
var existingMaterials = unitWork.Find<Material>(m => invCodes.Contains(m.MaterialCode))
|
||
.ToList();
|
||
|
||
// 待新增的物料列表
|
||
var newMaterials = new List<Material>();
|
||
|
||
// 创建入库单明细
|
||
var details = new List<ReceiptDetail>();
|
||
foreach (var item in orderDetails)
|
||
{
|
||
// 判断物料是否存在,不存在则创建
|
||
if (!string.IsNullOrWhiteSpace(item.InvCode))
|
||
{
|
||
var existingMaterial = existingMaterials.FirstOrDefault(m => m.MaterialCode == item.InvCode);
|
||
if (existingMaterial == null)
|
||
{
|
||
// 检查是否已在待新增列表中
|
||
if (!newMaterials.Any(m => m.MaterialCode == item.InvCode))
|
||
{
|
||
// 物料不存在,创建新物料
|
||
var newMaterial = new Material
|
||
{
|
||
MaterialCode = item.InvCode,
|
||
MaterialName = item.InvName,
|
||
MaterialSpec = item.InvStd,
|
||
MaterialType = item.InvDefine1, // 图号
|
||
Qty = 0,
|
||
CreateTime = DateTime.Now,
|
||
CreateBy = Wtm.LoginUserInfo?.ITCode ?? "系统"
|
||
};
|
||
newMaterials.Add(newMaterial);
|
||
}
|
||
}
|
||
}
|
||
|
||
var detail = new ReceiptDetail
|
||
{
|
||
HeaderId = header.ID,
|
||
SourceCode = header.SourceCode,
|
||
MaterialCode = item.InvCode,
|
||
MaterialType = item.InvDefine1, // 图号
|
||
MaterialName = item.InvName,
|
||
Lot = item.MoDetailId, // 明细id
|
||
MaterialSpec = item.InvStd,
|
||
Qty = item.Qty,
|
||
CompletedQty = 0,
|
||
Status = ReceiptStatusEnum.新建,
|
||
CreateTime = DateTime.Now,
|
||
CreateBy = Wtm.LoginUserInfo?.ITCode ?? "系统",
|
||
UpdateVersion = 1
|
||
};
|
||
|
||
details.Add(detail);
|
||
|
||
// 保存工序路线到 MaterialProcess 表
|
||
if (item.ProcessRoute != null && item.ProcessRoute.Count > 0)
|
||
{
|
||
await SaveProcessRoute(unitWork, item.InvCode, item.ProcessRoute);
|
||
}
|
||
}
|
||
|
||
// 批量新增物料
|
||
if (newMaterials.Count > 0)
|
||
{
|
||
unitWork.BatchAdd(newMaterials);
|
||
}
|
||
|
||
unitWork.BatchAdd(details);
|
||
|
||
Wtm.DoLog("保存订单数据", ActionLogTypesEnum.Normal, "ProcessOrderData",
|
||
request: $"订单号:{moCode}",
|
||
result: $"成功创建入库单 {header.Code},明细 {details.Count} 条");
|
||
}
|
||
else
|
||
{
|
||
// 订单已存在,更新数据
|
||
existingHeader.TotalQty = orderDetails.Sum(x => x.Qty);
|
||
existingHeader.TotalLines = orderDetails.Count;
|
||
existingHeader.Remark = orderDetails.First().Memo;
|
||
existingHeader.UpdateVersion++;
|
||
existingHeader.UpdateTime = DateTime.Now;
|
||
existingHeader.UpdateBy = Wtm.LoginUserInfo?.ITCode ?? "系统";
|
||
unitWork.Update(existingHeader);
|
||
|
||
// 更新明细
|
||
var oldDetails = unitWork.Find<ReceiptDetail>(d => d.HeaderId == existingHeader.ID).ToList();
|
||
|
||
// 获取订单明细中的所有物料编码
|
||
var invCodes = orderDetails
|
||
.Where(x => !string.IsNullOrWhiteSpace(x.InvCode))
|
||
.Select(x => x.InvCode)
|
||
.Distinct()
|
||
.ToList();
|
||
|
||
// 一次性查询本地数据库中已存在的物料(优化性能)
|
||
var existingMaterials = unitWork.Find<Material>(m => invCodes.Contains(m.MaterialCode))
|
||
.ToList();
|
||
|
||
// 待新增的物料列表
|
||
var newMaterials = new List<Material>();
|
||
|
||
var newDetails = new List<ReceiptDetail>();
|
||
foreach (var item in orderDetails)
|
||
{
|
||
// 判断明细是否存在 用MoDetailId 判断
|
||
if (!oldDetails.Any(x => x.Lot == item.MoDetailId))
|
||
{
|
||
// 判断物料是否存在,不存在则创建
|
||
if (!string.IsNullOrWhiteSpace(item.InvCode))
|
||
{
|
||
var existingMaterial = existingMaterials.FirstOrDefault(m => m.MaterialCode == item.InvCode);
|
||
if (existingMaterial == null)
|
||
{
|
||
// 检查是否已在待新增列表中
|
||
if (!newMaterials.Any(m => m.MaterialCode == item.InvCode))
|
||
{
|
||
// 物料不存在,创建新物料
|
||
var newMaterial = new Material
|
||
{
|
||
MaterialCode = item.InvCode,
|
||
MaterialName = item.InvName,
|
||
MaterialSpec = item.InvStd,
|
||
MaterialType = item.InvDefine1, // 图号
|
||
Qty = 0,
|
||
CreateTime = DateTime.Now,
|
||
CreateBy = Wtm.LoginUserInfo?.ITCode ?? "系统"
|
||
};
|
||
newMaterials.Add(newMaterial);
|
||
}
|
||
}
|
||
}
|
||
|
||
var detail = new ReceiptDetail
|
||
{
|
||
HeaderId = existingHeader.ID,
|
||
SourceCode = existingHeader.SourceCode,
|
||
MaterialCode = item.InvCode,
|
||
MaterialType = item.InvDefine1, // 图号
|
||
MaterialName = item.InvName,
|
||
Lot = item.MoDetailId, // 明细id
|
||
MaterialSpec = item.InvStd,
|
||
Qty = item.Qty,
|
||
CompletedQty = 0,
|
||
Status = ReceiptStatusEnum.新建,
|
||
Batch = item.Version,
|
||
CreateTime = DateTime.Now,
|
||
CreateBy = Wtm.LoginUserInfo?.ITCode ?? "系统",
|
||
UpdateVersion = 1
|
||
};
|
||
|
||
newDetails.Add(detail);
|
||
|
||
// 更新工序路线
|
||
if (item.ProcessRoute != null && item.ProcessRoute.Count > 0)
|
||
{
|
||
await SaveProcessRoute(unitWork, item.InvCode, item.ProcessRoute);
|
||
}
|
||
}
|
||
}
|
||
|
||
// 批量新增物料
|
||
if (newMaterials.Count > 0)
|
||
{
|
||
unitWork.BatchAdd(newMaterials);
|
||
}
|
||
|
||
unitWork.BatchAdd(newDetails);
|
||
|
||
Wtm.DoLog("更新订单数据", ActionLogTypesEnum.Normal, "ProcessOrderData",
|
||
request: $"订单号:{moCode}",
|
||
result: $"成功更新入库单 {existingHeader.Code},新增明细 {newDetails.Count} 条");
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 保存工序路线到 MaterialProcess 表
|
||
/// </summary>
|
||
private async Task SaveProcessRoute(IUnitWork unitWork, string materialCode, List<ProcessRouteItem> processRoute)
|
||
{
|
||
// 查询已存在的工序路线
|
||
var existingProcesses = unitWork.Find<MaterialProcess>(p => p.MaterialCode == materialCode).ToList();
|
||
|
||
// 待新增的工序
|
||
var processesToAdd = new List<MaterialProcess>();
|
||
// 待更新的工序
|
||
var processesToUpdate = new List<MaterialProcess>();
|
||
|
||
var processConfigs = unitWork.Find<ProcessConfig>(x => x.Code != "").ToList();
|
||
// 遍历新的工序路线
|
||
foreach (var route in processRoute)
|
||
{
|
||
// 检查工艺是否存在于ProcessConfig中,不存在则添加
|
||
var processConfig = processConfigs.Any(p => p.Code == route.ProcessCode);
|
||
if (!processConfig)
|
||
{
|
||
unitWork.Add(new ProcessConfig
|
||
{
|
||
Code = route.ProcessCode,
|
||
Name = route.ProcessName,
|
||
Zone = "Zone2",
|
||
Port = "EnterExit_P2005"
|
||
});
|
||
}
|
||
|
||
// 查找是否存在相同工序名称的记录
|
||
var existing = existingProcesses.FirstOrDefault(p =>
|
||
p.ProcessName == route.ProcessName);
|
||
|
||
if (existing != null)
|
||
{
|
||
// 已存在,检查是否需要更新
|
||
bool needUpdate = false;
|
||
|
||
if (existing.ProcessCode != route.ProcessCode || existing.ProcessOrder != route.Sort)
|
||
{
|
||
existing.ProcessCode = route.ProcessCode;
|
||
existing.ProcessOrder = route.Sort;
|
||
needUpdate = true;
|
||
}
|
||
|
||
if (!existing.IsActive)
|
||
{
|
||
existing.IsActive = true;
|
||
needUpdate = true;
|
||
}
|
||
|
||
if (needUpdate)
|
||
{
|
||
existing.UpdateTime = DateTime.Now;
|
||
existing.UpdateBy = Wtm.LoginUserInfo?.ITCode ?? "系统";
|
||
processesToUpdate.Add(existing);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
// 不存在,新增
|
||
var newProcess = new MaterialProcess
|
||
{
|
||
MaterialCode = materialCode,
|
||
ProcessCode = route.ProcessCode,
|
||
ProcessName = route.ProcessName,
|
||
ProcessOrder = route.Sort,
|
||
Weight = 0,
|
||
IsActive = true,
|
||
CreateTime = DateTime.Now,
|
||
CreateBy = Wtm.LoginUserInfo?.ITCode ?? "系统"
|
||
};
|
||
processesToAdd.Add(newProcess);
|
||
}
|
||
}
|
||
|
||
// 查找需要删除的工序(旧的多余的)
|
||
var processesToDelete = existingProcesses.Where(existing =>
|
||
!processRoute.Any(route =>
|
||
route.ProcessName == existing.ProcessName)
|
||
).ToList();
|
||
|
||
// 执行数据库操作
|
||
if (processesToAdd.Count > 0)
|
||
{
|
||
unitWork.BatchAdd(processesToAdd);
|
||
}
|
||
|
||
if (processesToUpdate.Count > 0)
|
||
{
|
||
unitWork.BatchUpdate(processesToUpdate);
|
||
}
|
||
|
||
if (processesToDelete.Count > 0)
|
||
{
|
||
unitWork.BatchDelete(processesToDelete);
|
||
}
|
||
|
||
await Task.CompletedTask;
|
||
}
|
||
}
|
||
}
|