using DapperORMCore.Context.DataContext;
using DapperORMCore.Model.CoreModel;
using DapperORMCore.Repository.IRepositorys;
using Microsoft.Extensions.Configuration;
using NXWMS.IService.NXWMS.Inspection;
using NXWMS.Model.AppModels.Condition.Inspection;
using NXWMS.Model.AppModels.Result.Inspection;
using NXWMS.Model.AppModels.Result.Instock;
using NXWMS.Model.Common;
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.Inspection
{
///
/// 质检单服务
///
[AutoInject(typeof(IWmsQaInspectionService), InjectType.Scope)]
public class WmsQaInspectionService : ServiceBase, IWmsQaInspectionService
{
#region 全局变量、构造注入
///
/// 系统操作仓储中转
///
private IDataRepositoryContext _dataContext;
///
/// SQL节点仓储
///
private ISQLNodeRepository _iSQLNodeRepository;
///
/// 配置
///
private IConfiguration _configuration;
///
/// 构造注入
///
///
///
///
public WmsQaInspectionService(IDataRepositoryContext dataRepositoryContext, IConfiguration configuration, ISQLNodeRepository iSQLNodeRepository)
{
this._dataContext = dataRepositoryContext;
this._configuration = configuration;
this._iSQLNodeRepository = iSQLNodeRepository;
}
#endregion
///
/// 分页查询质检单主表数据
///
/// 质检单查询条件实体类对象
///
public OperateResultInfo> GetWmsQaInspectionListForPage(WmsQaInspectionSearchMd inspectionSearchMd)
{
try
{
#region SQL语句生成
StringBuilder sqlCondition = new StringBuilder();
if (!string.IsNullOrEmpty(inspectionSearchMd.InspectionNoMsg))
{
sqlCondition.Append($" AND INSPECTION_NO = '{inspectionSearchMd.InspectionNoMsg}'");
}
if (!string.IsNullOrEmpty(inspectionSearchMd.MaterielTypeMsg))
{
sqlCondition.Append($" AND INSPECTION_ID in (SELECT INSPECTION_ID FROM VW_WMS_QA_INSPECTION_DTL WHERE MATERIEL_TYPE_CODE = '{inspectionSearchMd.MaterielTypeMsg}')");
}
if (!string.IsNullOrEmpty(inspectionSearchMd.MaterielMsg))
{
sqlCondition.Append($" AND INSPECTION_ID IN (SELECT INSPECTION_ID FROM VW_WMS_QA_INSPECTION_DTL WHERE MATERIEL_CODE LIKE '%{inspectionSearchMd.MaterielMsg}%' OR MATERIEL_NAME LIKE '%{inspectionSearchMd.MaterielMsg}%' OR MATERIEL_BARCODE LIKE '%{inspectionSearchMd.MaterielMsg}%'");
}
if (!string.IsNullOrEmpty(inspectionSearchMd.InspectionStatusMsg))
{
sqlCondition.Append($" AND INSPECTION_STATUS = '{inspectionSearchMd.InspectionStatusMsg}'");
}
if (!string.IsNullOrEmpty(inspectionSearchMd.InspectionTypeMsg))
{
sqlCondition.Append($" AND INSPECTION_TYPE = '{inspectionSearchMd.InspectionTypeMsg}'");
}
if (!string.IsNullOrEmpty(inspectionSearchMd.InspectionResultMsg))
{
sqlCondition.Append($" AND INSPECTION_ID in (SELECT INSPECTION_ID FROM VW_WMS_QA_INSPECTION_DTL WHERE INSPECTION_RESULT = '{inspectionSearchMd.InspectionResultMsg}')");
}
if (!string.IsNullOrEmpty(inspectionSearchMd.BillNoMsg))
{
sqlCondition.Append($" AND INSPECTION_ID in (SELECT INSPECTION_ID FROM VW_WMS_QA_INSPECTION_DTL WHERE BILL_NO = '{inspectionSearchMd.BillNoMsg}')");
}
if (!string.IsNullOrEmpty(inspectionSearchMd.RegionNoMsg))
{
sqlCondition.Append($" AND INSPECTION_ID in (SELECT INSPECTION_ID FROM VW_WMS_QA_INSPECTION_DTL WHERE REGION_CODE = '{inspectionSearchMd.RegionNoMsg}')");
}
if (!string.IsNullOrEmpty(inspectionSearchMd.BinNoMsg))
{
sqlCondition.Append($" AND INSPECTION_ID in (SELECT INSPECTION_ID FROM VW_WMS_QA_INSPECTION_DTL WHERE BIN_CODE = '{inspectionSearchMd.BinNoMsg}')");
}
if (!string.IsNullOrEmpty(inspectionSearchMd.SupplierMsg))
{
sqlCondition.Append($" AND INSPECTION_ID IN (SELECT INSPECTION_ID FROM VW_WMS_QA_INSPECTION_DTL WHERE SUPPLIER_CODE LIKE '%{inspectionSearchMd.SupplierMsg}%' OR SUPPLIER_NAME LIKE '%{inspectionSearchMd.SupplierMsg}%')");
}
if (!string.IsNullOrEmpty(inspectionSearchMd.StartCreateMsg))
{
sqlCondition.Append($" AND CREATE_TIME >= '{inspectionSearchMd.StartCreateMsg}'");
}
if (!string.IsNullOrEmpty(inspectionSearchMd.EndCreateMsg))
{
sqlCondition.Append($" AND CREATE_TIME <= '{inspectionSearchMd.EndCreateMsg}'");
}
if (!string.IsNullOrEmpty(inspectionSearchMd.StartInspectionTimeMsg))
{
sqlCondition.Append($" AND INSPECTION_TIME >= '{inspectionSearchMd.StartInspectionTimeMsg}'");
}
if (!string.IsNullOrEmpty(inspectionSearchMd.EndInspectionTimeMsg))
{
sqlCondition.Append($" AND INSPECTION_TIME <= '{inspectionSearchMd.EndInspectionTimeMsg}'");
}
StringBuilder sqlCountInspectionData = new StringBuilder($@"SELECT COUNT(1) FROM VW_WMS_QA_INSPECTION WHERE 1=1");
sqlCountInspectionData.Append(sqlCondition.ToString());
int pageStartIndex = (inspectionSearchMd.PageNum - 1) * inspectionSearchMd.EveryPageQty;
int pageEndIndex = inspectionSearchMd.PageNum * inspectionSearchMd.EveryPageQty;
StringBuilder sqlQueryInspectionData = new StringBuilder($@"
SELECT
INSPECTION_ID,
INSPECTION_NO,
INSPECTION_TYPE,
INSPECTION_TYPE_NAME,
INSPECTION_WAY,
INSPECTION_WAY_NAME,
INSPECTION_USER,
INSPECTION_TIME,
INSPECTION_STATUS,
INSPECTION_STATUS_NAME,
DESCRIBE,
CREATE_BY,
CREATE_NAME,
CREATE_TIME,
UPDATE_BY,
UPDATE_NAME,
UPDATE_TIME
FROM
VW_WMS_QA_INSPECTION
WHERE
1=1
{sqlCondition}
ORDER BY UPDATE_TIME DESC,CREATE_TIME DESC
OFFSET {pageStartIndex} ROWS
FETCH NEXT {inspectionSearchMd.EveryPageQty} ROWS ONLY
");
#endregion
int dataCount = Convert.ToInt32(new DataRepository