AlipayPayBack2Service.cs 45 KB

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