AlipayPayBack2Service.cs 39 KB

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