AlipayPayBack2Service.cs 35 KB

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