AlipayPayBack2Service.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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. DoOrderV2(db, OrderId);
  72. }
  73. }
  74. db.Dispose();
  75. }
  76. #region 新订单流程
  77. public void DoOrderV2(WebCMSEntities db, int OrderId)
  78. {
  79. Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId && m.Status == 0);
  80. if (order != null)
  81. {
  82. order.Status = 1;
  83. order.PayDate = DateTime.Now;
  84. order.PayStatus = 1;
  85. if(order.ParentOrderId > 0)
  86. {
  87. int total = db.Orders.Count(m => m.ParentOrderId == order.ParentOrderId);
  88. int paycount = db.Orders.Count(m => m.ParentOrderId == order.ParentOrderId && m.Status > 0);
  89. if(paycount + 1 >= total)
  90. {
  91. order = db.Orders.FirstOrDefault(m => m.Id == order.ParentOrderId && m.Status == 0);
  92. if (order != null)
  93. {
  94. order.Status = 1;
  95. order.PayDate = DateTime.Now;
  96. order.PayStatus = 1;
  97. }
  98. }
  99. }
  100. db.SaveChanges();
  101. if(order.ParentOrderId > 0)
  102. {
  103. return;
  104. }
  105. OrderProduct pro = db.OrderProduct.FirstOrDefault(m => m.OrderId == OrderId);
  106. if (pro != null)
  107. {
  108. List<int> couponIds = new List<int>();
  109. if (pro.ProductId == 10 || pro.ProductId == 11 || pro.ProductId == 12 || pro.ProductId == 13)
  110. {
  111. order.Status = 2;
  112. int BuyCount = pro.ProductCount;
  113. int Kind = 0;
  114. if (pro.ProductId == 10)
  115. {
  116. Kind = 1;
  117. }
  118. else if (pro.ProductId == 11)
  119. {
  120. Kind = 2;
  121. }
  122. else if (pro.ProductId == 12)
  123. {
  124. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  125. if(user != null)
  126. {
  127. user.LeaderLevel = 1;
  128. }
  129. }
  130. else if (pro.ProductId == 13)
  131. {
  132. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  133. if(user != null)
  134. {
  135. user.LeaderLevel = 2;
  136. }
  137. }
  138. if(Kind <= 2)
  139. {
  140. string Codes = "";
  141. 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();
  142. foreach (var coupon in coupons)
  143. {
  144. PosCoupons item = db.PosCoupons.FirstOrDefault(m => m.Id == coupon.Id);
  145. if (item != null)
  146. {
  147. item.CreateDate = DateTime.Now;
  148. item.UserId = order.UserId;
  149. item.UpdateDate = DateTime.Now.AddDays(180);
  150. Codes += item.ExchangeCode + ",";
  151. couponIds.Add(coupon.Id);
  152. }
  153. }
  154. order.SnNos = Codes.TrimEnd(',');
  155. }
  156. db.SaveChanges();
  157. if (pro.ProductId == 12 || pro.ProductId == 13)
  158. {
  159. //充值盟主储备金
  160. OpReserve(db, order, order.UserId, order.TotalPrice, 1);
  161. }
  162. }
  163. //推荐下单奖励
  164. if (pro.ProductId == 10 || pro.ProductId == 11)
  165. {
  166. bool checkPrize = db.UserAccountRecord.Any(m => m.QueryCount == OrderId);
  167. function.WriteLog("OrderId:" + OrderId, "推荐下单奖励监控日志");
  168. function.WriteLog("checkPrize:" + checkPrize, "推荐下单奖励监控日志");
  169. if (!checkPrize)
  170. {
  171. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  172. if (user != null)
  173. {
  174. if(user.LeaderLevel > 0)
  175. {
  176. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == order.UserId) ?? new UserAccount();
  177. if(acccount.LeaderReserve >= order.TotalPrice)
  178. {
  179. if(order.PayMode == 4)
  180. {
  181. //扣减备用金
  182. OpReserve(db, order, order.UserId, order.TotalPrice, 0, 0, "商城购机");
  183. //返回到余额
  184. OpAccount(db, order, order.UserId, order.TotalPrice);
  185. }
  186. }
  187. //获得100元奖励
  188. OpAccount(db, order, order.UserId, 100);
  189. //推荐奖励
  190. DirectPrize(db, order.Id, order.UserId);
  191. //推荐下单上级获得30天的机具循环天数
  192. var posList = db.PosMachinesTwo.Select(m => new { m.Id, m.UserId, m.BindingState, m.RecycEndDate }).Where(m => m.UserId == order.UserId && m.BindingState == 0 && m.RecycEndDate != null).ToList();
  193. foreach (var subPos in posList)
  194. {
  195. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == subPos.Id);
  196. if (pos != null)
  197. {
  198. pos.RecycEndDate = pos.RecycEndDate.Value.AddDays(30);
  199. }
  200. }
  201. db.SaveChanges();
  202. return;
  203. }
  204. bool directPrize = false;
  205. int ParentUserId = user.ParentUserId;
  206. while(ParentUserId > 0)
  207. {
  208. Users puser = db.Users.FirstOrDefault(m => m.Id == ParentUserId) ?? new Users();
  209. int machineCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.PosSnType == 0); //判断是否拥有3台购买机
  210. int ActiveCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  211. int couponCount = db.PosCoupons.Count(m => m.UserId == ParentUserId && m.IsUse == 0); //判断是否拥有3张券
  212. function.WriteLog("MakerCode:" + user.MakerCode, "推荐下单奖励监控日志");
  213. function.WriteLog("machineCount:" + machineCount, "推荐下单奖励监控日志");
  214. function.WriteLog("ActiveCount:" + ActiveCount, "推荐下单奖励监控日志");
  215. if (machineCount + ActiveCount + couponCount >= 3 && !directPrize)
  216. {
  217. function.WriteLog("满足条件", "推荐下单奖励监控日志");
  218. DirectPrize(db, order.Id, ParentUserId);
  219. //推荐下单上级获得30天的机具循环天数
  220. 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();
  221. foreach (var subPos in posList)
  222. {
  223. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == subPos.Id);
  224. if (pos != null)
  225. {
  226. pos.RecycEndDate = pos.RecycEndDate.Value.AddDays(30);
  227. }
  228. }
  229. db.SaveChanges();
  230. directPrize = true;
  231. }
  232. if(puser.LeaderLevel > 0)
  233. {
  234. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
  235. if(acccount.LeaderReserve >= order.TotalPrice)
  236. {
  237. //扣减备用金
  238. OpReserve(db, order, puser.Id, order.TotalPrice, 0, order.UserId, "购机奖励");
  239. //返回到余额
  240. OpAccount(db, order, puser.Id, order.TotalPrice);
  241. //购机奖励
  242. OpAccount(db, order, puser.Id, 100);
  243. }
  244. //如果是大盟主,则标记大盟主标签
  245. if(couponIds.Count > 0 && puser.LeaderLevel == 2)
  246. {
  247. foreach(int couponId in couponIds)
  248. {
  249. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  250. if(coupon != null)
  251. {
  252. coupon.LeaderUserId = puser.Id;
  253. }
  254. }
  255. db.SaveChanges();
  256. }
  257. }
  258. ParentUserId = puser.ParentUserId;
  259. }
  260. }
  261. }
  262. function.WriteLog("\r\n\r\n", "推荐下单奖励监控日志");
  263. }
  264. }
  265. else
  266. {
  267. if(order.ProductId > 0 && order.ErpMode > 0)
  268. {
  269. //盟主储备金-升级/购买
  270. if(order.ErpMode == 2)
  271. {
  272. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  273. if(user != null)
  274. {
  275. user.LeaderLevel = 2;
  276. db.SaveChanges();
  277. OpReserve(db, order, order.UserId, order.TotalPrice, 1);
  278. }
  279. }
  280. }
  281. }
  282. }
  283. }
  284. //操作储备金
  285. public void OpReserve(WebCMSEntities db, Orders order, int UserId, decimal Money, int ChangeType, int SourceUserId = 0, string Remark = "储备金购买")
  286. {
  287. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  288. if (account == null)
  289. {
  290. account = db.UserAccount.Add(new UserAccount()
  291. {
  292. Id = UserId,
  293. UserId = UserId,
  294. }).Entity;
  295. db.SaveChanges();
  296. }
  297. decimal BeforeAmount = account.LeaderReserve; //变更前总金额
  298. account.LeaderReserve += Money;
  299. decimal AfterAmount = account.LeaderReserve; //变更后总金额
  300. LeaderReserveRecord add = db.LeaderReserveRecord.Add(new LeaderReserveRecord()
  301. {
  302. CreateDate = DateTime.Now,
  303. ChangeType = ChangeType,
  304. OrderId = order.Id,
  305. Remark = Remark,
  306. AfterAmt = AfterAmount,
  307. BeforeAmt = BeforeAmount,
  308. ChangeAmt = Money,
  309. TradeDate = DateTime.Now.ToString("yyyyMMdd"),
  310. TradeMonth = DateTime.Now.ToString("yyyyMM"),
  311. UserId = UserId,
  312. SourceUserId = SourceUserId,
  313. }).Entity;
  314. db.SaveChanges();
  315. }
  316. //操作余额
  317. public void OpAccount(WebCMSEntities db, Orders order, int UserId, decimal Money)
  318. {
  319. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  320. if (account == null)
  321. {
  322. account = db.UserAccount.Add(new UserAccount()
  323. {
  324. Id = UserId,
  325. UserId = UserId,
  326. }).Entity;
  327. db.SaveChanges();
  328. }
  329. int ChangeType = 0;
  330. if(Money == 600)
  331. {
  332. ChangeType = 117;
  333. }
  334. else if(Money == 100)
  335. {
  336. ChangeType = 118;
  337. }
  338. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  339. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  340. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  341. account.BalanceAmount += Money;
  342. account.TotalAmount += Money;
  343. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  344. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  345. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  346. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  347. {
  348. CreateDate = DateTime.Now,
  349. UpdateDate = DateTime.Now,
  350. UserId = UserId, //创客
  351. ChangeType = ChangeType, //变动类型
  352. ChangeAmount = Money, //变更金额
  353. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  354. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  355. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  356. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  357. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  358. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  359. QueryCount = order.Id,
  360. }).Entity;
  361. db.SaveChanges();
  362. }
  363. public void DirectPrize(WebCMSEntities db, int OrderId, int UserId)
  364. {
  365. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  366. if (account == null)
  367. {
  368. account = db.UserAccount.Add(new UserAccount()
  369. {
  370. Id = UserId,
  371. UserId = UserId,
  372. }).Entity;
  373. db.SaveChanges();
  374. }
  375. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  376. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  377. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  378. account.BalanceAmount += 100;
  379. account.TotalAmount += 100;
  380. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  381. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  382. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  383. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  384. {
  385. CreateDate = DateTime.Now,
  386. UpdateDate = DateTime.Now,
  387. UserId = UserId, //创客
  388. ChangeType = 112, //变动类型
  389. ChangeAmount = 100, //变更金额
  390. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  391. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  392. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  393. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  394. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  395. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  396. QueryCount = OrderId,
  397. }).Entity;
  398. db.SaveChanges();
  399. }
  400. #endregion
  401. #region 老订单流程
  402. public void DoOrder(WebCMSEntities db, int OrderId)
  403. {
  404. Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId && m.Status == 0);
  405. if (order != null)
  406. {
  407. order.Status = 1;
  408. order.PayDate = DateTime.Now;
  409. order.PayStatus = 1;
  410. db.SaveChanges();
  411. RedisDbconn.Instance.Set("Orders:" + order.Id, order);
  412. //机具券逻辑
  413. OrderProduct pro = db.OrderProduct.FirstOrDefault(m => m.OrderId == OrderId);
  414. if (pro != null)
  415. {
  416. if (pro.ProductId == 10 || pro.ProductId == 11 || pro.ProductId == 12 || pro.ProductId == 13)
  417. {
  418. order.Status = 2;
  419. db.SaveChanges();
  420. int BuyCount = pro.ProductCount;
  421. int Kind = 0;
  422. if (pro.ProductId == 10)
  423. {
  424. Kind = 1;
  425. }
  426. else if (pro.ProductId == 11)
  427. {
  428. Kind = 2;
  429. }
  430. else if (pro.ProductId == 12)
  431. {
  432. if (pro.NormDetail == "电签POS")
  433. {
  434. Kind = 1;
  435. }
  436. else if (pro.NormDetail == "大POS")
  437. {
  438. Kind = 2;
  439. }
  440. }
  441. else if (pro.ProductId == 13)
  442. {
  443. if (pro.NormDetail == "300电签POS")
  444. {
  445. Kind = 1;
  446. }
  447. else if (pro.NormDetail == "200大POS")
  448. {
  449. Kind = 2;
  450. }
  451. else if (pro.NormDetail == "50大机券+225电签券")
  452. {
  453. Kind = 3;
  454. }
  455. else if (pro.NormDetail == "100大机券+150电签券")
  456. {
  457. Kind = 4;
  458. }
  459. else if (pro.NormDetail == "150大机券+75电签券")
  460. {
  461. Kind = 5;
  462. }
  463. }
  464. string Codes = "";
  465. if(Kind > 2)
  466. {
  467. Dictionary<int, int> couponData = new Dictionary<int, int>();
  468. if(Kind == 3)
  469. {
  470. couponData.Add(1, 225); //电签数量
  471. couponData.Add(2, 50); //大机数量
  472. }
  473. else if(Kind == 4)
  474. {
  475. couponData.Add(1, 150); //电签数量
  476. couponData.Add(2, 100); //大机数量
  477. }
  478. else if(Kind == 5)
  479. {
  480. couponData.Add(1, 75); //电签数量
  481. couponData.Add(2, 150); //大机数量
  482. }
  483. foreach(int kindNum in couponData.Keys)
  484. {
  485. int BuyCountByKind = couponData[kindNum];
  486. 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();
  487. foreach (var coupon in coupons)
  488. {
  489. PosCoupons item = db.PosCoupons.FirstOrDefault(m => m.Id == coupon.Id);
  490. if (item != null)
  491. {
  492. item.CreateDate = DateTime.Now;
  493. item.UserId = order.UserId;
  494. item.UpdateDate = DateTime.Now.AddDays(180);
  495. if (pro.ProductId == 13)
  496. {
  497. item.LeaderUserId = order.UserId;
  498. }
  499. Codes += item.ExchangeCode + ",";
  500. }
  501. }
  502. }
  503. }
  504. else
  505. {
  506. 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();
  507. foreach (var coupon in coupons)
  508. {
  509. PosCoupons item = db.PosCoupons.FirstOrDefault(m => m.Id == coupon.Id);
  510. if (item != null)
  511. {
  512. item.CreateDate = DateTime.Now;
  513. item.UserId = order.UserId;
  514. item.UpdateDate = DateTime.Now.AddDays(180);
  515. if (pro.ProductId == 13)
  516. {
  517. item.LeaderUserId = order.UserId;
  518. }
  519. Codes += item.ExchangeCode + ",";
  520. }
  521. }
  522. }
  523. order.SnNos = Codes.TrimEnd(',');
  524. string ChangeRecordNo = "SC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  525. PosCouponForUser forUser = db.PosCouponForUser.FirstOrDefault(m => m.Id == order.UserId);
  526. if (forUser == null)
  527. {
  528. forUser = db.PosCouponForUser.Add(new PosCouponForUser()
  529. {
  530. Id = order.UserId,
  531. }).Entity;
  532. db.SaveChanges();
  533. }
  534. int BeforeOut = forUser.OutNum;
  535. int BeforeTotal = forUser.TotalNum;
  536. int BeforeStock = forUser.StockNum;
  537. forUser.TotalNum += BuyCount;
  538. forUser.StockNum += BuyCount;
  539. int AfterOut = forUser.OutNum;
  540. int AfterTotal = forUser.TotalNum;
  541. int AfterStock = forUser.StockNum;
  542. PosCouponOrders stat = db.PosCouponOrders.Add(new PosCouponOrders()
  543. {
  544. QueryCount = Kind,
  545. CreateDate = DateTime.Now,
  546. ChangeKind = 1,
  547. ChangeCount = BuyCount,
  548. AfterOut = AfterOut,
  549. AfterTotal = AfterTotal,
  550. AfterStock = AfterStock,
  551. BeforeOut = BeforeOut,
  552. BeforeTotal = BeforeTotal,
  553. BeforeStock = BeforeStock,
  554. OrderNo = ChangeRecordNo,
  555. ToUserId = order.UserId,
  556. FromUserId = 0,
  557. }).Entity;
  558. db.SaveChanges();
  559. RedisDbconn.Instance.Clear("Orders:" + order.Id);
  560. }
  561. //推荐下单奖励
  562. if (pro.ProductId == 10 || pro.ProductId == 11)
  563. {
  564. bool checkPrize = db.UserAccountRecord.Any(m => m.QueryCount == OrderId);
  565. int SelfBuy = 0;
  566. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].ToString();
  567. 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");
  568. if (stat.Rows.Count > 0)
  569. {
  570. SelfBuy = int.Parse(function.CheckInt(stat.Rows[0][0].ToString()));
  571. }
  572. // OtherMySqlConn.connstr = ;
  573. function.WriteLog("OrderId:" + OrderId, "推荐下单奖励监控日志");
  574. function.WriteLog("checkPrize:" + checkPrize, "推荐下单奖励监控日志");
  575. function.WriteLog("SelfBuy:" + SelfBuy, "推荐下单奖励监控日志");
  576. if (SelfBuy == 1 && !checkPrize)
  577. {
  578. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  579. if (user != null)
  580. {
  581. int ParentUserId = user.ParentUserId;
  582. while(ParentUserId > 0)
  583. {
  584. int machineCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.PosSnType == 0); //判断是否拥有3台购买机
  585. int ActiveCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  586. int couponCount = db.PosCoupons.Count(m => m.UserId == ParentUserId && m.IsUse == 0); //判断是否拥有3张券
  587. function.WriteLog("MakerCode:" + user.MakerCode, "推荐下单奖励监控日志");
  588. function.WriteLog("machineCount:" + machineCount, "推荐下单奖励监控日志");
  589. function.WriteLog("ActiveCount:" + ActiveCount, "推荐下单奖励监控日志");
  590. if (machineCount + ActiveCount + couponCount >= 3)
  591. {
  592. function.WriteLog("满足条件", "推荐下单奖励监控日志");
  593. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == ParentUserId);
  594. if (account == null)
  595. {
  596. account = db.UserAccount.Add(new UserAccount()
  597. {
  598. Id = ParentUserId,
  599. UserId = ParentUserId,
  600. }).Entity;
  601. db.SaveChanges();
  602. }
  603. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  604. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  605. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  606. account.BalanceAmount += 100;
  607. account.TotalAmount += 100;
  608. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  609. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  610. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  611. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  612. {
  613. CreateDate = DateTime.Now,
  614. UpdateDate = DateTime.Now,
  615. UserId = ParentUserId, //创客
  616. ChangeType = 112, //变动类型
  617. ChangeAmount = 100, //变更金额
  618. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  619. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  620. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  621. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  622. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  623. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  624. QueryCount = OrderId,
  625. }).Entity;
  626. db.SaveChanges();
  627. RedisDbconn.Instance.Set("UserAccount:" + ParentUserId, account);
  628. //推荐下单上级获得30天的机具循环天数
  629. 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();
  630. foreach (var subPos in posList)
  631. {
  632. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == subPos.Id);
  633. if (pos != null)
  634. {
  635. pos.RecycEndDate = pos.RecycEndDate.Value.AddDays(30);
  636. }
  637. }
  638. db.SaveChanges();
  639. ParentUserId = 0;
  640. }
  641. else
  642. {
  643. Users puser = db.Users.FirstOrDefault(m => m.Id == ParentUserId) ?? new Users();
  644. ParentUserId = puser.ParentUserId;
  645. }
  646. }
  647. }
  648. }
  649. function.WriteLog("\r\n\r\n", "推荐下单奖励监控日志");
  650. }
  651. }
  652. }
  653. }
  654. public void checkOrder(int OrderId)
  655. {
  656. WebCMSEntities db = new WebCMSEntities();
  657. DoOrder(db, OrderId);
  658. db.Dispose();
  659. }
  660. public void addcoupon(int OrderId)
  661. {
  662. WebCMSEntities db = new WebCMSEntities();
  663. Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId);
  664. if (order != null)
  665. {
  666. //机具券逻辑
  667. OrderProduct pro = db.OrderProduct.FirstOrDefault(m => m.OrderId == OrderId);
  668. if (pro != null)
  669. {
  670. if (pro.ProductId == 10 || pro.ProductId == 11 || pro.ProductId == 12 || pro.ProductId == 13)
  671. {
  672. int BuyCount = pro.ProductCount;
  673. int Kind = 0;
  674. if (pro.ProductId == 10)
  675. {
  676. Kind = 1;
  677. }
  678. else if (pro.ProductId == 11)
  679. {
  680. Kind = 2;
  681. }
  682. else if (pro.ProductId == 12)
  683. {
  684. if (pro.NormDetail == "电签POS")
  685. {
  686. Kind = 1;
  687. }
  688. else if (pro.NormDetail == "大POS")
  689. {
  690. Kind = 2;
  691. }
  692. }
  693. else if (pro.ProductId == 13)
  694. {
  695. if (pro.NormDetail == "电签POS")
  696. {
  697. Kind = 1;
  698. }
  699. else if (pro.NormDetail == "大POS")
  700. {
  701. Kind = 2;
  702. }
  703. }
  704. string Codes = "";
  705. 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();
  706. foreach (var coupon in coupons)
  707. {
  708. PosCoupons item = db.PosCoupons.FirstOrDefault(m => m.Id == coupon.Id);
  709. if (item != null)
  710. {
  711. item.CreateDate = DateTime.Now;
  712. item.UserId = order.UserId;
  713. item.UpdateDate = DateTime.Now.AddDays(180);
  714. Codes += item.ExchangeCode + ",";
  715. }
  716. }
  717. order.SnNos = Codes.TrimEnd(',');
  718. string ChangeRecordNo = "SC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  719. PosCouponForUser forUser = db.PosCouponForUser.FirstOrDefault(m => m.Id == order.UserId);
  720. if (forUser == null)
  721. {
  722. forUser = db.PosCouponForUser.Add(new PosCouponForUser()
  723. {
  724. Id = order.UserId,
  725. }).Entity;
  726. db.SaveChanges();
  727. }
  728. int BeforeOut = forUser.OutNum;
  729. int BeforeTotal = forUser.TotalNum;
  730. int BeforeStock = forUser.StockNum;
  731. forUser.TotalNum += BuyCount;
  732. forUser.StockNum += BuyCount;
  733. int AfterOut = forUser.OutNum;
  734. int AfterTotal = forUser.TotalNum;
  735. int AfterStock = forUser.StockNum;
  736. PosCouponOrders stat = db.PosCouponOrders.Add(new PosCouponOrders()
  737. {
  738. QueryCount = Kind,
  739. CreateDate = DateTime.Now,
  740. ChangeKind = 1,
  741. ChangeCount = BuyCount,
  742. AfterOut = AfterOut,
  743. AfterTotal = AfterTotal,
  744. AfterStock = AfterStock,
  745. BeforeOut = BeforeOut,
  746. BeforeTotal = BeforeTotal,
  747. BeforeStock = BeforeStock,
  748. OrderNo = ChangeRecordNo,
  749. ToUserId = order.UserId,
  750. FromUserId = 0,
  751. }).Entity;
  752. db.SaveChanges();
  753. RedisDbconn.Instance.Clear("Orders:" + order.Id);
  754. }
  755. //推荐下单奖励
  756. if (pro.ProductId == 10 || pro.ProductId == 11)
  757. {
  758. int SelfBuy = db.OrderProduct.Count(m => m.UserId == order.UserId && m.OrderId != OrderId && (m.ProductId == 10 || m.ProductId == 11));
  759. if (SelfBuy == 1)
  760. {
  761. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  762. if (user != null)
  763. {
  764. int ParentUserId = user.ParentUserId;
  765. int machineCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.PosSnType == 0); //判断是否拥有3台购买机
  766. int ActiveCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  767. int couponCount = db.PosCoupons.Count(m => m.UserId == ParentUserId && m.IsUse == 0); //判断是否拥有3张券
  768. if (machineCount + ActiveCount + couponCount >= 3)
  769. {
  770. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == ParentUserId);
  771. if (account == null)
  772. {
  773. account = db.UserAccount.Add(new UserAccount()
  774. {
  775. Id = ParentUserId,
  776. UserId = ParentUserId,
  777. }).Entity;
  778. db.SaveChanges();
  779. }
  780. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  781. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  782. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  783. account.BalanceAmount += 100;
  784. account.TotalAmount += 100;
  785. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  786. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  787. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  788. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  789. {
  790. CreateDate = DateTime.Now,
  791. UpdateDate = DateTime.Now,
  792. UserId = ParentUserId, //创客
  793. ChangeType = 112, //变动类型
  794. ChangeAmount = 100, //变更金额
  795. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  796. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  797. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  798. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  799. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  800. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  801. }).Entity;
  802. db.SaveChanges();
  803. RedisDbconn.Instance.Set("UserAccount:" + ParentUserId, account);
  804. //推荐下单上级获得30天的机具循环天数
  805. 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);
  806. foreach (var subPos in posList)
  807. {
  808. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == subPos.Id);
  809. if (pos != null)
  810. {
  811. pos.RecycEndDate = pos.RecycEndDate.Value.AddDays(30);
  812. }
  813. }
  814. db.SaveChanges();
  815. }
  816. }
  817. }
  818. }
  819. }
  820. }
  821. db.Dispose();
  822. }
  823. #endregion
  824. }
  825. }