TestHaoDaService.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using MySystem.Models;
  6. using Library;
  7. using LitJson;
  8. using System.Threading;
  9. namespace MySystem
  10. {
  11. /// <summary>
  12. /// 好哒测试服务
  13. /// </summary>
  14. public class TestHaoDaService
  15. {
  16. public readonly static TestHaoDaService Instance = new TestHaoDaService();
  17. private TestHaoDaService()
  18. { }
  19. public void Start()
  20. {
  21. Thread th = new Thread(StartListen);
  22. th.IsBackground = true;
  23. th.Start();
  24. }
  25. public void StartListen()
  26. {
  27. // StartProfitShareListen(152455);//提交分账
  28. StartProfitShareListen(153249);//提交分账
  29. }
  30. public void StartProfitShareListen(int OrderId)
  31. {
  32. try
  33. {
  34. WebCMSEntities db = new WebCMSEntities();
  35. DateTime checkDate = DateTime.Now.AddMinutes(-1);
  36. // var list = db.ConsumerOrders.Select(m => new { m.Id, m.CreateDate, m.Status, m.IsAct, m.PayMode }).Where(m => m.Status == 1 && m.PayMode == 2 && m.IsAct == 1 && m.CreateDate < checkDate).OrderBy(m => m.Id).Take(10).ToList();
  37. var list = db.ConsumerOrders.Select(m => new { m.Id, m.CreateDate, m.Status, m.IsAct, m.PayMode }).Where(m => m.Id == OrderId).OrderBy(m => m.Id).Take(10).ToList();
  38. foreach (var sub in list)
  39. {
  40. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "微信分账队列异常");
  41. LogHelper.Instance.WriteLog("Id:" + sub.Id, "微信分账队列异常");
  42. ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == sub.Id);
  43. if (order != null)
  44. {
  45. order.Status = 2;
  46. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantInfo();
  47. MerchantParamSet set = db.MerchantParamSet.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantParamSet();
  48. LogHelper.Instance.WriteLog("IsAct:" + order.IsAct, "微信分账队列监听");
  49. LogHelper.Instance.WriteLog("PayMoney:" + order.PayMoney, "微信分账队列监听");
  50. LogHelper.Instance.WriteLog("MinPayMoney:" + set.MinPayMoney, "微信分账队列监听");
  51. if (order.IsAct == 1 && order.PayMoney >= set.MinPayMoney)
  52. {
  53. MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantAddInfo();
  54. //添加分账接收方
  55. if (merchant.Version == 0)
  56. {
  57. WeChatFunction.Instance.AddReceive(merchantAdd.SubMchid, "MERCHANT_ID", WeChatFunction.Instance.MchId, WeChatFunction.Instance.MchName, "PARTNER");
  58. merchant.Version = 1;
  59. db.SaveChanges();
  60. }
  61. LogHelper.Instance.WriteLog("准备分账", "微信分账队列监听");
  62. //发起分账
  63. decimal fee = order.PayMoney; //单位:分
  64. if (fee >= 1)
  65. {
  66. string TradeNo = order.SeoTitle;
  67. string OrderNo = order.OrderNo;
  68. decimal hdfee = 0.0038M * order.PayMoney * (1 - set.GetPercent - 0.01M);
  69. hdfee = decimal.Parse(hdfee.ToString("f2"));
  70. decimal servicefee = 0.01M * order.PayMoney - hdfee;
  71. servicefee = servicefee * 100;
  72. int servicefeeNum = int.Parse(servicefee.ToString("f0"));
  73. List<ReceiverList> Receivers = new List<ReceiverList>();
  74. Receivers.Add(new ReceiverList()
  75. {
  76. type = "MERCHANT_ID", //分账接收方类型
  77. account = WeChatFunction.Instance.MchId, //分账接收方账号
  78. amount = servicefeeNum, //分账金额
  79. description = "服务费", //分账描述
  80. });
  81. LogHelper.Instance.WriteLog("TradeNo:" + TradeNo, "微信分账队列监听");
  82. LogHelper.Instance.WriteLog("OrderNo:" + OrderNo, "微信分账队列监听");
  83. LogHelper.Instance.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(Receivers), "微信分账队列监听");
  84. string ProfitShareResult = WeChatFunction.Instance.ProfitShare(merchantAdd.SubMchid, TradeNo, OrderNo, Receivers);
  85. LogHelper.Instance.WriteLog("分账结果:" + ProfitShareResult, "微信分账队列监听");
  86. order.DivideLog = "请求分账日志:" + ProfitShareResult;
  87. db.SaveChanges();
  88. //开始监听分账状态
  89. Dictionary<string, object> req = new Dictionary<string, object>();
  90. req.Add("SubMchid", merchantAdd.SubMchid); //子商户号
  91. req.Add("TradeNo", TradeNo); //微信订单号
  92. req.Add("OrderNo", OrderNo); //商户订单号
  93. RedisDbconn.Instance.AddList("ProfitShareQueue", Newtonsoft.Json.JsonConvert.SerializeObject(req));
  94. }
  95. else
  96. {
  97. RedisDbconn.Instance.AddList("ConsumerOrders:Divi:2:List", order.Id.ToString());
  98. // RedisDbconn.Instance.AddRightList("ConsumerOrders:Divi:2:" + order.MerchantId, order);
  99. }
  100. }
  101. }
  102. LogHelper.Instance.WriteLog("\n\n", "微信分账队列监听");
  103. }
  104. db.SaveChanges();
  105. db.Dispose();
  106. }
  107. catch (Exception ex)
  108. {
  109. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "微信分账队列异常");
  110. }
  111. Thread.Sleep(2000);
  112. }
  113. }
  114. }