279 lines
11 KiB
C#
279 lines
11 KiB
C#
using Microsoft.AspNetCore.Http;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using System;
|
||
using WalkingTec.Mvvm.Core;
|
||
using WalkingTec.Mvvm.Mvc;
|
||
using WalkingTec.Mvvm.Core.Extensions;
|
||
using HHWMS.Web;
|
||
using HHWMS.Web.Models;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using System.Linq;
|
||
using Newtonsoft.Json;
|
||
using System.Collections.Generic;
|
||
using System.Reflection.Emit;
|
||
|
||
namespace HHWMS.Web.Controllers
|
||
{
|
||
[Area("receipt")]
|
||
[ActionDescription("盘料操作")]
|
||
public partial class ReceiptInController : BaseController
|
||
{
|
||
private readonly IUnitWork _unitWork;
|
||
public ReceiptInController(IUnitWork unitwork)
|
||
{
|
||
_unitWork = unitwork;
|
||
}
|
||
#region Search
|
||
[ActionDescription("Sys.Search")]
|
||
public ActionResult Index()
|
||
{
|
||
return PartialView("Index");
|
||
}
|
||
|
||
[HttpPost]
|
||
public ActionResult<dynamic> SelectReceiptCode()
|
||
{
|
||
var data = _unitWork.Find<ReceiptHeader>(x => x.FirstStatus < ReceiptStatusEnum.收完)
|
||
.Select(x => new { label = x.Code, value = x.Code });
|
||
return JsonMore(data);
|
||
}
|
||
|
||
[HttpPost]
|
||
public ActionResult<dynamic> SelectReceipt(string Code)
|
||
{
|
||
var data = _unitWork.Find<ReceiptHeader>(x => x.Code == Code)
|
||
.Include(x => x.ReceiptDetails.Where(a => a.Status < ReceiptStatusEnum.收完))
|
||
.FirstOrDefault();
|
||
if(data != null)
|
||
{
|
||
var result = data.ReceiptDetails.Select(x => new { name = x.UniqueCode });
|
||
return JsonMore(result);
|
||
}
|
||
else
|
||
{
|
||
return JsonMore(null);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 盘料入库扫描料箱号
|
||
/// </summary>
|
||
/// <param name="boxCode"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
public ActionResult ScanBox(string boxCode)
|
||
{
|
||
// 判断任务重复
|
||
if (_unitWork.IsExit<TaskHeader>(x => x.FromLocation == boxCode || x.ToLocation == boxCode))
|
||
{
|
||
return JsonMore(null, 500, $"库内已存在{boxCode}的任务,请勿重复操作!");
|
||
}
|
||
// 判断箱号是否合理
|
||
var box = _unitWork.FindSingle<Location>(x => x.Code.Equals(boxCode));
|
||
if (box != null)
|
||
{
|
||
if (string.IsNullOrEmpty(box.ContainerCode))
|
||
{
|
||
box.ContainerCode = boxCode;
|
||
_unitWork.Update(box);
|
||
}
|
||
//else
|
||
//{
|
||
// return JsonMore(null, 500, $"{boxCode}已经被其他任务占用,无法入库!");
|
||
//}
|
||
return JsonMore(null);
|
||
}
|
||
else
|
||
{
|
||
return JsonMore(null, 500, $"{boxCode}不正确!");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 入库盘料绑定并创建入库任务
|
||
/// </summary>
|
||
/// <param name="data"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
public ActionResult BindingData(List<Binding> data)
|
||
{
|
||
// 判断库内是否有充足库位执行入库 todo
|
||
/*var emptyLoc = _unitWork.Find<Location>(x => x.Type == LocationTypeEnum.盘料库位)
|
||
.ToList()
|
||
.GroupBy(x => Convert.ToInt32(x.Length));
|
||
var sizeEmptyNum = new List<dynamic>();
|
||
foreach (var group in emptyLoc)
|
||
{
|
||
var num = group.Sum(x => (x.Length - x.Width));
|
||
sizeEmptyNum.Add(new
|
||
{
|
||
size = group.Key,
|
||
num = Convert.ToInt32(num)
|
||
});
|
||
}
|
||
var sizeList = data.GroupBy(x => x.Size).ToList();
|
||
foreach(var item in sizeList)
|
||
{
|
||
var size = 23;
|
||
switch (item.Key)
|
||
{
|
||
case 8:
|
||
size = 23;
|
||
break;
|
||
case 10:
|
||
size = 19;
|
||
break;
|
||
case 16:
|
||
size = 16;
|
||
break;
|
||
case 24:
|
||
size = 13;
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
var num = sizeEmptyNum.First(x => x.size == item.Key).num
|
||
if(item.Count() > num)
|
||
return JsonMore(null, 500, $"尺寸{item.Key}的盘料盒无法满足入库需求,剩余数量{num}");
|
||
|
||
}*/
|
||
|
||
using (var tran = Wtm.DC.Database.BeginTransaction())
|
||
{
|
||
try
|
||
{
|
||
// 先捡货,后入库
|
||
if (_unitWork.IsExit<Inventory>(x => !string.IsNullOrEmpty(x.SourceCode)
|
||
&& x.LocationCode == data.First().BoxCode))
|
||
{
|
||
return JsonMore(null, 500, "周转箱存在未捡货任务,请捡货完成后再执行入库操作!");
|
||
}
|
||
/*
|
||
//单据明细验证
|
||
var errDetails = _unitWork.Find<ReceiptDetail>(x => data.Select(x => x.UniqueCode).ToList()
|
||
.Contains(x.UniqueCode)
|
||
&& x.Status != ReceiptStatusEnum.新建).ToList();
|
||
if (errDetails.Any())
|
||
{
|
||
var msg = "以下明细不是新建状态:\n";
|
||
foreach (var errDetail in errDetails)
|
||
{
|
||
msg += $"{errDetail.UniqueCode}\n";
|
||
}
|
||
return JsonMore(null, 500, msg);
|
||
}*/
|
||
|
||
var receiptHeaderList = _unitWork.Find<ReceiptHeader>(x =>
|
||
data.Select(x => x.ReceiptCode).ToList().Contains(x.Code))
|
||
.Include(x => x.ReceiptDetails).ToList();
|
||
// 查找明细,修改对应状态,并且 绑定物料周转箱
|
||
foreach (Binding item in data)
|
||
{
|
||
// 更新入库单明细状态
|
||
var header = receiptHeaderList.First(x => x.Code.Equals(item.ReceiptCode));
|
||
var receipt = header.ReceiptDetails.FirstOrDefault(x =>
|
||
x.UniqueCode.Equals(item.UniqueCode)
|
||
&& x.Status < ReceiptStatusEnum.收完);
|
||
//var receipt = _unitWork.FindSingle<ReceiptDetail>(x => x.UniqueCode == item.UniqueCode);
|
||
if (receipt != null)
|
||
{
|
||
receipt.CompletedQty = receipt.Qty;
|
||
receipt.Status = ReceiptStatusEnum.收完;
|
||
_unitWork.Update(receipt);
|
||
if (!header.ReceiptDetails.Any(x => x.Status < ReceiptStatusEnum.收完))
|
||
{
|
||
header.FirstStatus = ReceiptStatusEnum.收完;
|
||
_unitWork.Update(header);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
throw new Exception($"入库单{item.ReceiptCode}不存在{item.UniqueCode}的明细!");
|
||
}
|
||
|
||
// 新建库存
|
||
var inventory = new Inventory
|
||
{
|
||
ZoneCode = ZoneCodeEnum.盘料库,
|
||
UniqueCode = item.UniqueCode,
|
||
Flag = item.Size,
|
||
MaterialCode = receipt.MaterialCode,
|
||
MaterialType = receipt.MaterialType,
|
||
MaterialName = receipt.MaterialName,
|
||
MaterialSpec = receipt.MaterialSpec,
|
||
Batch = receipt.Batch,
|
||
MaterialUnit = receipt.MaterialUnit,
|
||
SupplierCode = receipt.SupplierCode,
|
||
Qty = receipt.Qty,
|
||
Lot = item.Index.ToString(),
|
||
ContainerCode = item.BoxCode,
|
||
Status = "0",
|
||
TaskStatus = InventoryTaskStatus.任务中
|
||
};
|
||
_unitWork.Add(inventory);
|
||
}
|
||
|
||
var boxcode = data.First().BoxCode;
|
||
//根据提交的uniquecode,创建入库任务
|
||
var invList = _unitWork.Find<Inventory>(x =>
|
||
data.Select(y => y.UniqueCode).ToList().Contains(x.UniqueCode)
|
||
&& x.TaskStatus == InventoryTaskStatus.任务中
|
||
&& x.ContainerCode.Equals(boxcode))
|
||
.ToList();
|
||
if (invList.Count == 0)
|
||
{
|
||
return JsonMore(null, 500, $"站台{boxcode}没有绑定数据,请正确操作!");
|
||
}
|
||
|
||
var task = new TaskHeader
|
||
{
|
||
TaskNo = _unitWork.GetSerialNumber("pin", 4),
|
||
TaskType = TaskTypeEnum.盘料入库,
|
||
Status = TaskStatusEnum.待下发,
|
||
ZoneCode = ZoneCodeEnum.盘料库,
|
||
FromLocation = boxcode,
|
||
TaskDetails = new List<TaskDetail>(),
|
||
};
|
||
foreach (var item in invList)
|
||
{
|
||
var detail = new TaskDetail
|
||
{
|
||
UniqueCode = item.UniqueCode,
|
||
FromLocation = item.Lot,
|
||
Status = TaskStatusEnum.新建,
|
||
CreateTime = DateTime.Now
|
||
};
|
||
task.TaskDetails.Add(detail);
|
||
}
|
||
|
||
_unitWork.Add(task);
|
||
|
||
tran.Commit();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
tran.Rollback();
|
||
return JsonMore(null, 500, ex.Message);
|
||
}
|
||
|
||
}
|
||
|
||
return JsonMore(null);
|
||
}
|
||
|
||
#endregion
|
||
|
||
}
|
||
|
||
public class Binding
|
||
{
|
||
public int ID { get; set; }
|
||
public string ReceiptCode { get; set; }
|
||
public string UniqueCode { get; set; }
|
||
public string BoxCode { get; set; }
|
||
public int Index { get; set; }
|
||
public int Size { get; set; }
|
||
public int BindQty { get; set; }
|
||
}
|
||
}
|