AlipayPayBack2Service.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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.LeaderLevel == 2 && result)
  334. {
  335. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == LeaderUserId);
  336. if(account != null)
  337. {
  338. if(account.LeaderReserve >= TotalPrice)
  339. {
  340. OpReserve(db, order, LeaderUserId, TotalPrice, 2, 0, "推荐小盟主");
  341. OpLeaderAccount(db, order, LeaderUserId, TotalPrice, 1, 1);
  342. LeaderUserId = 0;
  343. result = false;
  344. }
  345. else
  346. {
  347. LeaderUserId = user.ParentUserId;
  348. // decimal LeaderReserve = account.LeaderReserve;
  349. // OpReserve(db, order, LeaderUserId, LeaderReserve, 2, 0, "推荐小盟主");
  350. // OpLeaderAccount(db, order, LeaderUserId, LeaderReserve);
  351. }
  352. }
  353. }
  354. if(user.UserType == 1 && result)
  355. {
  356. OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
  357. OpModels.UserAccount account = opdb.UserAccount.FirstOrDefault(m => m.Id == LeaderUserId) ?? new OpModels.UserAccount();
  358. if(account.TotalAmt > 0)
  359. {
  360. if(account.TotalAmt < TotalPrice)
  361. {
  362. TotalPrice = account.TotalAmt;
  363. }
  364. if(TotalPrice > 0)
  365. {
  366. OperateAmountChange(opdb, LeaderUserId, TotalPrice, 2, 1, "商城购机");
  367. OperateAmountChange(opdb, LeaderUserId, TotalPrice, 1, 2, "商城购机");
  368. LeaderUserId = 0;
  369. result = false;
  370. }
  371. }
  372. }
  373. }
  374. else
  375. {
  376. LeaderUserId = user.ParentUserId;
  377. }
  378. }
  379. else
  380. {
  381. LeaderUserId = 0;
  382. }
  383. }
  384. return result;
  385. }
  386. //操作储备金
  387. private int GetLeaderReserveRecordType(string Remark)
  388. {
  389. Dictionary<int, string> data = new Dictionary<int, string>();
  390. data.Add(11, "兑换机具券,机具券兑换,系统增加(盟主储蓄金),系统扣减(盟主储蓄金),储备金购买");
  391. data.Add(12, "系统增加(可提现余额),系统扣减(可提现余额)");
  392. data.Add(13, "推荐大盟主,推荐小盟主,商城购机,购机奖励");
  393. var item = data.FirstOrDefault(m => m.Value.Contains(Remark));
  394. if(item.Key > 0)
  395. {
  396. return item.Key;
  397. }
  398. return 0;
  399. }
  400. public void OpReserve(WebCMSEntities db, Orders order, int UserId, decimal Money, int ChangeType, int SourceUserId = 0, string Remark = "储备金购买")
  401. {
  402. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  403. if (account == null)
  404. {
  405. account = db.UserAccount.Add(new UserAccount()
  406. {
  407. Id = UserId,
  408. UserId = UserId,
  409. }).Entity;
  410. db.SaveChanges();
  411. }
  412. decimal BeforeAmount = account.LeaderReserve; //变更前总金额
  413. if(ChangeType == 1)
  414. {
  415. account.LeaderReserve += Money;
  416. }
  417. else
  418. {
  419. account.LeaderReserve -= Money;
  420. }
  421. decimal AfterAmount = account.LeaderReserve; //变更后总金额
  422. LeaderReserveRecord add = db.LeaderReserveRecord.Add(new LeaderReserveRecord()
  423. {
  424. CreateDate = DateTime.Now,
  425. ChangeType = ChangeType,
  426. OrderId = order.Id,
  427. Remark = Remark,
  428. AfterAmt = AfterAmount,
  429. BeforeAmt = BeforeAmount,
  430. ChangeAmt = Money,
  431. TradeDate = DateTime.Now.ToString("yyyyMMdd"),
  432. TradeMonth = DateTime.Now.ToString("yyyyMM"),
  433. UserId = UserId,
  434. SourceUserId = SourceUserId,
  435. Sort = GetLeaderReserveRecordType(Remark),
  436. }).Entity;
  437. db.SaveChanges();
  438. }
  439. //操作余额
  440. public void OpAccount(WebCMSEntities db, Orders order, int UserId, decimal Money, int Count = 1)
  441. {
  442. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  443. if (account == null)
  444. {
  445. account = db.UserAccount.Add(new UserAccount()
  446. {
  447. Id = UserId,
  448. UserId = UserId,
  449. }).Entity;
  450. db.SaveChanges();
  451. }
  452. int ChangeType = 0;
  453. if(Money == 600)
  454. {
  455. ChangeType = 117;
  456. }
  457. else if(Money == 100)
  458. {
  459. ChangeType = 118;
  460. }
  461. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  462. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  463. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  464. account.BalanceAmount += Money * Count;
  465. account.TotalAmount += Money * Count;
  466. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  467. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  468. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  469. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  470. {
  471. CreateDate = DateTime.Now,
  472. UpdateDate = DateTime.Now,
  473. UserId = UserId, //创客
  474. ChangeType = ChangeType, //变动类型
  475. ChangeAmount = Money * Count, //变更金额
  476. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  477. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  478. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  479. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  480. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  481. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  482. QueryCount = order.Id,
  483. }).Entity;
  484. db.SaveChanges();
  485. }
  486. public void ChangeAccount(WebCMSEntities db, Orders order, int UserId, decimal Money, int ChangeType = 0, string Remark = "")
  487. {
  488. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  489. if (account == null)
  490. {
  491. account = db.UserAccount.Add(new UserAccount()
  492. {
  493. Id = UserId,
  494. UserId = UserId,
  495. }).Entity;
  496. db.SaveChanges();
  497. }
  498. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  499. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  500. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  501. account.BalanceAmount += Money;
  502. if(Money > 0)
  503. {
  504. account.TotalAmount += Money;
  505. }
  506. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  507. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  508. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  509. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  510. {
  511. CreateDate = DateTime.Now,
  512. UpdateDate = DateTime.Now,
  513. UserId = UserId, //创客
  514. ChangeType = ChangeType, //变动类型
  515. ChangeAmount = Math.Abs(Money), //变更金额
  516. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  517. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  518. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  519. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  520. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  521. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  522. QueryCount = order.Id,
  523. Remark = Remark,
  524. }).Entity;
  525. db.SaveChanges();
  526. }
  527. //操作盟主可提现余额
  528. public void OpLeaderAccount(WebCMSEntities db, Orders order, int UserId, decimal Money, int Count = 1, int Kind = 0)
  529. {
  530. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  531. if (account == null)
  532. {
  533. account = db.UserAccount.Add(new UserAccount()
  534. {
  535. Id = UserId,
  536. UserId = UserId,
  537. }).Entity;
  538. db.SaveChanges();
  539. }
  540. int ChangeType = 0;
  541. if(Money == 400)
  542. {
  543. ChangeType = 117;
  544. }
  545. int LeaderId = 0;
  546. decimal LeaderReserve = 0;
  547. if(Kind == 1)
  548. {
  549. LeaderId = UserId;
  550. LeaderReserve = Money * Count;
  551. }
  552. decimal BeforeLeaderBalanceAmount = account.LeaderBalanceAmount; //变更前余额
  553. account.LeaderBalanceAmount += Money * Count;
  554. decimal AfterLeaderBalanceAmount = account.LeaderBalanceAmount; //变更后余额
  555. LeaderAccountRecord leaderAccountRecord = db.LeaderAccountRecord.Add(new LeaderAccountRecord()
  556. {
  557. CreateDate = DateTime.Now,
  558. UpdateDate = DateTime.Now,
  559. UserId = UserId, //创客
  560. ChangeType = ChangeType, //变动类型
  561. ChangeAmount = Money * Count, //变更金额
  562. BeforeBalanceAmount = BeforeLeaderBalanceAmount, //变更前余额
  563. AfterBalanceAmount = AfterLeaderBalanceAmount, //变更后余额
  564. QueryCount = order.Id,
  565. Sort = order.UserId,
  566. LeaderId = LeaderId,
  567. LeaderReserve = LeaderReserve,
  568. }).Entity;
  569. db.SaveChanges();
  570. }
  571. public void DirectPrize(WebCMSEntities db, int OrderId, int UserId, int Count = 1)
  572. {
  573. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  574. if (account == null)
  575. {
  576. account = db.UserAccount.Add(new UserAccount()
  577. {
  578. Id = UserId,
  579. UserId = UserId,
  580. }).Entity;
  581. db.SaveChanges();
  582. }
  583. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  584. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  585. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  586. account.BalanceAmount += 100 * Count;
  587. account.TotalAmount += 100 * Count;
  588. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  589. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  590. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  591. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  592. {
  593. CreateDate = DateTime.Now,
  594. UpdateDate = DateTime.Now,
  595. UserId = UserId, //创客
  596. ChangeType = 112, //变动类型
  597. ChangeAmount = 100 * Count, //变更金额
  598. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  599. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  600. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  601. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  602. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  603. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  604. QueryCount = OrderId,
  605. }).Entity;
  606. db.SaveChanges();
  607. }
  608. // 推荐王逻辑(下单)
  609. public void RecommendMethod(int UserId, string TradeMonth)
  610. {
  611. string SendData = "{\"Kind\":\"1\",\"Data\":{\"UserId\":\"" + UserId + "\",\"TradeMonth\":\"" + TradeMonth + "\"}}";
  612. RedisDbconn.Instance.AddList("RecommendActStatQueue", SendData);
  613. }
  614. //操作运营中心额度
  615. public void OperateReserveBackFor(WebCMSEntities maindb, int UserId, decimal Money)
  616. {
  617. OpModels.WebCMSEntities db = new OpModels.WebCMSEntities();
  618. while(UserId > 0)
  619. {
  620. Users user = maindb.Users.FirstOrDefault(m => m.Id == UserId);
  621. if(user != null)
  622. {
  623. bool sys = db.SysAdmin.Any(m => m.UserId == UserId);
  624. if(sys)
  625. {
  626. OpModels.UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId) ?? new OpModels.UserAccount();
  627. if(account.TotalAmt <= 0)
  628. {
  629. return;
  630. }
  631. if(account.TotalAmt < Money)
  632. {
  633. Money = account.TotalAmt;
  634. }
  635. if(Money > 0)
  636. {
  637. OperateAmountChange(db, UserId, Money, 2, 1, "商城购机");
  638. OperateAmountChange(db, UserId, Money, 1, 2, "商城购机");
  639. }
  640. UserId = 0;
  641. }
  642. else
  643. {
  644. UserId = user.ParentUserId;
  645. }
  646. }
  647. else
  648. {
  649. UserId = 0;
  650. }
  651. }
  652. db.Dispose();
  653. }
  654. public void OperateReserveBack(int UserId, decimal Money)
  655. {
  656. OpModels.WebCMSEntities db = new OpModels.WebCMSEntities();
  657. OpModels.UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId) ?? new OpModels.UserAccount();
  658. if(account.TotalAmt <= 0)
  659. {
  660. return;
  661. }
  662. if(account.TotalAmt < Money)
  663. {
  664. Money = account.TotalAmt;
  665. }
  666. if(Money > 0)
  667. {
  668. OperateAmountChange(db, UserId, Money, 2, 1, "商城购机");
  669. OperateAmountChange(db, UserId, Money, 1, 2, "商城购机");
  670. }
  671. db.Dispose();
  672. }
  673. public void ActReserveBack(int UserId, decimal OpReserve1, decimal OpReserve2, decimal OpReserve3)
  674. {
  675. OpModels.WebCMSEntities db = new OpModels.WebCMSEntities();
  676. if(OpReserve1 > 0) OperateAmountChange(db, UserId, OpReserve1, 1, 1, "机具激活", true);
  677. if(OpReserve2 > 0) OperateAmountChange(db, UserId, OpReserve2, 1, 2, "机具激活", true);
  678. if(OpReserve3 > 0) OperateAmountChange(db, UserId, OpReserve3, 1, 3, "机具激活", true);
  679. db.Dispose();
  680. }
  681. public void OperateAmountChange(OpModels.WebCMSEntities db, int UserId, decimal Money, int OperateType, int AmountType, string Remark = "", bool record = false)
  682. {
  683. OpModels.UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  684. if (account == null)
  685. {
  686. account = db.UserAccount.Add(new OpModels.UserAccount()
  687. {
  688. Id = UserId,
  689. UserId = UserId,
  690. }).Entity;
  691. db.SaveChanges();
  692. }
  693. decimal BeforeAmount = account.ValidAmount + account.TotalAmt + account.ValidForGetAmount; //变更前总金额
  694. if(OperateType == 1)
  695. {
  696. if(AmountType == 1)
  697. {
  698. // BeforeAmount = account.TotalAmt;
  699. account.TotalAmt += Money;
  700. // AfterAmount = account.TotalAmt;
  701. }
  702. else if(AmountType == 2)
  703. {
  704. // BeforeAmount = account.ValidForGetAmount;
  705. account.ValidForGetAmount += Money;
  706. // AfterAmount = account.ValidForGetAmount;
  707. }
  708. else
  709. {
  710. // BeforeAmount = account.ValidAmount;
  711. account.ValidAmount += Money;
  712. // AfterAmount = account.ValidAmount;
  713. }
  714. }
  715. else
  716. {
  717. if(AmountType == 1)
  718. {
  719. // BeforeAmount = account.TotalAmt;
  720. account.TotalAmt -= Money;
  721. // AfterAmount = account.TotalAmt;
  722. }
  723. else if(AmountType == 2)
  724. {
  725. // BeforeAmount = account.ValidForGetAmount;
  726. account.ValidForGetAmount -= Money;
  727. // AfterAmount = account.ValidForGetAmount;
  728. }
  729. else
  730. {
  731. // BeforeAmount = account.ValidAmount;
  732. account.ValidAmount -= Money;
  733. // AfterAmount = account.ValidAmount;
  734. }
  735. }
  736. decimal AfterAmount = account.ValidAmount + account.TotalAmt + account.ValidForGetAmount; //变更后总金额
  737. if(record)
  738. {
  739. OpModels.AmountRecord add = db.AmountRecord.Add(new OpModels.AmountRecord()
  740. {
  741. CreateDate = DateTime.Now,
  742. UpdateDate = DateTime.Now,
  743. OperateType = OperateType,
  744. AfterAmount = AfterAmount,
  745. BeforeAmount = BeforeAmount,
  746. UseAmount = Money,
  747. UserId = UserId,
  748. SeoDescription = Remark,
  749. Version = AmountType,
  750. }).Entity;
  751. }
  752. db.SaveChanges();
  753. }
  754. #endregion
  755. #region 盟主推荐奖励
  756. public void LeaderRecommendPrize(WebCMSEntities db, Orders order, int UserId, decimal Money, int ChangeType)
  757. {
  758. int level = 0;
  759. while(UserId > 0)
  760. {
  761. level += 1;
  762. Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
  763. if(user != null)
  764. {
  765. if((user.UserType == 1 || user.LeaderLevel > 0) && level > 1)
  766. {
  767. ChangeAccount(db, order, UserId, Money, ChangeType);
  768. UserId = 0;
  769. }
  770. else
  771. {
  772. UserId = user.ParentUserId;
  773. }
  774. }
  775. else
  776. {
  777. UserId = 0;
  778. }
  779. }
  780. db.Dispose();
  781. }
  782. #endregion
  783. #region 购买盟主预设职级
  784. public void LeaderPreUserLevel(WebCMSEntities db, int UserId, int LeaderKind)
  785. {
  786. int Month = LeaderKind == 1 ? 6 : 9;
  787. int Rank = LeaderKind == 1 ? 5 : 7;
  788. LeaderRankWhite check = db.LeaderRankWhite.FirstOrDefault(m => m.Id == UserId);
  789. if(check == null)
  790. {
  791. db.LeaderRankWhite.Add(new LeaderRankWhite()
  792. {
  793. CreateDate = DateTime.Now, //设置时间
  794. UpdateDate = DateTime.Now.AddMonths(Month), //过期时间
  795. Rank = Rank,
  796. UserId = UserId, //用户
  797. Id = UserId,
  798. });
  799. }
  800. else
  801. {
  802. if(check.Rank < Rank)
  803. {
  804. check.Rank = Rank;
  805. check.UpdateDate = DateTime.Now.AddMonths(Month);
  806. }
  807. else if(check.Rank == Rank && check.UpdateDate < DateTime.Now.AddMonths(Month))
  808. {
  809. check.UpdateDate = DateTime.Now.AddMonths(Month);
  810. }
  811. }
  812. db.SaveChanges();
  813. }
  814. public void PreUserLevel(WebCMSEntities db, int UserId, int Rank, DateTime ExpiredDate)
  815. {
  816. LeaderRankWhite rank = db.LeaderRankWhite.FirstOrDefault(m => m.Id == UserId);
  817. if(rank == null)
  818. {
  819. rank = db.LeaderRankWhite.Add(new LeaderRankWhite()
  820. {
  821. CreateDate = DateTime.Now, //设置时间
  822. UserId = UserId, //用户
  823. Id = UserId,
  824. }).Entity;
  825. db.SaveChanges();
  826. }
  827. rank.Rank = Rank;
  828. rank.UpdateDate = ExpiredDate;
  829. db.SaveChanges();
  830. }
  831. //取消自留券数量
  832. public void CancelStayCoupon(WebCMSEntities db, int UserId, int CheckLevel = 5)
  833. {
  834. int myLevel = Utils.Instance.GetUserLevel(UserId);
  835. if(myLevel <= CheckLevel)
  836. {
  837. Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
  838. if(user != null)
  839. {
  840. user.Version = 0;
  841. db.SaveChanges();
  842. }
  843. }
  844. }
  845. #endregion
  846. #region 记录盟主
  847. public void AddLeader(WebCMSEntities db, int UserId, int LeaderLevel)
  848. {
  849. int Month = LeaderLevel == 1 ? 6 : 9;
  850. Leaders leader = db.Leaders.FirstOrDefault(m => m.Id == UserId);
  851. if(leader == null)
  852. {
  853. db.Leaders.Add(new Leaders()
  854. {
  855. Id = UserId,
  856. CreateDate = DateTime.Now, //设置时间
  857. UserId = UserId, //用户
  858. LeaderLevel = LeaderLevel,
  859. LastBuyDate = DateTime.Now,
  860. ExpiredDate = DateTime.Now.AddMonths(Month),
  861. });
  862. }
  863. else
  864. {
  865. leader.LeaderLevel = LeaderLevel;
  866. leader.LastBuyDate = DateTime.Now;
  867. leader.ExpiredDate = DateTime.Now.AddMonths(Month);
  868. }
  869. db.SaveChanges();
  870. SendLeaderMsg(db, UserId);
  871. }
  872. #endregion
  873. #region 盟主续费通知
  874. public void SendLeaderMsg(WebCMSEntities db, int UserId)
  875. {
  876. string msg = "";
  877. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  878. DateTime now = DateTime.Now;
  879. Leaders leader = db.Leaders.FirstOrDefault(m => m.Id == UserId);
  880. if(leader != null)
  881. {
  882. if(leader.ExpiredDate >= now)
  883. {
  884. msg = "尊敬的" + user.RealName + "盟主,恭喜您的盟主已成功开通";
  885. }
  886. else
  887. {
  888. msg = "尊敬的" + user.RealName + "盟主,恭喜您重新成为盟主";
  889. }
  890. }
  891. else
  892. {
  893. msg = "尊敬的" + user.RealName + "盟主,恭喜您的盟主已成功开通";
  894. }
  895. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  896. {
  897. UserId = Convert.ToInt32(UserId), //创客
  898. Title = "开通盟主提醒", //标题
  899. Content = msg, //内容
  900. Summary = msg,
  901. CreateDate = DateTime.Now,
  902. }));
  903. }
  904. #endregion
  905. }
  906. }