ConsumerOrdersReturnDoService.cs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using System.Collections.Generic;
  3. using Library;
  4. using LitJson;
  5. using System.Linq;
  6. using System.Threading;
  7. using MySystem.Models;
  8. namespace MySystem
  9. {
  10. public class ConsumerOrdersReturnDoService
  11. {
  12. public readonly static ConsumerOrdersReturnDoService Instance = new ConsumerOrdersReturnDoService();
  13. private ConsumerOrdersReturnDoService()
  14. { }
  15. public void Start()
  16. {
  17. Thread th = new Thread(DoRefund);
  18. th.IsBackground = true;
  19. th.Start();
  20. }
  21. private void DoRefund()
  22. {
  23. bool op = true;
  24. while (op)
  25. {
  26. DateTime start = DateTime.Now.AddDays(-1);
  27. WebCMSEntities db = new WebCMSEntities();
  28. var list = db.ConsumerProfit.Select(m => new { m.Id, m.Status, m.CreateDate }).Where(m => m.CreateDate >= start && m.Status == 0).Take(20).ToList();
  29. foreach(var sub in list)
  30. {
  31. try
  32. {
  33. ConsumerProfit item = db.ConsumerProfit.FirstOrDefault(m => m.Id == sub.Id);
  34. if(item != null)
  35. {
  36. bool success = false;
  37. if (item.QueryCount == 1)
  38. {
  39. // string resultString = new AlipayFunction().Refund(item.SeoTitle, item.GetMoney.ToString("f2"), "202202BB4fc8c2c03c914f2f87b6c678a77d7C03");
  40. // JsonData jsonObj = JsonMapper.ToObject(resultString);
  41. // if (jsonObj["alipay_trade_refund_response"]["code"].ToString() == "10000")
  42. // {
  43. // success = true;
  44. // }
  45. }
  46. else if (item.QueryCount == 2)
  47. {
  48. MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == item.MerchantId) ?? new MerchantAddInfo();
  49. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == item.MerchantId) ?? new MerchantInfo();
  50. string SubMchId = merchantAdd.SubMchid;
  51. string Description = merchant.Name;
  52. string RefundNo = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  53. Dictionary<string, string> dic = WeChatFunction.Instance.Refund(SubMchId, item.GetMoney, decimal.Parse(function.CheckNum(item.SeoKeyword)), item.SeoTitle, RefundNo);
  54. if (dic.ContainsKey("status"))
  55. {
  56. if (dic["status"] == "SUCCESS")
  57. {
  58. success = true;
  59. }
  60. }
  61. }
  62. if (success)
  63. {
  64. item.Status = 1;
  65. ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == item.OrderId);
  66. if (order != null)
  67. {
  68. order.Status = 2;
  69. }
  70. db.SaveChanges();
  71. }
  72. }
  73. }
  74. catch (Exception ex)
  75. {
  76. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "退款返现队列异常");
  77. }
  78. }
  79. db.Dispose();
  80. }
  81. }
  82. }
  83. }