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.Condition.SysSettings;
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(IRegionService), InjectType.Scope)]
public class RegionService : ServiceBase, IRegionService
{
///
/// 系统操作仓储中转
///
private IDataRepositoryContext _dataContext;
///
/// SQL节点仓储
///
private ISQLNodeRepository _iSQLNodeRepository;
///
/// 配置
///
private IConfiguration _configuration;
public RegionService(IDataRepositoryContext dataRepositoryContext, IConfiguration configuration, ISQLNodeRepository iSQLNodeRepository)
{
this._dataContext = dataRepositoryContext;
this._configuration = configuration;
this._iSQLNodeRepository = iSQLNodeRepository;
}
public OperateResultInfo> GetList(RegionSearchCondition info)
{
var sqlAndBuilder = new StringBuilder();
var sqlOrBuilder = new StringBuilder();
var sql = $@"SELECT
CreateName = (SELECT USER_NAME FROM SYS_USER A WHERE A.USER_ID=CREATE_BY),
UpdateName = (SELECT USER_NAME FROM SYS_USER B WHERE B.USER_ID=UPDATE_BY),
UsedFlagName = {_iSQLNodeRepository.GetEnumIntCaseString("USED_FLAG")},
{info.ItemSQL} FROM BAS_REGION WHERE 1=1 AND DEL_FLAG = 0 ";
sqlAndBuilder = info.Id != null ?
info.Id > 0 ?
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("REGION_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.RegionCode) ?
sqlAndBuilder :
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("REGION_CODE", info.RegionCode, DBOperationString._ContainIn));
sqlAndBuilder = string.IsNullOrWhiteSpace(info.RegionName) ?
sqlAndBuilder :
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("REGION_NAME", info.RegionName, 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 Add(RegionCondition info)
{
if (info == null)
{
return FailStatus();
}
if (string.IsNullOrWhiteSpace(info.RegionCode) && string.IsNullOrWhiteSpace(info.AreaCode) && string.IsNullOrWhiteSpace(info.WarehouseCode))
{
return FailMessageStatus("参数错误!");
}
if (new DataRepository(_dataContext).Query().Where(m => m.REGION_CODE == info.RegionCode
&& m.DEL_FLAG == 0 && m.USED_FLAG == 1).Any())
{
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("请选择正确可使用的库区编码!");
}
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_REGION
{
CREATE_BY = info.OperationUserId,
CREATE_TIME = now,
UPDATE_BY = info.OperationUserId,
UPDATE_TIME = now,
REGION_CODE = info.RegionCode,
REGION_NAME = info.RegionName,
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,
};
var affectedRows = new DataRepository(_dataContext).Add(entity, new string[1] { "REGION_ID" });
return GetStatus(affectedRows);
}
public OperateResultInfo Deleted(RegionCondition info)
{
var sqlAndBuilder = new StringBuilder();
sqlAndBuilder = info.Id != null ?
info.Id > 0 ?
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("REGION_ID", info.Id, DBOperationString._Equal)) :
sqlAndBuilder : sqlAndBuilder;
sqlAndBuilder = string.IsNullOrWhiteSpace(info.RegionCode) ?
sqlAndBuilder :
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("REGION_CODE", info.RegionCode, DBOperationString._Equal));
sqlAndBuilder = string.IsNullOrWhiteSpace(info.Ids) ?
sqlAndBuilder :
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("REGION_ID", info.Ids, DBOperationString._In));
if (sqlAndBuilder.Length == 0)
{
return FailMessageStatus("参数错误!");
}
var now = DateTime.Now;
var sql = $@"UPDATE BAS_REGION 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(RegionCondition info)
{
if (string.IsNullOrWhiteSpace(info.RegionCode) && info.Id == null)
{
return FailMessageStatus("参数错误!");
}
var whereList = new List()
.AddFieldKeyInfo(nameof(BAS_REGION.REGION_CODE), info.RegionCode, EnumCSharpPropertyType.STRING, DBOperationString._Equal,
string.IsNullOrWhiteSpace(info.RegionCode))
.AddFieldKeyInfo(nameof(BAS_REGION.REGION_ID), info.Id, EnumCSharpPropertyType.INT, DBOperationString._Equal,
info.Id != null);
var result = new DataRepository(_dataContext).Query(whereList).FirstOrDefault();
if (result != null)
{
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("请选择正确可使用的库区编码!");
}
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;
result.DESCRIBE = info.Describe;
result.REGION_ID = result.REGION_ID;
result.REGION_CODE = info.RegionCode;
result.REGION_NAME = info.RegionName;
result.AREA_CODE = info.AreaCode;
result.AREA_NAME = areaData.AREA_NAME;
result.WAREHOUSE_CODE = info.WarehouseCode;
result.WAREHOUSE_NAME = WarehouseData.WAREHOUSE_NAME;
result.UPDATE_BY = info.OperationUserId;
result.UPDATE_TIME = now;
result.USED_FLAG = info.IsUsed ? 1 : 0;
var affectedRows = new DataRepository(_dataContext).Update(result, "REGION_ID", "NEWID");
return GetStatus(affectedRows);
}
return FailMessageStatus("未查找到数据!");
}
public OperateResultInfo Remove(RegionCondition info)
{
if (string.IsNullOrWhiteSpace(info.RegionCode) && info.Id == null)
{
return FailMessageStatus("参数错误!");
}
var whereList = new List()
.AddFieldKeyInfo(nameof(BAS_REGION.REGION_CODE), info.RegionCode, EnumCSharpPropertyType.STRING, DBOperationString._Equal,
!string.IsNullOrWhiteSpace(info.RegionCode))
.AddFieldKeyInfo(nameof(BAS_REGION.REGION_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("REGION_ID", result.REGION_ID.ToString());
return GetStatus(affectedRows);
}
return FailMessageStatus("未查找到数据!");
}
}
}