AlipayPayBack2Service.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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(), "商城订单支付回调异常");
  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. sloveAlipayCallBack(content);
  49. }
  50. else
  51. {
  52. op = false;
  53. }
  54. }
  55. }
  56. public void sloveAlipayCallBack(string content)
  57. {
  58. JsonData jsonObj = JsonMapper.ToObject(content);
  59. string OrderNo = jsonObj["out_trade_no"].ToString();
  60. string TradeNo = jsonObj["transaction_id"].ToString();
  61. decimal TotalFee = decimal.Parse(function.CheckNum(jsonObj["total_fee"].ToString()));
  62. WebCMSEntities db = new WebCMSEntities();
  63. OrderForNo forNo = db.OrderForNo.FirstOrDefault(m => m.OrderNo == OrderNo);
  64. if (forNo != null)
  65. {
  66. string[] ids = forNo.OrderIds.Split(',');
  67. foreach (string idString in ids)
  68. {
  69. int OrderId = int.Parse(idString);
  70. Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId && m.Status == 0);
  71. if (order != null)
  72. {
  73. order.Status = 1;
  74. order.PayDate = DateTime.Now;
  75. order.PayStatus = 1;
  76. db.SaveChanges();
  77. RedisDbconn.Instance.Set("Orders:" + order.Id, order);
  78. //机具券逻辑
  79. OrderProduct pro = db.OrderProduct.FirstOrDefault(m => m.OrderId == OrderId);
  80. if (pro != null)
  81. {
  82. if (pro.ProductId == 10 || pro.ProductId == 11 || pro.ProductId == 12 || pro.ProductId == 13)
  83. {
  84. order.Status = 2;
  85. db.SaveChanges();
  86. int BuyCount = pro.ProductCount;
  87. int Kind = 0;
  88. if (pro.ProductId == 10)
  89. {
  90. Kind = 1;
  91. }
  92. else if (pro.ProductId == 11)
  93. {
  94. Kind = 2;
  95. }
  96. else if (pro.ProductId == 12)
  97. {
  98. if (pro.NormId == "12")
  99. {
  100. Kind = 1;
  101. }
  102. else if (pro.NormId == "13")
  103. {
  104. Kind = 2;
  105. }
  106. }
  107. else if (pro.ProductId == 13)
  108. {
  109. if (pro.NormId == "14")
  110. {
  111. Kind = 1;
  112. }
  113. else if (pro.NormId == "15")
  114. {
  115. Kind = 2;
  116. }
  117. }
  118. string Codes = "";
  119. 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();
  120. foreach (var coupon in coupons)
  121. {
  122. PosCoupons item = db.PosCoupons.FirstOrDefault(m => m.Id == coupon.Id);
  123. if (item != null)
  124. {
  125. item.CreateDate = DateTime.Now;
  126. item.UserId = order.UserId;
  127. item.UpdateDate = DateTime.Now.AddDays(180);
  128. Codes += item.ExchangeCode + ",";
  129. }
  130. }
  131. order.SnNos = Codes.TrimEnd(',');
  132. string ChangeRecordNo = "SC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  133. PosCouponForUser forUser = db.PosCouponForUser.FirstOrDefault(m => m.Id == order.UserId);
  134. if (forUser == null)
  135. {
  136. forUser = db.PosCouponForUser.Add(new PosCouponForUser()
  137. {
  138. Id = order.UserId,
  139. }).Entity;
  140. db.SaveChanges();
  141. }
  142. int BeforeOut = forUser.OutNum;
  143. int BeforeTotal = forUser.TotalNum;
  144. int BeforeStock = forUser.StockNum;
  145. forUser.OutNum += BuyCount;
  146. forUser.StockNum -= BuyCount;
  147. int AfterOut = forUser.OutNum;
  148. int AfterTotal = forUser.TotalNum;
  149. int AfterStock = forUser.StockNum;
  150. PosCouponOrders stat = db.PosCouponOrders.Add(new PosCouponOrders()
  151. {
  152. QueryCount = Kind,
  153. CreateDate = DateTime.Now,
  154. ChangeKind = 2,
  155. ChangeCount = BuyCount,
  156. AfterOut = AfterOut,
  157. AfterTotal = AfterTotal,
  158. AfterStock = AfterStock,
  159. BeforeOut = BeforeOut,
  160. BeforeTotal = BeforeTotal,
  161. BeforeStock = BeforeStock,
  162. OrderNo = ChangeRecordNo,
  163. ToUserId = order.UserId,
  164. FromUserId = 0,
  165. }).Entity;
  166. db.SaveChanges();
  167. RedisDbconn.Instance.Clear("Orders:" + order.Id);
  168. }
  169. //推荐下单奖励
  170. if (pro.ProductId == 10 || pro.ProductId == 11)
  171. {
  172. bool SelfBuy = db.OrderProduct.Any(m => m.UserId == order.UserId && m.OrderId != OrderId && (m.ProductId == 10 || m.ProductId == 11));
  173. if (!SelfBuy)
  174. {
  175. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  176. if (user != null)
  177. {
  178. int ParentUserId = user.ParentUserId;
  179. bool ParentBuy = db.OrderProduct.Any(m => m.UserId == ParentUserId && (m.ProductId == 10 || m.ProductId == 11));
  180. if (ParentBuy)
  181. {
  182. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == ParentUserId);
  183. if (account == null)
  184. {
  185. account = db.UserAccount.Add(new UserAccount()
  186. {
  187. Id = ParentUserId,
  188. UserId = ParentUserId,
  189. }).Entity;
  190. db.SaveChanges();
  191. }
  192. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  193. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  194. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  195. account.BalanceAmount += 100;
  196. account.TotalAmount += 100;
  197. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  198. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  199. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  200. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  201. {
  202. CreateDate = DateTime.Now,
  203. UpdateDate = DateTime.Now,
  204. UserId = ParentUserId, //创客
  205. ChangeType = 112, //变动类型
  206. ChangeAmount = 100, //变更金额
  207. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  208. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  209. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  210. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  211. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  212. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  213. }).Entity;
  214. db.SaveChanges();
  215. RedisDbconn.Instance.Set("UserAccount:" + ParentUserId, account);
  216. //推荐下单上级获得30天的机具循环天数
  217. var posList = db.PosMachinesTwo.Select(m => new { m.Id, m.UserId, m.BindingState, m.RecycEndDate }).Where(m => m.UserId == ParentUserId && m.BindingState == 0 && m.RecycEndDate != null);
  218. foreach (var subPos in posList)
  219. {
  220. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == subPos.Id);
  221. if (pos != null)
  222. {
  223. pos.RecycEndDate = pos.RecycEndDate.Value.AddDays(30);
  224. }
  225. }
  226. db.SaveChanges();
  227. }
  228. }
  229. }
  230. }
  231. }
  232. }
  233. }
  234. }
  235. db.Dispose();
  236. }
  237. }
  238. }