using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DSWeb.Areas.CommMng.Models;
using HcUtility.Comm;
using DSWeb.MvcShipping.Models.WMSDeliver;
using DSWeb.Areas.CommMng.DAL;
using DSWeb.MvcShipping.Helper;
using DSWeb.MvcShipping.Models.MsStevedores;
using DSWeb.MvcShipping.DAL.MsSysParamSet;
using System.Data.SqlClient;
using DSWeb.DataAccess;
using DSWeb.EntityDA;
//Please add references
namespace DSWeb.MvcShipping.DAL.WMSStock
{
///
/// 数据访问类:WMSDeliver
///
public partial class WMSEIPStockDAL
{
public WMSEIPStockDAL()
{ }
#region BasicMethod
//static Database dbEIP = DatabaseFactory.CreateDatabase("EIP");
static public List GetDataList(string strCondition, int start, int limit, string userid, string username, string companyid, out int total, string sort = null)
{
/*
var rangstr = GetRangDAStr("index", userid, username, companyid);
if (!string.IsNullOrEmpty(rangstr))
{
if (!string.IsNullOrEmpty(strCondition))
{
strCondition = strCondition + " and " + rangstr;
}
else
{
strCondition = rangstr;
}
}
*/
var strSql = new StringBuilder();
strSql.Append(@"SELECT BSNO,STOCKUPNO,CUSTOMERNAME,SALECONTRACT,GOODSNAMETT,CORPID,
convert(char(10),STOCKUPDATE,20) AS STOCKUPDATE,
convert(char(10),ENTERDATE,20) AS ENTERDATE,
ISNULL(ISRECVED,0) AS ISRECVED,
CASE BSStatus WHEN '0' THEN '新建'
WHEN '1' THEN '待备货' WHEN '2' THEN '备货中'
WHEN '3' THEN '已备货' WHEN '4' THEN '已装箱'
ELSE '出库完成' END AS BSSTATUS,ENTEROPERATER,
REMARK,EXPORTNO
FROM EIP_StockUp where 1=1 AND BSStatus = '3' AND (IsRecved is null or IsRecved='0') ");
if (!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" and " + strCondition);
}
if (!string.IsNullOrEmpty(sort))
{
var so = JsonConvert.Deserialize>(sort);
string orderbyStr = " order by ";
foreach (var s in so)
{
orderbyStr += s.property + " " + s.direction + ",";
}
orderbyStr = orderbyStr.Substring(0, orderbyStr.Length - 1);
strSql.Append(orderbyStr);
}
else
{
strSql.Append(" order by STOCKUPNO DESC ");
}
int totalCount = 0;
StringBuilder sb = PagerHelperEIP.PageSQL(strSql.ToString(), start, limit, out totalCount);
total = totalCount;
return SetData(sb);
}
///
/// 执行sql
///
///
///
private static List SetData(StringBuilder strSql)
{
#region 公共代码
var headList = new List();
Database dbEIP = DatabaseFactory.CreateDatabase("EIP");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = strSql.ToString();
cmd.CommandTimeout = 120000; //要加这一句
using (IDataReader row = dbEIP.ExecuteReader(cmd))
{
while (row.Read())
{
EIP_Stock model = new EIP_Stock();
if (row != null)
{
if (row["BSNO"] != null)
{
model.BSNO = row["BSNO"].ToString();
}
if (row["BSSTATUS"] != null)
{
model.BSSTATUS = row["BSSTATUS"].ToString();
}
if (row["STOCKUPNO"] != null)
{
model.STOCKUPNO = row["STOCKUPNO"].ToString();
}
if (row["CUSTOMERNAME"] != null)
{
model.CUSTOMERNAME = row["CUSTOMERNAME"].ToString();
}
if (row["SALECONTRACT"] != null)
{
model.SALECONTRACT = row["SALECONTRACT"].ToString();
}
if (row["GOODSNAMETT"] != null)
{
model.GOODSNAMETT = row["GOODSNAMETT"].ToString();
}
if (row["STOCKUPDATE"] != null && row["STOCKUPDATE"].ToString() != "")
{
model.STOCKUPDATE = row["STOCKUPDATE"].ToString();
}
if (row["REMARK"] != null)
{
model.REMARK = row["REMARK"].ToString();
}
if (row["ENTEROPERATER"] != null)
{
model.ENTEROPERATER = row["ENTEROPERATER"].ToString();
}
if (row["ENTERDATE"] != null && row["ENTERDATE"].ToString() != "")
{
model.ENTERDATE = row["ENTERDATE"].ToString();
}
if (row["CORPID"] != null && row["CORPID"].ToString() != "")
{
model.CORPID = row["CORPID"].ToString();
}
if (row["ISRECVED"] != null && row["ISRECVED"].ToString() != "")
{
model.ISRECVED = row["ISRECVED"].ToString();
}
if (row["EXPORTNO"] != null && row["EXPORTNO"].ToString() != "")
{
model.EXPORTNO = row["EXPORTNO"].ToString();
}
}
headList.Add(model);
}
row.Close();
}
return headList;
#endregion
}
static public List GetTrayList(string strBSNO)
{
/*
var rangstr = GetRangDAStr("index", userid, username, companyid);
if (!string.IsNullOrEmpty(rangstr))
{
if (!string.IsNullOrEmpty(strCondition))
{
strCondition = strCondition + " and " + rangstr;
}
else
{
strCondition = rangstr;
}
}
*/
var strSql = new StringBuilder();
strSql.Append("select s.GID,s.BSNO,s.STOCKUPNO,s.TRAYNO,s.GOODSNAMETT,");
strSql.Append(" s.TTPKGS,s.TTWEIGHT,s.WMSNO,s.WMSOUTNO,s.ISSTOCKUP,");
strSql.Append("s.CNTRNO,s.SEALNO,s.STORAGENAME,d.DeliverNO ");
strSql.Append(" from vw_eip_stock_tray s inner join vw_eip_deliver_tray d");
strSql.Append(" on s.gid=d.gid ");
if (strBSNO.Trim() != "")
{
strSql.Append(" where s.BSNO='" + strBSNO + "'");
}
return SetTrayData(strSql);
}
///
/// 执行sql
///
///
///
private static List SetTrayData(StringBuilder strSql)
{
#region 公共代码
var headList = new List();
Database dbEIP = DatabaseFactory.CreateDatabase("EIP");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = strSql.ToString();
cmd.CommandTimeout = 120000; //要加这一句
using (IDataReader row = dbEIP.ExecuteReader(cmd))
{
while (row.Read())
{
EIP_Tray model = new EIP_Tray();
#region 动软
if (row != null)
{
if (row["GID"] != null)
{
model.GID = row["GID"].ToString();
}
if (row["BSNO"] != null)
{
model.BSNO = row["BSNO"].ToString();
}
if (row["STOCKUPNO"] != null)
{
model.STOCKNO = row["STOCKUPNO"].ToString();
}
if (row["TRAYNO"] != null)
{
model.TRAYNO = row["TRAYNO"].ToString();
}
if (row["GOODSNAMETT"] != null)
{
model.GOODSNAMETT = row["GOODSNAMETT"].ToString();
}
if (row["TTPKGS"] != null && row["TTPKGS"].ToString() != "")
{
model.TTPKGS = (row["TTPKGS"].ToString());
}
if (row["TTWEIGHT"] != null && row["TTWEIGHT"].ToString() != "")
{
model.TTWEIGHT = (row["TTWEIGHT"].ToString());
}
if (row["WMSNO"] != null)
{
model.WMSNO = row["WMSNO"].ToString();
}
if (row["WMSOUTNO"] != null)
{
model.WMSOUTNO = row["WMSOUTNO"].ToString();
}
if (row["ISSTOCKUP"] != null)
{
model.ISSTOCKUP = row["ISSTOCKUP"].ToString();
}
if (row["CNTRNO"] != null)
{
model.CNTRNO = row["CNTRNO"].ToString();
}
if (row["SEALNO"] != null)
{
model.SEALNO = row["SEALNO"].ToString();
}
if (row["DELIVERNO"] != null)
{
model.DELIVERNO = row["DELIVERNO"].ToString();
}
if (row["STORAGENAME"] != null)
{
model.STORAGENAME = row["STORAGENAME"].ToString();
}
}
#endregion
headList.Add(model);
}
row.Close();
}
return headList;
#endregion
}
public static bool UpdateByStockNo(string bsno, string state)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("update EIP_StockUp set BSSTATUS='" + state + "' where BSNO='" + bsno + "'");
Database dbEIP = DatabaseFactory.CreateDatabase("EIP");
int rst = dbEIP.ExecuteNonQuery(CommandType.Text, strSql.ToString());
if (rst > 0)
{
return true;
}
else
{
return false;
}
}
public static bool UpdateRecvedByStockNo(string bsno)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("update EIP_StockUp set ISRECVED='1' where BSNO='" + bsno + "'");
Database dbEIP = DatabaseFactory.CreateDatabase("EIP");
int rst = dbEIP.ExecuteNonQuery(CommandType.Text, strSql.ToString());
if (rst > 0)
{
return true;
}
else
{
return false;
}
}
public static bool UpdateSealInfo(string TrayNo, string SealNo, string CntrNo)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("update EIP_Tray set SEALNO='" + SealNo + "',CNTRNO='" + CntrNo + "' where TRAYNO='" + TrayNo + "'");
Database dbEIP = DatabaseFactory.CreateDatabase("EIP");
int rst = dbEIP.ExecuteNonQuery(CommandType.Text, strSql.ToString());
if (rst > 0)
{
return true;
}
else
{
return false;
}
}
public static DBResult UpdateTrayState(string outbsno)
{
DBResult result = new DBResult();
Database dbEIP = DatabaseFactory.CreateDatabase("EIP");
using (var conn = dbEIP.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
//更新托
var cmdUpdateTray = dbEIP.GetSqlStringCommand("update EIP_Tray set ISSCAN = '0', ISSTOCKUP = '0' where GID in (select gid from VW_EIP_Stock_Tray where STOCKUPNO ='" + outbsno+"')");
dbEIP.ExecuteNonQuery(cmdUpdateTray, tran);
//更新备货单
var cmdUpdateStock = dbEIP.GetSqlStringCommand("update EIP_StockUP set BSSTATUS = '1', IsRecved = '0' where STOCKUPNO = '" + outbsno + "'");
dbEIP.ExecuteNonQuery(cmdUpdateStock, tran);
tran.Commit();
result.Success = true;
result.Message = "备货单状态已更新";
}
catch (Exception ee)
{
tran.Rollback();
result.Success = false;
result.Message = "部分操作失败,请尽快联系管理员解决,并告知返回值" + ee.Message + ",避免造成更大的数据损失!";
return result;
}
}
return result;
}
public static bool ISWMS(string BSNO)
{
string strSql = "select top 1 deliverstatus from VW_EIP_Stock_Tray where bsno='" + BSNO + "' ";
Database dbEIP = DatabaseFactory.CreateDatabase("EIP");
string deliverstatus = "";
using (IDataReader reader = dbEIP.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
deliverstatus = Convert.ToString(reader["deliverstatus"]);
break;
}
reader.Close();
}
if (deliverstatus != "3")
return false;
else
return true;
}
#endregion BasicMethod
#region ExtensionMethod
///
/// 获取待备货列表
///
///
public static DataTable GetStockUpList()
{
var strSql = new StringBuilder();
strSql.AppendLine("SELECT BSNO,STOCKUPNO,CUSTOMERNAME,SALECONTRACT,GOODSNAMETT,CORPID,");
strSql.AppendLine("convert(char(10),STOCKUPDATE,20) AS STOCKUPDATE,");
strSql.AppendLine("convert(char(10),ENTERDATE,20) AS ENTERDATE,ENTEROPERATER,");
strSql.AppendLine("C.SHOWNAME CompanyName,C.COMNAME CompanyFullName");
strSql.AppendLine("FROM EIP_StockUp SU");
strSql.AppendLine("JOIN [User] C ON SU.CORPID=C.COMNAME ");
strSql.AppendLine("where BSStatus = '1' OR BSStatus = '2' ");
Database dbEIP = DatabaseFactory.CreateDatabase("EIP");
var ds = dbEIP.ExecuteDataSet(CommandType.Text, strSql.ToString());
return ds.Tables[0];
}
///
/// 获取待备货列表
///
///
public static DataTable GetTrayListForStockUp(string BSNO)
{
var strSql = new StringBuilder();
strSql.AppendLine("SELECT T.GID,T.TRAYNO,T.GOODSNAMETT,T.TTPKGS,T.TTWEIGHT,SU.BSNO,ISNULL(T.ISSCAN,'0') ISSCAN,ISNULL(T.AREANAME,'') AREANAME");
strSql.AppendLine("FROM EIP_StockUp SU");
strSql.AppendLine("JOIN EIP_StockTray ST ON SU.BSNO=ST.BSNO");
strSql.AppendLine("JOIN EIP_Tray T ON T.GID=ST.TRGID");
strSql.AppendLine($"WHERE SU.BSNO='{BSNO}'");
Database dbEIP = DatabaseFactory.CreateDatabase("EIP");
var ds = dbEIP.ExecuteDataSet(CommandType.Text, strSql.ToString());
return ds.Tables[0];
}
public static bool SetStockUp(string trayNO, string bsno, out string msg)
{
//检验状态是否可以扫描
Database dbEIP = DatabaseFactory.CreateDatabase("EIP");
var sql = $@"SELECT D.BSSTATUS
FROM EIP_Tray T
JOIN EIP_Deliver D ON D.BSNO=t.BSNO
where t.trayno='{trayNO}'";
object objSta = dbEIP.ExecuteScalar(CommandType.Text, sql);
if (objSta == DBNull.Value || objSta.ToString() != "3")
{
msg = "此托号未入库,不允许扫描备货";
return false;
}
var strSql = new StringBuilder($"UPDATE EIP_Tray SET ISSCAN=1 WHERE TRAYNO='{trayNO}';");
strSql.AppendLine("IF (SELECT COUNT(1)");
strSql.AppendLine("FROM EIP_StockUp SU");
strSql.AppendLine("JOIN EIP_StockTray ST ON SU.BSNO=ST.BSNO");
strSql.AppendLine("JOIN EIP_Tray T ON T.GID=ST.TRGID");
strSql.AppendLine("JOIN EIP_Goods G ON G.TRGID=T.GID");
strSql.AppendLine($"WHERE (T.ISSCAN=0 OR T.ISSCAN IS NULL) AND SU.BSNO='{bsno}') >0");
strSql.AppendLine($" UPDATE EIP_StockUp SET BSStatus=2 WHERE BSNO='{bsno}';");
strSql.AppendLine("ELSE");
strSql.AppendLine($" UPDATE EIP_StockUp SET BSStatus=3 WHERE BSNO='{bsno}';");
int count = dbEIP.ExecuteNonQuery(CommandType.Text, strSql.ToString());
if (count > 0)
{
msg = string.Empty;
return true;
}
else
{
msg = "未找到相关记录";
return false;
}
}
#endregion ExtensionMethod
}
}