AlipayPayBack2Service.cs 42 KB

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