using Rhea.Common;
using Tiger.Model;
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.Sharetronic.Shelf;
using Tiger.Business.WMS.Sharetronic.Shelf;
using Tiger.IBusiness;
namespace Tiger.Business.WMS.Transaction
{
///
/// 一般移库事务
///
public class LocationTransfer : WMSTransactionBase, ILocationTransfer
{
public ILocationTransfer Init(string id, string userCode, string apiHost)
{
TransID = id;
UserCode = userCode;
ApiHost = apiHost;
Logger.Console.Info($"Start {this.GetType().Name} Transaction[ID: {TransID}]");
return this;
}
#region Propertys & Variables
public string UserId { get; set; }
public string UserCode { get; set; }
public string OrgCode { get; set; }
public WMS_SHELF CurShelf { get; set; }
public WMS_LOCATION CurLocation { get; set; }
#endregion
#region Functions
///
/// 扫描物料并复核,如果物料已经完成移库则货架上亮灯提醒储位
///
public async Task> ScanItem(BaseInput input)
{
var action = new ApiAction();
try
{
if (string.IsNullOrEmpty(input.SN))
{
action.IsSuccessed = false;
action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.SnEmptyFailure"); // $"条码不能为空!";
return action;
}
input.AuthOption.OrgCode = input.AuthOption.OrgCode.IsNullOrEmpty() ? input.AuthOption.CurOrg : input.AuthOption.OrgCode;
OrgCode =input.AuthOption.OrgCode;
var _action = await ScanShelf(input.SN);
//货架为空则扫描的认为是货架
if ((CurLocation.IsNullOrEmpty() && CurShelf.IsNullOrEmpty()) || _action.IsSuccessed)
{
if (!_action.IsSuccessed)
{
action.LocaleMsg = _action.LocaleMsg;
action.IsSuccessed = _action.IsSuccessed;
}
// 扫描货架代码,且为智能货架
if (_action.Data != null && _action.Data.SHELF_TYPE == WMS_SHELF.SHELF_TYPEs.Smart.GetValue() && _action.Data.SHELF_CODE == input.SN)
{
CurShelf = _action.Data.Shelf;
CurLocation = null;
}
// 扫描库位代码
else// if (_action.Data != null && _action.Data.LOCATION_CODE == input.SN)
{
if (_action.Data.IsNullOrEmpty())
{
action.IsSuccessed = false;
action.LocaleMsg = Biz.L($"当前储位不存在{input.SN}");
return action;
}
if (_action.Data.SHELF_TYPE == WMS_SHELF.SHELF_TYPEs.Smart.GetValue() || _action.Data.SHELF_TYPE == WMS_SHELF.SHELF_TYPEs.QRCode.GetValue())
{
var locationData = Biz.Db.Queryable().Where(q => q.LOCATION_ID == _action.Data.LOCATION_ID && q.AUTH_ORG == _action.Data.AUTH_ORG).First();
if (!locationData.IsNullOrEmpty())
{
action.IsSuccessed = false;
action.LocaleMsg = Biz.L($"系统当前库位存在料盘,请检查系统库存信息");
return action;
}
}
CurShelf = _action.Data?.Shelf;
CurLocation = _action.Data?.Location;
}
action.Data = new()
{
LocationCode = input.SN,
};
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 || !new[] { WMS_ITEM.STATUSs.InStore }.Contains(inv.Status))
{
action.IsSuccessed = false;
action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.StatusException", string.Join(',', inv.StatusList.Select(x => x.GetDesc())));
return action;
}
//原储位灭灯
if (inv?.Shelf?.SHELF_TYPE == WMS_SHELF.SHELF_TYPEs.Smart.GetValue() && !inv.Location.IsNullOrEmpty())
{
await Share.Shelf.DownSingle(TransID, inv.Location);
}
//物料验证
if (inv.ItemInfo.IsNullOrEmpty() || inv.ItemInfo.IS_ACTIVE == "N")
{
action.IsSuccessed = false;
action.LocaleMsg = Biz.L("WMS.ProdMReq.ScanItem.ItemCodeNotExistsOrNotActive", inv.ItemInfo?.ITEM_CODE.IsNullOrEmpty(inv.Barcode.ItemCode));
return action;
}
// 判断是否智能货架
WMS_LOCATION target = null;
if (CurLocation.IsNullOrEmpty() && (CurShelf.SHELF_TYPE == WMS_SHELF.SHELF_TYPEs.Smart.GetValue()|| CurShelf.SHELF_TYPE == WMS_SHELF.SHELF_TYPEs.QRCode.GetValue()))
{
ShelfApiResult shelfApiResult = await Share.Shelf.PutOn(TransID, CurShelf, inv.Items[0]);
if (!shelfApiResult.IsSuccess)
{
action.IsSuccessed = false;
action.LocaleMsg = Biz.L(shelfApiResult.GetData());
return action;
}
var reaultShelf = shelfApiResult.GetData();
target = reaultShelf.GetLocation();
if (target.IsNullOrEmpty())
{
action.IsSuccessed = false;
action.LocaleMsg = Biz.L($"系统不存在储位,请先维护货架信息");
return action;
}
var locationData = Biz.Db.Queryable().Where(q => q.LOCATION_ID == target.ID && q.AUTH_ORG==target.AUTH_ORG).First();
if (!locationData.IsNullOrEmpty())
{
action.IsSuccessed = false;
action.LocaleMsg = Biz.L($"系统当前库位存在料盘,请检查系统库存信息");
return action;
}
}
else
{
if (CurLocation.IsNullOrEmpty())
{
action.IsSuccessed = false;
action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.LocationEmptyFailure"); //$"请先扫描库位码";
return action;
}
target = CurLocation;
}
//执行入库
Result onResult = WMS_ITEM_Biz.WmsItem.PutOn(inv, input.AuthOption, target?.LOCATION_CODE, true);
if (!onResult.IsSuccessed)
{
action.IsSuccessed = false;
action.LocaleMsg = onResult.LocaleMsg;
return action;
}
PutOnInfo putOnInfo = onResult.Data;
//保存到数据库
var db = Biz.Db;
var dbTran = db.UseTran(() =>
{
//入库
db.Updateable(putOnInfo.Items, UserCode).ExecuteCommand();
db.Insertable(putOnInfo.History, UserCode).ExecuteCommand();
db.Updateable(putOnInfo.Packages, UserCode).ExecuteCommand();
});
if (!dbTran.IsSuccess)
{
Logger.Default.Fatal(dbTran.ErrorException, "Database transaction save exception");
this.Close(!dbTran.IsSuccess);
throw dbTran.ErrorException;
}
action.LocaleMsg = Biz.L($"储位调拨成功");
//if (inv?.Shelf?.SHELF_TYPE == WMS_SHELF.SHELF_TYPEs.Smart.GetValue())
//{
// //CurLocation = null;
//}
action.Data = new()
{
ItemCode = inv.ItemInfo.ITEM_CODE,
ItemName = inv.ItemInfo.ITEM_NAME,
SN = inv.SN,
Qty = inv.CurPkg.QTY,
LocationCode = putOnInfo.Location.LOCATION_CODE,
};
}
catch (Exception ex)
{
//取消当前操作
action.CatchExceptionWithLog(ex, $"扫描条码[{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("WMS.CustSupChk.ScanItem.ShelfCanNotEmpty");
return action;
}
// 查询货架信息
var whUnit = await Biz.Db.Queryable().Where(t => (t.SHELF_CODE.ToUpper() == code.ToUpper() || t.LOCATION_CODE.ToUpper() == code.ToUpper())&& t.AUTH_ORG == OrgCode).IncludesAllFirstLayer().FirstAsync();
if (whUnit.IsNullOrEmpty())
{
action.IsSuccessed = false;
action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.NotExist", code);
return action;
}
if (whUnit.IS_ACTIVE == "N")
{
action.IsSuccessed = false;
action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.ShelfOrLocationDisabled");
return action;
}
action.Data = whUnit;
}
catch (Exception ex)
{
action.CatchExceptionWithLog(ex, $"扫描货架或者储位[{code}]异常");
}
return action;
}
#endregion
public override bool Close(bool needSaveHistoryLog = false)
{
//保存操作日志
this.IsFinished = true;
return IsFinished ? base.Close(needSaveHistoryLog) : IsFinished;
}
}//endClass
}