You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DS7/DSWeb.DJY_RESP/DS7Date.cs

314 lines
15 KiB
C#

9 months ago
using log4net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Topshelf;
namespace DSWeb.Service.Output.DS7
{
public class DS7Date : ServiceControl
{
private static string MQURL = ConfigurationManager.AppSettings["DJYMQURL"];
private static string connStr = ConfigurationManager.AppSettings["ConnectionString"];
private static string virtualhost = ConfigurationManager.AppSettings["virtualhost"];
private static string ExchangeName = ConfigurationManager.AppSettings["MQExchangeName"];
private static string QueueName = ConfigurationManager.AppSettings["MQQueueName"];
private static string MAILSENDACCOUNT = ConfigurationManager.AppSettings["MAILSENDACCOUNT"];
private static string MAILSENDPASSWORD = ConfigurationManager.AppSettings["MAILSENDPASSWORD"];
private static string MAILSENDSERVICE = ConfigurationManager.AppSettings["MAILSENDSERVICE"];
private static string MAILSENDPORT = ConfigurationManager.AppSettings["MAILSENDPORT"];
private static string MAILISSSL = ConfigurationManager.AppSettings["MAILISSSL"];
private static string MAILTITLE = ConfigurationManager.AppSettings["MAILTITLE"];
private static string BSSQL = ConfigurationManager.AppSettings["BSSQL"];
private static IConnection mqConn;
private static ILog logger = LogManager.GetLogger("DS7Date");
public bool Start(HostControl hostControl)
{
logger.Debug($"启动接收大简云订舱数据回推");
var CompanyQueueName = QueueName;
ConnectionFactory factory = new ConnectionFactory();
factory.Uri = new Uri(MQURL);
//factory.UserName = "xgs_test";
//factory.Password = "xgs_test123";
factory.VirtualHost = virtualhost;
mqConn = factory.CreateConnection();
IModel model = mqConn.CreateModel();
model.ExchangeDeclare(ExchangeName, ExchangeType.Direct);
model.QueueDeclare(CompanyQueueName, false, false, false, null);
model.QueueBind(CompanyQueueName, ExchangeName, QueueName);
var consumer = new EventingBasicConsumer(model);
consumer.Received += (ch, ea) =>
{
var body = ea.Body;
var strBody = Encoding.UTF8.GetString(body.ToArray());
logger.Debug($"收到简云截单日期数据回推消息:{strBody}");
try
{
//回写数据
var dateback = JsonConvert.DeserializeAnonymousType(strBody, new { vessel = string.Empty, voyage = string.Empty, ship_agency = string.Empty, cut_off_time = string.Empty, etd = string.Empty });
if (!string.IsNullOrEmpty(dateback.vessel) && !string.IsNullOrEmpty(dateback.voyage) && !string.IsNullOrEmpty(dateback.ship_agency) && !string.IsNullOrEmpty(dateback.cut_off_time))
{
using (SqlConnection dbcon = new SqlConnection(connStr))
{
dbcon.Open();
string maildescrption = "";
var FORWARDER = GetCustEdi(dbcon, dateback.ship_agency);
var SendMailList = new List<SendMail>();
if (!string.IsNullOrEmpty(FORWARDER))
{
var opseaelist = GetBs(dbcon, "VESSEL='" + dateback.vessel + "' AND VOYNO='" + dateback.voyage + "' and FORWARDER='" + FORWARDER + "'");
if (opseaelist != null && opseaelist.Count != 0)
{
var opemail = "";
foreach (var opseae in opseaelist)
{
var tastStr = $" update op_seae set CLOSEDOCDATE='{dateback.cut_off_time}' where BSNO='{opseae.BSNO}' ";
SqlCommand cmdtast = new SqlCommand(tastStr, dbcon);
cmdtast.ExecuteNonQuery();
logger.Debug($"已更新截单日期{dateback.cut_off_time},{opseae.MBLNO},{opseae.BSNO}");
var sendmail = SendMailList.Find(x => x.OP == opseae.OP);
if (sendmail != null)
{
sendmail.MAILDESCRIPTION = sendmail.MAILDESCRIPTION + Environment.NewLine + opseae.MBLNO;
}
else
{
var newsendmail = new SendMail();
newsendmail.OP = opseae.OP;
newsendmail.MAIL = opseae.MAIL;
newsendmail.MAILDESCRIPTION = opseae.MBLNO;
SendMailList.Add(newsendmail);
}
}
}
}
else
{
logger.Debug($"{dateback.ship_agency}没有找到对应的订舱代理");
}
if (SendMailList != null && SendMailList.Count != 0)
{
foreach (var SendMail in SendMailList)
{
SendEmail se = new SendEmail(SendMail.MAIL, MAILSENDACCOUNT, "", "", "截港日期自动更新", SendMail.MAILDESCRIPTION, false);
se.SetSmtp(MAILSENDACCOUNT, MAILSENDPASSWORD, MAILSENDSERVICE, Convert.ToInt16(MAILSENDPORT), false, System.Net.Mail.MailPriority.Normal); //必须在所有参数设置完后调用此方法
logger.Debug($"已给操作{SendMail.OP}发送邮件{SendMail.MAIL}{SendMail.MAILDESCRIPTION}");
}
}
}
}
}
catch (Exception ex)
{
logger.Error($"处理简云订舱数据回推时出错:" + ex.Message);
logger.Error(ex.Message);
logger.Error(ex.StackTrace);
}
};
model.BasicConsume(CompanyQueueName, true, consumer);
//var task = Task.Run(() =>
//{
// while (true)
// {
// var result = model.BasicGet(queue: CompanyQueueName, autoAck: true);
// if (result == null) { Thread.Sleep(10); continue; };
// var strBody = Encoding.UTF8.GetString(result.Body.ToArray());
// logger.Debug($"收到简云截单日期数据回推消息:{strBody}");
// try
// {
// //回写数据
// var dateback = JsonConvert.DeserializeAnonymousType(strBody, new { vessel = string.Empty, voyage = string.Empty, ship_agency = string.Empty, cut_off_time = string.Empty, etd = string.Empty });
// if (!string.IsNullOrEmpty(dateback.vessel) && !string.IsNullOrEmpty(dateback.voyage) && !string.IsNullOrEmpty(dateback.ship_agency) && !string.IsNullOrEmpty(dateback.cut_off_time))
// {
// using (SqlConnection dbcon = new SqlConnection(connStr))
// {
// dbcon.Open();
// string maildescrption = "";
// var FORWARDER = GetCustEdi(dbcon, dateback.ship_agency);
// var SendMailList = new List<SendMail>();
// if (!string.IsNullOrEmpty(FORWARDER))
// {
// var opseaelist = GetBs(dbcon, "VESSEL='" + dateback.vessel + "' AND VOYNO='" + dateback.voyage + "' and FORWARDER='" + FORWARDER + "'" + BSSQL);
// if (opseaelist != null && opseaelist.Count != 0)
// {
// var opemail = "";
// foreach (var opseae in opseaelist)
// {
// var tastStr = $" update op_seae set CLOSEDOCDATE='{dateback.cut_off_time}' where BSNO='{opseae.BSNO}' ";
// SqlCommand cmdtast = new SqlCommand(tastStr, dbcon);
// cmdtast.ExecuteNonQuery();
// logger.Debug($"已更新截单日期{dateback.cut_off_time},{opseae.MBLNO},{opseae.BSNO}");
// var sendmail = SendMailList.Find(x => x.OP == opseae.OP);
// if (sendmail != null)
// {
// sendmail.MAILDESCRIPTION = sendmail.MAILDESCRIPTION + Environment.NewLine + opseae.MBLNO;
// }
// else
// {
// var newsendmail = new SendMail();
// newsendmail.OP = opseae.OP;
// newsendmail.MAIL = opseae.MAIL;
// newsendmail.MAILDESCRIPTION = opseae.MBLNO;
// SendMailList.Add(newsendmail);
// }
// }
// }
// }
// else
// {
// logger.Debug($"{dateback.ship_agency}没有找到对应的订舱代理");
// }
// if (SendMailList != null && SendMailList.Count != 0)
// {
// foreach (var SendMail in SendMailList)
// {
// SendEmail se = new SendEmail(SendMail.MAIL, MAILSENDACCOUNT, "", "", "截港日期自动更新", SendMail.MAILDESCRIPTION, false);
// se.SetSmtp(MAILSENDACCOUNT, MAILSENDPASSWORD, MAILSENDSERVICE, Convert.ToInt16(MAILSENDPORT), false, System.Net.Mail.MailPriority.Normal); //必须在所有参数设置完后调用此方法
// logger.Debug($"已给操作{SendMail.OP}发送邮件{SendMail.MAIL}{SendMail.MAILDESCRIPTION}");
// }
// }
// }
// }
// }
// catch (Exception ex)
// {
// logger.Error($"处理简云订舱数据回推时出错:" + ex.Message);
// logger.Error(ex.Message);
// logger.Error(ex.StackTrace);
// }
// //model.BasicConsume(CompanyQueueName, true, consumer);
// //Console.WriteLine(msg);
// }
//});
//task.Wait();
//model.Close();
//mqConn.Close();
return true;
}
public bool Stop(HostControl hostControl)
{
// 结束
logger.Debug("停止运行");
//StdSchedulerFactory.GetDefaultScheduler().Shutdown();
if (mqConn != null && mqConn.IsOpen)
{
mqConn.Close();
mqConn = null;
}
return true;
}
public class MsOpSeae
{
public string BSNO { get; set; }
public string MBLNO { get; set; }
public string OP { get; set; }
public string MAIL { get; set; }
}
public class SendMail
{
public string OP { get; set; }
public string MAIL { get; set; }
public string MAILDESCRIPTION { get; set; }
}
static public List<MsOpSeae> GetBs(SqlConnection dbcon, string constr)
{
var opseaeList = new List<MsOpSeae>();
var strSql = new StringBuilder();
strSql.Append("select BSNO,MBLNO,OP,FORWARDER,(SELECT TOP 1 EMAIL1 FROM VW_user WHERE SHOWNAME=op_seae.OP) EMAIL from op_seae where " + constr);
strSql.Append(" ORDER BY OP ");
SqlDataAdapter adapter = new SqlDataAdapter(strSql.ToString(), dbcon);
DataTable table = new DataTable();
adapter.Fill(table);
if (table.Rows.Count > 0)
{
foreach (DataRow row in table.Rows)
{
var opseae = new MsOpSeae();
opseae.BSNO = Convert.ToString(row["BSNO"].ToString());
opseae.MBLNO = Convert.ToString(row["MBLNO"].ToString());
opseae.OP = Convert.ToString(row["OP"].ToString());
opseae.MAIL = Convert.ToString(row["EMAIL"].ToString());
opseaeList.Add(opseae);
}
}
return opseaeList;
}
static public string GetCustEdi(SqlConnection dbcon, string constr)
{
string result = "";
var strSql = new StringBuilder();
strSql.Append("select CUST from code_cust_edi where EDINAME='DJY' AND EDICODE='" + constr + "'");
SqlDataAdapter adapter = new SqlDataAdapter(strSql.ToString(), dbcon);
DataTable table = new DataTable();
adapter.Fill(table);
if (table.Rows.Count > 0)
{
foreach (DataRow row in table.Rows)
{
result = Convert.ToString(row["CUST"].ToString());
}
}
return result;
}
}
}