using DapperORMCore.Context.DataContext;
using DapperORMCore.Model.BaseModel;
using DapperORMCore.Model.CoreModel;
using DapperORMCore.Repository.IRepositorys;
using DapperORMCore.String.Consts;
using DapperORMCore.String.Enums;
using Microsoft.Extensions.Configuration;
using NXWMS.Code;
using NXWMS.DataAccess.Entity;
using NXWMS.IService.NXWMS.Base;
using NXWMS.Model.AppModels.Condition.Base;
using NXWMS.Model.AppModels.Result.Base;
using NXWMS.Model.Common;
using NXWMS.String.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WestDistance.DapperORM.Repository.Repositorys;
namespace NXWMS.Service.NXWMS.Base
{
///
/// 库位服务
///
[AutoInject(typeof(IBinService), InjectType.Scope)]
public class BinService : ServiceBase, IBinService
{
///
/// 系统操作仓储中转
///
private IDataRepositoryContext _dataContext;
///
/// SQL节点仓储
///
private ISQLNodeRepository _iSQLNodeRepository;
///
/// 配置
///
private IConfiguration _configuration;
public BinService(IDataRepositoryContext dataRepositoryContext, IConfiguration configuration, ISQLNodeRepository iSQLNodeRepository)
{
this._dataContext = dataRepositoryContext;
this._configuration = configuration;
this._iSQLNodeRepository = iSQLNodeRepository;
}
public OperateResultInfo Add(BinCondition info)
{
if (info == null)
{
return FailStatus();
}
if (string.IsNullOrWhiteSpace(info.RegionCode) || string.IsNullOrWhiteSpace(info.BinType) || string.IsNullOrWhiteSpace(info.AreaCode) ||
string.IsNullOrWhiteSpace(info.WarehouseCode) || string.IsNullOrWhiteSpace(info.RegionCode) || string.IsNullOrWhiteSpace(info.ShelfCode) ||
info.BinRow == null || info.BinColumn == null || info.BinLayer == null)
{
return FailMessageStatus("参数错误!");
}
var regionData = new DataRepository(_dataContext).Query().Where(m => m.REGION_CODE == info.RegionCode
&& m.DEL_FLAG == 0 && m.USED_FLAG == 1).FirstOrDefault();
if (regionData == null)
{
return FailMessageStatus("库区编码错误!");
}
var areaData = new DataRepository(_dataContext).Query().Where(m => m.AREA_CODE == info.AreaCode
&& m.DEL_FLAG == 0 && m.USED_FLAG == 1).FirstOrDefault();
if (areaData == null)
{
return FailMessageStatus("请选择正确可使用的库区编码!");
}
//TODO货架排录入界面没做,暂时注释
//var shelfData = new DataRepository(_dataContext).Query().Where(m => m.SHELF_CODE == info.ShelfCode
// && m.DEL_FLAG == 0 && m.USED_FLAG == 1).FirstOrDefault();
//if (shelfData == null)
//{
// return FailMessageStatus("请选择正确可使用的货架排!");
//}
var WarehouseData = new DataRepository(_dataContext).Query().Where(m => m.WAREHOUSE_CODE == info.WarehouseCode
&& m.DEL_FLAG == 0 && m.USED_FLAG == 1).FirstOrDefault();
if (WarehouseData == null)
{
return FailMessageStatus("请选择正确可使用的仓库编码!");
}
var now = DateTime.Now;
var entity = new BAS_BIN
{
CREATE_BY = info.OperationUserId,
CREATE_TIME = now,
UPDATE_BY = info.OperationUserId,
UPDATE_TIME = now,
REGION_CODE = info.RegionCode,
REGION_NAME = regionData.REGION_NAME,
AREA_CODE = info.AreaCode,
AREA_NAME = areaData.AREA_NAME,
WAREHOUSE_CODE = info.WarehouseCode,
WAREHOUSE_NAME = WarehouseData.WAREHOUSE_NAME,
DESCRIBE = info.Describe,
USED_FLAG = Convert.ToInt32(info.IsUsed),
DEL_FLAG = 0,
DOWN_SHELF_ORDER = info.DownShelfOrder,
BIN_CODE = info.BinCode,
BIN_COLUMN = info.BinColumn.Value,
BIN_LAYER = info.BinLayer.Value,
BIN_NAME = info.BinName,
BLEND_BATCH_FLAG = Convert.ToInt32(info.IsBlendBatch),
BIN_ROW = info.BinRow.Value,
BLEND_PRODUCT_FLAG = Convert.ToInt32(info.IsBlendProduct),
PUT_SHELF_ORDER = info.PutShelfOrder,
SHELF_CODE = info.ShelfCode,
BIN_TYPE = info.BinType,
WIDTH = info.Wide,
HEIGHT = info.Height,
LENGTH = info.Length,
NUMBER_LIMIT = info.NumberLimit,
SHELF_NAME = "", //shelfData.SHELF_NAME, //TODO 货架排录入数据没做,暂时放空
TRAR_LIMIT = info.TrarLimit,
VOLUME_LIMIT = info.VolumnLimit,
WEIGHT_LIMIT = info.WeightLimit,
};
var affectedRows = new DataRepository(_dataContext).Add(entity, new string[1] { "BIN_ID" });
return GetStatus(affectedRows);
}
public OperateResultInfo Deleted(BinCondition info)
{
var sqlAndBuilder = new StringBuilder();
sqlAndBuilder = info.Id != null ?
info.Id > 0 ?
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("BIN_ID", info.Id, DBOperationString._Equal)) :
sqlAndBuilder : sqlAndBuilder;
sqlAndBuilder = string.IsNullOrWhiteSpace(info.BinCode) ?
sqlAndBuilder :
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("BIN_CODE", info.BinCode, DBOperationString._Equal));
sqlAndBuilder = string.IsNullOrWhiteSpace(info.Ids) ?
sqlAndBuilder :
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("BIN_ID", info.Ids, DBOperationString._In));
if (sqlAndBuilder.Length == 0)
{
return FailMessageStatus("参数错误!");
}
var now = DateTime.Now;
var sql = $@"UPDATE BAS_BIN SET DEL_FLAG=1,UPDATE_BY={info.OperationUserId},UPDATE_TIME='{now}'
WHERE 1=1 {sqlAndBuilder}";
var affectedRows = new DataRepository(_dataContext).Execute(sql);
return GetStatus(affectedRows, info.Ids.Split(',').Length);
}
public OperateResultInfo Edit(BinCondition info)
{
if (info == null)
{
return FailStatus();
}
if (string.IsNullOrWhiteSpace(info.RegionCode) || string.IsNullOrWhiteSpace(info.BinType) || string.IsNullOrWhiteSpace(info.AreaCode) ||
string.IsNullOrWhiteSpace(info.WarehouseCode) || string.IsNullOrWhiteSpace(info.RegionCode) || string.IsNullOrWhiteSpace(info.ShelfCode) ||
info.BinRow == null || info.BinColumn == null || info.BinLayer == null)
{
return FailMessageStatus("参数错误!");
}
var regionData = new DataRepository(_dataContext).Query().Where(m => m.REGION_CODE == info.RegionCode
&& m.DEL_FLAG == 0 && m.USED_FLAG == 1).FirstOrDefault();
if (regionData == null)
{
return FailMessageStatus("库位编码已经存在,请输入其它编码!");
}
var areaData = new DataRepository(_dataContext).Query().Where(m => m.AREA_CODE == info.AreaCode
&& m.DEL_FLAG == 0 && m.USED_FLAG == 1).FirstOrDefault();
if (areaData == null)
{
return FailMessageStatus("请选择正确可使用的库区编码!");
}
////TODO货架排录入界面没做,暂时注释
//var shelfData = new DataRepository(_dataContext).Query().Where(m => m.SHELF_CODE == info.ShelfCode
// && m.DEL_FLAG == 0 && m.USED_FLAG == 1).FirstOrDefault();
//if (shelfData == null)
//{
// return FailMessageStatus("请选择正确可使用的货架排!");
//}
var WarehouseData = new DataRepository(_dataContext).Query().Where(m => m.WAREHOUSE_CODE == info.WarehouseCode
&& m.DEL_FLAG == 0 && m.USED_FLAG == 1).FirstOrDefault();
if (WarehouseData == null)
{
return FailMessageStatus("请选择正确可使用的仓库编码!");
}
var whereList = new List()
.AddFieldKeyInfo(nameof(BAS_BIN.BIN_CODE), info.BinCode, EnumCSharpPropertyType.STRING, DBOperationString._Equal,
string.IsNullOrWhiteSpace(info.BinCode))
.AddFieldKeyInfo(nameof(BAS_BIN.BIN_ID), info.Id, EnumCSharpPropertyType.INT, DBOperationString._Equal,
info.Id != null);
var result = new DataRepository(_dataContext).Query(whereList).FirstOrDefault();
if (result != null)
{
var now = DateTime.Now;
result.DESCRIBE = info.Describe;
result.BIN_ID = result.BIN_ID;
result.REGION_CODE = info.RegionCode;
result.REGION_NAME = regionData.REGION_NAME;
result.AREA_CODE = info.AreaCode;
result.AREA_NAME = areaData.AREA_NAME;
result.WAREHOUSE_CODE = info.WarehouseCode;
result.WAREHOUSE_NAME = WarehouseData.WAREHOUSE_NAME;
result.DOWN_SHELF_ORDER = info.DownShelfOrder;
result.BIN_CODE = info.BinCode;
result.BIN_COLUMN = info.BinColumn.Value;
result.BIN_LAYER = info.BinLayer.Value;
result.BIN_NAME = info.BinName;
result.BLEND_BATCH_FLAG = Convert.ToInt32(info.IsBlendBatch);
result.BIN_ROW = info.BinRow.Value;
result.BLEND_PRODUCT_FLAG = Convert.ToInt32(info.IsBlendProduct);
result.PUT_SHELF_ORDER = info.PutShelfOrder;
result.SHELF_CODE = info.ShelfCode;
result.BIN_TYPE = info.BinType;
result.WIDTH = info.Wide;
result.HEIGHT = info.Height;
result.LENGTH = info.Length;
result.NUMBER_LIMIT = info.NumberLimit;
result.SHELF_NAME = ""; //shelfData.SHELF_NAME; //shelfData.SHELF_NAME, //TODO 货架排录入数据没做,暂时放空
result.TRAR_LIMIT = info.TrarLimit;
result.VOLUME_LIMIT = info.VolumnLimit;
result.WEIGHT_LIMIT = info.WeightLimit;
result.UPDATE_BY = info.OperationUserId;
result.UPDATE_TIME = now;
result.USED_FLAG = info.IsUsed ? 1 : 0;
var affectedRows = new DataRepository(_dataContext).Update(result, "BIN_ID", "NEWID");
return GetStatus(affectedRows);
}
return FailMessageStatus("未查找到数据!");
}
public OperateResultInfo> GetList(BinSearchCondition info)
{
var sqlAndBuilder = new StringBuilder();
var sqlOrBuilder = new StringBuilder();
if (info.PageIndex == 0 || info.PageSize == 0)
{
return FailMessageStatus("请传递分页码和分页个数!",
new PageQueryResultInfo());
}
var sql = $@"SELECT
CreateName = (SELECT A.USER_NAME FROM SYS_USER A WHERE A.USER_ID=CREATE_BY),
UpdateName = (SELECT B.USER_NAME FROM SYS_USER B WHERE B.USER_ID=UPDATE_BY),
UsedFlagName = {_iSQLNodeRepository.GetEnumIntCaseString("USED_FLAG")},
{info.ItemSQL} FROM BAS_BIN WHERE 1=1 AND DEL_FLAG = 0 ";
sqlAndBuilder = info.Id != null ?
info.Id > 0 ?
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("BIN_ID", info.Id, DBOperationString._Equal)) :
sqlAndBuilder : sqlAndBuilder;
sqlAndBuilder = info.IsUsed == null ?
sqlAndBuilder :
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("USED_FLAG", info.IsUsed, DBOperationString._Equal));
sqlAndBuilder = string.IsNullOrWhiteSpace(info.BinCode) ?
sqlAndBuilder :
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("BIN_CODE", info.BinCode, DBOperationString._ContainIn));
sqlAndBuilder = info.BinColumn > 0 ?
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("BIN_COLUMN", info.BinColumn, DBOperationString._ContainIn)) :
sqlAndBuilder;
sqlAndBuilder = info.BinLayer > 0 ?
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("BIN_LAYER", info.BinLayer, DBOperationString._ContainIn)) :
sqlAndBuilder;
sqlAndBuilder = info.BinRow > 0 ?
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("BIN_ROW", info.BinRow, DBOperationString._ContainIn)) :
sqlAndBuilder;
sqlAndBuilder = string.IsNullOrWhiteSpace(info.BinType) ?
sqlAndBuilder :
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("BIN_TYPE", info.BinType, DBOperationString._ContainIn));
sqlAndBuilder = string.IsNullOrWhiteSpace(info.RegionCode) ?
sqlAndBuilder :
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("REGION_CODE", info.RegionCode, DBOperationString._ContainIn));
sql = sql + (sqlAndBuilder.Length > 0 ? _iSQLNodeRepository.GetAndString(sqlAndBuilder, false) : "");
IEnumerable result;
IEnumerable totalResult;
totalResult = new DataRepository(_dataContext).Query(sql);
if (info.PageIndex == 0 || info.PageSize == 0)
{
result = totalResult.ToList();
}
else
{
result = new DataRepository(_dataContext).QueryPage(sql,
"CREATE_TIME", info.PageSize, info.PageIndex, true);
}
return SuccessStatus(new PageQueryResultInfo
{
RowData = result,
PageConditionInfo = info,
TotalCount = totalResult.Count(),
TotalPageCount = (int)Math.Ceiling((double)totalResult.Count() / info.PageSize)
});
}
public OperateResultInfo Remove(BinCondition info)
{
if (info == null)
{
return FailStatus();
}
if (string.IsNullOrWhiteSpace(info.RegionCode) || string.IsNullOrWhiteSpace(info.BinType) || string.IsNullOrWhiteSpace(info.AreaCode) ||
string.IsNullOrWhiteSpace(info.WarehouseCode) || string.IsNullOrWhiteSpace(info.RegionCode) || string.IsNullOrWhiteSpace(info.ShelfCode) ||
info.BinRow == null || info.BinColumn == null || info.BinLayer == null)
{
return FailMessageStatus("参数错误!");
}
var whereList = new List()
.AddFieldKeyInfo(nameof(BAS_BIN.BIN_CODE), info.BinCode, EnumCSharpPropertyType.STRING, DBOperationString._Equal,
string.IsNullOrWhiteSpace(info.BinCode))
.AddFieldKeyInfo(nameof(BAS_BIN.BIN_ID), info.Id, EnumCSharpPropertyType.INT, DBOperationString._Equal,
info.Id != null);
var result = new DataRepository(_dataContext).Query(whereList).FirstOrDefault();
if (result != null)
{
var affectedRows = new DataRepository(_dataContext).Remove("BIN_ID", result.BIN_ID.ToString());
return GetStatus(affectedRows);
}
return FailMessageStatus("未查找到数据!");
}
}
}