AlipayPayBack2Service.cs 46 KB

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