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 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 async Task AddPallets(int groupCount, int userId)
{
try
{
var db = DataContext.Db;
if (groupCount <= 0)
{
throw new Exception("组数需大于0");
}
var pallNo = db.Queryable().Max(m => m.PalletNo);
var str = pallNo.Substring(3, 5);
string remove = pallNo.Substring(0, 3);
int sibelius = 0;
var num = 0;
if (remove != "T" + DateTime.Now.ToString("yy"))
{
remove = "T" + DateTime.Now.ToString("yy");
sibelius = 0;
}
else
{
sibelius = Convert.ToInt16(str);
}
for (int i = 0; i < groupCount; i++)
{
sibelius += 1;
if (sibelius > 99999)
{
throw new Exception("托盘码位数已达上线");
}
string code = remove + Convert.ToString(sibelius).PadLeft(5, '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);
}
}
}
}