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;
namespace Tiger.Business.WMS.Transaction
{
///
/// 半成品入库打印标签事务
///
public class In_SemiProd : WMSTransactionBase, IIn_SemiProd
{
private readonly IMES_U9C _IMES_U9C = DI.Resolve();
public IIn_SemiProd Init(string id, string userCode, string apiHost, string orgCode)
{
TransID = id;
UserCode = userCode;
ApiHost = apiHost;
OrgCode = orgCode;
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 Vitem { get; set; } = new();
public int pageSize { get; set; }
public RcvRptDocCreateInput RcvRptInput { get; set; } = new RcvRptDocCreateInput();
public List ProdInBatchs { get; set; } = new List();
public List ErpProdInBths { get; set; } = new List();
public List ErpProdIns { get; set; } = new List();
///
/// 扫描列表
///
public List InStoreScanInfos { get; set; } = new List();
public List ErpProdInSns { get; set; } = new List();
public InStoreInfos inStoreInfos { get; set; }
#endregion
#region Functions
///
/// 扫描入口
///
public async Task> Scan(BaseInput input)
{
var action = new ApiAction(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().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;
}
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 = input.Data.ToInt32(),
IS_HANDLED = "N" //Y-已生成,N-未生成,F-失败
},
InStoreScanInfo = new()
{
SN = "",
SALES_ORDER = wo.SALES_ORDER,
WORK_ORDER = wo.ORDER_NO,
BATCH_NO = "",
SCAN_QTY = input.Data.ToInt32()
}
};
//要存入的批次入库单数据
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.LocaleMsg = Biz.L("WMS.InSemiProd.ScanItem.ScanSuccessed"); //$"工单条码[{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 async Task> ScanItem(BaseInput input)
{
var action = new ApiAction(new ScanOutput());
try
{
}
catch (Exception ex)
{
action.CatchExceptionWithLog(ex, Biz.L("半成品入库失败"));
}
return action;
}
///
/// 调用U9C接口生成入库单
///
///
///
public async Task> GenerateInStoreOrder()
{
var action = new ApiAction();
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.ToInt32(),
CompleteQty = item.SCAN_QTY.ToInt32(),
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;
}
#endregion
public override bool Close(bool needSaveHistoryLog = false)
{
needSaveHistoryLog = true;
//保存操作日志
this.IsFinished = true;
return IsFinished ? base.Close(needSaveHistoryLog) : IsFinished;
}
}//endClass
}