AlipayPayBack2Service.cs 49 KB

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