AlipayPayBack2Service.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using MySystem.PxcModels;
  6. using Library;
  7. using LitJson;
  8. namespace MySystem
  9. {
  10. public class AlipayPayBack2Service
  11. {
  12. public readonly static AlipayPayBack2Service Instance = new AlipayPayBack2Service();
  13. private AlipayPayBack2Service()
  14. { }
  15. public void Start(JobMqMsg jobInfo)
  16. {
  17. string content = "";
  18. try
  19. {
  20. dosomething();
  21. string Msg = "success";
  22. jobInfo.Status = Msg == "success" ? 1 : 0;
  23. jobInfo.Msg = Msg == "success" ? "执行完成" : Msg;
  24. RabbitMQClient.Instance.SendMsg(Newtonsoft.Json.JsonConvert.SerializeObject(jobInfo), "PublicBack");
  25. }
  26. catch (Exception ex)
  27. {
  28. if (!string.IsNullOrEmpty(content))
  29. {
  30. Dictionary<string, string> data = new Dictionary<string, string>();
  31. data.Add("ErrTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  32. data.Add("ErrMsg", ex.ToString());
  33. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(data), "public_err");
  34. }
  35. else
  36. {
  37. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "商城订单支付回调异常");
  38. }
  39. }
  40. }
  41. private void dosomething()
  42. {
  43. bool op = true;
  44. while (op)
  45. {
  46. string content = RedisDbconn.Instance.RPop<string>("PayCallBack2");
  47. if (!string.IsNullOrEmpty(content))
  48. {
  49. sloveAlipayCallBack(content);
  50. }
  51. else
  52. {
  53. op = false;
  54. }
  55. }
  56. }
  57. public void sloveAlipayCallBack(string content)
  58. {
  59. JsonData jsonObj = JsonMapper.ToObject(content);
  60. string OrderNo = jsonObj["out_trade_no"].ToString();
  61. string TradeNo = jsonObj["transaction_id"].ToString();
  62. decimal TotalFee = decimal.Parse(function.CheckNum(jsonObj["total_fee"].ToString()));
  63. WebCMSEntities db = new WebCMSEntities();
  64. OrderForNo forNo = db.OrderForNo.FirstOrDefault(m => m.OrderNo == OrderNo);
  65. if (forNo != null)
  66. {
  67. string[] ids = forNo.OrderIds.Split(',');
  68. foreach (string idString in ids)
  69. {
  70. int OrderId = int.Parse(idString);
  71. DoOrder(db, OrderId);
  72. }
  73. }
  74. db.Dispose();
  75. }
  76. public void DoOrder(WebCMSEntities db, int OrderId)
  77. {
  78. Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId && m.Status == 0);
  79. if (order != null)
  80. {
  81. order.Status = 1;
  82. order.PayDate = DateTime.Now;
  83. order.PayStatus = 1;
  84. db.SaveChanges();
  85. RedisDbconn.Instance.Set("Orders:" + order.Id, order);
  86. //机具券逻辑
  87. OrderProduct pro = db.OrderProduct.FirstOrDefault(m => m.OrderId == OrderId);
  88. if (pro != null)
  89. {
  90. if (pro.ProductId == 10 || pro.ProductId == 11 || pro.ProductId == 12 || pro.ProductId == 13)
  91. {
  92. order.Status = 2;
  93. db.SaveChanges();
  94. int BuyCount = pro.ProductCount;
  95. int Kind = 0;
  96. if (pro.ProductId == 10)
  97. {
  98. Kind = 1;
  99. }
  100. else if (pro.ProductId == 11)
  101. {
  102. Kind = 2;
  103. }
  104. else if (pro.ProductId == 12)
  105. {
  106. if (pro.NormDetail == "电签POS")
  107. {
  108. Kind = 1;
  109. }
  110. else if (pro.NormDetail == "大POS")
  111. {
  112. Kind = 2;
  113. }
  114. }
  115. else if (pro.ProductId == 13)
  116. {
  117. if (pro.NormDetail == "300电签POS")
  118. {
  119. Kind = 1;
  120. }
  121. else if (pro.NormDetail == "200大POS")
  122. {
  123. Kind = 2;
  124. }
  125. else if (pro.NormDetail == "50大机券+225电签券")
  126. {
  127. Kind = 3;
  128. }
  129. else if (pro.NormDetail == "100大机券+150电签券")
  130. {
  131. Kind = 4;
  132. }
  133. else if (pro.NormDetail == "150大机券+75电签券")
  134. {
  135. Kind = 5;
  136. }
  137. }
  138. string Codes = "";
  139. if(Kind > 2)
  140. {
  141. Dictionary<int, int> couponData = new Dictionary<int, int>();
  142. if(Kind == 3)
  143. {
  144. couponData.Add(1, 225); //电签数量
  145. couponData.Add(2, 50); //大机数量
  146. }
  147. else if(Kind == 4)
  148. {
  149. couponData.Add(1, 150); //电签数量
  150. couponData.Add(2, 100); //大机数量
  151. }
  152. else if(Kind == 5)
  153. {
  154. couponData.Add(1, 75); //电签数量
  155. couponData.Add(2, 150); //大机数量
  156. }
  157. foreach(int kindNum in couponData.Keys)
  158. {
  159. int BuyCountByKind = couponData[kindNum];
  160. var coupons = db.PosCoupons.Where(m => m.IsLock == 0 && m.IsUse == 0 && m.UserId == 0 && m.QueryCount == kindNum).OrderBy(m => m.Id).Take(BuyCountByKind).ToList();
  161. foreach (var coupon in coupons)
  162. {
  163. PosCoupons item = db.PosCoupons.FirstOrDefault(m => m.Id == coupon.Id);
  164. if (item != null)
  165. {
  166. item.CreateDate = DateTime.Now;
  167. item.UserId = order.UserId;
  168. item.UpdateDate = DateTime.Now.AddDays(180);
  169. if (pro.ProductId == 13)
  170. {
  171. item.LeaderUserId = order.UserId;
  172. }
  173. Codes += item.ExchangeCode + ",";
  174. }
  175. }
  176. }
  177. }
  178. else
  179. {
  180. 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();
  181. foreach (var coupon in coupons)
  182. {
  183. PosCoupons item = db.PosCoupons.FirstOrDefault(m => m.Id == coupon.Id);
  184. if (item != null)
  185. {
  186. item.CreateDate = DateTime.Now;
  187. item.UserId = order.UserId;
  188. item.UpdateDate = DateTime.Now.AddDays(180);
  189. if (pro.ProductId == 13)
  190. {
  191. item.LeaderUserId = order.UserId;
  192. }
  193. Codes += item.ExchangeCode + ",";
  194. }
  195. }
  196. }
  197. order.SnNos = Codes.TrimEnd(',');
  198. string ChangeRecordNo = "SC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  199. PosCouponForUser forUser = db.PosCouponForUser.FirstOrDefault(m => m.Id == order.UserId);
  200. if (forUser == null)
  201. {
  202. forUser = db.PosCouponForUser.Add(new PosCouponForUser()
  203. {
  204. Id = order.UserId,
  205. }).Entity;
  206. db.SaveChanges();
  207. }
  208. int BeforeOut = forUser.OutNum;
  209. int BeforeTotal = forUser.TotalNum;
  210. int BeforeStock = forUser.StockNum;
  211. forUser.TotalNum += BuyCount;
  212. forUser.StockNum += BuyCount;
  213. int AfterOut = forUser.OutNum;
  214. int AfterTotal = forUser.TotalNum;
  215. int AfterStock = forUser.StockNum;
  216. PosCouponOrders stat = db.PosCouponOrders.Add(new PosCouponOrders()
  217. {
  218. QueryCount = Kind,
  219. CreateDate = DateTime.Now,
  220. ChangeKind = 1,
  221. ChangeCount = BuyCount,
  222. AfterOut = AfterOut,
  223. AfterTotal = AfterTotal,
  224. AfterStock = AfterStock,
  225. BeforeOut = BeforeOut,
  226. BeforeTotal = BeforeTotal,
  227. BeforeStock = BeforeStock,
  228. OrderNo = ChangeRecordNo,
  229. ToUserId = order.UserId,
  230. FromUserId = 0,
  231. }).Entity;
  232. db.SaveChanges();
  233. RedisDbconn.Instance.Clear("Orders:" + order.Id);
  234. }
  235. //推荐下单奖励
  236. if (pro.ProductId == 10 || pro.ProductId == 11)
  237. {
  238. bool checkPrize = db.UserAccountRecord.Any(m => m.QueryCount == OrderId);
  239. int SelfBuy = 0;
  240. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].ToString();
  241. DataTable stat = OtherMySqlConn.dtable("select count(Id) from Orders where Id in (select OrderId from OrderProduct where UserId=" + order.UserId + " and (ProductId=10 or ProductId=11)) and Status>0 and TotalPrice>0");
  242. if (stat.Rows.Count > 0)
  243. {
  244. SelfBuy = int.Parse(function.CheckInt(stat.Rows[0][0].ToString()));
  245. }
  246. // OtherMySqlConn.connstr = ;
  247. function.WriteLog("OrderId:" + OrderId, "推荐下单奖励监控日志");
  248. function.WriteLog("checkPrize:" + checkPrize, "推荐下单奖励监控日志");
  249. function.WriteLog("SelfBuy:" + SelfBuy, "推荐下单奖励监控日志");
  250. if (SelfBuy == 1 && !checkPrize)
  251. {
  252. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  253. if (user != null)
  254. {
  255. int ParentUserId = user.ParentUserId;
  256. while(ParentUserId > 0)
  257. {
  258. int machineCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.PosSnType == 0); //判断是否拥有3台购买机
  259. int ActiveCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  260. int couponCount = db.PosCoupons.Count(m => m.UserId == ParentUserId && m.IsUse == 0); //判断是否拥有3张券
  261. function.WriteLog("MakerCode:" + user.MakerCode, "推荐下单奖励监控日志");
  262. function.WriteLog("machineCount:" + machineCount, "推荐下单奖励监控日志");
  263. function.WriteLog("ActiveCount:" + ActiveCount, "推荐下单奖励监控日志");
  264. if (machineCount + ActiveCount + couponCount >= 3)
  265. {
  266. function.WriteLog("满足条件", "推荐下单奖励监控日志");
  267. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == ParentUserId);
  268. if (account == null)
  269. {
  270. account = db.UserAccount.Add(new UserAccount()
  271. {
  272. Id = ParentUserId,
  273. UserId = ParentUserId,
  274. }).Entity;
  275. db.SaveChanges();
  276. }
  277. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  278. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  279. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  280. account.BalanceAmount += 100;
  281. account.TotalAmount += 100;
  282. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  283. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  284. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  285. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  286. {
  287. CreateDate = DateTime.Now,
  288. UpdateDate = DateTime.Now,
  289. UserId = ParentUserId, //创客
  290. ChangeType = 112, //变动类型
  291. ChangeAmount = 100, //变更金额
  292. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  293. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  294. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  295. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  296. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  297. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  298. QueryCount = OrderId,
  299. }).Entity;
  300. db.SaveChanges();
  301. RedisDbconn.Instance.Set("UserAccount:" + ParentUserId, account);
  302. //推荐下单上级获得30天的机具循环天数
  303. 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();
  304. foreach (var subPos in posList)
  305. {
  306. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == subPos.Id);
  307. if (pos != null)
  308. {
  309. pos.RecycEndDate = pos.RecycEndDate.Value.AddDays(30);
  310. }
  311. }
  312. db.SaveChanges();
  313. ParentUserId = 0;
  314. }
  315. else
  316. {
  317. Users puser = db.Users.FirstOrDefault(m => m.Id == ParentUserId) ?? new Users();
  318. ParentUserId = puser.ParentUserId;
  319. }
  320. }
  321. }
  322. }
  323. function.WriteLog("\r\n\r\n", "推荐下单奖励监控日志");
  324. }
  325. }
  326. }
  327. }
  328. public void checkOrder(int OrderId)
  329. {
  330. WebCMSEntities db = new WebCMSEntities();
  331. DoOrder(db, OrderId);
  332. db.Dispose();
  333. }
  334. public void addcoupon(int OrderId)
  335. {
  336. WebCMSEntities db = new WebCMSEntities();
  337. Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId);
  338. if (order != null)
  339. {
  340. //机具券逻辑
  341. OrderProduct pro = db.OrderProduct.FirstOrDefault(m => m.OrderId == OrderId);
  342. if (pro != null)
  343. {
  344. if (pro.ProductId == 10 || pro.ProductId == 11 || pro.ProductId == 12 || pro.ProductId == 13)
  345. {
  346. int BuyCount = pro.ProductCount;
  347. int Kind = 0;
  348. if (pro.ProductId == 10)
  349. {
  350. Kind = 1;
  351. }
  352. else if (pro.ProductId == 11)
  353. {
  354. Kind = 2;
  355. }
  356. else if (pro.ProductId == 12)
  357. {
  358. if (pro.NormDetail == "电签POS")
  359. {
  360. Kind = 1;
  361. }
  362. else if (pro.NormDetail == "大POS")
  363. {
  364. Kind = 2;
  365. }
  366. }
  367. else if (pro.ProductId == 13)
  368. {
  369. if (pro.NormDetail == "电签POS")
  370. {
  371. Kind = 1;
  372. }
  373. else if (pro.NormDetail == "大POS")
  374. {
  375. Kind = 2;
  376. }
  377. }
  378. string Codes = "";
  379. 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();
  380. foreach (var coupon in coupons)
  381. {
  382. PosCoupons item = db.PosCoupons.FirstOrDefault(m => m.Id == coupon.Id);
  383. if (item != null)
  384. {
  385. item.CreateDate = DateTime.Now;
  386. item.UserId = order.UserId;
  387. item.UpdateDate = DateTime.Now.AddDays(180);
  388. Codes += item.ExchangeCode + ",";
  389. }
  390. }
  391. order.SnNos = Codes.TrimEnd(',');
  392. string ChangeRecordNo = "SC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  393. PosCouponForUser forUser = db.PosCouponForUser.FirstOrDefault(m => m.Id == order.UserId);
  394. if (forUser == null)
  395. {
  396. forUser = db.PosCouponForUser.Add(new PosCouponForUser()
  397. {
  398. Id = order.UserId,
  399. }).Entity;
  400. db.SaveChanges();
  401. }
  402. int BeforeOut = forUser.OutNum;
  403. int BeforeTotal = forUser.TotalNum;
  404. int BeforeStock = forUser.StockNum;
  405. forUser.TotalNum += BuyCount;
  406. forUser.StockNum += BuyCount;
  407. int AfterOut = forUser.OutNum;
  408. int AfterTotal = forUser.TotalNum;
  409. int AfterStock = forUser.StockNum;
  410. PosCouponOrders stat = db.PosCouponOrders.Add(new PosCouponOrders()
  411. {
  412. QueryCount = Kind,
  413. CreateDate = DateTime.Now,
  414. ChangeKind = 1,
  415. ChangeCount = BuyCount,
  416. AfterOut = AfterOut,
  417. AfterTotal = AfterTotal,
  418. AfterStock = AfterStock,
  419. BeforeOut = BeforeOut,
  420. BeforeTotal = BeforeTotal,
  421. BeforeStock = BeforeStock,
  422. OrderNo = ChangeRecordNo,
  423. ToUserId = order.UserId,
  424. FromUserId = 0,
  425. }).Entity;
  426. db.SaveChanges();
  427. RedisDbconn.Instance.Clear("Orders:" + order.Id);
  428. }
  429. //推荐下单奖励
  430. if (pro.ProductId == 10 || pro.ProductId == 11)
  431. {
  432. int SelfBuy = db.OrderProduct.Count(m => m.UserId == order.UserId && m.OrderId != OrderId && (m.ProductId == 10 || m.ProductId == 11));
  433. if (SelfBuy == 1)
  434. {
  435. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  436. if (user != null)
  437. {
  438. int ParentUserId = user.ParentUserId;
  439. int machineCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.PosSnType == 0); //判断是否拥有3台购买机
  440. int ActiveCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  441. int couponCount = db.PosCoupons.Count(m => m.UserId == ParentUserId && m.IsUse == 0); //判断是否拥有3张券
  442. if (machineCount + ActiveCount + couponCount >= 3)
  443. {
  444. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == ParentUserId);
  445. if (account == null)
  446. {
  447. account = db.UserAccount.Add(new UserAccount()
  448. {
  449. Id = ParentUserId,
  450. UserId = ParentUserId,
  451. }).Entity;
  452. db.SaveChanges();
  453. }
  454. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  455. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  456. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  457. account.BalanceAmount += 100;
  458. account.TotalAmount += 100;
  459. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  460. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  461. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  462. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  463. {
  464. CreateDate = DateTime.Now,
  465. UpdateDate = DateTime.Now,
  466. UserId = ParentUserId, //创客
  467. ChangeType = 112, //变动类型
  468. ChangeAmount = 100, //变更金额
  469. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  470. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  471. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  472. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  473. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  474. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  475. }).Entity;
  476. db.SaveChanges();
  477. RedisDbconn.Instance.Set("UserAccount:" + ParentUserId, account);
  478. //推荐下单上级获得30天的机具循环天数
  479. 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);
  480. foreach (var subPos in posList)
  481. {
  482. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == subPos.Id);
  483. if (pos != null)
  484. {
  485. pos.RecycEndDate = pos.RecycEndDate.Value.AddDays(30);
  486. }
  487. }
  488. db.SaveChanges();
  489. }
  490. }
  491. }
  492. }
  493. }
  494. }
  495. db.Dispose();
  496. }
  497. }
  498. }