AlipayPayBack2Service.cs 33 KB

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