AlipayPayBack2Service.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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.NormDetail == "电签POS")
  99. {
  100. Kind = 1;
  101. }
  102. else if (pro.NormDetail == "大POS")
  103. {
  104. Kind = 2;
  105. }
  106. }
  107. else if (pro.ProductId == 13)
  108. {
  109. if (pro.NormDetail == "电签POS")
  110. {
  111. Kind = 1;
  112. }
  113. else if (pro.NormDetail == "大POS")
  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.TotalNum += 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 = 1,
  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. int SelfBuy = db.OrderProduct.Count(m => m.UserId == order.UserId && m.OrderId != OrderId && (m.ProductId == 10 || m.ProductId == 11));
  173. if (SelfBuy == 1)
  174. {
  175. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  176. if (user != null)
  177. {
  178. int ParentUserId = user.ParentUserId;
  179. int machineCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.PosSnType == 0); //判断是否拥有3台购买机
  180. int ActiveCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  181. if (machineCount + ActiveCount >= 3)
  182. {
  183. function.WriteLog("OrderId:" + OrderId, "推荐下单奖励监控日志");
  184. function.WriteLog("order.Id:" + order.Id, "推荐下单奖励监控日志");
  185. function.WriteLog("ParentUserId:" + ParentUserId, "推荐下单奖励监控日志");
  186. function.WriteLog("order.UserId:" + order.UserId, "推荐下单奖励监控日志");
  187. function.WriteLog("\r\n\r\n", "推荐下单奖励监控日志");
  188. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == ParentUserId);
  189. if (account == null)
  190. {
  191. account = db.UserAccount.Add(new UserAccount()
  192. {
  193. Id = ParentUserId,
  194. UserId = ParentUserId,
  195. }).Entity;
  196. db.SaveChanges();
  197. }
  198. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  199. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  200. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  201. account.BalanceAmount += 100;
  202. account.TotalAmount += 100;
  203. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  204. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  205. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  206. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  207. {
  208. CreateDate = DateTime.Now,
  209. UpdateDate = DateTime.Now,
  210. UserId = ParentUserId, //创客
  211. ChangeType = 112, //变动类型
  212. ChangeAmount = 100, //变更金额
  213. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  214. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  215. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  216. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  217. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  218. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  219. }).Entity;
  220. db.SaveChanges();
  221. RedisDbconn.Instance.Set("UserAccount:" + ParentUserId, account);
  222. //推荐下单上级获得30天的机具循环天数
  223. 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).ToList();
  224. foreach (var subPos in posList)
  225. {
  226. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == subPos.Id);
  227. if (pos != null)
  228. {
  229. pos.RecycEndDate = pos.RecycEndDate.Value.AddDays(30);
  230. }
  231. }
  232. db.SaveChanges();
  233. }
  234. }
  235. }
  236. }
  237. }
  238. }
  239. }
  240. }
  241. db.Dispose();
  242. }
  243. public void checkOrder(int OrderId)
  244. {
  245. WebCMSEntities db = new WebCMSEntities();
  246. Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId);
  247. if (order != null)
  248. {
  249. if (order.Status == 0)
  250. {
  251. order.Status = 1;
  252. order.PayDate = DateTime.Now;
  253. order.PayStatus = 1;
  254. db.SaveChanges();
  255. RedisDbconn.Instance.Set("Orders:" + order.Id, order);
  256. }
  257. //机具券逻辑
  258. OrderProduct pro = db.OrderProduct.FirstOrDefault(m => m.OrderId == OrderId);
  259. if (pro != null)
  260. {
  261. if (pro.ProductId == 10 || pro.ProductId == 11 || pro.ProductId == 12 || pro.ProductId == 13)
  262. {
  263. if (order.Status == 1)
  264. {
  265. order.Status = 2;
  266. db.SaveChanges();
  267. }
  268. int BuyCount = pro.ProductCount;
  269. int Kind = 0;
  270. if (pro.ProductId == 10)
  271. {
  272. Kind = 1;
  273. }
  274. else if (pro.ProductId == 11)
  275. {
  276. Kind = 2;
  277. }
  278. else if (pro.ProductId == 12)
  279. {
  280. if (pro.NormDetail == "电签POS")
  281. {
  282. Kind = 1;
  283. }
  284. else if (pro.NormDetail == "大POS")
  285. {
  286. Kind = 2;
  287. }
  288. }
  289. else if (pro.ProductId == 13)
  290. {
  291. if (pro.NormDetail == "电签POS")
  292. {
  293. Kind = 1;
  294. }
  295. else if (pro.NormDetail == "大POS")
  296. {
  297. Kind = 2;
  298. }
  299. }
  300. string Codes = "";
  301. 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();
  302. foreach (var coupon in coupons)
  303. {
  304. PosCoupons item = db.PosCoupons.FirstOrDefault(m => m.Id == coupon.Id);
  305. if (item != null)
  306. {
  307. item.CreateDate = DateTime.Now;
  308. item.UserId = order.UserId;
  309. item.UpdateDate = DateTime.Now.AddDays(180);
  310. Codes += item.ExchangeCode + ",";
  311. }
  312. }
  313. order.SnNos = Codes.TrimEnd(',');
  314. string ChangeRecordNo = "SC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  315. PosCouponForUser forUser = db.PosCouponForUser.FirstOrDefault(m => m.Id == order.UserId);
  316. if (forUser == null)
  317. {
  318. forUser = db.PosCouponForUser.Add(new PosCouponForUser()
  319. {
  320. Id = order.UserId,
  321. }).Entity;
  322. db.SaveChanges();
  323. }
  324. int BeforeOut = forUser.OutNum;
  325. int BeforeTotal = forUser.TotalNum;
  326. int BeforeStock = forUser.StockNum;
  327. forUser.TotalNum += BuyCount;
  328. forUser.StockNum += BuyCount;
  329. int AfterOut = forUser.OutNum;
  330. int AfterTotal = forUser.TotalNum;
  331. int AfterStock = forUser.StockNum;
  332. PosCouponOrders stat = db.PosCouponOrders.Add(new PosCouponOrders()
  333. {
  334. QueryCount = Kind,
  335. CreateDate = DateTime.Now,
  336. ChangeKind = 1,
  337. ChangeCount = BuyCount,
  338. AfterOut = AfterOut,
  339. AfterTotal = AfterTotal,
  340. AfterStock = AfterStock,
  341. BeforeOut = BeforeOut,
  342. BeforeTotal = BeforeTotal,
  343. BeforeStock = BeforeStock,
  344. OrderNo = ChangeRecordNo,
  345. ToUserId = order.UserId,
  346. FromUserId = 0,
  347. }).Entity;
  348. db.SaveChanges();
  349. RedisDbconn.Instance.Clear("Orders:" + order.Id);
  350. }
  351. //推荐下单奖励
  352. if (pro.ProductId == 10 || pro.ProductId == 11)
  353. {
  354. int SelfBuy = db.OrderProduct.Count(m => m.UserId == order.UserId && m.OrderId != OrderId && (m.ProductId == 10 || m.ProductId == 11));
  355. if (SelfBuy == 1)
  356. {
  357. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  358. if (user != null)
  359. {
  360. int ParentUserId = user.ParentUserId;
  361. int machineCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.PosSnType == 0); //判断是否拥有3台购买机
  362. int ActiveCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  363. if (machineCount >= 3 || ActiveCount >= 3)
  364. {
  365. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == ParentUserId);
  366. if (account == null)
  367. {
  368. account = db.UserAccount.Add(new UserAccount()
  369. {
  370. Id = ParentUserId,
  371. UserId = ParentUserId,
  372. }).Entity;
  373. db.SaveChanges();
  374. }
  375. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  376. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  377. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  378. account.BalanceAmount += 100;
  379. account.TotalAmount += 100;
  380. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  381. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  382. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  383. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  384. {
  385. CreateDate = DateTime.Now,
  386. UpdateDate = DateTime.Now,
  387. UserId = ParentUserId, //创客
  388. ChangeType = 112, //变动类型
  389. ChangeAmount = 100, //变更金额
  390. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  391. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  392. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  393. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  394. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  395. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  396. }).Entity;
  397. db.SaveChanges();
  398. RedisDbconn.Instance.Set("UserAccount:" + ParentUserId, account);
  399. //推荐下单上级获得30天的机具循环天数
  400. 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);
  401. foreach (var subPos in posList)
  402. {
  403. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == subPos.Id);
  404. if (pos != null)
  405. {
  406. pos.RecycEndDate = pos.RecycEndDate.Value.AddDays(30);
  407. }
  408. }
  409. db.SaveChanges();
  410. }
  411. }
  412. }
  413. }
  414. }
  415. }
  416. db.Dispose();
  417. }
  418. }
  419. }