using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
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_DATA
{
///
/// 根据入库单号获取报文
///
///
///
public static string GetXMLMessageWithWMSNO ( string wmsno, string userid, string trade_code,string unit,string ip,out string msg)
{
//签名
TRANSMIT t = new TRANSMIT();
t.BOXTYPE = "2005";
//库存信息
List storelist = new List();
StringBuilder sb = new StringBuilder();
sb.Append(@"SELECT
w.BZTCHNO AS EMS_NO,
(SELECT goodno FROM code_goods WHERE GID=(SELECT TOP 1 GOODSNAMEID FROM wms_in WHERE ASSOCIATEDNO=w.GID)) AS COP_G_NO,
(SELECT SUM(GOODSPACK) FROM wms_in WHERE ASSOCIATEDNO = w.GID) AS QTY,
(select c.UNIT from user_company uc JOIN company c on c.GID = uc.COMPANYID where uc.USERID ='@userid') AS UNIT,
(select c.TRADE_CODE from user_company uc JOIN company c on c.GID = uc.COMPANYID where uc.USERID = '@userid') AS TRADE_CODE,
CONVERT(varchar(100), w.WMSDATE, 23) AS CHECK_DATE,
'C' AS DATA_TYPE,
WHS_CODE AS WHS_CODE,
AREANAME AS LOCATION_CODE
FROM wms w
WHERE WMSNO in (" + wmsno+") ");
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_INIT si = new STORE_INIT();
si.EMS_NO = reader["EMS_NO"].ToString();
si.COP_G_NO = reader["COP_G_NO"].ToString();
si.QTY = reader["QTY"].ToString();
si.COP_G_NO = reader["COP_G_NO"].ToString();
si.UNIT =UNIT;
si.CHECK_DATE = reader["CHECK_DATE"].ToString();
si.DATA_TYPE = reader["DATA_TYPE"].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_INIT = 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_INIT;
foreach (var si in list)
{
if (si.EMS_NO == null || si.EMS_NO == "")
{
msg += "EMS_NO(电子账册号),";
}
if (si.COP_G_NO == null || si.COP_G_NO == "")
{
msg += "COP_G_NO(料号)" + ",";
}
if (si.QTY == null || si.QTY == "")
{
msg += "QTY(期初数量)" + ",";
}
if (si.UNIT == null || si.UNIT == "")
{
msg += "UNIT(申报单位)" + ",";
}
if (si.CHECK_DATE == null || si.CHECK_DATE == "")
{
msg += "CHECK_DATE(期初日期)" + ",";
}
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();
}
}
}
}