using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Model.ModelDto.SysDto;
using WMS.Entity.SysEntity;
using WMS.IBLL.ISysServer;
using WMS.IDAL.ISysInterface;
using Model.ModelVm.SysVm;
namespace WMS.BLL.SysServer
{
public class PackagServer : IPackagServer
{
public IPackagRepository PackagRst { get; set; }
public PackagServer(IPackagRepository packagRst)
{
PackagRst = packagRst;
}
///
/// 获取包装信息集合
///
/// 包装编号
/// 包装名称
/// 等级
/// 包装集合
public List GetPackagList(string packagNo, string packagName, int? level, int page, int limit, out int count)
{
try
{
Expression> item = Expressionable.Create() //创建表达式
.AndIF(!string.IsNullOrWhiteSpace(packagNo), it => it.PackagNo.Contains(packagNo.Trim()))
.AndIF(!string.IsNullOrWhiteSpace(packagName), it => it.PackagName.Contains(packagName.Trim()))
.AndIF(level != null, it => it.Level.Equals(level))
.ToExpression();//注意 这一句 不能少
var data = PackagRst.GetAllByOrderPageAsync(item, limit, page, out int counts)
.Includes(x => x.CreateUserInfo)
.Includes(x => x.UpdateUserInfo).ToList();
count = counts;
return data.Select(m => new PackagDto()
{
Id = m.Id,
IsValid = m.IsDel,
PackagNo = m.PackagNo,
PackagName = m.PackagName,
Level = m.Level,
L1 = m.L1Num == null ? "" : m.L1Num.ToString() + "/" + m.L1Name,
L2 = m.L2Num == null ? "" : m.L2Num.ToString() + "/" + m.L2Name,
L3 = m.L3Num == null ? "" : m.L3Num.ToString() + "/" + m.L3Name,
L4 = m.L4Num == null ? "" : m.L4Num.ToString() + "/" + m.L4Name,
L5 = m.L5Num == null ? "" : m.L5Num.ToString() + "/" + m.L5Name,
//L1Num = int.Parse(m.L1Num.ToString()),
//L1Name = m.L1Name,
//L2Num = int.Parse(m.L2Num.ToString()),
//L2Name = m.L2Name,
//L3Num = int.Parse(m.L3Num.ToString()),
//L3Name = m.L3Name,
//L4Num = int.Parse(m.L4Num.ToString()),
//L4Name = m.L4Name,
//L5Num = int.Parse(m.L5Num.ToString()),
//L5Name = m.L5Name,
CreateTime = m.CreateTime,
CreateUserName = m.CreateUserInfo == null ? "" : m.CreateUserInfo.RealName,
UpdateTime = m.UpdateTime,
UpdateUserName = m.UpdateUserInfo == null ? "" : m.UpdateUserInfo.RealName
}).ToList();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
///
/// 根据id获取包装信息
///
/// 包装id
///
public SysPackag GetPackagById(int id)
{
SysPackag packag = PackagRst.GetOneById(id);
return packag;
}
///
/// 添加包装信息
///
/// 包装信息
/// true:成功 flase:失败
public int AddPackag(AddEditPackagVm model)
{
try
{
var rowCount = PackagRst.GetAllWhereAsync(m => m.PackagNo == model.PackagNo).Count();
if (rowCount > 0)
{
throw new Exception("包装编号已存在!");
}
rowCount = PackagRst.GetAllWhereAsync(m => m.PackagName == model.PackagName).Count();
if (rowCount > 0)
{
throw new Exception("包装名称已存在!");
}
SysPackag packagModel = new SysPackag();
packagModel.PackagNo = model.PackagNo;
packagModel.PackagName = model.PackagName;
packagModel.Level = 0;
//判断五级包装是否为空
if (!string.IsNullOrEmpty(model.L5Num) && !string.IsNullOrEmpty(model.L5Name))
{
packagModel.L5Num = int.Parse(model.L5Num);
packagModel.L5Name = model.L5Name;
packagModel.Level += 1;
//判断四级包装是否为空
if (string.IsNullOrEmpty(model.L4Num) && string.IsNullOrEmpty(model.L4Name))
{
return 2;
}
}
//判断四级包装是否为空
if (!string.IsNullOrEmpty(model.L4Num) && !string.IsNullOrEmpty(model.L4Name))
{
packagModel.L4Num = int.Parse(model.L4Num);
packagModel.L4Name = model.L4Name;
packagModel.Level += 1;
//判断三级包装是否为空
if (string.IsNullOrEmpty(model.L3Num) && string.IsNullOrEmpty(model.L3Name))
{
return 2;
}
}
//判断三级包装是否为空
if (!string.IsNullOrEmpty(model.L3Num) && !string.IsNullOrEmpty(model.L3Name))
{
packagModel.L3Num = int.Parse(model.L3Num);
packagModel.L3Name = model.L3Name;
packagModel.Level += 1;
//判断二级包装是否为空
if (string.IsNullOrEmpty(model.L2Num) && string.IsNullOrEmpty(model.L2Name))
{
return 2;
}
}
//判断二级包装是否为空
if (!string.IsNullOrEmpty(model.L2Num) && !string.IsNullOrEmpty(model.L2Name))
{
packagModel.L2Num = int.Parse(model.L2Num);
packagModel.L2Name = model.L2Name;
packagModel.Level += 1;
//判断一级包装是否为空
if (string.IsNullOrEmpty(model.L1Num) && string.IsNullOrEmpty(model.L1Name))
{
return 2;
}
}
//判断一级包装是否为空
if (!string.IsNullOrEmpty(model.L1Num) && !string.IsNullOrEmpty(model.L1Name))
{
packagModel.L1Num = int.Parse(model.L1Num);
packagModel.L1Name = model.L1Name;
packagModel.Level += 1;
}
else
{
return 2;
}
packagModel.CreateUser = (int)model.CreateUser;
int rowNum = PackagRst.Add(packagModel);
if (rowNum > 0)
{
return 1;
}
return 0;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
///
/// 修改包装信息
///
/// 包装信息
/// true:成功 flase:失败
public int EditPackag(AddEditPackagVm model)
{
try
{
if (string.IsNullOrEmpty(model.PackagName))
{
var date = PackagRst.GetAllWhereAsync(m => m.Id != model.Id && (m.PackagName == model.PackagName)).Count();
if (date > 0)
{
throw new Exception("包装名称已存在!");
}
}
var packagItems = PackagRst.GetOneById(model.Id);
packagItems.Id = model.Id; //id
packagItems.PackagNo = model.PackagNo; //包装编号
packagItems.PackagName = model.PackagName; //包装描述
//packagItems.IsDel = model.IsDel; 原因: 修改时不需要修改IsDel字段
packagItems.UpdateTime = DateTime.Now; //修改时间
packagItems.UpdateUser = model.CreateUser; //修改人
packagItems.Level = 0;
//判断五级包装是否为空
if (!string.IsNullOrEmpty(model.L5Num) && !string.IsNullOrEmpty(model.L5Name))
{
packagItems.L5Num = int.Parse(model.L5Num);
packagItems.L5Name = model.L5Name;
packagItems.Level += 1;
//判断四级包装是否为空
if (string.IsNullOrEmpty(model.L4Num) && string.IsNullOrEmpty(model.L4Name))
{
return 2;
}
}
else
{
packagItems.L5Num = null;
packagItems.L5Name = null;
}
//判断四级包装是否为空
if (!string.IsNullOrEmpty(model.L4Num) && !string.IsNullOrEmpty(model.L4Name))
{
packagItems.L4Num = int.Parse(model.L4Num);
packagItems.L4Name = model.L4Name;
packagItems.Level += 1;
//判断三级包装是否为空
if (string.IsNullOrEmpty(model.L3Num) && string.IsNullOrEmpty(model.L3Name))
{
return 2;
}
}
else
{
packagItems.L4Num = null;
packagItems.L4Name = null;
}
//判断三级包装是否为空
if (!string.IsNullOrEmpty(model.L3Num) && !string.IsNullOrEmpty(model.L3Name))
{
packagItems.L3Num = int.Parse(model.L3Num);
packagItems.L3Name = model.L3Name;
packagItems.Level += 1;
//判断二级包装是否为空
if (string.IsNullOrEmpty(model.L2Num) && string.IsNullOrEmpty(model.L2Name))
{
return 2;
}
}
else
{
packagItems.L3Num = null;
packagItems.L3Name = null;
}
//判断二级包装是否为空
if (!string.IsNullOrEmpty(model.L2Num) && !string.IsNullOrEmpty(model.L2Name))
{
packagItems.L2Num = int.Parse(model.L2Num);
packagItems.L2Name = model.L2Name;
packagItems.Level += 1;
//判断一级包装是否为空
if (string.IsNullOrEmpty(model.L1Num) && string.IsNullOrEmpty(model.L1Name))
{
return 2;
}
}
else
{
packagItems.L3Num = null;
packagItems.L3Name = null;
}
//判断一级包装是否为空
if (!string.IsNullOrEmpty(model.L1Num) && !string.IsNullOrEmpty(model.L1Name))
{
packagItems.L1Num = int.Parse(model.L1Num);
packagItems.L1Name = model.L1Name;
packagItems.Level += 1;
}
else
{
packagItems.L1Num = null;
packagItems.L1Name = null;
return 2;
}
var rowCount = PackagRst.Edit(packagItems);
return rowCount;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
///
/// 删除包装信息
///
/// 包装信息主键ID
/// true:成功 flase:失败
public bool DelPackag(int packagId, int userId)
{
try
{
var packagItems = PackagRst.GetOneById(packagId);
if (packagItems == null)
{
throw new Exception("包装信息不存在!");
}
packagItems.IsDel = "1";
packagItems.UpdateTime = DateTime.Now;
packagItems.UpdateUser = userId;
var rowCount = PackagRst.Edit(packagItems);
return rowCount > 0;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
}
}