AlipayPayBack2Service.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. if (pro.ProductId == 28) //购买小盟主,给上级大盟主返储备金
  178. {
  179. int LeaderUserId = order.UserId;
  180. int level = 0;
  181. while(LeaderUserId > 0)
  182. {
  183. level += 1;
  184. Users user = db.Users.FirstOrDefault(m => m.Id == LeaderUserId);
  185. if(user != null)
  186. {
  187. if(user.LeaderLevel == 2 && level > 1)
  188. {
  189. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == LeaderUserId);
  190. if(account != null)
  191. {
  192. if(account.LeaderReserve >= order.TotalPrice)
  193. {
  194. OpReserve(db, order, LeaderUserId, order.TotalPrice, 2, 0, "推荐小盟主");
  195. OpLeaderAccount(db, order, LeaderUserId, order.TotalPrice);
  196. }
  197. else
  198. {
  199. OpReserve(db, order, LeaderUserId, account.LeaderReserve, 2, 0, "推荐小盟主");
  200. OpLeaderAccount(db, order, LeaderUserId, account.LeaderReserve);
  201. }
  202. }
  203. LeaderUserId = 0;
  204. }
  205. else
  206. {
  207. LeaderUserId = user.ParentUserId;
  208. }
  209. }
  210. else
  211. {
  212. LeaderUserId = 0;
  213. }
  214. }
  215. }
  216. }
  217. //推荐下单奖励
  218. if (pro.ProductId == 10 || pro.ProductId == 11)
  219. {
  220. bool checkPrize = db.UserAccountRecord.Any(m => m.QueryCount == OrderId && m.ChangeType == 112);
  221. function.WriteLog("OrderId:" + OrderId, "推荐下单奖励监控日志");
  222. function.WriteLog("checkPrize:" + checkPrize, "推荐下单奖励监控日志");
  223. if (!checkPrize)
  224. {
  225. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  226. if (user != null)
  227. {
  228. bool directPrize = false; //直推奖标记
  229. bool bigLeaderPrize = false; //大盟主券标记
  230. bool buyPrize = false; //返100购机奖励标记
  231. int leaderFlag = 0; //返600备用金标记
  232. if(user.LeaderLevel > 0)
  233. {
  234. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == order.UserId) ?? new UserAccount();
  235. if(acccount.LeaderReserve >= order.TotalPrice)
  236. {
  237. if(order.PayMode == 4)
  238. {
  239. //扣减备用金
  240. OpReserve(db, order, order.UserId, order.TotalPrice, 2, 0, "商城购机");
  241. //返回到余额
  242. // OpAccount(db, order, order.UserId, order.TotalPrice / pro.ProductCount, pro.ProductCount);
  243. }
  244. else
  245. {
  246. //扣减备用金
  247. OpReserve(db, order, order.UserId, order.TotalPrice, 2, 0, "商城购机");
  248. //返回到余额
  249. OpLeaderAccount(db, order, order.UserId, order.TotalPrice / pro.ProductCount, pro.ProductCount);
  250. }
  251. }
  252. //获得100元奖励
  253. OpAccount(db, order, order.UserId, 100, pro.ProductCount);
  254. //推荐奖励
  255. DirectPrize(db, order.Id, order.UserId, pro.ProductCount);
  256. //推荐下单上级获得30天的机具循环天数(盟主自己得)
  257. 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();
  258. foreach (var subPos in posList)
  259. {
  260. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == subPos.Id);
  261. if (pos != null)
  262. {
  263. pos.RecycEndDate = pos.RecycEndDate.Value.AddDays(30);
  264. }
  265. }
  266. db.SaveChanges();
  267. if(user.LeaderLevel == 2)
  268. {
  269. if(couponIds.Count > 0)
  270. {
  271. foreach(int couponId in couponIds)
  272. {
  273. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  274. if(coupon != null)
  275. {
  276. coupon.LeaderUserId = user.Id;
  277. }
  278. }
  279. db.SaveChanges();
  280. }
  281. }
  282. else
  283. {
  284. int PUserId = user.ParentUserId;
  285. while(PUserId > 0)
  286. {
  287. Users puser = db.Users.FirstOrDefault(m => m.Id == PUserId) ?? new Users();
  288. if(puser.LeaderLevel == 2)
  289. {
  290. UserAccount acc = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
  291. if(acc.LeaderReserve >= order.TotalPrice)
  292. {
  293. //扣减备用金
  294. OpReserve(db, order, puser.Id, order.TotalPrice, 2, order.UserId, "购机奖励");
  295. //返回到余额
  296. OpLeaderAccount(db, order, puser.Id, order.TotalPrice / pro.ProductCount, pro.ProductCount);
  297. }
  298. PUserId = 0;
  299. }
  300. else
  301. {
  302. PUserId = puser.ParentUserId;
  303. }
  304. }
  305. }
  306. return;
  307. }
  308. int ParentUserId = user.ParentUserId;
  309. // List<int> proids = new List<int>();
  310. // proids.Add(10);
  311. // proids.Add(11);
  312. while(ParentUserId > 0)
  313. {
  314. Users puser = db.Users.FirstOrDefault(m => m.Id == ParentUserId) ?? new Users();
  315. int machineCount = db.PosMachinesTwo.Count(m => m.BuyUserId == ParentUserId && m.PosSnType == 0 && m.ActivationState == 0); //判断是否拥有3台购买机
  316. int ActiveCount = db.PosMachinesTwo.Count(m => m.BuyUserId == ParentUserId && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  317. int couponCount = db.PosCoupons.Count(m => m.UserId == ParentUserId && m.IsUse == 0); //判断是否拥有3张券
  318. function.WriteLog("MakerCode:" + puser.MakerCode, "推荐下单奖励监控日志");
  319. function.WriteLog("machineCount:" + machineCount, "推荐下单奖励监控日志");
  320. function.WriteLog("ActiveCount:" + ActiveCount, "推荐下单奖励监控日志");
  321. if ((machineCount + ActiveCount + couponCount >= 3 || puser.LeaderLevel > 0) && !directPrize)
  322. {
  323. // function.WriteLog("满足条件", "推荐下单奖励监控日志");
  324. // if(puser.LeaderLevel == 0) // 非盟主直推奖励,每个创客第一次下单,上级可得
  325. // {
  326. // List<int> oids = new List<int>();
  327. // var orderpros = db.OrderProduct.Select(m => new { m.OrderId,m.UserId,m.ProductId }).Where(m => m.UserId == order.UserId && proids.Contains(m.ProductId)).ToList();
  328. // foreach(var orderpro in orderpros)
  329. // {
  330. // oids.Add(orderpro.OrderId);
  331. // }
  332. // bool check = db.Orders.Any(m => oids.Contains(m.Id) && m.Status > 0);
  333. // if(!check)
  334. // {
  335. // DirectPrize(db, order.Id, ParentUserId, pro.ProductCount);
  336. // directPrize = true;
  337. // }
  338. // }
  339. // else
  340. // {
  341. // 盟主直推奖励,可以每次下单获得
  342. DirectPrize(db, order.Id, ParentUserId, pro.ProductCount);
  343. directPrize = true;
  344. // }
  345. //推荐下单上级获得30天的机具循环天数
  346. 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();
  347. foreach (var subPos in posList)
  348. {
  349. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == subPos.Id);
  350. if (pos != null)
  351. {
  352. pos.RecycEndDate = pos.RecycEndDate.Value.AddDays(30);
  353. }
  354. }
  355. db.SaveChanges();
  356. }
  357. if(puser.LeaderLevel > 0)
  358. {
  359. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
  360. if(acccount.LeaderReserve >= order.TotalPrice && !buyPrize)
  361. {
  362. //购机奖励
  363. OpAccount(db, order, puser.Id, 100, pro.ProductCount);
  364. buyPrize = true;
  365. }
  366. if(acccount.LeaderReserve >= order.TotalPrice && leaderFlag == 0)
  367. {
  368. //扣减备用金
  369. OpReserve(db, order, puser.Id, order.TotalPrice, 2, order.UserId, "购机奖励");
  370. //返回到余额
  371. OpLeaderAccount(db, order, puser.Id, order.TotalPrice / pro.ProductCount, pro.ProductCount);
  372. leaderFlag = puser.LeaderLevel;
  373. }
  374. //如果是大盟主,则标记大盟主标签
  375. if(couponIds.Count > 0 && puser.LeaderLevel == 2 && acccount.LeaderReserve >= order.TotalPrice && !bigLeaderPrize)
  376. {
  377. foreach(int couponId in couponIds)
  378. {
  379. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  380. if(coupon != null)
  381. {
  382. coupon.LeaderUserId = puser.Id;
  383. }
  384. }
  385. db.SaveChanges();
  386. bigLeaderPrize = true;
  387. }
  388. }
  389. ParentUserId = puser.ParentUserId;
  390. }
  391. }
  392. }
  393. function.WriteLog("\r\n\r\n", "推荐下单奖励监控日志");
  394. }
  395. //推荐王
  396. if(pro.ProductId == 29)
  397. {
  398. RecommendMethod(order.UserId, order.CreateDate.Value.ToString("yyyyMM"));
  399. }
  400. //分仓向总仓申请机具额度
  401. if(pro.ProductId == 30 || pro.ProductId == 31 || pro.ProductId == 32)
  402. {
  403. string SendData = "{\"Kind\":\"1\",\"Data\":{\"OrderId\":\"" + order.Id + "\"}}";
  404. RedisDbconn.Instance.AddList("StoreApplyQueue", SendData);
  405. }
  406. }
  407. else
  408. {
  409. if(order.ProductId > 0 && order.ErpMode > 0)
  410. {
  411. //盟主储备金-升级/购买 ErpMode:1-升级,2-购买
  412. if(order.ErpMode > 0)
  413. {
  414. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  415. if(user != null)
  416. {
  417. if(order.ErpMode == 1)
  418. {
  419. user.LeaderLevel = 2;
  420. db.SaveChanges();
  421. }
  422. OpReserve(db, order, order.UserId, order.TotalPrice, 1);
  423. }
  424. }
  425. }
  426. }
  427. }
  428. }
  429. //操作储备金
  430. public void OpReserve(WebCMSEntities db, Orders order, int UserId, decimal Money, int ChangeType, int SourceUserId = 0, string Remark = "储备金购买")
  431. {
  432. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  433. if (account == null)
  434. {
  435. account = db.UserAccount.Add(new UserAccount()
  436. {
  437. Id = UserId,
  438. UserId = UserId,
  439. }).Entity;
  440. db.SaveChanges();
  441. }
  442. decimal BeforeAmount = account.LeaderReserve; //变更前总金额
  443. if(ChangeType == 1)
  444. {
  445. account.LeaderReserve += Money;
  446. }
  447. else
  448. {
  449. account.LeaderReserve -= Money;
  450. }
  451. decimal AfterAmount = account.LeaderReserve; //变更后总金额
  452. LeaderReserveRecord add = db.LeaderReserveRecord.Add(new LeaderReserveRecord()
  453. {
  454. CreateDate = DateTime.Now,
  455. ChangeType = ChangeType,
  456. OrderId = order.Id,
  457. Remark = Remark,
  458. AfterAmt = AfterAmount,
  459. BeforeAmt = BeforeAmount,
  460. ChangeAmt = Money,
  461. TradeDate = DateTime.Now.ToString("yyyyMMdd"),
  462. TradeMonth = DateTime.Now.ToString("yyyyMM"),
  463. UserId = UserId,
  464. SourceUserId = SourceUserId,
  465. }).Entity;
  466. db.SaveChanges();
  467. }
  468. //操作余额
  469. public void OpAccount(WebCMSEntities db, Orders order, int UserId, decimal Money, int Count = 1)
  470. {
  471. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  472. if (account == null)
  473. {
  474. account = db.UserAccount.Add(new UserAccount()
  475. {
  476. Id = UserId,
  477. UserId = UserId,
  478. }).Entity;
  479. db.SaveChanges();
  480. }
  481. int ChangeType = 0;
  482. if(Money == 600)
  483. {
  484. ChangeType = 117;
  485. }
  486. else if(Money == 100)
  487. {
  488. ChangeType = 118;
  489. }
  490. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  491. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  492. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  493. account.BalanceAmount += Money * Count;
  494. account.TotalAmount += Money * Count;
  495. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  496. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  497. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  498. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  499. {
  500. CreateDate = DateTime.Now,
  501. UpdateDate = DateTime.Now,
  502. UserId = UserId, //创客
  503. ChangeType = ChangeType, //变动类型
  504. ChangeAmount = Money * Count, //变更金额
  505. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  506. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  507. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  508. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  509. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  510. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  511. QueryCount = order.Id,
  512. }).Entity;
  513. db.SaveChanges();
  514. }
  515. //操作盟主可提现余额
  516. public void OpLeaderAccount(WebCMSEntities db, Orders order, int UserId, decimal Money, int Count = 1)
  517. {
  518. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  519. if (account == null)
  520. {
  521. account = db.UserAccount.Add(new UserAccount()
  522. {
  523. Id = UserId,
  524. UserId = UserId,
  525. }).Entity;
  526. db.SaveChanges();
  527. }
  528. int ChangeType = 0;
  529. if(Money == 600)
  530. {
  531. ChangeType = 117;
  532. }
  533. decimal BeforeLeaderBalanceAmount = account.LeaderBalanceAmount; //变更前余额
  534. account.LeaderBalanceAmount += Money * Count;
  535. decimal AfterLeaderBalanceAmount = account.LeaderBalanceAmount; //变更后余额
  536. LeaderAccountRecord leaderAccountRecord = db.LeaderAccountRecord.Add(new LeaderAccountRecord()
  537. {
  538. CreateDate = DateTime.Now,
  539. UpdateDate = DateTime.Now,
  540. UserId = UserId, //创客
  541. ChangeType = ChangeType, //变动类型
  542. ChangeAmount = Money * Count, //变更金额
  543. BeforeBalanceAmount = BeforeLeaderBalanceAmount, //变更前余额
  544. AfterBalanceAmount = AfterLeaderBalanceAmount, //变更后余额
  545. QueryCount = order.Id,
  546. }).Entity;
  547. db.SaveChanges();
  548. }
  549. public void DirectPrize(WebCMSEntities db, int OrderId, int UserId, int Count = 1)
  550. {
  551. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  552. if (account == null)
  553. {
  554. account = db.UserAccount.Add(new UserAccount()
  555. {
  556. Id = UserId,
  557. UserId = UserId,
  558. }).Entity;
  559. db.SaveChanges();
  560. }
  561. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  562. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  563. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  564. account.BalanceAmount += 100 * Count;
  565. account.TotalAmount += 100 * Count;
  566. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  567. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  568. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  569. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  570. {
  571. CreateDate = DateTime.Now,
  572. UpdateDate = DateTime.Now,
  573. UserId = UserId, //创客
  574. ChangeType = 112, //变动类型
  575. ChangeAmount = 100 * Count, //变更金额
  576. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  577. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  578. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  579. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  580. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  581. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  582. QueryCount = OrderId,
  583. }).Entity;
  584. db.SaveChanges();
  585. }
  586. // 推荐王逻辑(下单)
  587. public void RecommendMethod(int UserId, string TradeMonth)
  588. {
  589. string SendData = "{\"Kind\":\"1\",\"Data\":{\"UserId\":\"" + UserId + "\",\"TradeMonth\":\"" + TradeMonth + "\"}}";
  590. RedisDbconn.Instance.AddList("RecommendActStatQueue", SendData);
  591. }
  592. #endregion
  593. }
  594. }