AlipayPayBack2Service.cs 50 KB

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