using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Model.ModelDto.LogDto;
using Model.ModelDto.SysDto;
using SqlSugar;
using WMS.BLL.LogServer;
using WMS.Entity.Context;
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,
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 liuShuiCode ;
if (!string.IsNullOrWhiteSpace(palletNo))
{
var code = PalletsRst.GetAllWhereAsync(m => m.PalletNo == palletNo).First();
if (code == null)
{
throw new Exception("托盘码在系统中未查询到");
}
return code.PalletNo;
}
else
{
var code = PalletsRst.GetAllAsync().OrderByDescending(m => m.PalletNo).First();
if (code != null)
{
liuShuiCode = int.Parse(code.PalletNo.Substring(2, 6)) + 1;
}
else
{
liuShuiCode = int.Parse("000001");
}
var pallet = "LN" + Convert.ToString(liuShuiCode).PadLeft(6, '0');
return pallet;
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
public async Task AddPallets(int groupCount, int userId)
{
try
{
var db = DataContext.Db;
if (groupCount <= 0)
{
throw new Exception("组数需大于0");
}
//托盘条码规则 LN+6位流水 例如 LN000001
var pallNo = db.Queryable().Max(m => m.PalletNo);
if (string.IsNullOrWhiteSpace(pallNo))
{
pallNo = "LN000000";
}
var str = pallNo.Substring(2, 6); //
string remove = pallNo.Substring(0, 2);
int sibelius = Convert.ToInt16(str);
var num = 0;
for (int i = 0; i < groupCount; i++)
{
sibelius += 1;
if (sibelius>999999)
{
throw new Exception("托盘码位数已达上线");
}
string code = remove + Convert.ToString(sibelius).PadLeft(6, '0');
if (db.Queryable().Count(m=>m.PalletNo == code)>=1)
{
continue;
}
var item = new SysPallets
{
PalletNo = code,
Status = "0",
Type = "0",
CreateUser = userId
};
Thread.Sleep(100);
num += db.Insertable(item).ExecuteCommand();
if (num > 0)
{
if (groupCount > 1)
{
await _operation.InsertOperation("仓库设置", "条码管理", item.PalletNo, "批量添加", "批量添加托盘信息 托盘号:" + item.PalletNo, userId);
}
else
{
await _operation.InsertOperation("仓库设置", "条码管理", item.PalletNo, "添加", "添加托盘信息 托盘号:" + item.PalletNo, userId);
}
}
}
return num > 0;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
}
}