AlipayPayBack2Service.cs 34 KB

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