AlipayPayBack2Service.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using System.Threading;
  6. using MySystem.PxcModels;
  7. using Library;
  8. using LitJson;
  9. namespace MySystem
  10. {
  11. public class AlipayPayBack2Service
  12. {
  13. public readonly static AlipayPayBack2Service Instance = new AlipayPayBack2Service();
  14. private AlipayPayBack2Service()
  15. { }
  16. public void Start()
  17. {
  18. Thread th = new Thread(dosomething);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. private void dosomething()
  23. {
  24. bool op = true;
  25. while (op)
  26. {
  27. string content = RedisDbconn.Instance.RPop<string>("PayCallBack2");
  28. if (!string.IsNullOrEmpty(content))
  29. {
  30. try
  31. {
  32. sloveAlipayCallBack(content);
  33. }
  34. catch(Exception ex)
  35. {
  36. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "支付宝支付回调异常");
  37. }
  38. }
  39. else
  40. {
  41. Thread.Sleep(2000);
  42. }
  43. }
  44. }
  45. public void sloveAlipayCallBack(string content)
  46. {
  47. JsonData jsonObj = JsonMapper.ToObject(content);
  48. string OrderNo = jsonObj["out_trade_no"].ToString();
  49. string TradeNo = jsonObj["transaction_id"].ToString();
  50. decimal TotalFee = decimal.Parse(function.CheckNum(jsonObj["total_fee"].ToString()));
  51. WebCMSEntities db = new WebCMSEntities();
  52. OrderForNo forNo = db.OrderForNo.FirstOrDefault(m => m.OrderNo == OrderNo);
  53. if (forNo != null)
  54. {
  55. string[] ids = forNo.OrderIds.Split(',');
  56. foreach (string idString in ids)
  57. {
  58. int OrderId = int.Parse(idString);
  59. DoOrderV2(db, OrderId);
  60. Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId && m.Status > 0);
  61. if(order != null)
  62. {
  63. Products product = db.Products.FirstOrDefault(m => m.Id == order.ProductId) ?? new Products();
  64. if(product.ProductKind == 2)
  65. {
  66. order.Status = 2;
  67. order.SendStatus = 1;
  68. order.SendDate = DateTime.Now;
  69. db.SaveChanges();
  70. }
  71. }
  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. db.SaveChanges();
  86. if(order.ParentOrderId > 0)
  87. {
  88. int total = db.Orders.Count(m => m.ParentOrderId == order.ParentOrderId);
  89. int paycount = db.Orders.Count(m => m.ParentOrderId == order.ParentOrderId && m.Status > 0);
  90. if(paycount >= total)
  91. {
  92. // order = db.Orders.FirstOrDefault(m => m.Id == order.ParentOrderId && m.Status == 0);
  93. // if (order != null)
  94. // {
  95. // order.Status = 2;
  96. // order.PayDate = DateTime.Now;
  97. // order.PayStatus = 1;
  98. // OrderId = order.Id;
  99. // }
  100. DoOrderV2(db, order.ParentOrderId);
  101. }
  102. return;
  103. }
  104. OrderProduct pro = db.OrderProduct.FirstOrDefault(m => m.OrderId == OrderId);
  105. if (pro != null)
  106. {
  107. order.ProductId = pro.ProductId;
  108. //扣减对应商品的库存
  109. Products product = db.Products.FirstOrDefault(m => m.Id == pro.ProductId);
  110. if(product != null)
  111. {
  112. product.Stock -= pro.ProductCount;
  113. product.BuyCount += pro.ProductCount;
  114. }
  115. db.SaveChanges();
  116. if(order.ErpMode > 0)
  117. {
  118. pro.ProductId = order.ProductId;
  119. }
  120. List<int> couponIds = new List<int>();
  121. if (pro.ProductId == 10 || pro.ProductId == 11 || pro.ProductId == 27 || pro.ProductId == 28 || pro.ProductId == 34 || pro.ProductId == -2)
  122. {
  123. order.Status = 2;
  124. int BuyCount = pro.ProductCount;
  125. int Kind = 0;
  126. int BeforeLeaderLevel = 0;
  127. if (pro.ProductId == 10 || pro.ProductId == 34)
  128. {
  129. Kind = 1;
  130. }
  131. else if (pro.ProductId == 11 || pro.ProductId == -2)
  132. {
  133. Kind = 2;
  134. }
  135. else if (pro.ProductId == 28)
  136. {
  137. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId && m.LeaderLevel == 0);
  138. if(user != null)
  139. {
  140. BeforeLeaderLevel = user.LeaderLevel;
  141. user.LeaderLevel = 1;
  142. }
  143. }
  144. else if (pro.ProductId == 27)
  145. {
  146. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId && m.LeaderLevel < 2);
  147. if(user != null)
  148. {
  149. BeforeLeaderLevel = user.LeaderLevel;
  150. user.LeaderLevel = 2;
  151. }
  152. }
  153. if(Kind <= 2 && (pro.ProductId == 10 || pro.ProductId == 11 || pro.ProductId == 34 || pro.ProductId == -2))
  154. {
  155. // 购买600一组机具券,返券
  156. int CouponCount = 0;
  157. if(Kind == 1 || Kind == 3)
  158. {
  159. CouponCount = 3 * pro.ProductCount;
  160. }
  161. else if(Kind == 2)
  162. {
  163. CouponCount = 2 * pro.ProductCount;
  164. }
  165. if(pro.ProductId == 34) //助利宝商机券购买,50张起购
  166. {
  167. CouponCount = 50 * pro.ProductCount;
  168. }
  169. string Codes = "";
  170. 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();
  171. int RecordId = 0;
  172. if(coupons.Count > 0 && (pro.ProductId == 34 || pro.ProductId == -2))
  173. {
  174. var adds = db.HelpProfitExchange.Add(new HelpProfitExchange()
  175. {
  176. CreateDate = DateTime.Now, //创建时间
  177. UserId = order.UserId,
  178. ExchangeCount = coupons.Count,
  179. }).Entity;
  180. db.SaveChanges();
  181. RecordId = adds.Id;
  182. }
  183. foreach (var coupon in coupons) // TODO: 数量多的话,会慢
  184. {
  185. PosCoupons item = db.PosCoupons.FirstOrDefault(m => m.Id == coupon.Id);
  186. if (item != null)
  187. {
  188. item.CreateDate = DateTime.Now;
  189. item.UserId = order.UserId;
  190. item.UpdateDate = DateTime.Now.AddDays(180);
  191. if(pro.ProductId == 34 || pro.ProductId == -2)
  192. {
  193. item.HelpProfitFlag = 1;
  194. db.HelpProfitExchangeDetail.Add(new HelpProfitExchangeDetail()
  195. {
  196. CreateDate = DateTime.Now, //创建时间
  197. UserId = order.UserId,
  198. PosCouponId = item.Id,
  199. ExchangeCode = item.ExchangeCode,
  200. RecordId = RecordId,
  201. });
  202. }
  203. Codes += item.ExchangeCode + ",";
  204. couponIds.Add(coupon.Id);
  205. }
  206. }
  207. order.SnNos = Codes.TrimEnd(',');
  208. }
  209. db.SaveChanges();
  210. if (pro.ProductId == 27 || pro.ProductId == 28)
  211. {
  212. //充值盟主储备金
  213. decimal TotalPrice = 10000;
  214. if(pro.ProductId == 27)
  215. {
  216. TotalPrice = 40000;
  217. }
  218. OpReserve(db, order, order.UserId, TotalPrice, 1);
  219. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId) ?? new Users();
  220. if(pro.ProductId == 27)
  221. {
  222. OperateReserveBackFor(db, user.Id, 40000);
  223. //自留券数量清除
  224. CancelStayCoupon(db, order.UserId, 7);
  225. //预设大盟主职级
  226. LeaderPreUserLevel(db, order.UserId, 2);
  227. AddLeader(db, order.UserId, 2);
  228. decimal Prize = decimal.Parse(function.CheckNum(PublicFunction.GetPublicParam(db, "BigLeaderPrize")));
  229. if(Prize > 0 && BeforeLeaderLevel < 2 && user.UserType == 0)
  230. {
  231. LeaderRecommendPrize(db, order, user.Id, Prize, 122);
  232. }
  233. }
  234. }
  235. if (pro.ProductId == 28) //购买小盟主,给上级大盟主返储备金
  236. {
  237. bool check = LeaderBack(db, order);
  238. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId) ?? new Users();
  239. //自留券数量清除
  240. CancelStayCoupon(db, order.UserId, 5);
  241. if (check) OperateReserveBackFor(db, user.Id, 10000);
  242. //预设小盟主职级
  243. LeaderPreUserLevel(db, order.UserId, 1);
  244. AddLeader(db, order.UserId, 1);
  245. decimal Prize = decimal.Parse(function.CheckNum(PublicFunction.GetPublicParam(db, "SmallLeaderPrize")));
  246. if(Prize > 0 && BeforeLeaderLevel < 1 && user.UserType == 0)
  247. {
  248. LeaderRecommendPrize(db, order, user.Id, Prize, 123);
  249. }
  250. }
  251. }
  252. // 购买盟主储蓄金
  253. if (pro.ProductId == 39 || pro.ProductId == 40)
  254. {
  255. order.Status = 2;
  256. //充值盟主储备金
  257. decimal TotalPrice = 10000;
  258. if(pro.ProductId == 39)
  259. {
  260. TotalPrice = 40000;
  261. }
  262. OpReserve(db, order, order.UserId, TotalPrice, 1);
  263. if(pro.ProductId == 39) //购买大盟主储蓄金,给上级运营中心返可提现金额
  264. {
  265. OperateReserveBackFor(db, order.UserId, 40000);
  266. AddLeader(db, order.UserId, 2);
  267. LeaderPreUserLevel(db, order.UserId, 2);
  268. }
  269. else if(pro.ProductId == 40) //购买小盟主储蓄金,给上级大盟主返储备金
  270. {
  271. LeaderBack(db, order);
  272. AddLeader(db, order.UserId, 1);
  273. LeaderPreUserLevel(db, order.UserId, 1);
  274. }
  275. }
  276. //推荐下单奖励
  277. RedisDbconn.Instance.AddList("PosCouponPrizeQueue", order.Id.ToString());
  278. //推荐王
  279. if(pro.ProductId == 29)
  280. {
  281. RecommendMethod(order.UserId, order.CreateDate.Value.ToString("yyyyMM"));
  282. }
  283. //购买分仓临时额度
  284. if(pro.ProductId == 30 || pro.ProductId == 31 || pro.ProductId == 32)
  285. {
  286. string SendData = "{\"Kind\":\"1\",\"Data\":{\"OrderId\":\"" + order.Id + "\"}}";
  287. RedisDbconn.Instance.AddList("StoreApplyQueue", SendData);
  288. }
  289. //购买小分仓临时额度
  290. if(pro.ProductId == 56 || pro.ProductId == 57 || pro.ProductId == 58)
  291. {
  292. string SendData = "{\"Kind\":\"1\",\"Data\":{\"OrderId\":\"" + order.Id + "\"}}";
  293. RedisDbconn.Instance.AddList("PreStoreApplyQueue", SendData);
  294. }
  295. }
  296. else
  297. {
  298. if(order.ProductId > 0 && order.ErpMode > 0)
  299. {
  300. //盟主储备金-升级/购买 ErpMode:1-升级,2-购买
  301. if(order.ErpMode > 0)
  302. {
  303. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  304. if(user != null)
  305. {
  306. if(order.ErpMode == 1)
  307. {
  308. user.LeaderLevel = 2;
  309. db.SaveChanges();
  310. }
  311. OpReserve(db, order, order.UserId, order.TotalPrice, 1);
  312. }
  313. }
  314. }
  315. }
  316. }
  317. }
  318. //小盟主购买逻辑
  319. public bool LeaderBack(WebCMSEntities db, Orders order)
  320. {
  321. int LeaderUserId = order.UserId;
  322. int level = 0;
  323. bool result = true;
  324. while(LeaderUserId > 0)
  325. {
  326. level += 1;
  327. Users user = db.Users.FirstOrDefault(m => m.Id == LeaderUserId);
  328. if(user != null)
  329. {
  330. decimal TotalPrice = 10000;
  331. if((user.LeaderLevel == 2 || user.UserType == 1) && level > 1)
  332. {
  333. if(user.UserType == 1 && result)
  334. {
  335. OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
  336. OpModels.UserAccount account = opdb.UserAccount.FirstOrDefault(m => m.Id == LeaderUserId) ?? new OpModels.UserAccount();
  337. if(account.TotalAmt > 0)
  338. {
  339. if(account.TotalAmt < TotalPrice)
  340. {
  341. TotalPrice = account.TotalAmt;
  342. }
  343. if(TotalPrice > 0)
  344. {
  345. OperateAmountChange(opdb, LeaderUserId, TotalPrice, 2, 1, "商城购机");
  346. OperateAmountChange(opdb, LeaderUserId, TotalPrice, 1, 2, "商城购机");
  347. LeaderUserId = 0;
  348. result = false;
  349. }
  350. }
  351. else
  352. {
  353. LeaderUserId = user.ParentUserId;
  354. }
  355. }
  356. else if(user.LeaderLevel == 2 && result)
  357. {
  358. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == LeaderUserId);
  359. if(account != null)
  360. {
  361. if(account.LeaderReserve >= TotalPrice)
  362. {
  363. OpReserve(db, order, LeaderUserId, TotalPrice, 2, 0, "推荐小盟主");
  364. OpLeaderAccount(db, order, LeaderUserId, TotalPrice, 1, 1);
  365. LeaderUserId = 0;
  366. result = false;
  367. }
  368. else
  369. {
  370. LeaderUserId = user.ParentUserId;
  371. // decimal LeaderReserve = account.LeaderReserve;
  372. // OpReserve(db, order, LeaderUserId, LeaderReserve, 2, 0, "推荐小盟主");
  373. // OpLeaderAccount(db, order, LeaderUserId, LeaderReserve);
  374. }
  375. }
  376. }
  377. else
  378. {
  379. LeaderUserId = user.ParentUserId;
  380. }
  381. }
  382. else
  383. {
  384. LeaderUserId = user.ParentUserId;
  385. }
  386. }
  387. else
  388. {
  389. LeaderUserId = 0;
  390. }
  391. }
  392. return result;
  393. }
  394. //操作储备金
  395. private int GetLeaderReserveRecordType(string Remark)
  396. {
  397. Dictionary<int, string> data = new Dictionary<int, string>();
  398. data.Add(11, "兑换机具券,机具券兑换,系统增加(盟主储蓄金),系统扣减(盟主储蓄金),储备金购买");
  399. data.Add(12, "系统增加(可提现余额),系统扣减(可提现余额)");
  400. data.Add(13, "推荐大盟主,推荐小盟主,商城购机,购机奖励");
  401. var item = data.FirstOrDefault(m => m.Value.Contains(Remark));
  402. if(item.Key > 0)
  403. {
  404. return item.Key;
  405. }
  406. return 0;
  407. }
  408. public void OpReserve(WebCMSEntities db, Orders order, int UserId, decimal Money, int ChangeType, int SourceUserId = 0, string Remark = "储备金购买")
  409. {
  410. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  411. if (account == null)
  412. {
  413. account = db.UserAccount.Add(new UserAccount()
  414. {
  415. Id = UserId,
  416. UserId = UserId,
  417. }).Entity;
  418. db.SaveChanges();
  419. }
  420. decimal BeforeAmount = account.LeaderReserve; //变更前总金额
  421. if(ChangeType == 1)
  422. {
  423. account.LeaderReserve += Money;
  424. }
  425. else
  426. {
  427. account.LeaderReserve -= Money;
  428. }
  429. decimal AfterAmount = account.LeaderReserve; //变更后总金额
  430. LeaderReserveRecord add = db.LeaderReserveRecord.Add(new LeaderReserveRecord()
  431. {
  432. CreateDate = DateTime.Now,
  433. ChangeType = ChangeType,
  434. OrderId = order.Id,
  435. Remark = Remark,
  436. AfterAmt = AfterAmount,
  437. BeforeAmt = BeforeAmount,
  438. ChangeAmt = Money,
  439. TradeDate = DateTime.Now.ToString("yyyyMMdd"),
  440. TradeMonth = DateTime.Now.ToString("yyyyMM"),
  441. UserId = UserId,
  442. SourceUserId = SourceUserId,
  443. Sort = GetLeaderReserveRecordType(Remark),
  444. }).Entity;
  445. db.SaveChanges();
  446. }
  447. //操作余额
  448. public void OpAccount(WebCMSEntities db, Orders order, int UserId, decimal Money, int Count = 1)
  449. {
  450. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  451. if (account == null)
  452. {
  453. account = db.UserAccount.Add(new UserAccount()
  454. {
  455. Id = UserId,
  456. UserId = UserId,
  457. }).Entity;
  458. db.SaveChanges();
  459. }
  460. int ChangeType = 0;
  461. if(Money == 600)
  462. {
  463. ChangeType = 117;
  464. }
  465. else if(Money == 100)
  466. {
  467. ChangeType = 118;
  468. }
  469. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  470. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  471. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  472. account.BalanceAmount += Money * Count;
  473. account.TotalAmount += Money * Count;
  474. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  475. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  476. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  477. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  478. {
  479. CreateDate = DateTime.Now,
  480. UpdateDate = DateTime.Now,
  481. UserId = UserId, //创客
  482. ChangeType = ChangeType, //变动类型
  483. ChangeAmount = Money * Count, //变更金额
  484. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  485. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  486. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  487. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  488. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  489. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  490. QueryCount = order.Id,
  491. }).Entity;
  492. db.SaveChanges();
  493. }
  494. public void ChangeAccount(WebCMSEntities db, Orders order, int UserId, decimal Money, int ChangeType = 0, string Remark = "")
  495. {
  496. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  497. if (account == null)
  498. {
  499. account = db.UserAccount.Add(new UserAccount()
  500. {
  501. Id = UserId,
  502. UserId = UserId,
  503. }).Entity;
  504. db.SaveChanges();
  505. }
  506. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  507. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  508. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  509. account.BalanceAmount += Money;
  510. if(Money > 0)
  511. {
  512. account.TotalAmount += Money;
  513. }
  514. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  515. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  516. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  517. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  518. {
  519. CreateDate = DateTime.Now,
  520. UpdateDate = DateTime.Now,
  521. UserId = UserId, //创客
  522. ChangeType = ChangeType, //变动类型
  523. ChangeAmount = Math.Abs(Money), //变更金额
  524. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  525. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  526. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  527. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  528. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  529. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  530. QueryCount = order.Id,
  531. Remark = Remark,
  532. }).Entity;
  533. db.SaveChanges();
  534. }
  535. //操作盟主可提现余额
  536. public void OpLeaderAccount(WebCMSEntities db, Orders order, int UserId, decimal Money, int Count = 1, int Kind = 0)
  537. {
  538. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  539. if (account == null)
  540. {
  541. account = db.UserAccount.Add(new UserAccount()
  542. {
  543. Id = UserId,
  544. UserId = UserId,
  545. }).Entity;
  546. db.SaveChanges();
  547. }
  548. int ChangeType = 0;
  549. if(Money == 400)
  550. {
  551. ChangeType = 117;
  552. }
  553. int LeaderId = 0;
  554. decimal LeaderReserve = 0;
  555. if(Kind == 1)
  556. {
  557. LeaderId = UserId;
  558. LeaderReserve = Money * Count;
  559. }
  560. decimal BeforeLeaderBalanceAmount = account.LeaderBalanceAmount; //变更前余额
  561. account.LeaderBalanceAmount += Money * Count;
  562. decimal AfterLeaderBalanceAmount = account.LeaderBalanceAmount; //变更后余额
  563. LeaderAccountRecord leaderAccountRecord = db.LeaderAccountRecord.Add(new LeaderAccountRecord()
  564. {
  565. CreateDate = DateTime.Now,
  566. UpdateDate = DateTime.Now,
  567. UserId = UserId, //创客
  568. ChangeType = ChangeType, //变动类型
  569. ChangeAmount = Money * Count, //变更金额
  570. BeforeBalanceAmount = BeforeLeaderBalanceAmount, //变更前余额
  571. AfterBalanceAmount = AfterLeaderBalanceAmount, //变更后余额
  572. QueryCount = order.Id,
  573. Sort = order.UserId,
  574. LeaderId = LeaderId,
  575. LeaderReserve = LeaderReserve,
  576. }).Entity;
  577. db.SaveChanges();
  578. }
  579. public void DirectPrize(WebCMSEntities db, int OrderId, int UserId, int Count = 1)
  580. {
  581. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  582. if (account == null)
  583. {
  584. account = db.UserAccount.Add(new UserAccount()
  585. {
  586. Id = UserId,
  587. UserId = UserId,
  588. }).Entity;
  589. db.SaveChanges();
  590. }
  591. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  592. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  593. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  594. account.BalanceAmount += 100 * Count;
  595. account.TotalAmount += 100 * Count;
  596. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  597. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  598. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  599. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  600. {
  601. CreateDate = DateTime.Now,
  602. UpdateDate = DateTime.Now,
  603. UserId = UserId, //创客
  604. ChangeType = 112, //变动类型
  605. ChangeAmount = 100 * Count, //变更金额
  606. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  607. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  608. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  609. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  610. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  611. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  612. QueryCount = OrderId,
  613. }).Entity;
  614. db.SaveChanges();
  615. }
  616. // 推荐王逻辑(下单)
  617. public void RecommendMethod(int UserId, string TradeMonth)
  618. {
  619. string SendData = "{\"Kind\":\"1\",\"Data\":{\"UserId\":\"" + UserId + "\",\"TradeMonth\":\"" + TradeMonth + "\"}}";
  620. RedisDbconn.Instance.AddList("RecommendActStatQueue", SendData);
  621. }
  622. //操作运营中心额度
  623. public void OperateReserveBackFor(WebCMSEntities maindb, int UserId, decimal Money)
  624. {
  625. OpModels.WebCMSEntities db = new OpModels.WebCMSEntities();
  626. while(UserId > 0)
  627. {
  628. Users user = maindb.Users.FirstOrDefault(m => m.Id == UserId);
  629. if(user != null)
  630. {
  631. bool sys = db.SysAdmin.Any(m => m.UserId == UserId);
  632. if(sys)
  633. {
  634. OpModels.UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId) ?? new OpModels.UserAccount();
  635. if(account.TotalAmt <= 0)
  636. {
  637. return;
  638. }
  639. if(account.TotalAmt < Money)
  640. {
  641. Money = account.TotalAmt;
  642. }
  643. if(Money > 0)
  644. {
  645. OperateAmountChange(db, UserId, Money, 2, 1, "商城购机");
  646. OperateAmountChange(db, UserId, Money, 1, 2, "商城购机");
  647. }
  648. UserId = 0;
  649. }
  650. else
  651. {
  652. UserId = user.ParentUserId;
  653. }
  654. }
  655. else
  656. {
  657. UserId = 0;
  658. }
  659. }
  660. db.Dispose();
  661. }
  662. public void OperateReserveBack(int UserId, decimal Money)
  663. {
  664. OpModels.WebCMSEntities db = new OpModels.WebCMSEntities();
  665. OpModels.UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId) ?? new OpModels.UserAccount();
  666. if(account.TotalAmt <= 0)
  667. {
  668. return;
  669. }
  670. if(account.TotalAmt < Money)
  671. {
  672. Money = account.TotalAmt;
  673. }
  674. if(Money > 0)
  675. {
  676. OperateAmountChange(db, UserId, Money, 2, 1, "商城购机");
  677. OperateAmountChange(db, UserId, Money, 1, 2, "商城购机");
  678. }
  679. db.Dispose();
  680. }
  681. public void ActReserveBack(int UserId, decimal OpReserve1, decimal OpReserve2, decimal OpReserve3)
  682. {
  683. OpModels.WebCMSEntities db = new OpModels.WebCMSEntities();
  684. if(OpReserve1 > 0) OperateAmountChange(db, UserId, OpReserve1, 1, 1, "机具激活", true);
  685. if(OpReserve2 > 0) OperateAmountChange(db, UserId, OpReserve2, 1, 2, "机具激活", true);
  686. if(OpReserve3 > 0) OperateAmountChange(db, UserId, OpReserve3, 1, 3, "机具激活", true);
  687. db.Dispose();
  688. }
  689. public void OperateAmountChange(OpModels.WebCMSEntities db, int UserId, decimal Money, int OperateType, int AmountType, string Remark = "", bool record = false)
  690. {
  691. OpModels.UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  692. if (account == null)
  693. {
  694. account = db.UserAccount.Add(new OpModels.UserAccount()
  695. {
  696. Id = UserId,
  697. UserId = UserId,
  698. }).Entity;
  699. db.SaveChanges();
  700. }
  701. decimal BeforeAmount = account.ValidAmount + account.TotalAmt + account.ValidForGetAmount; //变更前总金额
  702. if(OperateType == 1)
  703. {
  704. if(AmountType == 1)
  705. {
  706. // BeforeAmount = account.TotalAmt;
  707. account.TotalAmt += Money;
  708. // AfterAmount = account.TotalAmt;
  709. }
  710. else if(AmountType == 2)
  711. {
  712. // BeforeAmount = account.ValidForGetAmount;
  713. account.ValidForGetAmount += Money;
  714. // AfterAmount = account.ValidForGetAmount;
  715. }
  716. else
  717. {
  718. // BeforeAmount = account.ValidAmount;
  719. account.ValidAmount += Money;
  720. // AfterAmount = account.ValidAmount;
  721. }
  722. }
  723. else
  724. {
  725. if(AmountType == 1)
  726. {
  727. // BeforeAmount = account.TotalAmt;
  728. account.TotalAmt -= Money;
  729. // AfterAmount = account.TotalAmt;
  730. }
  731. else if(AmountType == 2)
  732. {
  733. // BeforeAmount = account.ValidForGetAmount;
  734. account.ValidForGetAmount -= Money;
  735. // AfterAmount = account.ValidForGetAmount;
  736. }
  737. else
  738. {
  739. // BeforeAmount = account.ValidAmount;
  740. account.ValidAmount -= Money;
  741. // AfterAmount = account.ValidAmount;
  742. }
  743. }
  744. decimal AfterAmount = account.ValidAmount + account.TotalAmt + account.ValidForGetAmount; //变更后总金额
  745. if(record)
  746. {
  747. OpModels.AmountRecord add = db.AmountRecord.Add(new OpModels.AmountRecord()
  748. {
  749. CreateDate = DateTime.Now,
  750. UpdateDate = DateTime.Now,
  751. OperateType = OperateType,
  752. AfterAmount = AfterAmount,
  753. BeforeAmount = BeforeAmount,
  754. UseAmount = Money,
  755. UserId = UserId,
  756. SeoDescription = Remark,
  757. Version = AmountType,
  758. }).Entity;
  759. }
  760. db.SaveChanges();
  761. }
  762. #endregion
  763. #region 盟主推荐奖励
  764. public void LeaderRecommendPrize(WebCMSEntities db, Orders order, int UserId, decimal Money, int ChangeType)
  765. {
  766. int level = 0;
  767. while(UserId > 0)
  768. {
  769. level += 1;
  770. Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
  771. if(user != null)
  772. {
  773. if((user.UserType == 1 || user.LeaderLevel > 0) && level > 1)
  774. {
  775. ChangeAccount(db, order, UserId, Money, ChangeType);
  776. UserId = 0;
  777. }
  778. else
  779. {
  780. UserId = user.ParentUserId;
  781. }
  782. }
  783. else
  784. {
  785. UserId = 0;
  786. }
  787. }
  788. db.Dispose();
  789. }
  790. #endregion
  791. #region 购买盟主预设职级
  792. public void LeaderPreUserLevel(WebCMSEntities db, int UserId, int LeaderKind)
  793. {
  794. int Month = LeaderKind == 1 ? 6 : 9;
  795. int Rank = LeaderKind == 1 ? 5 : 7;
  796. LeaderRankWhite check = db.LeaderRankWhite.FirstOrDefault(m => m.Id == UserId);
  797. if(check == null)
  798. {
  799. db.LeaderRankWhite.Add(new LeaderRankWhite()
  800. {
  801. CreateDate = DateTime.Now, //设置时间
  802. UpdateDate = DateTime.Now.AddMonths(Month), //过期时间
  803. Rank = Rank,
  804. UserId = UserId, //用户
  805. Id = UserId,
  806. });
  807. }
  808. else
  809. {
  810. if(check.Rank < Rank)
  811. {
  812. check.Rank = Rank;
  813. check.UpdateDate = DateTime.Now.AddMonths(Month);
  814. }
  815. else if(check.Rank == Rank && check.UpdateDate < DateTime.Now.AddMonths(Month))
  816. {
  817. check.UpdateDate = DateTime.Now.AddMonths(Month);
  818. }
  819. }
  820. db.SaveChanges();
  821. }
  822. public void PreUserLevel(WebCMSEntities db, int UserId, int Rank, DateTime ExpiredDate)
  823. {
  824. LeaderRankWhite rank = db.LeaderRankWhite.FirstOrDefault(m => m.Id == UserId);
  825. if(rank == null)
  826. {
  827. rank = db.LeaderRankWhite.Add(new LeaderRankWhite()
  828. {
  829. CreateDate = DateTime.Now, //设置时间
  830. UserId = UserId, //用户
  831. Id = UserId,
  832. }).Entity;
  833. db.SaveChanges();
  834. }
  835. rank.Rank = Rank;
  836. rank.UpdateDate = ExpiredDate;
  837. db.SaveChanges();
  838. }
  839. //取消自留券数量
  840. public void CancelStayCoupon(WebCMSEntities db, int UserId, int CheckLevel = 5)
  841. {
  842. int myLevel = Utils.Instance.GetUserLevel(UserId);
  843. if(myLevel <= CheckLevel)
  844. {
  845. Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
  846. if(user != null)
  847. {
  848. user.Version = 0;
  849. db.SaveChanges();
  850. }
  851. }
  852. }
  853. #endregion
  854. #region 记录盟主
  855. public void AddLeader(WebCMSEntities db, int UserId, int LeaderLevel)
  856. {
  857. int Month = LeaderLevel == 1 ? 6 : 9;
  858. Leaders leader = db.Leaders.FirstOrDefault(m => m.Id == UserId);
  859. if(leader == null)
  860. {
  861. db.Leaders.Add(new Leaders()
  862. {
  863. Id = UserId,
  864. CreateDate = DateTime.Now, //设置时间
  865. UserId = UserId, //用户
  866. LeaderLevel = LeaderLevel,
  867. LastBuyDate = DateTime.Now,
  868. ExpiredDate = DateTime.Now.AddMonths(Month),
  869. });
  870. }
  871. else
  872. {
  873. leader.LeaderLevel = LeaderLevel;
  874. leader.LastBuyDate = DateTime.Now;
  875. leader.ExpiredDate = DateTime.Now.AddMonths(Month);
  876. }
  877. db.SaveChanges();
  878. SendLeaderMsg(db, UserId);
  879. }
  880. #endregion
  881. #region 盟主续费通知
  882. public void SendLeaderMsg(WebCMSEntities db, int UserId)
  883. {
  884. string msg = "";
  885. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  886. DateTime now = DateTime.Now;
  887. Leaders leader = db.Leaders.FirstOrDefault(m => m.Id == UserId);
  888. if(leader != null)
  889. {
  890. if(leader.ExpiredDate >= now)
  891. {
  892. msg = "尊敬的" + user.RealName + "盟主,恭喜您的盟主已成功开通";
  893. }
  894. else
  895. {
  896. msg = "尊敬的" + user.RealName + "盟主,恭喜您重新成为盟主";
  897. }
  898. }
  899. else
  900. {
  901. msg = "尊敬的" + user.RealName + "盟主,恭喜您的盟主已成功开通";
  902. }
  903. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  904. {
  905. UserId = Convert.ToInt32(UserId), //创客
  906. Title = "开通盟主提醒", //标题
  907. Content = msg, //内容
  908. Summary = msg,
  909. CreateDate = DateTime.Now,
  910. }));
  911. }
  912. #endregion
  913. }
  914. }