Service1.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using Autofac;
  2. using Bowin.Common.Log;
  3. using EMIS.CommonLogic.SystemServices;
  4. using EMIS.Utility.OnlinePay;
  5. using Senparc.Weixin.TenPay.V3;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Data;
  10. using System.Diagnostics;
  11. using System.Linq;
  12. using System.ServiceProcess;
  13. using System.Text;
  14. namespace EMIS.Service
  15. {
  16. public partial class Service1 : ServiceBase
  17. {
  18. private System.Timers.Timer timer1 = new System.Timers.Timer();
  19. public Service1()
  20. {
  21. timer1.Interval = 180000;
  22. timer1.Elapsed += Timer1_Elapsed;
  23. InitializeComponent();
  24. }
  25. private void Timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  26. {
  27. timer1.Stop();
  28. try
  29. {
  30. LogHelper.WriteLog(LogType.ServiceLog, "----开始根据微信订单同步缴费状态-----");
  31. using (var scop = Program.AutofacContainer.BeginLifetimeScope())
  32. {
  33. IWechatPayServices WechatPayServices = scop.Resolve<IWechatPayServices>();
  34. var wecharPayList = WechatPayServices.SearchWeChatPayList();
  35. List<OrderQueryResult> orderList = new List<OrderQueryResult>();
  36. List<String> CanCloseOrderIDList = new List<string>();
  37. foreach (var wecharPay in wecharPayList)
  38. {
  39. OrderQueryResult query = WechatHelper.OrderQuery(wecharPay.OrderID);
  40. if (wecharPay.CreateTime != null)
  41. {
  42. if (DateTime.Now.Subtract(wecharPay.CreateTime.Value).TotalMinutes > 6)
  43. {
  44. var canClose = WechatHelper.CloseOrder(wecharPay.OrderID);
  45. if (canClose)
  46. {
  47. CanCloseOrderIDList.Add(wecharPay.OrderID);
  48. LogHelper.WriteLog(LogType.ServiceLog, "关单成功:商家订单号:" + wecharPay.OrderID + ";微信订单号:" + query.transaction_id + ";状态result_code:" + query.result_code + ";支付状态:" + query.trade_state);
  49. }
  50. else
  51. {
  52. LogHelper.WriteLog(LogType.ServiceLog, "关单失败:商家订单号:" + wecharPay.OrderID + ";微信订单号:" + query.transaction_id + ";状态result_code:" + query.result_code + ";支付状态:" + query.trade_state);
  53. }
  54. }
  55. }
  56. LogHelper.WriteLog(LogType.ServiceLog, "商家订单号:" + wecharPay.OrderID + ";微信订单号:" + query.transaction_id + ";状态result_code:" + query.result_code + ";支付状态:" + query.trade_state);
  57. orderList.Add(query);
  58. }
  59. WechatPayServices.UpdateWeChatPay(orderList);
  60. WechatPayServices.DeleteCanCloseWeChatPay(CanCloseOrderIDList);
  61. }
  62. LogHelper.WriteLog(LogType.ServiceLog, "----结束根据微信订单同步缴费状态-----");
  63. }
  64. catch (System.Exception err)
  65. {
  66. LogHelper.WriteLog(LogType.ServiceLog, "----根据微信订单同步缴费状态处理异常-----");
  67. LogHelper.WriteLog(LogType.ServiceLog, err.Message+err.StackTrace);
  68. }
  69. timer1.Start();
  70. }
  71. protected override void OnStart(string[] args)
  72. {
  73. timer1.Start();
  74. }
  75. protected override void OnStop()
  76. {
  77. timer1.Stop();
  78. }
  79. }
  80. }