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/D7DJYMQ/DS7Date.cs

264 lines
12 KiB
C#

12 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 D7DJYMQ
{
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($"启动接收大简云订舱数据回推");
12 months ago
var dbconlist = GetDbConList();
if (dbconlist == null || dbconlist.Count == 0)
{
logger.Debug($"没有数据库连接");
}
else
{
12 months ago
12 months ago
var CompanyQueueName = QueueName;
ConnectionFactory factory = new ConnectionFactory();
factory.Uri = new Uri(MQURL);
//factory.UserName = "xgs_test";
//factory.Password = "xgs_test123";
factory.VirtualHost = virtualhost;
12 months ago
12 months ago
mqConn = factory.CreateConnection();
12 months ago
12 months ago
IModel model = mqConn.CreateModel();
//model.ExchangeDeclare(ExchangeName, ExchangeType.Direct);
model.QueueDeclare(CompanyQueueName, false, false, false, null);
//model.QueueBind(CompanyQueueName, ExchangeName, QueueName);
12 months ago
12 months ago
var consumer = new EventingBasicConsumer(model);
consumer.Received += (ch, ea) =>
12 months ago
{
12 months ago
var body = ea.Body;
var strBody = Encoding.UTF8.GetString(body.ToArray());
logger.Debug($"收到简云截单日期数据回推消息:{strBody}");
try
{
12 months ago
//回写数据
var dateback = JsonConvert.DeserializeAnonymousType(strBody, new { vessel = string.Empty, voyage = string.Empty, ship_agency = string.Empty, cut_off_time = string.Empty, etd = string.Empty });
12 months ago
if (!string.IsNullOrEmpty(dateback.vessel) && !string.IsNullOrEmpty(dateback.voyage) && !string.IsNullOrEmpty(dateback.cut_off_time))
// if (!string.IsNullOrEmpty(dateback.vessel) && !string.IsNullOrEmpty(dateback.voyage) && !string.IsNullOrEmpty(dateback.ship_agency) && !string.IsNullOrEmpty(dateback.cut_off_time))
{
foreach (var emdbcon in dbconlist)
12 months ago
{
12 months ago
using (SqlConnection dbcon = new SqlConnection(emdbcon.connStr))
12 months ago
{
12 months ago
dbcon.Open();
string maildescrption = "";
//var FORWARDER = GetCustEdi(dbcon, dateback.ship_agency);
var FORWARDER = "111";
var SendMailList = new List<SendMail>();
if (!string.IsNullOrEmpty(FORWARDER))
12 months ago
{
12 months ago
var opseaelist = GetBs(dbcon, "VESSEL='" + dateback.vessel + "' AND VOYNO='" + dateback.voyage + "'");
// var opseaelist = GetBs(dbcon, "VESSEL='" + dateback.vessel + "' AND VOYNO='" + dateback.voyage + "' and FORWARDER='" + FORWARDER + "'");
if (opseaelist != null && opseaelist.Count != 0)
12 months ago
{
12 months ago
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 = "船名:{" + dateback.vessel + "},航次:{" + dateback.voyage + "},订舱代理:{" + dateback.ship_agency+ "}下列提单号截单日期已更新至{" + dateback.cut_off_time + "}:" + Environment.NewLine + opseae.MBLNO;
SendMailList.Add(newsendmail);
}
}
12 months ago
}
12 months ago
}
else
{
logger.Debug($"{dateback.ship_agency}没有找到对应的订舱代理");
12 months ago
}
12 months ago
if (SendMailList != null && SendMailList.Count != 0)
{
foreach (var SendMail in SendMailList)
{
11 months ago
SendEmail se = new SendEmail(SendMail.MAIL, MAILSENDACCOUNT, "", "", "截单日期自动更新", SendMail.MAILDESCRIPTION, false);
12 months ago
12 months ago
se.SetSmtp(MAILSENDACCOUNT, MAILSENDPASSWORD, MAILSENDSERVICE, Convert.ToInt16(MAILSENDPORT), false, System.Net.Mail.MailPriority.Normal); //必须在所有参数设置完后调用此方法
logger.Debug($"已给操作{SendMail.OP}发送邮件{SendMail.MAIL}{SendMail.MAILDESCRIPTION}");
12 months ago
12 months ago
}
}
12 months ago
12 months ago
dbcon.Close();
12 months ago
}
}
}
12 months ago
}
catch (Exception ex)
{
logger.Error($"处理简云订舱数据回推时出错:" + ex.Message);
logger.Error(ex.Message);
logger.Error(ex.StackTrace);
}
};
model.BasicConsume(CompanyQueueName, true, consumer);
}
12 months ago
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; }
}
12 months ago
public class MsDbCon
{
public string connStr { get; set; }
}
static public List<MsDbCon> GetDbConList()
{
var MsDbConList = new List<MsDbCon>();
string[] keys = ConfigurationManager.AppSettings.AllKeys;
for (int i = 0; i < keys.Length; i++)
{
if (keys[i].IndexOf("ConnectionString:") == 0)
{
var MsDbCon = new MsDbCon();
MsDbCon.connStr = ConfigurationManager.AppSettings[keys[i]];
MsDbConList.Add(MsDbCon);
}
}
return MsDbConList;
}
12 months ago
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;
}
}
}