AlipayPayBack2Service.cs 44 KB

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