using Commom.Utility;
using Common;
using Model;
using Org.BouncyCastle.Bcpg.OpenPgp;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
namespace BLL.DAL
{
public class DALCheckTask
{
///
/// 加载盘库任务明细
///
/// 查询条件
/// 分页信息
///
public IList GetList(CheckTask checkTask, ref PageInfo page)
{
try
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("select * from View_CheckTask_Userinfo where IsDel = 0 " + $" AND DepartGuid='{checkTask.DepartGuid}' ");
if (!string.IsNullOrEmpty(checkTask.CDNo))
{
stringBuilder.Append("and CDNO like '%" + checkTask.CDNo + "%' ");
}
if (!string.IsNullOrEmpty(checkTask.LocationCode))
{
stringBuilder.Append("and (LocationCode1 like '%" + checkTask.LocationCode + "%'" +
"or LocationCode2 like '%" + checkTask.LocationCode + "%') ");
}
if (!string.IsNullOrEmpty(checkTask.Palno))
{
stringBuilder.Append("and (Palno1 like '%" + checkTask.Palno + "%' or " +
"Palno1 like '%" + checkTask.Palno + "%') ");
}
if (!string.IsNullOrEmpty(checkTask.Statu))
{
stringBuilder.Append("and StatuId = " + checkTask.Statu.AddQuotes());
}
SqlParam[] para = null;
DataTable dt = DataFactory.SqlDataBase().GetPageList(stringBuilder.ToString(), para, "ID", "ASC", ref page);
IList list = ModelConvertHelper.DataTableToModel(dt);
return list;
}
catch
{
throw new NotImplementedException();
}
}
///
/// 获取托盘内物料明细信息
///
///
///
public DataTable GetCheckTask(string TaskID)
{
try
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("select * from CheckTask where Guid = " + TaskID.AddQuotes());
DataTable dt = DataFactory.SqlDataBase().GetDataTableBySQL(stringBuilder);
stringBuilder.Clear();
stringBuilder.Append("select '" + TaskID + "' as TaskID,unit as UnitFrist, * from CheckDataDetail where IsDel = 0 ");
stringBuilder.Append(" and CDNO = " + dt.Rows[0]["CDNO"].ToString().AddQuotes());
stringBuilder.Append(" and LocationCode in (" + dt.Rows[0]["LocationCode1"].ToString().AddQuotes() + ",");
stringBuilder.Append(dt.Rows[0]["LocationCode2"].ToString().AddQuotes() + ") ");
stringBuilder.Append("and Palno in (" + dt.Rows[0]["Palno1"].ToString().AddQuotes() + ",");
stringBuilder.Append(dt.Rows[0]["Palno2"].ToString().AddQuotes() + ") ");
DataTable dtItems = DataFactory.SqlDataBase().GetDataTableBySQL(stringBuilder);
return dtItems;
}
catch
{
return null;
}
}
///
/// 实时保存物料实际数量
///
///
///
///
public bool SetItems(string LoginUserCode, CheckMat Mats)
{
bool result = false;
try
{
StringBuilder sqlstr = new StringBuilder();
int rowCount = 0;
Hashtable ht = new Hashtable();
ht["AQuant"] = Mats.AQuant.AddQuotes();
ht["CheckUser"] = LoginUserCode.AddQuotes();
ht["UpdateTime"] = "convert(varchar(20),getdate(),120)";
rowCount += DataFactory.SqlDataBase().UpdateByHashtable("CheckDataDetail",
"Guid", Mats.Guid.AddQuotes(), ht);
if (rowCount > 0)
{
result = true;
}
return result;
}
catch
{
return result;
}
}
///
/// 自动生成盘库指令
///
///
///
public bool AutoOrders(string LoginUserCode)
{
bool result = false;
try
{
StringBuilder stringBuilder = new StringBuilder();
// 查询WH_CMD表 确认是否存在盘库单待执行的指令
stringBuilder.Append("select count(CMDID) from WH_CMD where NumberNo like 'CTNO%' and CMDStatu = 0 and IsDel = 0;");
DataRow row = DataFactory.SqlDataBase().GetDataRowBySQL(stringBuilder);
// 不存在
if (row[0].ToString() == "0")
{
stringBuilder.Clear();
stringBuilder.Append("select top 1 * from CheckTask where statu = '01' and IsDel = 0 order by ID asc");
DataRow taskRow = DataFactory.SqlDataBase().GetDataRowBySQL(stringBuilder);
if (taskRow != null && taskRow["guid"].ToString() != "")
{
result = this.CreateOrder(taskRow["guid"].ToString(), LoginUserCode);
}
}
else
{
result = false;
}
}
catch
{
return result;
}
return result;
}
///
/// 生成盘库指令
///
///
///
///
public bool CreateOrder(string TaskId, string LoginUserCode)
{
bool result = false;
try
{
// 获取任务明细
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append($@"
SELECT ct.*,
cd.AccessCode,
d.TypeName AccessName
FROM dbo.CheckTask ct
JOIN dbo.CheckData cd
ON cd.CDNO = ct.CDNO
LEFT OUTER JOIN dbo.Dictionary d
ON cd.AccessCode=d.Code AND d.TopCode='Access'
WHERE ct.statu = '01'
AND ct.guid = '{TaskId}';");
DataTable dt = DataFactory.SqlDataBase().GetDataTableBySQL(stringBuilder);
if (dt != null && dt.Rows.Count > 0)
{
DALWMSApi dalWMSApi = new DALWMSApi();
foreach (DataRow drTask in dt.Rows)
{
string locationCode = drTask["LocationCode1"].ToString();
string palNo = drTask["Palno1"].ToString();
stringBuilder.Clear();
stringBuilder.Append($"SELECT TurnoverDemand FROM dbo.DepotsLocation WHERE LocationCode='{locationCode}' AND IsDel=0");
DataTable dtDL = DataFactory.SqlDataBase().GetDataTableBySQL(stringBuilder);
if (dtDL != null && dtDL.Rows.Count >= 0)
{
string turnoverDemand = dtDL.Rows[0]["TurnoverDemand"].ToString();
switch (turnoverDemand)
{
case "02":
{
dalWMSApi.Send(palNo, locationCode, drTask["AccessCode"].ToString());
stringBuilder.Clear();
stringBuilder.Append($"UPDATE dbo.DepotsLocation SET TurnoverDemand='04' WHERE LocationCode='{locationCode}'");
DataFactory.SqlDataBase().ExecuteBySql(stringBuilder);
break;
}
case "03":
{
var resultApi = dalWMSApi.OutStorage(palNo, locationCode);
if (resultApi.Code != "01")
{
return false;
}
break;
}
default:
break;
}
}
}
// 更改盘点任务数据状态
Hashtable htt = new Hashtable();
htt["statu"] = "'02'";
htt["CheckUser"] = LoginUserCode.AddQuotes();
htt["CheckTime"] = "convert(varchar(20),getdate(),120)";
DataFactory.SqlDataBase().UpdateByHashtable("CheckTask",
"Guid", TaskId.AddQuotes(), htt);
return true;
}
#region 原方法,注释
/*
if (dt.Rows.Count > 0)
{
DataRow row = dt.Rows[0];
Hashtable ht = new Hashtable();
ht["TaskID"] = TaskId.AddQuotes();
ht["CMDStatu"] = 0;
ht["CreateUser"] = LoginUserCode.AddQuotes();
ht["NumberNo"] = row["CDNO"].ToString().AddQuotes();
if (row["LocationCode2"].ToString() != "") // 外货位是否有盘库任务
{
// 生成外货位指令
ht["Palno"] = row["Palno2"].ToString().AddQuotes();
ht["CMDType"] = "'out'";
ht["OldAddre"] = row["LocationCode2"].ToString().AddQuotes();
DataFactory.SqlDataBase().InsertByHashtableNullParam("WH_CMD", ht);
// 收到WCS出库成功回执后 将货位状态设置为待入库状态 占住此位置
if (row["LocationCode1"].ToString() != "") // 判断内货位是否有盘库任务
{
// 生成内货位出库指令
ht["Palno"] = row["Palno1"].ToString().AddQuotes();
ht["CMDType"] = "'out'";
ht["OldAddre"] = row["LocationCode1"].ToString().AddQuotes();
DataFactory.SqlDataBase().InsertByHashtableNullParam("WH_CMD", ht);
// 生成内货位回库记录 -- 不主动下发wcs用来接收到申请货位请求时返回货位地址
ht["CMDType"] = "'to'";
DataFactory.SqlDataBase().InsertByHashtableNullParam("WH_CMD", ht);
}
// 生成外货位回库记录 -- 不主动下发wcs用来接收到申请货位请求时返回货位地址
ht["Palno"] = row["Palno2"].ToString().AddQuotes();
ht["CMDType"] = "'to'";
ht["OldAddre"] = row["LocationCode2"].ToString().AddQuotes();
DataFactory.SqlDataBase().InsertByHashtableNullParam("WH_CMD", ht);
// 更改盘点任务数据状态
Hashtable htt = new Hashtable();
htt["statu"] = "'02'";
htt["CheckUser"] = LoginUserCode.AddQuotes();
htt["CheckTime"] = "convert(varchar(20),getdate(),120)";
DataFactory.SqlDataBase().UpdateByHashtable("CheckTask",
"Guid", TaskId.AddQuotes(), htt);
return true;
}
else if (row["LocationCode1"].ToString() != "") // 内货位是否有出库任务
{
// 获取内货位对应的外货位是否存在托盘
DataRow dttRow = this.GetlLocationItem(row["LocationCode1"].ToString());
string newLocation = string.Empty;
if (dttRow != null)
{
// 存在托盘 先生成外货位移库命令
newLocation = this.GetNewLocation(dttRow["Addre"].ToString());
ht["Palno"] = dttRow["Palno"].ToString().AddQuotes();
ht["OldAddre"] = dttRow["Addre"].ToString().AddQuotes();
ht["NowAddre"] = newLocation.AddQuotes();
// 如果没有空余货位,直接出库
if (newLocation == "")
{
ht["CMDType"] = "'out'";
}
else
{
ht["CMDType"] = "'move'";
}
DataFactory.SqlDataBase().InsertByHashtableNullParam("WH_CMD", ht);
}
// 生成内货位出库命令
ht["Palno"] = row["Palno1"].ToString().AddQuotes();
ht["CMDType"] = "'out'";
ht["OldAddre"] = row["LocationCode1"].ToString().AddQuotes();
ht["NowAddre"] = "''";
DataFactory.SqlDataBase().InsertByHashtableNullParam("WH_CMD", ht);
// 生成内货位回库命令 -- 不主动下发wcs,用来接收到申请货位请求时返回货位地址
ht["CMDType"] = "'to'";
DataFactory.SqlDataBase().InsertByHashtableNullParam("WH_CMD", ht);
if (dttRow != null)
{
// 生成移库货位回移指令 ,移库不需要回移暂时不删除以防有变。
ht["Palno"] = dttRow["Palno"].ToString().AddQuotes();
ht["CMDType"] = "'move'";
ht["NowAddre"] = dttRow["Addre"].ToString().AddQuotes();
ht["OldAddre"] = newLocation.AddQuotes();
// 移库回库
if (newLocation == "")
{
ht["CMDType"] = "'to'";
ht["NowAddre"] = newLocation.AddQuotes(); ;
ht["OldAddre"] = dttRow["Addre"].ToString().AddQuotes();
}
else
{
ht["CMDType"] = "'move'";
ht["NowAddre"] = dttRow["Addre"].ToString().AddQuotes();
ht["OldAddre"] = newLocation.AddQuotes();
}
DataFactory.SqlDataBase().InsertByHashtableNullParam("WH_CMD", ht);
}
// 更改盘库任务状态
Hashtable htt = new Hashtable();
htt["statu"] = "'02'";
htt["CheckUser"] = LoginUserCode.AddQuotes();
htt["CheckTime"] = "convert(varchar(20),getdate(),120)";
DataFactory.SqlDataBase().UpdateByHashtable("CheckTask",
"Guid", TaskId.AddQuotes(), htt);
return true;
}
}
*/
#endregion
return result;
}
catch
{
return result;
}
}
///
/// 获取指定货位地址的外货位地址
///
///
private DataRow GetlLocationItem(string Locationcode1)
{
string Locationcode2 = string.Empty;
if (Locationcode1.Contains("-001"))
{
Locationcode2 = Locationcode1.Replace("-001", "-002");
}
else
{
Locationcode2 = Locationcode1.Replace("-004", "-003");
}
StringBuilder stringBuilder = new StringBuilder(); // 一个货位地址只会对应一个托盘码
stringBuilder.Append("select top 1 * from log_Store where LocationCode = " + Locationcode2.AddQuotes());
DataTable dt = DataFactory.SqlDataBase().GetDataTableBySQL(stringBuilder);
if (dt.Rows.Count > 0)
{
return dt.Rows[0];
}
return null;
}
///
/// 获取移库空货位地址
///
/// 当前货位地址
/// 空货位地址
private string GetNewLocation(string Location)
{
string newLocation = ""; // 移库目标货位
int lRow = int.Parse(Location.Substring(3, 3));
int lColumn = int.Parse(Location.Substring(6, 3));
int lLayer = int.Parse(Location.Substring(9, 3));
// 获取外货架的所有空货位
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("select LocationCode,LRow,LColumn,LLayer from DepotsLocation ");
stringBuilder.Append(" where IsDel = 0 and TurnoverDemand = '01'");
stringBuilder.Append(" and Type >= (select Type from DepotsLocation where LocationCode = " + Location.AddQuotes() + ") ");
stringBuilder.Append(" and (LocationCode like '%-002%' or LocationCode like '%-003%') ");
stringBuilder.Append(" order by LocationCode; ");
DataTable dt = DataFactory.SqlDataBase().GetDataTableBySQL(stringBuilder);
if (dt != null)
{
int value = 600;
foreach (DataRow row in dt.Rows)
{
int dtrow = int.Parse(row["LRow"].ToString());
int dtColumn = int.Parse(row["LColumn"].ToString());
int dtLayer = int.Parse(row["LLayer"].ToString());
int number = 0;
int a = lRow - dtrow;
if (a < 0)
{
number += (0 - a);
}
else
{
number += a;
}
int b = lColumn - dtColumn;
if (b < 0)
{
number += (0 - b);
}
else
{
number += b;
}
int c = lLayer - dtLayer;
if (c < 0)
{
number += (0 - c);
}
else
{
number += c;
}
if (value > number)
{
value = number;
newLocation = row["LocationCode"].ToString();
}
}
return newLocation;
}
else
{
stringBuilder.Clear();
stringBuilder.Append("select LocationCode,LRow,LColumn,LLayer from DepotsLocation ");
stringBuilder.Append(" where IsDel = 0 and TurnoverDemand = '01'");
stringBuilder.Append(" and Type >= (select Type from DepotsLocation where LocationCode = " + Location.AddQuotes() + ") ");
stringBuilder.Append(" and (LocationCode like '%-001%' or LocationCode like '%-004%') ");
stringBuilder.Append(" order by LocationCode); ");
dt = DataFactory.SqlDataBase().GetDataTableBySQL(stringBuilder);
if (dt != null)
{
int value = 600;
foreach (DataRow row in dt.Rows)
{
int dtrow = int.Parse(row["LRow"].ToString());
int dtColumn = int.Parse(row["LColumn"].ToString());
int dtLayer = int.Parse(row["LLayer"].ToString());
int number = 0;
int a = lRow - dtrow;
if (a < 0)
{
number += (0 - a);
}
else
{
number += a;
}
int b = lColumn - dtColumn;
if (b < 0)
{
number += (0 - b);
}
else
{
number += b;
}
int c = lLayer - dtLayer;
if (c < 0)
{
number += (0 - c);
}
else
{
number += c;
}
if (value > number)
{
value = number;
newLocation = row["LocationCode"].ToString();
}
}
return newLocation;
}
}
return newLocation;
}
}
}