using DSWeb.Common.DB; using log4net; using Newtonsoft.Json; using RabbitMQ.Client; using RabbitMQ.Client.Events; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DSWeb.EventBus { public static class MqProcBillcenterDs6Feedback { private const string ExchangeName = "billcenter"; private const string QueueName = "billcenter.feedback.ds6"; private static string FeedbackMqUri = ConfigurationManager.AppSettings["FeedbackMQUri"]; private static IConnection mqConn; private static ILog logger = LogManager.GetLogger("MqProcBillcenterDs6Feedback"); public static void DoProcess() { logger.Debug($"ds6账单反馈开始执行"); ConnectionFactory factory = new ConnectionFactory(); factory.Uri = new Uri(FeedbackMqUri); mqConn = factory.CreateConnection("ds6 feedback"); IModel model = mqConn.CreateModel(); model.ExchangeDeclare(ExchangeName, ExchangeType.Direct); model.QueueDeclare(QueueName, false, false, false, null); var consumer = new EventingBasicConsumer(model); consumer.Received += (ch, ea) => { var body = ea.Body; var strBody = Encoding.UTF8.GetString(body.ToArray()); logger.Debug($"收到ds6账单反馈:{strBody}"); try { //回写数据 var feedback = JsonConvert.DeserializeAnonymousType(strBody, new { ReceiveId = string.Empty, Success = false, Reason = string.Empty }); BillCenterDataContext billCenterData = new BillCenterDataContext(); var bill = billCenterData.BillCenterRecord.FirstOrDefault(b => b.GID == feedback.ReceiveId); if (bill != null) { bill.CustSysRecStatus = feedback.Success ? "接收成功" : "接收失败"; bill.CustSysRecTime = DateTime.Now; bill.CustSysRecResult = feedback.Reason; billCenterData.SaveChanges(); //更新附件发送状态 var attach = billCenterData.Attachments.First(a => a.GID == bill.AttachId); var staListInAtt = billCenterData.BillCenterRecord.AsNoTracking() .Where(x => x.AttachId == bill.AttachId) .Select(x => x.CustSysRecStatus) .Distinct() .ToList(); if (staListInAtt.Where(x => x == "接收成功").Count() == staListInAtt.Count) { attach.SendStatus = "发送成功"; } else if (staListInAtt.Contains("接收失败")) { attach.SendStatus = "发送失败"; } billCenterData.SaveChanges(); //更细邮件状态 var mail = billCenterData.Mails.First(m => m.GID == attach.MailGID); var staListInMail = billCenterData.Attachments.AsNoTracking() .Where(a => a.MailGID == attach.MailGID) .Select(a => a.SendStatus) .Distinct() .ToList(); if (staListInMail.Where(x => x == "发送成功").Count() == staListInMail.Count) { mail.Status = "Success"; } else if (staListInMail.Contains("发送失败")) { mail.Status = "Processing"; } billCenterData.SaveChanges(); } else { logger.Error($"处理账单中心DS6反馈状态时出错:无效的数据ID {feedback.ReceiveId}"); } } catch (Exception ex) { logger.Error($"处理账单中心DS6反馈状态时出错:"); logger.Error(ex.Message); logger.Error(ex.StackTrace); } }; model.BasicConsume(QueueName, true, consumer); } public static void StopProcess() { if (mqConn != null && mqConn.IsOpen) { mqConn.Close(); mqConn = null; } } } }