using log4net; using Quartz; using System; using System.Data; using System.Data.SqlClient; using RabbitMQ.Client; using System.IO; using System.Linq; using System.Text; using MailKit.Net.Smtp; using MimeKit; using HtmlAgilityPack; using System.Collections.Generic; using System.Configuration; using System.Threading.Tasks; using System.Threading; using Newtonsoft.Json; namespace JobSendAgentMail { 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; } } public class JobDjy2D7Date : IJob { private ILog log = LogManager.GetLogger(typeof(JobDjy2D7Date)); private static IConnection mqConn; public void Execute(IJobExecutionContext context) { string connStr = context.JobDetail.JobDataMap.GetString("ConnectString"); string MQURL = context.JobDetail.JobDataMap.GetString("DJYMQURL"); string virtualhost = context.JobDetail.JobDataMap.GetString("virtualhost"); string ExchangeName = context.JobDetail.JobDataMap.GetString("MQExchangeName"); string QueueName = context.JobDetail.JobDataMap.GetString("MQQueueName"); string BSSQL = context.JobDetail.JobDataMap.GetString("BSSQL"); string MAILSENDACCOUNT = context.JobDetail.JobDataMap.GetString("MAILSENDACCOUNT"); string MAILSENDPASSWORD = context.JobDetail.JobDataMap.GetString("MAILSENDPASSWORD"); string MAILSENDSERVICE = context.JobDetail.JobDataMap.GetString("MAILSENDSERVICE"); string MAILSENDPORT = context.JobDetail.JobDataMap.GetString("MAILSENDPORT"); string MAILISSSL = context.JobDetail.JobDataMap.GetString("MAILISSSL"); log.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(); 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()); log.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(); 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(); log.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 { log.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); //必须在所有参数设置完后调用此方法 log.Debug($"已给操作{SendMail.OP}发送邮件{SendMail.MAIL}{SendMail.MAILDESCRIPTION}"); } } } } } catch (Exception ex) { log.Error($"处理大简云数据回推时出错:" + ex.Message); log.Error(ex.Message); log.Error(ex.StackTrace); } //model.BasicConsume(CompanyQueueName, true, consumer); //Console.WriteLine(msg); } }); task.Wait(); model.Close(); mqConn.Close(); } static public List GetBs(SqlConnection dbcon, string constr) { var opseaeList = new List(); 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; } } }