using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Security.Policy;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Model.InterFaceModel;
using Model.ModelDto.LogDto;
using Model.ModelDto.SysDto;
using SqlSugar;
using Utility.Tools;
using WMS.BLL.LogServer;
using WMS.Entity.Context;
using WMS.Entity.DataEntity;
using WMS.Entity.SysEntity;
using WMS.IBLL.ILogServer;
using WMS.IBLL.ISysServer;
using WMS.IDAL.ISysInterface;
namespace WMS.BLL.SysServer
{
public class PalletsServer : IPalletsServer
{
public IPalletsRepository PalletsRst { get; set; }
private readonly IOperationSysServer _operation; //操作日志
public PalletsServer(IPalletsRepository palletsRst, IOperationSysServer operation)
{
PalletsRst = palletsRst;
_operation = operation;
}
///
/// 查询托盘表信息
///
/// 托盘码
/// 状态 0未使用 1使用中
///
///
///
///
public List GetPalletsList(string palletNo, string status, int page, int limit, out int count)
{
try
{
//创建表达式
Expression> item = Expressionable.Create()
.AndIF(!string.IsNullOrWhiteSpace(palletNo), it => it.PalletNo.Contains(palletNo.Trim()))
.AndIF(!string.IsNullOrWhiteSpace(status), it => it.Status == status)
.ToExpression();//注意 这一句 不能少
var data = PalletsRst.GetAllByOrderPageAsync(item, limit, page, out int counts)
.Includes(x => x.CreateUserInfo)
.ToList();
count = counts;
return data.Select(m => new PalletsDto()
{
Id = m.Id,
PalletNo = m.PalletNo,
Standard = m.Standard,
Type = m.Type == "0" ? "托盘" : m.Type == "1" ? "中转箱" : "",
Status = m.Status,
LastUse = m.LastUse,
CreateTime = m.CreateTime,
CreateUserName = m.CreateUserInfo == null ? "" : m.CreateUserInfo.RealName
}).ToList();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
public string GetPalletsNo(string palletNo)
{
try
{
var time = DateTime.Now.ToString("yyyy");
var time2 = time.Substring(2, 2);
int codeId;
if (!string.IsNullOrWhiteSpace(palletNo))
{
var code = PalletsRst.GetAllWhereAsync(m => m.PalletNo == palletNo).First();
return code.PalletNo;
}
else
{
var code = PalletsRst.GetAllAsync().OrderByDescending(m => m.PalletNo).First();
if (code != null)
{
string riQi = code.PalletNo.Substring(1, 2);
if (riQi == time2)
{
codeId = int.Parse(code.PalletNo.Substring(3, 5)) + 1;
}
else
{
codeId = int.Parse("00001");
}
}
else
{
codeId = int.Parse("00001");
}
int liuShuiId = codeId;
var pallet = "T"+time2 + Convert.ToString(liuShuiId).PadLeft(5, '0');
return pallet;
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
///
/// 添加桶信息并增加库存
///
///
///
///
///
public void AddPallets(string palletNo,string locatNo, string deviceCode, string url, int userId)
{
var db = DataContext.Db;
try
{
if (string.IsNullOrEmpty(palletNo))
{
throw new Exception("请输入桶号");
}
if (string.IsNullOrEmpty(locatNo))
{
throw new Exception("请输入储位地址");
}
var palletModel = db.Queryable().First(w => w.PalletNo == palletNo && w.IsDel == "0");
if (palletModel != null)
{
throw new Exception("该桶号已存在");
}
var locatModel= db.Queryable().First(w => w.LocatNo == locatNo && w.IsDel == "0");
if (locatModel == null)
{
throw new Exception("储位地址不存在");
}
if (locatModel.Status != "0")
{
throw new Exception("该储位地址非空闲状态");
}
var areaModel = db.Queryable().First(w => w.IsDel == "0" && w.AreaNo == locatModel.AreaNo);
if (areaModel == null)
{
throw new Exception("储位地址所属区域不存在");
}
if (!areaModel.AreaName.Contains("净桶"))
{
throw new Exception("新添加的桶只能绑定净桶区");
}
//if (!string.IsNullOrEmpty(deviceCode))
//{
// var areaInfo = db.Queryable().First(w => w.IsDel == "0" && w.DeviceCode.Contains(deviceCode));
// if (areaInfo == null)
// {
// throw new Exception("未查到设备所属区域信息");
// }
// //125设备是接料和混料一体机
// if (deviceCode != "125")
// {
// if (!areaInfo.AreaName.Contains("接料设备"))
// {
// throw new Exception("只能绑定接料设备");
// }
// if (areaInfo.WareHouseNo != "M03")
// {
// throw new Exception("只有大单体车间才能绑定设备");
// }
// }
//}
if (!string.IsNullOrEmpty(deviceCode))
{
switch (deviceCode)
{
case "400":
deviceCode = "400L";
break;
case "800":
deviceCode = "800L";
break;
case "1000":
deviceCode = "1000L";
break;
default:
throw new Exception("不能录入400,800,1000以外的值");
}
}
//开启事务
db.BeginTran();
palletModel = new SysPallets();
palletModel.PalletNo = palletNo;
palletModel.Standard = deviceCode;
palletModel.Type = "0";
palletModel.Status = "0";
palletModel.CreateUser = userId;
//添加桶
db.Insertable(palletModel).ExecuteCommand();
var comTime = DateTime.Now;
var model = new DataStockDetail()
{
ASNNo = "",
Qty = 0,
LockQty = 0,
FrozenQty = 0,
InspectQty = 0,
WareHouseNo = locatModel.WareHouseNo,//所属仓库
RoadwayNo = "",//所属巷道
AreaNo = locatModel.AreaNo,//所属区域
LocatNo = locatModel.LocatNo,//储位地址
PalletNo = palletNo,
PalletNo2 = "",
PalletNo3 = "",
PalletTags = "0",
CompleteTime = comTime,
ProductionTime = null,
ExpirationTime = null,
Status = "0",
InspectMark = "0",
InspectStatus = "0",//待检验
BitPalletMark = "0",
PackagNo = "",
IsBale = "0",
IsBelt = "0",
SkuNo = "",
SkuName = "",
LotNo = "",
PalletStatus = "0",//净桶
Standard = deviceCode, //规格
IsDel = "0",
CreateUser = userId,
CreateTime = comTime,
UpdateTime=comTime,
};
//插入库存明细
db.Insertable(model).ExecuteCommand();
//更新储位状态
locatModel.Status = "1";//有物品
db.Updateable(locatModel).ExecuteCommand();
//下发AGV绑定载具接口
//调用AGV接口下发任务
//string agvMsg = string.Empty;
////给下车下发任务
//var agvResult = RcsHelper.BindPalletAndSite(palletNo, locatNo,url, out agvMsg);
//if (agvResult)//成功
//{
// //增加日志
//}
//else//失败
//{
// throw new Exception("小车载具和库位关系绑定失败,原因:"+agvMsg);
//}
//提交事务
db.CommitTran();
}
catch (Exception e)
{
//回滚事务
db.RollbackTran();
throw new Exception(e.Message);
}
}
}
}