using System;
using System.Collections.Generic;
using System.Text;
using DSWeb.Areas.MvcShipping.Models.Message;
using DSWeb.Areas.MvcShipping.Models.Message.HCHX_DATA;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using DSWeb.Areas.MvcShipping.Comm;
using System.Data.SqlClient;
using System.Messaging;
using System.Text.RegularExpressions;
namespace DSWeb.Areas.MvcShipping.DAL.Message
{
///
/// 出库入库报文
///
public static class MSGDAL_HCHX_DATA2
{
///
/// 根据入出库单号获取报文
///
/// 出入库单号
/// 用户id
/// 类型0:入库 1:出库
///
public static string GetXMLMessageWithWMSBSNO ( string wmsbsnos, string userid,out string msg,string trade_code,string unit,string ip,int type =0)
{
//签名
TRANSMIT t = new TRANSMIT();
t.BOXTYPE = "4001";
//库存信息
List storelist = new List();
StringBuilder sb = new StringBuilder();
sb.Append(@"SELECT
w.BZTCHNO AS EMS_NO,");
if (type==0)
{
sb.Append(@"(SELECT WMSNO from wms WHERE WMSNO=w.WMSNO) AS IO_NO,");
sb.Append(@" CONVERT(varchar(100), w.WMSDATE, 23) AS IO_DATE,");
sb.Append(@"(SELECT goodno FROM code_goods WHERE GID=(SELECT TOP 1 GOODSNAMEID FROM wms_in WHERE ASSOCIATEDNO=w.GID)) AS COP_G_NO,");
sb.Append("'I0001' AS [TYPE],");
sb.Append("(SELECT SUM(GOODSPACK) FROM wms_in WHERE ASSOCIATEDNO = w.GID) AS QTY,");
}
else
{
sb.Append(@"(SELECT BSNO from wms_out WHERE BSNO=w.BSNO) AS IO_NO,");
sb.Append(@" CONVERT(varchar(100), w.DODATE, 23) AS IO_DATE,");
sb.Append(@"(SELECT goodno FROM code_goods WHERE GID=(SELECT TOP 1 GOODSNAMEID FROM wms_out_detail WHERE OUTBSNO=w.BSNO)) AS COP_G_NO,");
sb.Append("'E0002' AS [TYPE],");
sb.Append(" (0-(SELECT SUM(GOODSPACKPFSL) FROM wms_out_detail WHERE OUTBSNO = w.BSNO)) AS QTY,");
}
sb.Append(@" (select c.TRADE_CODE from user_company uc JOIN company c on c.GID = uc.COMPANYID where uc.USERID = @userid) AS TRADE_CODE,");
sb.Append(@"(select c.UNIT from user_company uc JOIN company c on c.GID = uc.COMPANYID where uc.USERID =@userid) AS UNIT,
WHS_CODE AS WHS_CODE,
AREANAME AS LOCATION_CODE ");
if (type==0)
{
sb.Append(@" FROM WMS w
WHERE WMSNO in (" + wmsbsnos + ") ");
}
else
{
sb.Append(@" FROM WMS_OUT w
WHERE BSNO in (" + wmsbsnos + ") ");
}
SqlCommand com = new SqlCommand(sb.ToString());
SqlParameter sp = new SqlParameter("@userid", userid);
com.Parameters.Add(sp);
Database db = DatabaseFactory.CreateDatabase();
string TRADE_CODE = trade_code;
string UNIT = unit;
string EMS_NO = "";
using (IDataReader reader = db.ExecuteReader(com))
{
while (reader.Read())
{
EMS_NO = reader["EMS_NO"].ToString();
STORE_TRANS si = new STORE_TRANS();
si.EMS_NO = reader["EMS_NO"].ToString();
si.COP_G_NO = reader["COP_G_NO"].ToString();
si.TYPE = reader["TYPE"].ToString();
si.QTY = reader["QTY"].ToString();
si.UNIT = UNIT;
si.IO_DATE = reader["IO_DATE"].ToString();
si.IO_NO = reader["IO_NO"].ToString();
si.WHS_CODE = reader["WHS_CODE"].ToString();
si.LOCATION_CODE = reader["LOCATION_CODE"].ToString();
storelist.Add(si);
}
}
t.EMS_NO = EMS_NO;
t.TRADE_CODE = TRADE_CODE;
//序列化XML
HCHX_DATA hd = new HCHX_DATA();
hd.TRANSMIT = t;
hd.STORE_TRANS = storelist;
string xml = XmlHelper.XmlSerialize(hd, Encoding.UTF8, "HCHX_DATA",ip);
//正则过滤
var reg = new Regex(@"(\r\n)*\s*(?=)|(?<=)(\r\n)*\s*");
xml = reg.Replace(xml, "");
string message = checkNullAndEmpty(hd);
msg = message;
if (SendMessage(xml))
{
msg = "";
}
else
{
msg = "发送失败";
}
return xml;
}
private static string checkNullAndEmpty (HCHX_DATA data) {
string msg = "";
if (data.TRANSMIT.EMS_NO==null||data.TRANSMIT.EMS_NO=="")
{
msg += "EMS_NO(电子账册号),";
}
if (data.TRANSMIT.TRADE_CODE == null || data.TRANSMIT.TRADE_CODE == "")
{
msg += "TRADE_CODE(企业编码),";
}
if (data.TRANSMIT.BOXTYPE == null || data.TRANSMIT.BOXTYPE == "")
{
msg += "BOXTYPE(业务类型),";
}
var list = data.STORE_TRANS;
foreach (var si in list)
{
if (si.COP_G_NO == null || si.COP_G_NO == "")
{
msg += "COP_G_NO(料号)" + ",";
}
if (si.TYPE == null || si.TYPE == "")
{
msg += "TYPE(进出库类型)" + ",";
}
if (si.QTY == null || si.QTY == "")
{
msg += "QTY(期初数量)" + ",";
}
if (si.UNIT == null || si.UNIT == "")
{
msg += "UNIT(申报单位)" + ",";
}
if (si.IO_DATE == null || si.IO_DATE == "")
{
msg += "IO_DATE(出入库时间)" + ",";
}
if (si.IO_NO == null || si.IO_NO == "")
{
msg += "IO_NO(出入库单号)" + ",";
}
if (si.WHS_CODE == null || si.WHS_CODE == "")
{
msg += "WHS_CODE(库别)" + ",";
}
if (si.LOCATION_CODE == null || si.LOCATION_CODE == "")
{
msg += "LOCATION_CODE(储位)" + ",";
}
}
return msg;
}
public static bool SendMessage(string xml)
{
MessageQueue SmsQueue = new MessageQueue();
try
{
//string mqPath = "124.133.23.188\\Private$\\testjnzs";//给基站告警用的(wz的程序)
string mqPath = "124.133.23.188\\Private$\\zsplat2cust";//给基站告警用的(wz的程序)
mqPath = string.Format(@"FormatName:DIRECT=TCP:{0}", mqPath);
SmsQueue.MessageReadPropertyFilter.Body = true;
SmsQueue.MessageReadPropertyFilter.AppSpecific = true;
SmsQueue.MessageReadPropertyFilter.Priority = true;
SmsQueue.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
SmsQueue.Path = mqPath;
System.Messaging.Message msg = new System.Messaging.Message();
msg.Body = xml;
SmsQueue.Send(msg, MessageQueueTransactionType.Single);
return true;
}
catch (Exception ex)
{
return false;
}
finally
{
SmsQueue.Close();
}
}
}
}