using Autofac; using Bowin.Common.Log; using EMIS.CommonLogic.SystemServices; using EMIS.Utility.OnlinePay; using Senparc.Weixin.TenPay.V3; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; namespace EMIS.Service { public partial class Service1 : ServiceBase { private System.Timers.Timer timer1 = new System.Timers.Timer(); public Service1() { timer1.Interval = 180000; timer1.Elapsed += Timer1_Elapsed; InitializeComponent(); } private void Timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { timer1.Stop(); try { LogHelper.WriteLog(LogType.ServiceLog, "----开始根据微信订单同步缴费状态-----"); using (var scop = Program.AutofacContainer.BeginLifetimeScope()) { IWechatPayServices WechatPayServices = scop.Resolve(); var wecharPayList = WechatPayServices.SearchWeChatPayList(); List orderList = new List(); List CanCloseOrderIDList = new List(); foreach (var wecharPay in wecharPayList) { OrderQueryResult query = WechatHelper.OrderQuery(wecharPay.OrderID); if (wecharPay.CreateTime != null) { if (DateTime.Now.Subtract(wecharPay.CreateTime.Value).TotalMinutes > 6) { var canClose = WechatHelper.CloseOrder(wecharPay.OrderID); if (canClose) { CanCloseOrderIDList.Add(wecharPay.OrderID); LogHelper.WriteLog(LogType.ServiceLog, "关单成功:商家订单号:" + wecharPay.OrderID + ";微信订单号:" + query.transaction_id + ";状态result_code:" + query.result_code + ";支付状态:" + query.trade_state); } else { LogHelper.WriteLog(LogType.ServiceLog, "关单失败:商家订单号:" + wecharPay.OrderID + ";微信订单号:" + query.transaction_id + ";状态result_code:" + query.result_code + ";支付状态:" + query.trade_state); } } } LogHelper.WriteLog(LogType.ServiceLog, "商家订单号:" + wecharPay.OrderID + ";微信订单号:" + query.transaction_id + ";状态result_code:" + query.result_code + ";支付状态:" + query.trade_state); orderList.Add(query); } WechatPayServices.UpdateWeChatPay(orderList); WechatPayServices.DeleteCanCloseWeChatPay(CanCloseOrderIDList); } LogHelper.WriteLog(LogType.ServiceLog, "----结束根据微信订单同步缴费状态-----"); } catch (System.Exception err) { LogHelper.WriteLog(LogType.ServiceLog, "----根据微信订单同步缴费状态处理异常-----"); LogHelper.WriteLog(LogType.ServiceLog, err.Message+err.StackTrace); } timer1.Start(); } protected override void OnStart(string[] args) { timer1.Start(); } protected override void OnStop() { timer1.Stop(); } } }