using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Model.ModelDto.SysDto;
using SqlSugar;
using WMS.Entity.SysEntity;
using WMS.IBLL.ISysServer;
using WMS.IDAL.ISysInterface;
namespace WMS.BLL.SysServer
{
public class StorageAreaServer: IStorageAreaServer
{
public IStorageAreaRepository AreaRst { get; set; }
public StorageAreaServer(IStorageAreaRepository areaRst)
{
AreaRst = areaRst;
}
///
/// 获取库区域信息
///
/// 区域名称
/// 仓库编号
/// 状态
/// 类别
///
///
///
///
public List GetStorageAreaList(string areaName, string wareHouseNo, string status, string type,int page, int limit,out int count)
{
try
{
//创建表达式
Expression> item = Expressionable.Create()
.AndIF(!string.IsNullOrWhiteSpace(areaName), it => it.AreaName.Contains(areaName.Trim()))
.AndIF(!string.IsNullOrWhiteSpace(wareHouseNo), it => it.WareHouseNo == wareHouseNo)
.AndIF(!string.IsNullOrWhiteSpace(status), it => it.Status == status)
.AndIF(!string.IsNullOrWhiteSpace(type), it => it.Type == type)
.ToExpression();//注意 这一句 不能少
var data = AreaRst.GetAllByOrderPageAsync(item, limit, page, out int counts)
.Includes(x => x.WareHouseInfo)
.Includes(x => x.TemperatureInfo)
.Includes(x => x.CreateUserInfo)
.Includes(x => x.UpdateUserInfo).ToList();
count = counts;
return data.Select(m => new AreaDto()
{
Id = m.Id,
AreaNo = m.AreaNo,
AreaName = m.AreaName,
WareHouseNo = m.WareHouseNo,
WareHouseName = m.WareHouseInfo == null ? "" : m.WareHouseInfo.WareHouseName,
RoadwayNo = m.RoadwayNo,
Status = m.Status,
Priority = m.Priority,
Type = m.Type,
TypeName = GetTypeName(m.Type),//类别名称
Temp = m.Temperature,
TempName = m.TemperatureInfo == null ? "" : m.TemperatureInfo.DictName,
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
///
public SysStorageArea GetStorageArea(int id)
{
try
{
var data = AreaRst.GetOneById(id);
return data;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
///
/// 获取区域信息(根据仓库号)
///
/// 仓库号
///
public List GetStorageAreaByHouseNo(string wareHouseNo)
{
try
{
var data = AreaRst.GetAllAsync().ToList();
if (!string.IsNullOrWhiteSpace(wareHouseNo))
{
data = data.Where(m => m.WareHouseNo == wareHouseNo).ToList();
}
return data;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
///
/// 修改库区域信息
///
/// ID
/// 区域名称
/// 优先级
/// 类别
/// 存储环境
/// 操作人
/// 是否成功
public bool EditStorageArea(int id, string name, int priority, string type, string temp, int userId)
{
try
{
//判断除当前id信息以外是否含有编号和名称
var bol = AreaRst.GetAllAsync()
.Any(m => m.Id != id && m.AreaName == name.Trim());
if (bol)
{
throw new Exception("当前区域号或区域名称已存在");
}
var area = AreaRst.GetOneById(id);
if (area == null)
{
throw new Exception("为查询到当前区域信息");
}
area.AreaName = name;
area.Priority = priority;
area.Type = type;
area.Temperature = temp;
area.UpdateUser = userId;
area.UpdateTime = DateTime.Now;
var num = AreaRst.Edit(area);
if (num > 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
///
/// 修改区域状态
///
/// Id
/// 状态 0 启用 1停用
/// 操作人ID
///
public bool EditStorageAreaStatus(int id,string status,int userId)
{
try
{
var area = AreaRst.GetOneById(id);
if (area == null)
{
throw new Exception("为查询到当前区域信息");
}
area.Status = status;
area.UpdateUser = userId;
area.UpdateTime = DateTime.Now;
var num = AreaRst.EditStorageAreaStatus(area);
if (num > 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
///
/// 获取区域类型
///
/// 类别编号
///
public string GetTypeName(string code)
{
if (string.IsNullOrWhiteSpace(code))
{
return "";
}
else
{
var str = "";
switch (code)
{
case "1":
str = "成品区";
break;
case "2":
str = "原料区";
break;
case "3":
str = "包材区";
break;
case "4":
str = "空托区";
break;
default:
str = "";
break;
}
return str;
}
}
}
}