AlipayPayBack2Service.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MySystem.PxcModels;
  5. using Library;
  6. using LitJson;
  7. namespace MySystem
  8. {
  9. public class AlipayPayBack2Service
  10. {
  11. public readonly static AlipayPayBack2Service Instance = new AlipayPayBack2Service();
  12. private AlipayPayBack2Service()
  13. { }
  14. public void Start(JobMqMsg jobInfo)
  15. {
  16. string content = "";
  17. try
  18. {
  19. dosomething();
  20. string Msg = "success";
  21. jobInfo.Status = Msg == "success" ? 1 : 0;
  22. jobInfo.Msg = Msg == "success" ? "执行完成" : Msg;
  23. RabbitMQClient.Instance.SendMsg(Newtonsoft.Json.JsonConvert.SerializeObject(jobInfo), "PublicBack");
  24. }
  25. catch (Exception ex)
  26. {
  27. if (!string.IsNullOrEmpty(content))
  28. {
  29. Dictionary<string, string> data = new Dictionary<string, string>();
  30. data.Add("ErrTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  31. data.Add("ErrMsg", ex.ToString());
  32. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(data), "public_err");
  33. }
  34. else
  35. {
  36. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "public_service");
  37. }
  38. }
  39. }
  40. private void dosomething()
  41. {
  42. bool op = true;
  43. while (op)
  44. {
  45. string content = RedisDbconn.Instance.RPop<string>("PayCallBack2");
  46. if (!string.IsNullOrEmpty(content))
  47. {
  48. JsonData jsonObj = JsonMapper.ToObject(content);
  49. string OrderNo = jsonObj["out_trade_no"].ToString();
  50. string TradeNo = jsonObj["transaction_id"].ToString();
  51. decimal TotalFee = decimal.Parse(function.CheckNum(jsonObj["total_fee"].ToString()));
  52. WebCMSEntities db = new WebCMSEntities();
  53. OrderForNo forNo = db.OrderForNo.FirstOrDefault(m => m.OrderNo == OrderNo);
  54. if (forNo != null)
  55. {
  56. string[] ids = forNo.OrderIds.Split(',');
  57. foreach (string idString in ids)
  58. {
  59. int OrderId = int.Parse(idString);
  60. Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId && m.Status == 0);
  61. if (order != null)
  62. {
  63. order.Status = 1;
  64. order.PayDate = DateTime.Now;
  65. order.PayStatus = 1;
  66. db.SaveChanges();
  67. RedisDbconn.Instance.Set("Orders:" + order.Id, order);
  68. OrderProduct pro = db.OrderProduct.FirstOrDefault(m => m.OrderId == OrderId);
  69. if (pro != null)
  70. {
  71. KqProducts brand = db.KqProducts.FirstOrDefault(m => m.Sort == pro.ProductId);
  72. if (brand != null)
  73. {
  74. int BuyCount = pro.ProductCount;
  75. int Kind = 0;
  76. if(pro.ProductPrice == 200) Kind = 1;
  77. if(pro.ProductPrice == 300) Kind = 2;
  78. string Codes = "";
  79. var coupons = db.PosCoupons.Where(m => m.IsLock == 0 && m.IsUse == 0 && m.UserId == 0 && m.QueryCount == Kind).OrderBy(m => m.Id).Take(BuyCount).ToList();
  80. foreach (var coupon in coupons)
  81. {
  82. PosCoupons item = db.PosCoupons.FirstOrDefault(m => m.Id == coupon.Id);
  83. if (item != null)
  84. {
  85. item.CreateDate = DateTime.Now;
  86. item.UserId = order.UserId;
  87. item.UpdateDate = DateTime.Now.AddDays(180);
  88. Codes += item.ExchangeCode + ",";
  89. }
  90. }
  91. order.SnNos = Codes.TrimEnd(',');
  92. string ChangeRecordNo = "SC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  93. PosCouponForUser forUser = db.PosCouponForUser.FirstOrDefault(m => m.Id == order.UserId);
  94. if (forUser == null)
  95. {
  96. forUser = db.PosCouponForUser.Add(new PosCouponForUser()
  97. {
  98. Id = order.UserId,
  99. }).Entity;
  100. db.SaveChanges();
  101. }
  102. int BeforeOut = forUser.OutNum;
  103. int BeforeTotal = forUser.TotalNum;
  104. int BeforeStock = forUser.StockNum;
  105. forUser.OutNum += BuyCount;
  106. forUser.StockNum -= BuyCount;
  107. int AfterOut = forUser.OutNum;
  108. int AfterTotal = forUser.TotalNum;
  109. int AfterStock = forUser.StockNum;
  110. PosCouponOrders stat = db.PosCouponOrders.Add(new PosCouponOrders()
  111. {
  112. QueryCount = Kind,
  113. CreateDate = DateTime.Now,
  114. ChangeKind = 2,
  115. ChangeCount = BuyCount,
  116. AfterOut = AfterOut,
  117. AfterTotal = AfterTotal,
  118. AfterStock = AfterStock,
  119. BeforeOut = BeforeOut,
  120. BeforeTotal = BeforeTotal,
  121. BeforeStock = BeforeStock,
  122. OrderNo = ChangeRecordNo,
  123. ToUserId = order.UserId,
  124. FromUserId = 0,
  125. }).Entity;
  126. db.SaveChanges();
  127. RedisDbconn.Instance.Clear("Orders:" + order.Id);
  128. }
  129. }
  130. }
  131. }
  132. }
  133. db.Dispose();
  134. }
  135. else
  136. {
  137. op = false;
  138. }
  139. }
  140. }
  141. }
  142. }