AlipayPayBack2Service.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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. bool checkPrize = db.UserAccountRecord.Any(m => m.QueryCount == OrderId);
  173. int SelfBuy = db.OrderProduct.Count(m => m.UserId == order.UserId && (m.ProductId == 10 || m.ProductId == 11));
  174. function.WriteLog("OrderId:" + OrderId, "推荐下单奖励监控日志");
  175. function.WriteLog("checkPrize:" + checkPrize, "推荐下单奖励监控日志");
  176. function.WriteLog("SelfBuy:" + SelfBuy, "推荐下单奖励监控日志");
  177. if (SelfBuy == 1 && !checkPrize)
  178. {
  179. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  180. if (user != null)
  181. {
  182. int ParentUserId = user.ParentUserId;
  183. while(ParentUserId > 0)
  184. {
  185. int machineCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.PosSnType == 0); //判断是否拥有3台购买机
  186. int ActiveCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  187. int couponCount = db.PosCoupons.Count(m => m.UserId == ParentUserId && m.IsUse == 0); //判断是否拥有3张券
  188. function.WriteLog("MakerCode:" + user.MakerCode, "推荐下单奖励监控日志");
  189. function.WriteLog("machineCount:" + machineCount, "推荐下单奖励监控日志");
  190. function.WriteLog("ActiveCount:" + ActiveCount, "推荐下单奖励监控日志");
  191. if (machineCount + ActiveCount + couponCount >= 3)
  192. {
  193. function.WriteLog("满足条件", "推荐下单奖励监控日志");
  194. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == ParentUserId);
  195. if (account == null)
  196. {
  197. account = db.UserAccount.Add(new UserAccount()
  198. {
  199. Id = ParentUserId,
  200. UserId = ParentUserId,
  201. }).Entity;
  202. db.SaveChanges();
  203. }
  204. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  205. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  206. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  207. account.BalanceAmount += 100;
  208. account.TotalAmount += 100;
  209. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  210. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  211. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  212. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  213. {
  214. CreateDate = DateTime.Now,
  215. UpdateDate = DateTime.Now,
  216. UserId = ParentUserId, //创客
  217. ChangeType = 112, //变动类型
  218. ChangeAmount = 100, //变更金额
  219. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  220. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  221. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  222. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  223. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  224. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  225. QueryCount = OrderId,
  226. }).Entity;
  227. db.SaveChanges();
  228. RedisDbconn.Instance.Set("UserAccount:" + ParentUserId, account);
  229. //推荐下单上级获得30天的机具循环天数
  230. 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();
  231. foreach (var subPos in posList)
  232. {
  233. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == subPos.Id);
  234. if (pos != null)
  235. {
  236. pos.RecycEndDate = pos.RecycEndDate.Value.AddDays(30);
  237. }
  238. }
  239. db.SaveChanges();
  240. ParentUserId = 0;
  241. }
  242. else
  243. {
  244. Users puser = db.Users.FirstOrDefault(m => m.Id == ParentUserId) ?? new Users();
  245. ParentUserId = puser.ParentUserId;
  246. }
  247. }
  248. }
  249. }
  250. function.WriteLog("\r\n\r\n", "推荐下单奖励监控日志");
  251. }
  252. }
  253. }
  254. }
  255. }
  256. db.Dispose();
  257. }
  258. public void checkOrder(int OrderId)
  259. {
  260. WebCMSEntities db = new WebCMSEntities();
  261. Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId);
  262. if (order != null)
  263. {
  264. if (order.Status == 0)
  265. {
  266. order.Status = 1;
  267. order.PayDate = DateTime.Now;
  268. order.PayStatus = 1;
  269. db.SaveChanges();
  270. RedisDbconn.Instance.Set("Orders:" + order.Id, order);
  271. }
  272. //机具券逻辑
  273. OrderProduct pro = db.OrderProduct.FirstOrDefault(m => m.OrderId == OrderId);
  274. if (pro != null)
  275. {
  276. if (pro.ProductId == 10 || pro.ProductId == 11 || pro.ProductId == 12 || pro.ProductId == 13)
  277. {
  278. if (order.Status == 1)
  279. {
  280. order.Status = 2;
  281. db.SaveChanges();
  282. }
  283. int BuyCount = pro.ProductCount;
  284. int Kind = 0;
  285. if (pro.ProductId == 10)
  286. {
  287. Kind = 1;
  288. }
  289. else if (pro.ProductId == 11)
  290. {
  291. Kind = 2;
  292. }
  293. else if (pro.ProductId == 12)
  294. {
  295. if (pro.NormDetail == "电签POS")
  296. {
  297. Kind = 1;
  298. }
  299. else if (pro.NormDetail == "大POS")
  300. {
  301. Kind = 2;
  302. }
  303. }
  304. else if (pro.ProductId == 13)
  305. {
  306. if (pro.NormDetail == "电签POS")
  307. {
  308. Kind = 1;
  309. }
  310. else if (pro.NormDetail == "大POS")
  311. {
  312. Kind = 2;
  313. }
  314. }
  315. string Codes = "";
  316. 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();
  317. foreach (var coupon in coupons)
  318. {
  319. PosCoupons item = db.PosCoupons.FirstOrDefault(m => m.Id == coupon.Id);
  320. if (item != null)
  321. {
  322. item.CreateDate = DateTime.Now;
  323. item.UserId = order.UserId;
  324. item.UpdateDate = DateTime.Now.AddDays(180);
  325. Codes += item.ExchangeCode + ",";
  326. }
  327. }
  328. order.SnNos = Codes.TrimEnd(',');
  329. string ChangeRecordNo = "SC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  330. PosCouponForUser forUser = db.PosCouponForUser.FirstOrDefault(m => m.Id == order.UserId);
  331. if (forUser == null)
  332. {
  333. forUser = db.PosCouponForUser.Add(new PosCouponForUser()
  334. {
  335. Id = order.UserId,
  336. }).Entity;
  337. db.SaveChanges();
  338. }
  339. int BeforeOut = forUser.OutNum;
  340. int BeforeTotal = forUser.TotalNum;
  341. int BeforeStock = forUser.StockNum;
  342. forUser.TotalNum += BuyCount;
  343. forUser.StockNum += BuyCount;
  344. int AfterOut = forUser.OutNum;
  345. int AfterTotal = forUser.TotalNum;
  346. int AfterStock = forUser.StockNum;
  347. PosCouponOrders stat = db.PosCouponOrders.Add(new PosCouponOrders()
  348. {
  349. QueryCount = Kind,
  350. CreateDate = DateTime.Now,
  351. ChangeKind = 1,
  352. ChangeCount = BuyCount,
  353. AfterOut = AfterOut,
  354. AfterTotal = AfterTotal,
  355. AfterStock = AfterStock,
  356. BeforeOut = BeforeOut,
  357. BeforeTotal = BeforeTotal,
  358. BeforeStock = BeforeStock,
  359. OrderNo = ChangeRecordNo,
  360. ToUserId = order.UserId,
  361. FromUserId = 0,
  362. }).Entity;
  363. db.SaveChanges();
  364. RedisDbconn.Instance.Clear("Orders:" + order.Id);
  365. }
  366. //推荐下单奖励
  367. if (pro.ProductId == 10 || pro.ProductId == 11)
  368. {
  369. int SelfBuy = db.OrderProduct.Count(m => m.UserId == order.UserId && m.OrderId != OrderId && (m.ProductId == 10 || m.ProductId == 11));
  370. if (SelfBuy == 1)
  371. {
  372. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  373. if (user != null)
  374. {
  375. int ParentUserId = user.ParentUserId;
  376. int machineCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.PosSnType == 0); //判断是否拥有3台购买机
  377. int ActiveCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  378. int couponCount = db.PosCoupons.Count(m => m.UserId == ParentUserId && m.IsUse == 0); //判断是否拥有3张券
  379. if (machineCount + ActiveCount + couponCount >= 3)
  380. {
  381. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == ParentUserId);
  382. if (account == null)
  383. {
  384. account = db.UserAccount.Add(new UserAccount()
  385. {
  386. Id = ParentUserId,
  387. UserId = ParentUserId,
  388. }).Entity;
  389. db.SaveChanges();
  390. }
  391. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  392. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  393. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  394. account.BalanceAmount += 100;
  395. account.TotalAmount += 100;
  396. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  397. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  398. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  399. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  400. {
  401. CreateDate = DateTime.Now,
  402. UpdateDate = DateTime.Now,
  403. UserId = ParentUserId, //创客
  404. ChangeType = 112, //变动类型
  405. ChangeAmount = 100, //变更金额
  406. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  407. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  408. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  409. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  410. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  411. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  412. }).Entity;
  413. db.SaveChanges();
  414. RedisDbconn.Instance.Set("UserAccount:" + ParentUserId, account);
  415. //推荐下单上级获得30天的机具循环天数
  416. 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);
  417. foreach (var subPos in posList)
  418. {
  419. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == subPos.Id);
  420. if (pos != null)
  421. {
  422. pos.RecycEndDate = pos.RecycEndDate.Value.AddDays(30);
  423. }
  424. }
  425. db.SaveChanges();
  426. }
  427. }
  428. }
  429. }
  430. }
  431. }
  432. db.Dispose();
  433. }
  434. public void addcoupon(int OrderId)
  435. {
  436. WebCMSEntities db = new WebCMSEntities();
  437. Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId);
  438. if (order != null)
  439. {
  440. //机具券逻辑
  441. OrderProduct pro = db.OrderProduct.FirstOrDefault(m => m.OrderId == OrderId);
  442. if (pro != null)
  443. {
  444. if (pro.ProductId == 10 || pro.ProductId == 11 || pro.ProductId == 12 || pro.ProductId == 13)
  445. {
  446. int BuyCount = pro.ProductCount;
  447. int Kind = 0;
  448. if (pro.ProductId == 10)
  449. {
  450. Kind = 1;
  451. }
  452. else if (pro.ProductId == 11)
  453. {
  454. Kind = 2;
  455. }
  456. else if (pro.ProductId == 12)
  457. {
  458. if (pro.NormDetail == "电签POS")
  459. {
  460. Kind = 1;
  461. }
  462. else if (pro.NormDetail == "大POS")
  463. {
  464. Kind = 2;
  465. }
  466. }
  467. else if (pro.ProductId == 13)
  468. {
  469. if (pro.NormDetail == "电签POS")
  470. {
  471. Kind = 1;
  472. }
  473. else if (pro.NormDetail == "大POS")
  474. {
  475. Kind = 2;
  476. }
  477. }
  478. string Codes = "";
  479. 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();
  480. foreach (var coupon in coupons)
  481. {
  482. PosCoupons item = db.PosCoupons.FirstOrDefault(m => m.Id == coupon.Id);
  483. if (item != null)
  484. {
  485. item.CreateDate = DateTime.Now;
  486. item.UserId = order.UserId;
  487. item.UpdateDate = DateTime.Now.AddDays(180);
  488. Codes += item.ExchangeCode + ",";
  489. }
  490. }
  491. order.SnNos = Codes.TrimEnd(',');
  492. string ChangeRecordNo = "SC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  493. PosCouponForUser forUser = db.PosCouponForUser.FirstOrDefault(m => m.Id == order.UserId);
  494. if (forUser == null)
  495. {
  496. forUser = db.PosCouponForUser.Add(new PosCouponForUser()
  497. {
  498. Id = order.UserId,
  499. }).Entity;
  500. db.SaveChanges();
  501. }
  502. int BeforeOut = forUser.OutNum;
  503. int BeforeTotal = forUser.TotalNum;
  504. int BeforeStock = forUser.StockNum;
  505. forUser.TotalNum += BuyCount;
  506. forUser.StockNum += BuyCount;
  507. int AfterOut = forUser.OutNum;
  508. int AfterTotal = forUser.TotalNum;
  509. int AfterStock = forUser.StockNum;
  510. PosCouponOrders stat = db.PosCouponOrders.Add(new PosCouponOrders()
  511. {
  512. QueryCount = Kind,
  513. CreateDate = DateTime.Now,
  514. ChangeKind = 1,
  515. ChangeCount = BuyCount,
  516. AfterOut = AfterOut,
  517. AfterTotal = AfterTotal,
  518. AfterStock = AfterStock,
  519. BeforeOut = BeforeOut,
  520. BeforeTotal = BeforeTotal,
  521. BeforeStock = BeforeStock,
  522. OrderNo = ChangeRecordNo,
  523. ToUserId = order.UserId,
  524. FromUserId = 0,
  525. }).Entity;
  526. db.SaveChanges();
  527. RedisDbconn.Instance.Clear("Orders:" + order.Id);
  528. }
  529. //推荐下单奖励
  530. if (pro.ProductId == 10 || pro.ProductId == 11)
  531. {
  532. int SelfBuy = db.OrderProduct.Count(m => m.UserId == order.UserId && m.OrderId != OrderId && (m.ProductId == 10 || m.ProductId == 11));
  533. if (SelfBuy == 1)
  534. {
  535. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  536. if (user != null)
  537. {
  538. int ParentUserId = user.ParentUserId;
  539. int machineCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.PosSnType == 0); //判断是否拥有3台购买机
  540. int ActiveCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  541. int couponCount = db.PosCoupons.Count(m => m.UserId == ParentUserId && m.IsUse == 0); //判断是否拥有3张券
  542. if (machineCount + ActiveCount + couponCount >= 3)
  543. {
  544. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == ParentUserId);
  545. if (account == null)
  546. {
  547. account = db.UserAccount.Add(new UserAccount()
  548. {
  549. Id = ParentUserId,
  550. UserId = ParentUserId,
  551. }).Entity;
  552. db.SaveChanges();
  553. }
  554. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  555. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  556. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  557. account.BalanceAmount += 100;
  558. account.TotalAmount += 100;
  559. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  560. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  561. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  562. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  563. {
  564. CreateDate = DateTime.Now,
  565. UpdateDate = DateTime.Now,
  566. UserId = ParentUserId, //创客
  567. ChangeType = 112, //变动类型
  568. ChangeAmount = 100, //变更金额
  569. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  570. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  571. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  572. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  573. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  574. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  575. }).Entity;
  576. db.SaveChanges();
  577. RedisDbconn.Instance.Set("UserAccount:" + ParentUserId, account);
  578. //推荐下单上级获得30天的机具循环天数
  579. 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);
  580. foreach (var subPos in posList)
  581. {
  582. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == subPos.Id);
  583. if (pos != null)
  584. {
  585. pos.RecycEndDate = pos.RecycEndDate.Value.AddDays(30);
  586. }
  587. }
  588. db.SaveChanges();
  589. }
  590. }
  591. }
  592. }
  593. }
  594. }
  595. db.Dispose();
  596. }
  597. }
  598. }