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;
namespace Tiger.Business.WMS.Transaction
{
///
/// 标准上架事务
///
public class In_Default : WMSTransactionBase, IIn_Default
{
public IIn_Default 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 Inventory CurInvItem { get; set; }
public ScanShelfInfo CurScanShelf { get; set; }
#endregion
#region Functions
///
/// 扫描物料并复核,如果物料已经完成移库则货架上亮灯提醒储位
///
public async Task ScanItem(BaseInput input)
{
var action = new ApiAction();
try
{
if (input.SN.IsNullOrEmpty())
{
action.IsSuccessed = false;
//action.LocaleMsg = Biz.L("条码不能为空");
action.LocaleMsg = Biz.L("WMS.Default.ScanItem.SnEmptyFailure");
return action;
}
//货架为空则扫描的认为是货架
if (string.IsNullOrEmpty(CurScanShelf?.RackCode))
{
var _action = await ScanShelf(input.SN);
action.LocaleMsg = _action.LocaleMsg;
action.IsSuccessed = _action.IsSuccessed;
action.Data = _action.Data;
return action;
}
Result result = WMS_ITEM_Biz.WmsItem.Get(input.SN, input.AuthOption, true);
if (!result.IsSuccessed)
{
action.IsSuccessed = false;
action.LocaleMsg = result.LocaleMsg;
return action;
}
var inv = result.Data as Inventory;
//验证条码是否正确
if (!inv.isNormalStatus || inv.Status.GetValue() != WMS_ITEM.STATUSs.NotExists.GetValue() && inv.Status.GetValue() <= WMS_ITEM.STATUSs.InStore.GetValue())
{
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 action;
}
//物料验证
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 action;
}
if (!inv.IsNullOrEmpty())
{
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.ToDecimal(),
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.ToDecimal(),
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.ToDecimal();
inv.Items.First().ERP_WH = CurScanShelf.WarehouseCode;
inv.Items.First().AUTH_ORG = input.AuthOption.CurOrg;
inv.Packages.First().QTY = inv.Barcode.Qty.ToDecimal();
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;
}
}
}
var nLocation = new WMS_LOCATION();
// 判断是否智能货架
if (CurScanShelf.Shelf.IsLightShelf)
{
ShelfApiResult shelfApiResult = await Share.Shelf.PutOn(TransID, CurScanShelf.Shelf, inv.Items[0]);
if (!shelfApiResult.IsSuccess)
{
action.IsSuccessed = false;
action.LocaleMsg = Biz.L(shelfApiResult.GetData());
return action;
}
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 action;
}
var locationData = Biz.Db.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 action;
}
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 action;
}
nLocation = await Biz.Db.Queryable().Where(t => t.LOCATION_CODE == CurScanShelf.LocationCode && t.AUTH_ORG == OrgCode).FirstAsync();
}
//判断储位是否单放
if (nLocation.IS_SINGLE == "Y" && inv.Items.Count > 1)
{
action.IsSuccessed = false;
//action.LocaleMsg = Biz.L("储位[{0}]只能存放一个物料");
action.LocaleMsg = Biz.L("WMS.Default.ScanItem.LocationSingleFailure", nLocation.LOCATION_CODE);
return action;
}
//执行上架
foreach (var item in inv.Items)
{
item.TRANS_CODE = "In_Default";
item.TRANS_NO = $"In_Default_{DateTime.Now:yyyyMMdd}";
}
Result putonResult = WMS_ITEM_Biz.WmsItem.PutOn(inv, input.AuthOption, nLocation.LOCATION_CODE);
if (!putonResult.IsSuccessed)
{
action.IsSuccessed = false;
action.LocaleMsg = putonResult.LocaleMsg;
return action;
}
PutOnInfo putOnInfo = putonResult.Data;
//保存到数据库
var db = Business.Biz.Db;
var dbTran = db.UseTran(() =>
{
//入库
db.Storageable(putOnInfo.Items, UserCode).ExecuteCommand();
db.Storageable(putOnInfo.Packages, UserCode).ExecuteCommand();
db.Insertable(putOnInfo.History, UserCode).ExecuteCommand();
var x = db.Storageable(putOnInfo.ItemsExt, UserCode).ToStorage();
x.AsInsertable.ExecuteCommand();//不存在插入
x.AsUpdateable.ExecuteCommand();//存在更新
});
if (!dbTran.IsSuccess)
{
Logger.Default.Fatal(dbTran.ErrorException, "Database transaction save exception");
throw dbTran.ErrorException;
}
// 返回其他入库对象
action.Data = new DefaultInStoreOutput
{
Barcode = inv.SN,
MaterialCode = inv.ItemInfo.ITEM_CODE,
MaterialName = inv.ItemInfo.ITEM_NAME,
CurrentQty = inv.Items[0].QTY,
Unit = inv.Items[0].UNIT,
DateCode = inv.Items[0].PROD_DATE,
WarehouseCode = putOnInfo.Warehouse.WH_CODE,
RegionCode = putOnInfo.Region.REGION_CODE,
ShelfCode = putOnInfo.Shelf.SHELF_CODE,
LocationCode = putOnInfo.Location.LOCATION_CODE,
};
//action.LocaleMsg = Biz.L($"扫描条码[{0}]上架到储位[{1}]成功");
action.LocaleMsg = Biz.L("WMS.Default.ScanItem.PutOnSucceeded", inv.SN, putOnInfo.Location.LOCATION_CODE);
}
catch (Exception ex)
{
//取消当前操作
ResetInfo();
//action.CatchExceptionWithLog(ex, $"扫描条码[{input.SN}]复核异常");
action.CatchExceptionWithLog(ex, Biz.L("WMS.Default.ScanItem.ScanException", input.SN));
}
return action;
}
///
/// 扫描货架或者储位
///
public async Task> ScanShelf(string Code)
{
var action = new ApiAction();
try
{
if (Code.IsNullOrEmpty())
{
action.IsSuccessed = false;
//action.LocaleMsg = Biz.L("请输入或扫描有效的货架/储位码");
action.LocaleMsg = Biz.L("WMS.Default.ScanShelf.ShelfCanNotEmpty");
return action;
}
CurScanShelf = new ScanShelfInfo();
// 查询货架信息
var whUnit = await Biz.Db.Queryable().Where(t => t.SHELF_CODE.ToUpper() == Code.ToUpper() && t.AUTH_ORG == OrgCode).IncludesAllFirstLayer().FirstAsync();
// 扫描货架代码,且为智能货架
if (whUnit != null && whUnit.SHELF_TYPE == WMS_SHELF.SHELF_TYPEs.Smart.GetValue())
{
CurScanShelf.Shelf = whUnit.Shelf;
CurScanShelf.WarehouseCode = whUnit.WH_CODE;
CurScanShelf.RegionCode = whUnit.REGION_CODE;
CurScanShelf.ShelfCode = whUnit.SHELF_CODE;
CurScanShelf.LocationCode = whUnit.LOCATION_CODE;
CurScanShelf.ShelfType = whUnit.SHELF_TYPE;
CurScanShelf.IsSmartRack = true;
}
// 扫描库位代码
else
{
var nLocation = await Biz.Db.Queryable().Where(t => t.LOCATION_CODE.ToUpper() == Code.ToUpper() && t.AUTH_ORG == OrgCode).FirstAsync();
if (nLocation == null)
{
action.IsSuccessed = false;
//action.LocaleMsg = Biz.L("储位[{0}]不存在");
action.LocaleMsg = Biz.L("WMS.Default.ScanShelf.LocationNotExist", Code);
return action;
}
if (nLocation.IS_ACTIVE == "N")
{
action.IsSuccessed = false;
//action.LocaleMsg = Biz.L("扫描的储位[{0}]未启用");
action.LocaleMsg = Biz.L("WMS.Default.ScanShelf.ShelfOrLocationDisabled", Code);
return action;
}
if (nLocation.Shelf.IsLightShelf || nLocation.Location.IS_SINGLE == "Y")
{
var locationData = Biz.Db.Queryable().Where(q => q.LOCATION_ID == nLocation.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.Location = nLocation.Location;
CurScanShelf.WarehouseCode = nLocation.WH_CODE;
CurScanShelf.RegionCode = nLocation.REGION_CODE;
CurScanShelf.ShelfCode = nLocation.SHELF_CODE;
CurScanShelf.LocationCode = nLocation.LOCATION_CODE;
CurScanShelf.IsSmartRack = false;
}
CurScanShelf.IsScanShelf = true;
//action.LocaleMsg = Biz.L("扫描货架/储位[{0}]成功");
action.LocaleMsg = Biz.L("WMS.Default.ScanShelf.ScanSucceeded", Code);
action.Data = CurScanShelf;
}
catch (Exception ex)
{
//取消当前操作
ResetInfo();
//action.CatchExceptionWithLog(ex, $"扫描货架/储位[{0}]异常");
action.CatchExceptionWithLog(ex, Biz.L("WMS.Default.ScanShelf.ScanException", Code));
}
return action;
}
#endregion
///
/// 重置当前操作,有需要则重写此方法
///
public override void ResetInfo()
{
base.ResetInfo();
CurInvItem = null;
CurScanShelf = null;
}
public override bool Close(bool needSaveHistoryLog = false)
{
this.IsFinished = true;
return IsFinished ? base.Close(needSaveHistoryLog) : IsFinished;
}
}//endClass
}