AlipayPayBack2Service.cs 35 KB

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