using Rhea.Common;
|
using Microsoft.AspNetCore.Http;
|
using SqlSugar;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Linq.Expressions;
|
using System.Text;
|
using System.Threading;
|
using System.Threading.Tasks;
|
using Tiger.Model;
|
using Apache.NMS;
|
using System.Drawing.Drawing2D;
|
using Tiger.Model.Sharetronic.Shelf;
|
using Tiger.IBusiness;
|
using Tiger.Model.MES.Yada;
|
using Apache.NMS.ActiveMQ.Commands;
|
using System.Diagnostics;
|
using static Microsoft.CodeAnalysis.CSharp.SyntaxTokenParser;
|
using Tiger.Model.Entitys.MES.U9C;
|
using Tiger.Model.Entitys.MES.BizPrintInstoreDoc;
|
|
namespace Tiger.Business.WMS.Transaction
|
{
|
/// <summary>
|
/// 半成品入库打印标签事务
|
/// </summary>
|
public class In_SemiProd : WMSTransactionBase, IIn_SemiProd
|
{
|
private readonly IMES_U9C _IMES_U9C = DI.Resolve<IMES_U9C>();
|
public IIn_SemiProd Init(string id, string apiHost, InstoreDocInput input)
|
{
|
TransID = id;
|
UserCode = input.UserCode;
|
ApiHost = apiHost;
|
OrgCode = input.OrgCode;
|
pageSize = input.pageSize;
|
_GetScannedList();
|
Logger.Console.Info($"Start {this.GetType().Name} Transaction[ID: {TransID}]");
|
return this;
|
}
|
|
#region Propertys & Variables
|
public string UserCode { get; set; }
|
public long UserId { get; set; }
|
public string OrgCode { get; set; }
|
public List<V_WMS_ITEM> Vitem { get; set; } = new();
|
public int pageSize { get; set; }
|
public RcvRptDocCreateInput RcvRptInput { get; set; } = new RcvRptDocCreateInput();
|
public List<ProdInBatch> ProdInBatchs { get; set; } = new List<ProdInBatch>();
|
public List<BIZ_ERP_PROD_IN_BTH> ErpProdInBths { get; set; } = new List<BIZ_ERP_PROD_IN_BTH>();
|
public List<BIZ_ERP_PROD_IN> ErpProdIns { get; set; } = new List<BIZ_ERP_PROD_IN>();
|
/// <summary>
|
/// 扫描列表
|
/// </summary>
|
public List<InStoreScanInfo> InStoreScanInfos { get; set; } = new List<InStoreScanInfo>();
|
public List<BIZ_ERP_PROD_IN_SN> ErpProdInSns { get; set; } = new List<BIZ_ERP_PROD_IN_SN>();
|
public InStoreInfos inStoreInfos { get; set; }
|
|
#endregion
|
|
#region Functions
|
/// <summary>
|
/// 扫描入口
|
/// </summary>
|
public async Task<ApiAction<ScanOutput>> Submit(BaseInput input)
|
{
|
var action = new ApiAction<ScanOutput>(new ScanOutput());
|
try
|
{
|
if (input.SN.IsNullOrEmpty())
|
{
|
action.IsSuccessed = false;
|
action.LocaleMsg = Biz.L("WMS.InSemiProd.ScanItem.SnEmptyFailure");
|
return SetOutPutMqttMsg(action, input.Locale);
|
}
|
var wo = await Biz.Db.Queryable<BIZ_MES_WO>().Where(q => q.ORDER_NO == input.SN).IncludesAllFirstLayer().IncludesAllSecondLayer(q => q._ItemInfos).FirstAsync();
|
if (wo == null)
|
{
|
action.IsSuccessed = false;
|
action.LocaleMsg = Biz.L("WMS.InSemiProd.ScanItem.WoEmptyFailure", input.SN); // $"工单[{input.SN}]不存在"
|
return action;
|
}
|
|
var semiTemplateQty = (input.Data ?? "").JsonToObject<SemiTemplateQty>() ?? new SemiTemplateQty();
|
|
InStoreInfo Info = new()
|
{
|
ErpProdInBth = new()
|
{
|
WORK_ORDER = wo.ORDER_NO,
|
BATCH_NO = "",
|
SALES_ORDER = wo.SALES_ORDER,
|
ITEM_CODE = wo.ITEM_CODE,
|
ITEM_NAME = wo.ItemInfo?.ITEM_DESC,
|
SapCode = wo.ItemInfo?.ExtInfo?.SapCode,
|
SCAN_QTY = semiTemplateQty.PackQty,
|
CompleteQty = semiTemplateQty.CompleteQty,
|
IS_HANDLED = "N" //Y-已生成,N-未生成,F-失败
|
},
|
InStoreScanInfo = new()
|
{
|
SN = "",
|
SALES_ORDER = wo.SALES_ORDER,
|
WORK_ORDER = wo.ORDER_NO,
|
BATCH_NO = "",
|
SCAN_QTY = semiTemplateQty.PackQty.ToDouble()
|
}
|
};
|
|
//要存入的批次入库单数据
|
int n = InStoreScanInfos.Where(q => q.WORK_ORDER == Info.ErpProdInBth.WORK_ORDER).Count();
|
if (!ErpProdInBths.Any(q => q.WORK_ORDER == Info.ErpProdInBth.WORK_ORDER))
|
{
|
Info.ErpProdInBth.AUTH_ORG = OrgCode;
|
Info.ErpProdInBth.BATCH_NO = "";
|
Info.ErpProdInBth.CREATE_USER = UserCode;
|
Info.ErpProdInBth.CREATE_TIME = DateTime.Now;
|
Info.ErpProdInBth.SCANED_DATE = DateTime.Now;
|
Info.ErpProdInBth.HANDLED_DATE = DateTime.Now;
|
Info.ErpProdInBth.IS_SCANED = "Y";
|
Info.ErpProdInBth.ORDER_NO = UserCode;
|
Info.ErpProdInBth.IS_HANDLED = "N";
|
Info.ErpProdInBth.CartonQty = n == 0 ? 1 : n;
|
BIZ_ERP_PROD_IN prodIn = new()
|
{
|
AUTH_ORG = OrgCode,
|
BILLCODE = UserCode,
|
BILLDATE = DateTime.Now,
|
STATUS = 0,
|
HANDLED = -1,
|
WAREHOUSECODE = "",
|
SOURCECODE = Info.ErpProdInBth.WORK_ORDER,
|
HANDLED_DATE = DateTime.Now,
|
};
|
ErpProdInBths.Add(Info.ErpProdInBth);
|
ErpProdIns.Add(prodIn);
|
}
|
//else
|
//{
|
// if (!InStoreScanInfos.Any(q => q.WORK_ORDER == Info?.ErpProdInBth.WORK_ORDER && q.SN == CurSN))
|
// {
|
// var erpbth = ErpProdInBths.Where(q => q.WORK_ORDER == Info.ErpProdInBth.WORK_ORDER).FirstOrDefault();
|
// if (!erpbth.IsNullOrEmpty())
|
// {
|
// erpbth.SCAN_QTY += Info.InStoreScanInfo.SCAN_QTY.ToDouble();
|
// erpbth.CartonQty = n + 1;
|
// }
|
// }
|
//}
|
|
//生成打印实体
|
if (RcvRptInput.PrintJsons.Count == 0)
|
{
|
RcvRptInput.PrintJsons.Add(new()
|
{
|
ID = Guid.NewGuid().ToString("N"),
|
Items = new() {
|
Info.ErpProdInBth
|
}
|
});
|
}
|
else
|
{
|
//拼分页
|
bool isAdded = false;
|
foreach (var item in RcvRptInput.PrintJsons)
|
{
|
if (!item.Items.Any(q => q.WORK_ORDER == Info.ErpProdInBth.WORK_ORDER))
|
{
|
if (item.Items.Count < pageSize)
|
{
|
item.Items.Add(Info.ErpProdInBth);
|
isAdded = true;
|
}
|
}
|
else
|
{
|
isAdded = true;
|
}
|
}
|
if (!isAdded)
|
{
|
RcvRptInput.PrintJsons.Add(new()
|
{
|
ID = Guid.NewGuid().ToString("N"),
|
Items = new() {
|
Info.ErpProdInBth
|
}
|
});
|
}
|
}
|
|
//保存数据库
|
var db = Business.Biz.Db;
|
var dbTran = db.UseTran(() =>
|
{
|
var x = db.Storageable(ErpProdInBths, $"InStoreOrderNo_{UserCode}")
|
.WhereColumns(t => new { t.ORDER_NO, t.WORK_ORDER, t.GHOST_ROW })
|
.ToStorage();
|
x.AsInsertable.ExecuteCommand();
|
x.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
|
|
//var s = db.Storageable(ErpProdInSns, $"InStoreOrderNo_{UserCode}")
|
// .WhereColumns(t => new { t.CARTONNO, t.SN, t.GHOST_ROW })
|
// .ToStorage();
|
//s.AsInsertable.ExecuteCommand();
|
//s.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
|
});
|
if (!dbTran.IsSuccess)
|
{
|
Logger.Default.Fatal(dbTran.ErrorException, "Database transaction save exception");
|
this.Close(!dbTran.IsSuccess);
|
throw dbTran.ErrorException;
|
}
|
RcvRptInput.RcvRptDocBases = new();
|
RcvRptInput.labels = new();
|
RcvRptInput.ErpProdInBths = ErpProdInBths;
|
RcvRptInput.userId = UserCode;
|
inStoreInfos = new()
|
{
|
ErpProdInBths = ErpProdInBths,
|
InStoreScanInfos = InStoreScanInfos,
|
};
|
|
action.Data.Data = inStoreInfos;
|
action.Data.Command = "";
|
action.LocaleMsg = Biz.L("WMS.InSemiProd.ScanItem.ScanSuccessed", input.SN); //$"工单条码[{input.SN}]扫描成功,请填入工单数量后点提交"
|
}
|
catch (Exception ex)
|
{
|
//action.CatchExceptionWithLog(ex, $"扫描[{input.SN}]异常");
|
action.CatchExceptionWithLog(ex, Biz.L("WMS.RePrint.Scan.ScanException", input.SN));
|
}
|
return SetOutPutMqttMsg(action, input.Locale);
|
}
|
|
public ApiAction<InStoreInfos> GetInStoreInfos()
|
{
|
return new() { Data = inStoreInfos };
|
}
|
|
|
/// <summary>
|
/// 查询已扫描列表
|
/// </summary>
|
/// <returns></returns>
|
public async Task<ApiAction<InStoreInfos>> GetScannedList()
|
{
|
var action = new ApiAction<InStoreInfos>();
|
try
|
{
|
_GetScannedList();
|
action.Data = inStoreInfos;
|
}
|
catch (Exception ex)
|
{
|
action.CatchExceptionWithLog(ex, $"查询已扫描列表异常");
|
}
|
return action;
|
}
|
|
/// <summary>
|
/// 从数据库读回操作者原本操作的记录
|
/// </summary>
|
/// <returns></returns>
|
private RcvRptDocCreateInput _GetScannedList()
|
{
|
RcvRptInput.RcvRptDocBases = new();
|
RcvRptInput.ErpProdInBths = new();
|
RcvRptInput.PrintJsons = new();
|
RcvRptInput.labels = new();
|
RcvRptInput.userId = UserCode;
|
ErpProdInSns.Clear();
|
ErpProdIns.Clear();
|
ErpProdInBths.Clear();
|
InStoreScanInfos.Clear();
|
RcvRptInput.ErpProdInBths = Biz.Db.Queryable<BIZ_ERP_PROD_IN_BTH>()
|
.Where(q => q.BATCH_NO == SqlFunc.Subqueryable<BIZ_ERP_PROD_IN_BTH>().Where(s => s.ORDER_NO == UserCode && s.GHOST_ROW == false).Select(s => s.BATCH_NO) && q.IS_HANDLED == "N")
|
.IncludesAllFirstLayer().ToList();
|
foreach (var item in RcvRptInput.ErpProdInBths)
|
{
|
var values = item.ProdInSns.GroupBy(x => x.CARTONNO)
|
.Select(g => new InStoreScanInfo() { SN = g.Key, SALES_ORDER = item.SALES_ORDER, BATCH_NO = item.BATCH_NO, WORK_ORDER = item.WORK_ORDER, SCAN_QTY = g.Sum(x => x.Qty).ToDouble() });
|
foreach (var t in values)
|
{
|
InStoreScanInfos.Add(t);
|
}
|
item.CartonQty = values.Count();
|
|
ErpProdIns.Add(new()
|
{
|
AUTH_ORG = OrgCode,
|
BILLCODE = UserCode,
|
BILLDATE = DateTime.Now,
|
STATUS = 0,
|
HANDLED = -1,
|
WAREHOUSECODE = "",
|
SOURCECODE = item.WORK_ORDER,
|
HANDLED_DATE = DateTime.Now,
|
});
|
ErpProdInSns.AddRange(item.ProdInSns);
|
|
//生成打印实体
|
if (RcvRptInput.PrintJsons.Count == 0)
|
{
|
RcvRptInput.PrintJsons.Add(new()
|
{
|
ID = Guid.NewGuid().ToString("N"),
|
Items = new() {
|
item
|
}
|
});
|
}
|
else
|
{
|
//拼分页
|
bool isAdded = false;
|
foreach (var p in RcvRptInput.PrintJsons)
|
{
|
if (!p.Items.Any(q => q.WORK_ORDER == item.WORK_ORDER))
|
{
|
if (p.Items.Count < pageSize)
|
{
|
p.Items.Add(item);
|
isAdded = true;
|
}
|
}
|
else
|
{
|
isAdded = true;
|
}
|
}
|
if (!isAdded)
|
{
|
RcvRptInput.PrintJsons.Add(new()
|
{
|
ID = Guid.NewGuid().ToString("N"),
|
Items = new() {
|
item
|
}
|
});
|
}
|
}
|
}
|
ErpProdInBths = RcvRptInput.ErpProdInBths;
|
|
inStoreInfos = new()
|
{
|
ErpProdInBths = ErpProdInBths,
|
InStoreScanInfos = InStoreScanInfos,
|
};
|
return RcvRptInput;
|
}
|
|
/// <summary>
|
/// 调用U9C接口生成入库单
|
/// </summary>
|
/// <param name="code"></param>
|
/// <returns></returns>
|
public async Task<ApiAction<RcvRptDocCreateInput>> GenerateInStoreOrder()
|
{
|
var action = new ApiAction<RcvRptDocCreateInput>();
|
try
|
{
|
//如果扫描记录为0
|
if (!RcvRptInput.ErpProdInBths.Any(q => q.IS_HANDLED == "N"))
|
{
|
action.IsSuccessed = false;
|
action.LocaleMsg = Biz.L($"没有要入库的产品数据,请扫描工单重新生成入库单据!");
|
return action;
|
}
|
if (RcvRptInput.RcvRptDocBases.IsNullOrEmpty() || RcvRptInput.RcvRptDocBases.Count == 0)
|
{
|
foreach (var item in RcvRptInput.ErpProdInBths)
|
{
|
var DocBase = new RcvRptDocBase()
|
{
|
WorkOrder = item.WORK_ORDER,
|
ItemCode = item.ITEM_CODE,
|
WhCode = "10105",
|
PkgQty = item.SCAN_QTY,
|
CompleteQty = item.CompleteQty,
|
OutputType = 0,
|
StorageType = 4,
|
DocState = 1,
|
Status = "N"
|
};
|
if (!RcvRptInput.RcvRptDocBases.Any(q => q.WorkOrder == item.WORK_ORDER))
|
{
|
RcvRptInput.RcvRptDocBases.Add(DocBase);
|
}
|
}
|
}
|
else
|
{
|
action.IsSuccessed = false;
|
action.LocaleMsg = Biz.L($"有存在未提交的入库单,不能生成入库单");
|
return action;
|
}
|
action = await _IMES_U9C.RcvRptDocCreate(RcvRptInput);
|
if (action.IsSuccessed)
|
{
|
foreach (var sn in ErpProdInSns)
|
{
|
var d = action.Data.ErpProdInBths.Where(q => q.WORK_ORDER == sn.SOURCECODE).FirstOrDefault();
|
if (!d.IsNullOrEmpty())
|
{
|
sn.BUSINESSCODE = d.ORDER_NO;
|
sn.UPDATE_USER = UserCode;
|
sn.UPDATE_TIME = DateTime.Now;
|
}
|
}
|
foreach (var p in ErpProdIns)
|
{
|
var d = action.Data.ErpProdInBths.Where(q => q.WORK_ORDER == p.SOURCECODE).FirstOrDefault();
|
if (!d.IsNullOrEmpty())
|
{
|
p.BILLCODE = d.ORDER_NO;
|
p.UPDATE_USER = UserCode;
|
p.UPDATE_TIME = DateTime.Now;
|
p.HANDLED = 0;
|
}
|
}
|
//保存数据库
|
var db = Business.Biz.Db;
|
var dbTran = db.UseTran(() =>
|
{
|
db.Updateable(ErpProdInSns, $"InStoreOrderNo_{UserCode}").UpdateColumns(q => new { q.UPDATE_TIME, q.UPDATE_USER, q.BUSINESSCODE }).ExecuteCommand();
|
|
var x = db.Storageable(ErpProdIns, $"InStoreOrderNo_{UserCode}")
|
.WhereColumns(t => new { t.BILLCODE, t.SOURCECODE, t.GHOST_ROW })
|
.ToStorage();
|
x.AsInsertable.ExecuteCommand();
|
x.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
|
});
|
if (!dbTran.IsSuccess)
|
{
|
Logger.Default.Fatal(dbTran.ErrorException, "Database transaction save exception");
|
this.Close(!dbTran.IsSuccess);
|
throw dbTran.ErrorException;
|
}
|
}
|
|
}
|
catch (Exception ex)
|
{
|
action.CatchExceptionWithLog(ex, $"调用U9C接口生成入库单异常");
|
}
|
return action;
|
}
|
|
/// <summary>
|
/// 清除
|
/// </summary>
|
/// <returns></returns>
|
public async Task<ApiAction> Clear()
|
{
|
var action = new ApiAction();
|
try
|
{
|
RcvRptInput = new();
|
ProdInBatchs = new() { };
|
ErpProdInBths = new() { };
|
ErpProdIns = new();
|
/// <summary>
|
/// 扫描列表
|
/// </summary>
|
InStoreScanInfos = new() { };
|
ErpProdInSns = new();
|
inStoreInfos = new() { };
|
|
//保存数据库
|
var db = Business.Biz.Db;
|
var dbTran = db.UseTran(() =>
|
{
|
db.Deleteable<BIZ_ERP_PROD_IN_BTH>().Where(x => x.ORDER_NO == UserCode && x.IS_HANDLED == "N").ExecuteCommand();
|
db.Deleteable<BIZ_ERP_PROD_IN_SN>().Where(x => x.BUSINESSCODE == UserCode).ExecuteCommand();
|
});
|
if (!dbTran.IsSuccess)
|
{
|
Logger.Default.Fatal(dbTran.ErrorException, "Database transaction save exception");
|
this.Close(!dbTran.IsSuccess);
|
throw dbTran.ErrorException;
|
}
|
}
|
catch (Exception ex)
|
{
|
action.CatchExceptionWithLog(ex, $"查询已扫描列表异常");
|
}
|
return action;
|
}
|
|
#endregion
|
|
public override bool Close(bool needSaveHistoryLog = false)
|
{
|
needSaveHistoryLog = true;
|
//保存操作日志
|
|
this.IsFinished = true;
|
return IsFinished ? base.Close(needSaveHistoryLog) : IsFinished;
|
}
|
|
}//endClass
|
}
|