AlipayPayBack2Service.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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 || user.UserType == 1) //盟主或运营中心
  224. {
  225. if(user.LeaderLevel > 0)
  226. {
  227. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == order.UserId) ?? new UserAccount();
  228. if(acccount.LeaderReserve >= order.TotalPrice)
  229. {
  230. if(order.PayMode == 4)
  231. {
  232. //扣减备用金
  233. OpReserve(db, order, order.UserId, order.TotalPrice, 2, 0, "商城购机(储备金支付)");
  234. //返回到余额
  235. // OpAccount(db, order, order.UserId, order.TotalPrice / pro.ProductCount, pro.ProductCount);
  236. }
  237. else
  238. {
  239. //扣减备用金
  240. OpReserve(db, order, order.UserId, order.TotalPrice, 2, 0, "商城购机");
  241. //返回到余额
  242. OpLeaderAccount(db, order, order.UserId, order.TotalPrice / pro.ProductCount, pro.ProductCount);
  243. }
  244. }
  245. }
  246. //获得100元奖励
  247. OpAccount(db, order, order.UserId, 100, pro.ProductCount);
  248. //推荐奖励
  249. DirectPrize(db, order.Id, order.UserId, pro.ProductCount);
  250. //推荐下单上级获得30天的机具循环天数(盟主自己得)
  251. 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();
  252. foreach (var subPos in posList)
  253. {
  254. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == subPos.Id);
  255. if (pos != null)
  256. {
  257. pos.RecycEndDate = pos.RecycEndDate.Value.AddDays(30);
  258. }
  259. }
  260. db.SaveChanges();
  261. if(user.LeaderLevel == 2 || user.UserType == 1)
  262. {
  263. if(couponIds.Count > 0)
  264. {
  265. foreach(int couponId in couponIds)
  266. {
  267. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  268. if(coupon != null)
  269. {
  270. if(user.LeaderLevel == 2)
  271. {
  272. coupon.LeaderUserId = user.Id;
  273. }
  274. if(user.UserType == 1)
  275. {
  276. coupon.OpId = user.Id;
  277. }
  278. }
  279. }
  280. db.SaveChanges();
  281. }
  282. }
  283. // else
  284. // {
  285. // int PUserId = user.ParentUserId;
  286. // while(PUserId > 0)
  287. // {
  288. // Users puser = db.Users.FirstOrDefault(m => m.Id == PUserId) ?? new Users();
  289. // if(puser.LeaderLevel == 2)
  290. // {
  291. // UserAccount acc = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
  292. // if(acc.LeaderReserve >= order.TotalPrice)
  293. // {
  294. // //扣减备用金
  295. // OpReserve(db, order, puser.Id, order.TotalPrice, 2, order.UserId, "购机奖励");
  296. // //返回到余额
  297. // OpLeaderAccount(db, order, puser.Id, order.TotalPrice / pro.ProductCount, pro.ProductCount);
  298. // }
  299. // PUserId = 0;
  300. // }
  301. // else
  302. // {
  303. // PUserId = puser.ParentUserId;
  304. // }
  305. // }
  306. // }
  307. return;
  308. }
  309. int ParentUserId = user.ParentUserId;
  310. // List<int> proids = new List<int>();
  311. // proids.Add(10);
  312. // proids.Add(11);
  313. while(ParentUserId > 0)
  314. {
  315. Users puser = db.Users.FirstOrDefault(m => m.Id == ParentUserId) ?? new Users();
  316. int machineCount = db.PosMachinesTwo.Count(m => m.BuyUserId == ParentUserId && m.PosSnType == 0 && m.ActivationState == 0); //判断是否拥有3台购买机
  317. int ActiveCount = db.PosMachinesTwo.Count(m => m.BuyUserId == ParentUserId && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  318. int couponCount = db.PosCoupons.Count(m => m.UserId == ParentUserId && m.IsUse == 0); //判断是否拥有3张券
  319. function.WriteLog("MakerCode:" + puser.MakerCode, "推荐下单奖励监控日志");
  320. function.WriteLog("machineCount:" + machineCount, "推荐下单奖励监控日志");
  321. function.WriteLog("ActiveCount:" + ActiveCount, "推荐下单奖励监控日志");
  322. if ((machineCount + ActiveCount + couponCount >= 3 || puser.LeaderLevel > 0) && !directPrize)
  323. {
  324. // function.WriteLog("满足条件", "推荐下单奖励监控日志");
  325. // if(puser.LeaderLevel == 0) // 非盟主直推奖励,每个创客第一次下单,上级可得
  326. // {
  327. // List<int> oids = new List<int>();
  328. // var orderpros = db.OrderProduct.Select(m => new { m.OrderId,m.UserId,m.ProductId }).Where(m => m.UserId == order.UserId && proids.Contains(m.ProductId)).ToList();
  329. // foreach(var orderpro in orderpros)
  330. // {
  331. // oids.Add(orderpro.OrderId);
  332. // }
  333. // bool check = db.Orders.Any(m => oids.Contains(m.Id) && m.Status > 0);
  334. // if(!check)
  335. // {
  336. // DirectPrize(db, order.Id, ParentUserId, pro.ProductCount);
  337. // directPrize = true;
  338. // }
  339. // }
  340. // else
  341. // {
  342. // 盟主直推奖励,可以每次下单获得
  343. DirectPrize(db, order.Id, ParentUserId, pro.ProductCount);
  344. directPrize = true;
  345. // }
  346. //推荐下单上级获得30天的机具循环天数
  347. 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();
  348. foreach (var subPos in posList)
  349. {
  350. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == subPos.Id);
  351. if (pos != null)
  352. {
  353. pos.RecycEndDate = pos.RecycEndDate.Value.AddDays(30);
  354. }
  355. }
  356. db.SaveChanges();
  357. }
  358. if(puser.LeaderLevel > 0)
  359. {
  360. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
  361. if(acccount.LeaderReserve >= order.TotalPrice && !buyPrize)
  362. {
  363. //购机奖励
  364. OpAccount(db, order, puser.Id, 100, pro.ProductCount);
  365. buyPrize = true;
  366. }
  367. if(acccount.LeaderReserve >= order.TotalPrice && leaderFlag == 0)
  368. {
  369. //扣减备用金
  370. OpReserve(db, order, puser.Id, order.TotalPrice, 2, order.UserId, "购机奖励");
  371. //返回到余额
  372. OpLeaderAccount(db, order, puser.Id, order.TotalPrice / pro.ProductCount, pro.ProductCount);
  373. leaderFlag = puser.LeaderLevel;
  374. }
  375. //如果是大盟主,则标记大盟主标签
  376. if(couponIds.Count > 0 && puser.LeaderLevel == 2 && acccount.LeaderReserve >= order.TotalPrice && !bigLeaderPrize)
  377. {
  378. foreach(int couponId in couponIds)
  379. {
  380. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  381. if(coupon != null)
  382. {
  383. coupon.LeaderUserId = puser.Id;
  384. }
  385. }
  386. db.SaveChanges();
  387. bigLeaderPrize = true;
  388. }
  389. }
  390. ParentUserId = puser.ParentUserId;
  391. }
  392. }
  393. }
  394. function.WriteLog("\r\n\r\n", "推荐下单奖励监控日志");
  395. }
  396. //推荐王
  397. if(pro.ProductId == 29)
  398. {
  399. RecommendMethod(order.UserId, order.CreateDate.Value.ToString("yyyyMM"));
  400. }
  401. //购买分仓临时额度
  402. if(pro.ProductId == 30 || pro.ProductId == 31 || pro.ProductId == 32)
  403. {
  404. string SendData = "{\"Kind\":\"1\",\"Data\":{\"OrderId\":\"" + order.Id + "\"}}";
  405. RedisDbconn.Instance.AddList("StoreApplyQueue", SendData);
  406. }
  407. }
  408. else
  409. {
  410. if(order.ProductId > 0 && order.ErpMode > 0)
  411. {
  412. //盟主储备金-升级/购买 ErpMode:1-升级,2-购买
  413. if(order.ErpMode > 0)
  414. {
  415. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  416. if(user != null)
  417. {
  418. if(order.ErpMode == 1)
  419. {
  420. user.LeaderLevel = 2;
  421. db.SaveChanges();
  422. }
  423. OpReserve(db, order, order.UserId, order.TotalPrice, 1);
  424. }
  425. }
  426. }
  427. }
  428. }
  429. }
  430. //小盟主购买逻辑
  431. public void LeaderBack(WebCMSEntities db, Orders order)
  432. {
  433. int LeaderUserId = order.UserId;
  434. int level = 0;
  435. while(LeaderUserId > 0)
  436. {
  437. level += 1;
  438. Users user = db.Users.FirstOrDefault(m => m.Id == LeaderUserId);
  439. if(user != null)
  440. {
  441. if(user.LeaderLevel == 2 && level > 1)
  442. {
  443. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == LeaderUserId);
  444. if(account != null)
  445. {
  446. if(account.LeaderReserve >= order.TotalPrice)
  447. {
  448. OpReserve(db, order, LeaderUserId, order.TotalPrice, 2, 0, "推荐小盟主");
  449. OpLeaderAccount(db, order, LeaderUserId, order.TotalPrice);
  450. }
  451. else
  452. {
  453. decimal LeaderReserve = account.LeaderReserve;
  454. OpReserve(db, order, LeaderUserId, LeaderReserve, 2, 0, "推荐小盟主");
  455. OpLeaderAccount(db, order, LeaderUserId, LeaderReserve);
  456. }
  457. }
  458. LeaderUserId = 0;
  459. }
  460. else
  461. {
  462. LeaderUserId = user.ParentUserId;
  463. }
  464. }
  465. else
  466. {
  467. LeaderUserId = 0;
  468. }
  469. }
  470. }
  471. //操作储备金
  472. public void OpReserve(WebCMSEntities db, Orders order, int UserId, decimal Money, int ChangeType, int SourceUserId = 0, string Remark = "储备金购买")
  473. {
  474. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  475. if (account == null)
  476. {
  477. account = db.UserAccount.Add(new UserAccount()
  478. {
  479. Id = UserId,
  480. UserId = UserId,
  481. }).Entity;
  482. db.SaveChanges();
  483. }
  484. decimal BeforeAmount = account.LeaderReserve; //变更前总金额
  485. if(ChangeType == 1)
  486. {
  487. account.LeaderReserve += Money;
  488. }
  489. else
  490. {
  491. account.LeaderReserve -= Money;
  492. }
  493. decimal AfterAmount = account.LeaderReserve; //变更后总金额
  494. LeaderReserveRecord add = db.LeaderReserveRecord.Add(new LeaderReserveRecord()
  495. {
  496. CreateDate = DateTime.Now,
  497. ChangeType = ChangeType,
  498. OrderId = order.Id,
  499. Remark = Remark,
  500. AfterAmt = AfterAmount,
  501. BeforeAmt = BeforeAmount,
  502. ChangeAmt = Money,
  503. TradeDate = DateTime.Now.ToString("yyyyMMdd"),
  504. TradeMonth = DateTime.Now.ToString("yyyyMM"),
  505. UserId = UserId,
  506. SourceUserId = SourceUserId,
  507. }).Entity;
  508. db.SaveChanges();
  509. }
  510. //操作余额
  511. public void OpAccount(WebCMSEntities db, Orders order, int UserId, decimal Money, int Count = 1)
  512. {
  513. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  514. if (account == null)
  515. {
  516. account = db.UserAccount.Add(new UserAccount()
  517. {
  518. Id = UserId,
  519. UserId = UserId,
  520. }).Entity;
  521. db.SaveChanges();
  522. }
  523. int ChangeType = 0;
  524. if(Money == 600)
  525. {
  526. ChangeType = 117;
  527. }
  528. else if(Money == 100)
  529. {
  530. ChangeType = 118;
  531. }
  532. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  533. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  534. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  535. account.BalanceAmount += Money * Count;
  536. account.TotalAmount += Money * Count;
  537. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  538. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  539. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  540. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  541. {
  542. CreateDate = DateTime.Now,
  543. UpdateDate = DateTime.Now,
  544. UserId = UserId, //创客
  545. ChangeType = ChangeType, //变动类型
  546. ChangeAmount = Money * Count, //变更金额
  547. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  548. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  549. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  550. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  551. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  552. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  553. QueryCount = order.Id,
  554. }).Entity;
  555. db.SaveChanges();
  556. }
  557. public void ChangeAccount(WebCMSEntities db, Orders order, int UserId, decimal Money, int ChangeType = 0)
  558. {
  559. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  560. if (account == null)
  561. {
  562. account = db.UserAccount.Add(new UserAccount()
  563. {
  564. Id = UserId,
  565. UserId = UserId,
  566. }).Entity;
  567. db.SaveChanges();
  568. }
  569. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  570. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  571. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  572. account.BalanceAmount += Money;
  573. account.TotalAmount += Money;
  574. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  575. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  576. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  577. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  578. {
  579. CreateDate = DateTime.Now,
  580. UpdateDate = DateTime.Now,
  581. UserId = UserId, //创客
  582. ChangeType = ChangeType, //变动类型
  583. ChangeAmount = Money, //变更金额
  584. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  585. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  586. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  587. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  588. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  589. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  590. QueryCount = order.Id,
  591. }).Entity;
  592. db.SaveChanges();
  593. }
  594. //操作盟主可提现余额
  595. public void OpLeaderAccount(WebCMSEntities db, Orders order, int UserId, decimal Money, int Count = 1)
  596. {
  597. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  598. if (account == null)
  599. {
  600. account = db.UserAccount.Add(new UserAccount()
  601. {
  602. Id = UserId,
  603. UserId = UserId,
  604. }).Entity;
  605. db.SaveChanges();
  606. }
  607. int ChangeType = 0;
  608. if(Money == 600)
  609. {
  610. ChangeType = 117;
  611. }
  612. decimal BeforeLeaderBalanceAmount = account.LeaderBalanceAmount; //变更前余额
  613. account.LeaderBalanceAmount += Money * Count;
  614. decimal AfterLeaderBalanceAmount = account.LeaderBalanceAmount; //变更后余额
  615. LeaderAccountRecord leaderAccountRecord = db.LeaderAccountRecord.Add(new LeaderAccountRecord()
  616. {
  617. CreateDate = DateTime.Now,
  618. UpdateDate = DateTime.Now,
  619. UserId = UserId, //创客
  620. ChangeType = ChangeType, //变动类型
  621. ChangeAmount = Money * Count, //变更金额
  622. BeforeBalanceAmount = BeforeLeaderBalanceAmount, //变更前余额
  623. AfterBalanceAmount = AfterLeaderBalanceAmount, //变更后余额
  624. QueryCount = order.Id,
  625. }).Entity;
  626. db.SaveChanges();
  627. }
  628. public void DirectPrize(WebCMSEntities db, int OrderId, int UserId, int Count = 1)
  629. {
  630. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  631. if (account == null)
  632. {
  633. account = db.UserAccount.Add(new UserAccount()
  634. {
  635. Id = UserId,
  636. UserId = UserId,
  637. }).Entity;
  638. db.SaveChanges();
  639. }
  640. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  641. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  642. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  643. account.BalanceAmount += 100 * Count;
  644. account.TotalAmount += 100 * Count;
  645. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  646. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  647. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  648. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  649. {
  650. CreateDate = DateTime.Now,
  651. UpdateDate = DateTime.Now,
  652. UserId = UserId, //创客
  653. ChangeType = 112, //变动类型
  654. ChangeAmount = 100 * Count, //变更金额
  655. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  656. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  657. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  658. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  659. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  660. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  661. QueryCount = OrderId,
  662. }).Entity;
  663. db.SaveChanges();
  664. }
  665. // 推荐王逻辑(下单)
  666. public void RecommendMethod(int UserId, string TradeMonth)
  667. {
  668. string SendData = "{\"Kind\":\"1\",\"Data\":{\"UserId\":\"" + UserId + "\",\"TradeMonth\":\"" + TradeMonth + "\"}}";
  669. RedisDbconn.Instance.AddList("RecommendActStatQueue", SendData);
  670. }
  671. //操作运营中心额度
  672. public void OperateAmountChange(OpModels.WebCMSEntities db, Orders order, int UserId, decimal Money, int OperateType, string Remark = "储备金购买")
  673. {
  674. OpModels.UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  675. if (account == null)
  676. {
  677. account = db.UserAccount.Add(new OpModels.UserAccount()
  678. {
  679. Id = UserId,
  680. UserId = UserId,
  681. }).Entity;
  682. db.SaveChanges();
  683. }
  684. decimal BeforeAmount = account.ValidAmount; //变更前总金额
  685. if(OperateType == 1)
  686. {
  687. account.ValidAmount += Money;
  688. }
  689. else
  690. {
  691. account.ValidAmount -= Money;
  692. }
  693. decimal AfterAmount = account.ValidAmount; //变更后总金额
  694. OpModels.AmountRecord add = db.AmountRecord.Add(new OpModels.AmountRecord()
  695. {
  696. CreateDate = DateTime.Now,
  697. UpdateDate = DateTime.Now,
  698. OperateType = OperateType,
  699. AfterAmount = AfterAmount,
  700. BeforeAmount = BeforeAmount,
  701. UseAmount = Money,
  702. UserId = UserId,
  703. SeoDescription = Remark,
  704. }).Entity;
  705. db.SaveChanges();
  706. }
  707. #endregion
  708. }
  709. }