using Rhea.Common;
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 Tiger.IBusiness;
using Tiger.Model.Sharetronic.Shelf;
using Tiger.Business.WMS.Sharetronic.Shelf;
//using Microsoft.IdentityModel.Tokens;
using Tiger.Model.MES.Yada;
using static IronPython.Modules.PythonThread;
using Tiger.Model.Entitys.MES.Position;
namespace Tiger.Business.WMS.Transaction
{
///
/// 标准上架事务
///
public class In_Default : WmsTask, IIn_Default
{
public IIn_Default Init(string id, string userCode, string apiHost, string orgCode)
{
base.Init(id, userCode, apiHost, orgCode);
Logger.Console.Info($"Start {this.GetType().Name} Transaction[ID: {TransID}]");
return this;
}
#region Propertys & Variables
#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("条码不能为空");
action.LocaleMsg = Biz.L("WMS.Default.ScanItem.SnEmptyFailure");
return SetOutPutMqttMsg(action, input.Locale);
}
//判断扫描的是否货架
var whUnit = await MainDB.Queryable().Where(t => (t.SHELF_CODE.ToUpper() == input.SN || t.LOCATION_CODE.ToUpper() == input.SN) && t.AUTH_ORG == OrgCode).IncludesAllFirstLayer().FirstAsync();
//if (string.IsNullOrEmpty(CurScanShelf?.ShelfCode))
if (!whUnit.IsNullOrEmpty() || string.IsNullOrEmpty(CurScanShelf?.ShelfCode))
{
action = await ScanShelf(input.SN, whUnit);
}
else//扫描物料并复核
{
action = await ScanItem(input);
}
}
catch (Exception ex)
{
//取消当前操作
ResetScan();
//action.CatchExceptionWithLog(ex, $"扫描[{input.SN}]异常");
action.CatchExceptionWithLog(ex, Biz.L("WMS.Default.Scan.ScanException", input.SN));
}
return SetOutPutMqttMsg(action, input.Locale);
}
///
/// 扫描物料并复核,如果物料已经完成移库则货架上亮灯提醒储位
///
public async Task> ScanItem(BaseInput input)
{
var action = new ApiAction(new ScanOutput() { Command = Command });
try
{
if (CurInvItem.IsNullOrEmpty())
{
//解析条码
Result result = WMS_ITEM_Biz.WmsItem.Get(input.SN, input.AuthOption, true);
if (!result.IsSuccessed)
{
action.IsSuccessed = false;
action.LocaleMsg = result.LocaleMsg;
return SetOutPutMqttMsg(action, input.Locale);
}
var inv = result.Data as Inventory;
//验证条码是否正确
if (new[] { WMS_ITEM.STATUSs.InStore, WMS_ITEM.STATUSs.Loaded, WMS_ITEM.STATUSs.UseUp }.Contains(inv.Status))
{
action.IsSuccessed = false;
//action.LocaleMsg = Biz.L("状态[{0}]异常,请重新扫描");
action.LocaleMsg = Biz.L("WMS.Default.ScanItem.StatusException", string.Join(',', inv.StatusList.Select(q => q.GetDesc())));
return SetOutPutMqttMsg(action, input.Locale);
}
//物料验证
if (inv.ItemInfo.IsNullOrEmpty() || inv.ItemInfo.IS_ACTIVE == "N")
{
action.IsSuccessed = false;
//action.LocaleMsg = Biz.L("物料编码[{0}]不存在或者该物料未启用");
action.LocaleMsg = Biz.L("WMS.Default.ScanItem.ItemCodeNotExistsOrNotActive", inv.ItemInfo?.ITEM_CODE.IsNullOrEmpty(inv.Barcode.ItemCode));
return SetOutPutMqttMsg(action, input.Locale);
}
if (inv.Items.Count == 0)
{
List ItemHistorys = new List();
WMS_ITEM Item;
WMS_ITEM_PKG ItemPkgs;
Item = new()
{
SN = inv.Barcode.SN,
ITEM_CODE = inv.Barcode.ItemCode,
AUTH_ORG = input.AuthOption.CurOrg,
STATUS = WMS_ITEM.STATUSs.WaitIn.GetValue(),
QTY = inv.Barcode.Qty.ToDouble(),
PROD_DATE = inv.Barcode.DateCode.ToDateTime(),
ERP_WH = CurScanShelf.WarehouseCode,
UNIT = inv.Barcode.Unit,
};
WMS_ITEM_HIS his = new(Item, $"标准上架入库");
ItemHistorys.Add(his);
ItemPkgs = new()
{
SN = inv.Barcode.SN,
AUTH_ORG = input.AuthOption.CurOrg,
ITEM_CODE = inv.Barcode.ItemCode,
QTY = inv.Barcode.Qty.ToDouble(),
ERP_WH = CurScanShelf.WarehouseCode,
UNIT = inv.Barcode.Unit,
};
var noExt = inv.Items.Where(q => !inv.ItemsExt.Any(s => s.SN == q.SN)).ToList();
//扩展表不存在时新建
foreach (var item in noExt)
{
inv.ItemsExt.Add(new()
{
SN = item.SN,
META_SN = inv.Barcode.MetaSn,
QR_CODE = inv.Barcode.MetaSn
});
}
inv.Items.Add(Item);
inv.History.Add(his);
inv.Packages.Add(ItemPkgs);
}
//雅达不从条码上更新数量
//else if (inv.Items.Count == 1)
//{
// inv.Items.First().QTY = inv.Barcode.Qty.ToDouble();
// inv.Items.First().ERP_WH = CurScanShelf.WarehouseCode;
// inv.Items.First().AUTH_ORG = input.AuthOption.CurOrg;
// inv.Packages.First().QTY = inv.Barcode.Qty.ToDouble();
// inv.Packages.First().ERP_WH = CurScanShelf.WarehouseCode;
// inv.Packages.First().AUTH_ORG = input.AuthOption.CurOrg;
//}
//如果扫描外箱的不能更新数量
else
{
foreach (var item in inv.Items)
{
item.ERP_WH = CurScanShelf.WarehouseCode;
item.AUTH_ORG = input.AuthOption.CurOrg;
}
foreach (var item in inv.Packages)
{
item.ERP_WH = CurScanShelf.WarehouseCode;
item.AUTH_ORG = input.AuthOption.CurOrg;
}
}
CurInvItem = inv;
//检查物料数量,如果没有则查询雅达条码主档,把数量返回前端确认
//if (CurInvItem.Items.Any(q => q.QTY <= 0))
{
var qtyList = Biz.DataSource["YadaU9C"].Client.Queryable().Where(q => CurInvItem.Items.Select(q => q.SN).Contains(q.Code)).ToList();
foreach (var qty in qtyList)
{
var item = CurInvItem.Items.First(q => q.SN == qty.Code);
item.QTY = qty.WhQty;
item.SUPP_LOTNO = qty.Segment7;
}
action.Data.Data = CurInvItem.Items.Select(q => new { q.SN, q.QTY });
action.Data.Command = Command = "ComfirmQty";
//action.LocaleMsg = Biz.L("请确认条码[{0}]的数量是否正确");
action.LocaleMsg = Biz.L("WMS.Default.ScanItem.ComfirmQty", input.SN);
return SetOutPutMqttMsg(action, input.Locale);
}
}
//更新已确认物料数量
if (Command == "ComfirmQty")
{
var qtyList = (input.Data ?? "").JsonToObject>() ?? new List();
if (!qtyList.Any() || qtyList.Any(q => q.QTY <= 0))
{
action.IsSuccessed = false;
action.Data.Data = CurInvItem.Items.Select(q => new { q.SN, q.QTY });
action.Data.Command = Command = "ComfirmQty";
//action.LocaleMsg = Biz.L("数量不能小于等于零,请重新确认条码[{0}]的数量");
action.LocaleMsg = Biz.L("WMS.Default.ScanItem.ReComfirmQty", input.SN);
return SetOutPutMqttMsg(action, input.Locale);
}
else
{
foreach (var qty in qtyList)
{
var item = CurInvItem.Items.First(q => q.SN == qty.SN);
item.QTY = qty.QTY;
var pkg = CurInvItem.Packages.First(q => q.SN == qty.SN);
pkg.QTY = qty.QTY;
}
CurInvItem.UpdatePkgQty();
Command = "Normal";
}
}
var nLocation = new WMS_LOCATION();
// 判断是否智能货架
if (CurScanShelf.Shelf.IsLightShelf)
{
ShelfApiResult shelfApiResult = await Share.Shelf.PutOn(TransID, CurScanShelf.Shelf, CurInvItem.Items[0]);
if (!shelfApiResult.IsSuccess)
{
action.IsSuccessed = false;
action.LocaleMsg = Biz.L(shelfApiResult.GetData());
return SetOutPutMqttMsg(action, input.Locale);
}
var reaultShelf = shelfApiResult.GetData();
nLocation = reaultShelf.GetLocation();
if (nLocation == null)
{
action.IsSuccessed = false;
//action.LocaleMsg = Biz.L($"货架[{0}]中不存在id为[{1}]的储位,请先维护货架信息");
action.LocaleMsg = Biz.L("WMS.Default.ScanItem.LocationNotExistsInShelf", CurScanShelf.Shelf.SHELF_CODE, reaultShelf.ledAddr);
return SetOutPutMqttMsg(action, input.Locale);
}
var locationData = MainDB.Queryable().Where(q => q.LOCATION_ID == nLocation.ID).First();
if (!locationData.IsNullOrEmpty())
{
action.IsSuccessed = false;
//action.LocaleMsg = Biz.L("储位[{0}]已存有物料[{1}],请检查系统库存信息");
action.LocaleMsg = Biz.L($"WMS.Default.ScanShelf.ItemAlreadyExistsInLocation", nLocation.LOCATION_CODE, locationData.SN);
return SetOutPutMqttMsg(action, input.Locale);
}
CurScanShelf.LocationCode = nLocation.LOCATION_CODE;
}
else
{
if (CurScanShelf.LocationCode.IsNullOrEmpty() || CurScanShelf.WarehouseCode.IsNullOrEmpty())
{
action.IsSuccessed = false;
//action.LocaleMsg = Biz.L("请输入或扫描有效的货架/储位码");
action.LocaleMsg = Biz.L("WMS.Default.ScanShelf.ShelfCanNotEmpty");
return SetOutPutMqttMsg(action, input.Locale);
}
nLocation = await MainDB.Queryable().Where(t => t.LOCATION_CODE == CurScanShelf.LocationCode && t.AUTH_ORG == OrgCode).FirstAsync();
}
//判断储位是否单放
if (nLocation.IS_SINGLE == "Y" && CurInvItem.Items.Count > 1)
{
action.IsSuccessed = false;
//action.LocaleMsg = Biz.L("储位[{0}]只能存放一个物料");
action.LocaleMsg = Biz.L("WMS.Default.ScanItem.LocationSingleFailure", nLocation.LOCATION_CODE);
ResetScan();
return SetOutPutMqttMsg(action, input.Locale);
}
//执行上架数据处理
foreach (var item in CurInvItem.Items)
{
item.TRANS_CODE = "In_Default";
item.TRANS_NO = $"In_Default_{DateTime.Now:yyyyMMdd}";
}
Result putonResult = PutOn(input.AuthOption, nLocation.LOCATION_CODE);
if (!putonResult.IsSuccessed)
{
action.IsSuccessed = false;
action.LocaleMsg = putonResult.LocaleMsg;
ResetScan();
return SetOutPutMqttMsg(action, input.Locale);
}
//完成所有处理后使用事务保存数据
action = DoIfFinish(action, input.Locale);
}
catch (Exception ex)
{
//取消当前操作
ResetScan();
//action.CatchExceptionWithLog(ex, $"扫描条码[{input.SN}]复核异常");
action.CatchExceptionWithLog(ex, Biz.L("WMS.Default.ScanItem.ScanException", input.SN));
}
return SetOutPutMqttMsg(action, input.Locale);
}
///
/// 扫描货架或者储位
///
public async Task> ScanShelf(string Code, V_WH_UNIT whUnit)
{
var action = new ApiAction(new ScanOutput() { Command = Command });
try
{
if (Code.IsNullOrEmpty())
{
action.IsSuccessed = false;
//action.LocaleMsg = Biz.L("请输入或扫描有效的货架/储位码");
action.LocaleMsg = Biz.L("WMS.Default.ScanShelf.ShelfCanNotEmpty");
return action;
}
CurScanShelf = new ScanShelfInfo();
// 查询货架信息
whUnit = whUnit.IsNullOrEmpty(await MainDB.Queryable().Where(t => (t.SHELF_CODE.ToUpper() == Code || t.LOCATION_CODE.ToUpper() == Code) && t.AUTH_ORG == OrgCode).IncludesAllFirstLayer().FirstAsync());
// 扫描货架代码,且为智能货架
if (!whUnit.IsNullOrEmpty() && whUnit.SHELF_CODE == Code)
{
CurScanShelf.Shelf = whUnit.Shelf;
CurScanShelf.WarehouseCode = whUnit.WH_CODE;
CurScanShelf.RegionCode = whUnit.REGION_CODE;
CurScanShelf.ShelfCode = whUnit.SHELF_CODE;
CurScanShelf.ShelfType = whUnit.SHELF_TYPE;
CurScanShelf.IsSmartRack = true;
}
// 扫描库位代码
else if (!whUnit.IsNullOrEmpty() && whUnit.LOCATION_CODE == Code)
{
//var nLocation = await MainDB.Queryable().Where(t => t.LOCATION_CODE.ToUpper() == Code.ToUpper() && t.AUTH_ORG == OrgCode).IncludesAllFirstLayer().FirstAsync();
//if (nLocation == null)
//{
// action.IsSuccessed = false;
// //action.LocaleMsg = Biz.L("储位[{0}]不存在");
// action.LocaleMsg = Biz.L("WMS.Default.ScanShelf.LocationNotExist", Code);
// return action;
//}
if (whUnit.IS_ACTIVE == "N")
{
action.IsSuccessed = false;
//action.LocaleMsg = Biz.L("扫描的储位[{0}]未启用");
action.LocaleMsg = Biz.L("WMS.Default.ScanShelf.ShelfOrLocationDisabled", Code);
return action;
}
if (whUnit.Shelf.IsLightShelf || whUnit.Location.IS_SINGLE == "Y")
{
var locationData = MainDB.Queryable().Where(q => q.LOCATION_ID == whUnit.LOCATION_ID && q.AUTH_ORG == OrgCode).First();
if (!locationData.IsNullOrEmpty())
{
action.IsSuccessed = false;
//action.LocaleMsg = Biz.L("储位[{0}]已存有物料[{1}],请检查系统库存信息");
action.LocaleMsg = Biz.L($"WMS.Default.ScanShelf.ItemAlreadyExistsInLocation", Code, locationData.SN);
return action;
}
}
CurScanShelf.Shelf = whUnit.Shelf;
CurScanShelf.Location = whUnit.Location;
CurScanShelf.WarehouseCode = whUnit.WH_CODE;
CurScanShelf.RegionCode = whUnit.REGION_CODE;
CurScanShelf.ShelfCode = whUnit .SHELF_CODE;
CurScanShelf.LocationCode = whUnit.LOCATION_CODE;
CurScanShelf.IsSmartRack = false;
}
else
{
action.IsSuccessed = false;
//action.LocaleMsg = Biz.L("请输入或扫描有效的货架/储位码");
action.LocaleMsg = Biz.L("WMS.Default.ScanShelf.ShelfCanNotEmpty");
return action;
}
CurScanShelf.IsScanShelf = true;
//action.LocaleMsg = Biz.L("扫描货架/储位[{0}]成功");
action.LocaleMsg = Biz.L("WMS.Default.ScanShelf.ScanSucceeded", Code);
action.Data.Data = CurScanShelf;
}
catch (Exception ex)
{
//取消当前操作
ResetScan();
//action.CatchExceptionWithLog(ex, $"扫描货架/储位[{0}]异常");
action.CatchExceptionWithLog(ex, Biz.L("WMS.Default.ScanShelf.ScanException", Code));
}
return action;
}
///
/// 完成所有处理后使用事务保存数据
///
///
///
public ApiAction DoIfFinish(ApiAction action, string locale)
{
//保存数据库
SaveCommitListToDB();
// 返回数据
action.Data.Data = new DefaultInStoreOutput
{
Barcode = CurInvItem.SN,
MaterialCode = CurInvItem.ItemInfo.ITEM_CODE,
MaterialName = CurInvItem.ItemInfo.ITEM_NAME,
CurrentQty = CurInvItem.Items[0].QTY,
Unit = CurInvItem.Items[0].UNIT,
DateCode = CurInvItem.Items[0].PROD_DATE,
WarehouseCode = CurInvItem.Warehouse.WH_CODE,
RegionCode = CurInvItem.Region.REGION_CODE,
ShelfCode = CurInvItem.Shelf.SHELF_CODE,
LocationCode = CurInvItem.Location.LOCATION_CODE,
};
//action.LocaleMsg = Biz.L($"扫描条码[{0}]上架到储位[{1}]成功");
action.LocaleMsg = Biz.L("WMS.Default.ScanItem.PutOnSucceeded", CurInvItem.SN, CurInvItem.Location.LOCATION_CODE);
//重置扫码信息
ResetScanInfo();
return SetOutPutMqttMsg(action, locale);
}
#endregion
///
/// 重置当前操作,有需要则重写此方法
///
public override void ResetScan()
{
base.ResetScan();
ResetScanInfo();
CurScanShelf = null;
}
///
/// 重置扫码信息
///
public void ResetScanInfo()
{
Command = null;
CurInvItem = null;
}
public override bool Close(bool needSaveHistoryLog = false)
{
this.IsFinished = true;
return IsFinished ? base.Close(needSaveHistoryLog) : IsFinished;
}
}//endClass
}