AlipayPayBack2Service.cs 41 KB

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