AlipayPayBack2Service.cs 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using MySystem.PxcModels;
  6. using Library;
  7. using LitJson;
  8. namespace MySystem
  9. {
  10. public class AlipayPayBack2Service
  11. {
  12. public readonly static AlipayPayBack2Service Instance = new AlipayPayBack2Service();
  13. private AlipayPayBack2Service()
  14. { }
  15. public void Start(JobMqMsg jobInfo)
  16. {
  17. string content = "";
  18. try
  19. {
  20. dosomething();
  21. // string Msg = "success";
  22. // jobInfo.Status = Msg == "success" ? 1 : 0;
  23. // jobInfo.Msg = Msg == "success" ? "执行完成" : Msg;
  24. // RabbitMQClient.Instance.SendMsg(Newtonsoft.Json.JsonConvert.SerializeObject(jobInfo), "PublicBack");
  25. }
  26. catch (Exception ex)
  27. {
  28. if (!string.IsNullOrEmpty(content))
  29. {
  30. Dictionary<string, string> data = new Dictionary<string, string>();
  31. data.Add("ErrTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  32. data.Add("ErrMsg", ex.ToString());
  33. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(data), "public_err");
  34. }
  35. else
  36. {
  37. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "商城订单支付回调异常");
  38. }
  39. }
  40. }
  41. private void dosomething()
  42. {
  43. bool op = true;
  44. while (op)
  45. {
  46. string content = RedisDbconn.Instance.RPop<string>("PayCallBack2");
  47. if (!string.IsNullOrEmpty(content))
  48. {
  49. sloveAlipayCallBack(content);
  50. }
  51. else
  52. {
  53. op = false;
  54. }
  55. }
  56. }
  57. public void sloveAlipayCallBack(string content)
  58. {
  59. JsonData jsonObj = JsonMapper.ToObject(content);
  60. string OrderNo = jsonObj["out_trade_no"].ToString();
  61. string TradeNo = jsonObj["transaction_id"].ToString();
  62. decimal TotalFee = decimal.Parse(function.CheckNum(jsonObj["total_fee"].ToString()));
  63. WebCMSEntities db = new WebCMSEntities();
  64. OrderForNo forNo = db.OrderForNo.FirstOrDefault(m => m.OrderNo == OrderNo);
  65. if (forNo != null)
  66. {
  67. string[] ids = forNo.OrderIds.Split(',');
  68. foreach (string idString in ids)
  69. {
  70. int OrderId = int.Parse(idString);
  71. DoOrderV2(db, OrderId);
  72. }
  73. }
  74. db.Dispose();
  75. }
  76. #region 新订单流程
  77. public void DoOrderV2(WebCMSEntities db, int OrderId)
  78. {
  79. Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId && m.Status <= 0);
  80. if (order != null)
  81. {
  82. order.Status = 1;
  83. order.PayDate = DateTime.Now;
  84. order.PayStatus = 1;
  85. 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)
  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. LeaderPreUserLevel(db, order.UserId, 2);
  225. AddLeader(db, order.UserId, 2);
  226. decimal Prize = decimal.Parse(function.CheckNum(PublicFunction.GetPublicParam(db, "BigLeaderPrize")));
  227. if(Prize > 0 && BeforeLeaderLevel < 2 && user.UserType == 0)
  228. {
  229. LeaderRecommendPrize(db, order, user.Id, Prize, 122);
  230. }
  231. }
  232. }
  233. if (pro.ProductId == 28) //购买小盟主,给上级大盟主返储备金
  234. {
  235. bool check = LeaderBack(db, order);
  236. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId) ?? new Users();
  237. if (check) OperateReserveBackFor(db, user.Id, 10000);
  238. //预设小盟主职级
  239. LeaderPreUserLevel(db, order.UserId, 1);
  240. AddLeader(db, order.UserId, 1);
  241. decimal Prize = decimal.Parse(function.CheckNum(PublicFunction.GetPublicParam(db, "SmallLeaderPrize")));
  242. if(Prize > 0 && BeforeLeaderLevel < 1 && user.UserType == 0)
  243. {
  244. LeaderRecommendPrize(db, order, user.Id, Prize, 123);
  245. }
  246. }
  247. }
  248. // 购买盟主储蓄金
  249. if (pro.ProductId == 39 || pro.ProductId == 40)
  250. {
  251. order.Status = 2;
  252. //充值盟主储备金
  253. decimal TotalPrice = 10000;
  254. if(pro.ProductId == 39)
  255. {
  256. TotalPrice = 40000;
  257. }
  258. OpReserve(db, order, order.UserId, TotalPrice, 1);
  259. if(pro.ProductId == 39) //购买大盟主储蓄金,给上级运营中心返可提现金额
  260. {
  261. OperateReserveBackFor(db, order.UserId, 40000);
  262. }
  263. else if(pro.ProductId == 40) //购买小盟主储蓄金,给上级大盟主返储备金
  264. {
  265. LeaderBack(db, order);
  266. }
  267. }
  268. //推荐下单奖励
  269. if (pro.ProductId == 10 || pro.ProductId == 11)
  270. {
  271. bool checkPrize = db.UserAccountRecord.Any(m => m.QueryCount == OrderId && m.ChangeType == 112);
  272. function.WriteLog("OrderId:" + OrderId, "推荐下单奖励监控日志");
  273. function.WriteLog("checkPrize:" + checkPrize, "推荐下单奖励监控日志");
  274. if (checkPrize)
  275. {
  276. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  277. if (user != null)
  278. {
  279. if(user.LeaderLevel > 0 || user.UserType == 1) //盟主或运营中心
  280. {
  281. //获得100元奖励
  282. OpAccount(db, order, order.UserId, 100, pro.ProductCount);
  283. }
  284. }
  285. }
  286. else
  287. {
  288. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  289. if (user != null)
  290. {
  291. bool directPrize = false; //直推奖标记
  292. bool bigLeaderPrize = false; //大盟主券标记
  293. bool operateFlag = false; //运营中心标记
  294. bool buyPrize = false; //返100购机奖励标记或返100购机奖励标记-运营中心
  295. int leaderFlag = 0; //返600备用金标记
  296. if(user.LeaderLevel > 0 || user.UserType == 1) //盟主或运营中心
  297. {
  298. if(user.LeaderLevel > 0)
  299. {
  300. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == order.UserId) ?? new UserAccount();
  301. if(acccount.LeaderReserve >= order.TotalPrice)
  302. {
  303. if(order.PayMode == 4)
  304. {
  305. //扣减备用金
  306. OpReserve(db, order, order.UserId, order.TotalPrice, 0, 0, "商城购机(储备金支付)");
  307. }
  308. }
  309. }
  310. //获得100元奖励
  311. OpAccount(db, order, order.UserId, 100, pro.ProductCount);
  312. //推荐奖励
  313. DirectPrize(db, order.Id, order.UserId, pro.ProductCount);
  314. //推荐下单上级获得30天的机具循环天数(盟主自己得)
  315. var posList = db.PosMachinesTwo.Select(m => new { m.Id, m.UserId, m.BindingState, m.RecycEndDate }).Where(m => m.UserId == order.UserId && m.BindingState == 0 && m.RecycEndDate != null).ToList();
  316. foreach (var subPos in posList)
  317. {
  318. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == subPos.Id);
  319. if (pos != null)
  320. {
  321. pos.RecycEndDate = pos.RecycEndDate.Value.AddDays(30);
  322. }
  323. }
  324. db.SaveChanges();
  325. if(order.PayMode == 4 && user.LeaderLevel > 0) //使用盟主储蓄金,并且是盟主
  326. {
  327. if(user.UserType == 1)
  328. {
  329. if(couponIds.Count > 0)
  330. {
  331. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == user.Id) ?? new UserAccount();
  332. foreach(int couponId in couponIds)
  333. {
  334. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  335. if(coupon != null)
  336. {
  337. if(acccount.LeaderReserve >= 400 * pro.ProductCount)
  338. {
  339. coupon.LeaderUserId = user.Id;
  340. }
  341. coupon.OpId = user.Id;
  342. }
  343. }
  344. }
  345. db.SaveChanges();
  346. }
  347. else
  348. {
  349. //寻找最近运营中心额度大于0的运营中心
  350. int PUserId = user.Id;
  351. bool OperateFlag = true;
  352. bool OncePrizeFlag1 = true;
  353. while(PUserId > 0)
  354. {
  355. Users puser = db.Users.FirstOrDefault(m => m.Id == PUserId);
  356. if(puser != null)
  357. {
  358. if(puser.UserType == 1 && OperateFlag == true) //运营中心
  359. {
  360. if(couponIds.Count > 0)
  361. {
  362. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == order.UserId) ?? new UserAccount();
  363. foreach(int couponId in couponIds)
  364. {
  365. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  366. if(coupon != null)
  367. {
  368. if(acccount.LeaderReserve >= 400 * pro.ProductCount)
  369. {
  370. coupon.LeaderUserId = user.Id;
  371. }
  372. coupon.OpId = puser.Id;
  373. }
  374. }
  375. db.SaveChanges();
  376. if(acccount.LeaderReserve >= 400 * pro.ProductCount && OncePrizeFlag1)
  377. {
  378. //扣减备用金
  379. OpReserve(db, order, order.UserId, 400 * pro.ProductCount, 2, 0, "商城购机");
  380. //返回到余额
  381. OpLeaderAccount(db, order, order.UserId, 400, pro.ProductCount);
  382. OncePrizeFlag1 = false;
  383. }
  384. OperateFlag = false;
  385. }
  386. }
  387. PUserId = puser.ParentUserId;
  388. }
  389. else
  390. {
  391. PUserId = 0;
  392. }
  393. }
  394. }
  395. }
  396. else if(order.PayMode == 4 && user.UserType == 1) //使用盟主储蓄金,并且是运营中心
  397. {
  398. if(couponIds.Count > 0)
  399. {
  400. foreach(int couponId in couponIds)
  401. {
  402. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  403. if(coupon != null)
  404. {
  405. coupon.OpId = user.Id;
  406. }
  407. }
  408. }
  409. db.SaveChanges();
  410. //寻找最近储蓄金充足的大盟主
  411. int PUserId = user.Id;
  412. bool LeaderFlag = true;
  413. bool OncePrizeFlag2 = true;
  414. while(PUserId > 0)
  415. {
  416. Users puser = db.Users.FirstOrDefault(m => m.Id == PUserId);
  417. if(puser != null)
  418. {
  419. if(puser.LeaderLevel == 2 && LeaderFlag == true) //大盟主
  420. {
  421. if(couponIds.Count > 0)
  422. {
  423. UserAccount pacccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
  424. foreach(int couponId in couponIds)
  425. {
  426. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  427. if(coupon != null && pacccount.LeaderReserve >= 400 * pro.ProductCount)
  428. {
  429. coupon.LeaderUserId = puser.Id;
  430. }
  431. }
  432. db.SaveChanges();
  433. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == order.UserId) ?? new UserAccount();
  434. if(acccount.LeaderReserve >= 400 * pro.ProductCount && OncePrizeFlag2)
  435. {
  436. //扣减备用金
  437. OpReserve(db, order, order.UserId, 400 * pro.ProductCount, 2, 0, "商城购机");
  438. //返回到余额
  439. OpLeaderAccount(db, order, order.UserId, 400, pro.ProductCount);
  440. OncePrizeFlag2 = false;
  441. }
  442. LeaderFlag = false;
  443. }
  444. }
  445. PUserId = puser.ParentUserId;
  446. }
  447. else
  448. {
  449. PUserId = 0;
  450. }
  451. }
  452. }
  453. else
  454. {
  455. //寻找最近储蓄金充足的大盟主及最近运营中心额度大于0的运营中心(含自身)
  456. int PUserId = user.Id;
  457. int LeaderFlag = 0;
  458. bool OperateFlag = true;
  459. bool OncePrizeFlag3 = true;
  460. while(PUserId > 0)
  461. {
  462. Users puser = db.Users.FirstOrDefault(m => m.Id == PUserId);
  463. if(puser != null)
  464. {
  465. if(puser.LeaderLevel > 0 && puser.LeaderLevel > LeaderFlag && LeaderFlag < 2) //大盟主
  466. {
  467. if(couponIds.Count > 0)
  468. {
  469. if(puser.LeaderLevel == 2)
  470. {
  471. UserAccount pacccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
  472. foreach(int couponId in couponIds)
  473. {
  474. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  475. if(coupon != null && pacccount.LeaderReserve >= 400 * pro.ProductCount)
  476. {
  477. coupon.LeaderUserId = puser.Id;
  478. }
  479. }
  480. db.SaveChanges();
  481. }
  482. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
  483. if(acccount.LeaderReserve >= 400 * pro.ProductCount && OncePrizeFlag3)
  484. {
  485. //扣减备用金
  486. OpReserve(db, order, puser.Id, 400 * pro.ProductCount, 2, 0, "商城购机");
  487. //返回到余额
  488. OpLeaderAccount(db, order, puser.Id, 400, pro.ProductCount);
  489. LeaderFlag = puser.LeaderLevel;
  490. OncePrizeFlag3 = false;
  491. }
  492. }
  493. }
  494. if(puser.UserType == 1 && OperateFlag == true) //运营中心
  495. {
  496. if(couponIds.Count > 0)
  497. {
  498. foreach(int couponId in couponIds)
  499. {
  500. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  501. if(coupon != null)
  502. {
  503. coupon.OpId = puser.Id;
  504. }
  505. }
  506. db.SaveChanges();
  507. OperateFlag = false;
  508. }
  509. }
  510. PUserId = puser.ParentUserId;
  511. }
  512. else
  513. {
  514. PUserId = 0;
  515. }
  516. }
  517. //运营中心返额度(下单人自己是运营中心)
  518. // OperateReserveBackFor(db, order.UserId, order.TotalPrice);
  519. }
  520. return;
  521. }
  522. int ParentUserId = user.ParentUserId;
  523. // List<int> proids = new List<int>();
  524. // proids.Add(10);
  525. // proids.Add(11);
  526. // 普通创客购买600一组的机具券
  527. bool OncePrizeFlag4 = true;
  528. while(ParentUserId > 0)
  529. {
  530. Users puser = db.Users.FirstOrDefault(m => m.Id == ParentUserId) ?? new Users();
  531. int machineCount = db.PosMachinesTwo.Count(m => m.BuyUserId == ParentUserId && m.PosSnType == 0 && m.ActivationState == 0); //判断是否拥有3台兑换机
  532. int ActiveCount = db.PosMachinesTwo.Count(m => m.BuyUserId == ParentUserId && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  533. int couponCount = db.PosCoupons.Count(m => m.UserId == ParentUserId && m.IsUse == 0); //判断是否拥有3张券
  534. function.WriteLog("MakerCode:" + puser.MakerCode, "推荐下单奖励监控日志");
  535. function.WriteLog("machineCount:" + machineCount, "推荐下单奖励监控日志");
  536. function.WriteLog("ActiveCount:" + ActiveCount, "推荐下单奖励监控日志");
  537. if ((machineCount + ActiveCount + couponCount >= 3 || puser.LeaderLevel > 0) && !directPrize)
  538. {
  539. // function.WriteLog("满足条件", "推荐下单奖励监控日志");
  540. // if(puser.LeaderLevel == 0) // 非盟主直推奖励,每个创客第一次下单,上级可得
  541. // {
  542. // List<int> oids = new List<int>();
  543. // var orderpros = db.OrderProduct.Select(m => new { m.OrderId,m.UserId,m.ProductId }).Where(m => m.UserId == order.UserId && proids.Contains(m.ProductId)).ToList();
  544. // foreach(var orderpro in orderpros)
  545. // {
  546. // oids.Add(orderpro.OrderId);
  547. // }
  548. // bool check = db.Orders.Any(m => oids.Contains(m.Id) && m.Status > 0);
  549. // if(!check)
  550. // {
  551. // DirectPrize(db, order.Id, ParentUserId, pro.ProductCount);
  552. // directPrize = true;
  553. // }
  554. // }
  555. // else
  556. // {
  557. // 盟主直推奖励,可以每次下单获得
  558. DirectPrize(db, order.Id, ParentUserId, pro.ProductCount);
  559. directPrize = true;
  560. // }
  561. //推荐下单上级获得30天的机具循环天数
  562. var posList = db.PosMachinesTwo.Select(m => new { m.Id, m.UserId, m.BindingState, m.RecycEndDate }).Where(m => m.UserId == ParentUserId && m.BindingState == 0 && m.RecycEndDate != null).ToList();
  563. foreach (var subPos in posList)
  564. {
  565. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == subPos.Id);
  566. if (pos != null)
  567. {
  568. pos.RecycEndDate = pos.RecycEndDate.Value.AddDays(30);
  569. }
  570. }
  571. db.SaveChanges();
  572. }
  573. if(puser.LeaderLevel > 0)
  574. {
  575. function.WriteLog("OrderId:" + order.Id, "监控大盟主购机奖励");
  576. function.WriteLog("UserId:" + puser.Id, "监控大盟主购机奖励");
  577. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
  578. function.WriteLog("LeaderReserve:" + acccount.LeaderReserve, "监控大盟主购机奖励");
  579. function.WriteLog("buyPrize:" + buyPrize, "监控大盟主购机奖励");
  580. if(acccount.LeaderReserve >= 400 * pro.ProductCount && !buyPrize)
  581. {
  582. //购机奖励
  583. OpAccount(db, order, puser.Id, 100, pro.ProductCount);
  584. buyPrize = true;
  585. }
  586. function.WriteLog("LeaderReserve:" + acccount.LeaderReserve, "监控大盟主购机奖励");
  587. function.WriteLog("leaderFlag:" + leaderFlag, "监控大盟主购机奖励");
  588. function.WriteLog("OncePrizeFlag4:" + OncePrizeFlag4, "监控大盟主购机奖励");
  589. function.WriteLog("-----------------------------", "监控大盟主购机奖励");
  590. if(acccount.LeaderReserve >= 400 * pro.ProductCount && puser.LeaderLevel > leaderFlag && leaderFlag < 2 && OncePrizeFlag4)
  591. {
  592. //扣减备用金
  593. OpReserve(db, order, puser.Id, 400 * pro.ProductCount, 2, order.UserId, "购机奖励");
  594. //返回到余额
  595. OpLeaderAccount(db, order, puser.Id, 400, pro.ProductCount);
  596. leaderFlag = puser.LeaderLevel;
  597. OncePrizeFlag4 = false;
  598. }
  599. //如果是大盟主,则标记大盟主标签
  600. if(couponIds.Count > 0 && puser.LeaderLevel == 2 && acccount.LeaderReserve >= 400 * pro.ProductCount && !bigLeaderPrize)
  601. {
  602. foreach(int couponId in couponIds)
  603. {
  604. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  605. if(coupon != null)
  606. {
  607. coupon.LeaderUserId = puser.Id;
  608. }
  609. }
  610. db.SaveChanges();
  611. bigLeaderPrize = true;
  612. }
  613. }
  614. if(puser.UserType == 1)
  615. {
  616. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
  617. if(!buyPrize)
  618. {
  619. //购机奖励
  620. ChangeAccount(db, order, puser.Id, 100 * pro.ProductCount, 120);
  621. buyPrize = true;
  622. }
  623. //标记标签
  624. if(couponIds.Count > 0 && !operateFlag)
  625. {
  626. foreach(int couponId in couponIds)
  627. {
  628. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  629. if(coupon != null)
  630. {
  631. coupon.OpId= puser.Id;
  632. }
  633. }
  634. db.SaveChanges();
  635. operateFlag = true;
  636. }
  637. }
  638. ParentUserId = puser.ParentUserId;
  639. }
  640. //运营中心返额度
  641. // OperateReserveBackFor(db, order.UserId, order.TotalPrice);
  642. }
  643. }
  644. function.WriteLog("\r\n\r\n", "推荐下单奖励监控日志");
  645. }
  646. //推荐王
  647. if(pro.ProductId == 29)
  648. {
  649. RecommendMethod(order.UserId, order.CreateDate.Value.ToString("yyyyMM"));
  650. }
  651. //购买分仓临时额度
  652. if(pro.ProductId == 30 || pro.ProductId == 31 || pro.ProductId == 32)
  653. {
  654. string SendData = "{\"Kind\":\"1\",\"Data\":{\"OrderId\":\"" + order.Id + "\"}}";
  655. RedisDbconn.Instance.AddList("StoreApplyQueue", SendData);
  656. }
  657. //购买小分仓临时额度
  658. if(pro.ProductId == 45 || pro.ProductId == 46 || pro.ProductId == 47)
  659. {
  660. string SendData = "{\"Kind\":\"1\",\"Data\":{\"OrderId\":\"" + order.Id + "\"}}";
  661. RedisDbconn.Instance.AddList("PreStoreApplyQueue", SendData);
  662. }
  663. }
  664. else
  665. {
  666. if(order.ProductId > 0 && order.ErpMode > 0)
  667. {
  668. //盟主储备金-升级/购买 ErpMode:1-升级,2-购买
  669. if(order.ErpMode > 0)
  670. {
  671. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  672. if(user != null)
  673. {
  674. if(order.ErpMode == 1)
  675. {
  676. user.LeaderLevel = 2;
  677. db.SaveChanges();
  678. }
  679. OpReserve(db, order, order.UserId, order.TotalPrice, 1);
  680. }
  681. }
  682. }
  683. }
  684. }
  685. }
  686. //小盟主购买逻辑
  687. public bool LeaderBack(WebCMSEntities db, Orders order)
  688. {
  689. int LeaderUserId = order.UserId;
  690. int level = 0;
  691. bool result = true;
  692. while(LeaderUserId > 0)
  693. {
  694. level += 1;
  695. Users user = db.Users.FirstOrDefault(m => m.Id == LeaderUserId);
  696. if(user != null)
  697. {
  698. if(user.LeaderLevel == 2 && level > 1)
  699. {
  700. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == LeaderUserId);
  701. if(account != null)
  702. {
  703. decimal TotalPrice = 10000;
  704. if(account.LeaderReserve >= TotalPrice)
  705. {
  706. OpReserve(db, order, LeaderUserId, TotalPrice, 2, 0, "推荐小盟主");
  707. OpLeaderAccount(db, order, LeaderUserId, TotalPrice);
  708. LeaderUserId = 0;
  709. result = false;
  710. }
  711. else
  712. {
  713. LeaderUserId = user.ParentUserId;
  714. // decimal LeaderReserve = account.LeaderReserve;
  715. // OpReserve(db, order, LeaderUserId, LeaderReserve, 2, 0, "推荐小盟主");
  716. // OpLeaderAccount(db, order, LeaderUserId, LeaderReserve);
  717. }
  718. }
  719. }
  720. else
  721. {
  722. LeaderUserId = user.ParentUserId;
  723. }
  724. }
  725. else
  726. {
  727. LeaderUserId = 0;
  728. }
  729. }
  730. return result;
  731. }
  732. //操作储备金
  733. public void OpReserve(WebCMSEntities db, Orders order, int UserId, decimal Money, int ChangeType, int SourceUserId = 0, string Remark = "储备金购买")
  734. {
  735. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  736. if (account == null)
  737. {
  738. account = db.UserAccount.Add(new UserAccount()
  739. {
  740. Id = UserId,
  741. UserId = UserId,
  742. }).Entity;
  743. db.SaveChanges();
  744. }
  745. decimal BeforeAmount = account.LeaderReserve; //变更前总金额
  746. if(ChangeType == 1)
  747. {
  748. account.LeaderReserve += Money;
  749. }
  750. else
  751. {
  752. account.LeaderReserve -= Money;
  753. }
  754. decimal AfterAmount = account.LeaderReserve; //变更后总金额
  755. LeaderReserveRecord add = db.LeaderReserveRecord.Add(new LeaderReserveRecord()
  756. {
  757. CreateDate = DateTime.Now,
  758. ChangeType = ChangeType,
  759. OrderId = order.Id,
  760. Remark = Remark,
  761. AfterAmt = AfterAmount,
  762. BeforeAmt = BeforeAmount,
  763. ChangeAmt = Money,
  764. TradeDate = DateTime.Now.ToString("yyyyMMdd"),
  765. TradeMonth = DateTime.Now.ToString("yyyyMM"),
  766. UserId = UserId,
  767. SourceUserId = SourceUserId,
  768. }).Entity;
  769. db.SaveChanges();
  770. }
  771. //操作余额
  772. public void OpAccount(WebCMSEntities db, Orders order, int UserId, decimal Money, int Count = 1)
  773. {
  774. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  775. if (account == null)
  776. {
  777. account = db.UserAccount.Add(new UserAccount()
  778. {
  779. Id = UserId,
  780. UserId = UserId,
  781. }).Entity;
  782. db.SaveChanges();
  783. }
  784. int ChangeType = 0;
  785. if(Money == 600)
  786. {
  787. ChangeType = 117;
  788. }
  789. else if(Money == 100)
  790. {
  791. ChangeType = 118;
  792. }
  793. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  794. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  795. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  796. account.BalanceAmount += Money * Count;
  797. account.TotalAmount += Money * Count;
  798. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  799. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  800. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  801. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  802. {
  803. CreateDate = DateTime.Now,
  804. UpdateDate = DateTime.Now,
  805. UserId = UserId, //创客
  806. ChangeType = ChangeType, //变动类型
  807. ChangeAmount = Money * Count, //变更金额
  808. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  809. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  810. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  811. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  812. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  813. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  814. QueryCount = order.Id,
  815. }).Entity;
  816. db.SaveChanges();
  817. }
  818. public void ChangeAccount(WebCMSEntities db, Orders order, int UserId, decimal Money, int ChangeType = 0, string Remark = "")
  819. {
  820. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  821. if (account == null)
  822. {
  823. account = db.UserAccount.Add(new UserAccount()
  824. {
  825. Id = UserId,
  826. UserId = UserId,
  827. }).Entity;
  828. db.SaveChanges();
  829. }
  830. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  831. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  832. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  833. account.BalanceAmount += Money;
  834. if(Money > 0)
  835. {
  836. account.TotalAmount += Money;
  837. }
  838. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  839. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  840. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  841. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  842. {
  843. CreateDate = DateTime.Now,
  844. UpdateDate = DateTime.Now,
  845. UserId = UserId, //创客
  846. ChangeType = ChangeType, //变动类型
  847. ChangeAmount = Math.Abs(Money), //变更金额
  848. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  849. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  850. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  851. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  852. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  853. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  854. QueryCount = order.Id,
  855. Remark = Remark,
  856. }).Entity;
  857. db.SaveChanges();
  858. }
  859. //操作盟主可提现余额
  860. public void OpLeaderAccount(WebCMSEntities db, Orders order, int UserId, decimal Money, int Count = 1)
  861. {
  862. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  863. if (account == null)
  864. {
  865. account = db.UserAccount.Add(new UserAccount()
  866. {
  867. Id = UserId,
  868. UserId = UserId,
  869. }).Entity;
  870. db.SaveChanges();
  871. }
  872. int ChangeType = 0;
  873. if(Money == 400)
  874. {
  875. ChangeType = 117;
  876. }
  877. decimal BeforeLeaderBalanceAmount = account.LeaderBalanceAmount; //变更前余额
  878. account.LeaderBalanceAmount += Money * Count;
  879. decimal AfterLeaderBalanceAmount = account.LeaderBalanceAmount; //变更后余额
  880. LeaderAccountRecord leaderAccountRecord = db.LeaderAccountRecord.Add(new LeaderAccountRecord()
  881. {
  882. CreateDate = DateTime.Now,
  883. UpdateDate = DateTime.Now,
  884. UserId = UserId, //创客
  885. ChangeType = ChangeType, //变动类型
  886. ChangeAmount = Money * Count, //变更金额
  887. BeforeBalanceAmount = BeforeLeaderBalanceAmount, //变更前余额
  888. AfterBalanceAmount = AfterLeaderBalanceAmount, //变更后余额
  889. QueryCount = order.Id,
  890. }).Entity;
  891. db.SaveChanges();
  892. }
  893. public void DirectPrize(WebCMSEntities db, int OrderId, int UserId, int Count = 1)
  894. {
  895. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  896. if (account == null)
  897. {
  898. account = db.UserAccount.Add(new UserAccount()
  899. {
  900. Id = UserId,
  901. UserId = UserId,
  902. }).Entity;
  903. db.SaveChanges();
  904. }
  905. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  906. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  907. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  908. account.BalanceAmount += 100 * Count;
  909. account.TotalAmount += 100 * Count;
  910. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  911. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  912. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  913. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  914. {
  915. CreateDate = DateTime.Now,
  916. UpdateDate = DateTime.Now,
  917. UserId = UserId, //创客
  918. ChangeType = 112, //变动类型
  919. ChangeAmount = 100 * Count, //变更金额
  920. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  921. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  922. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  923. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  924. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  925. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  926. QueryCount = OrderId,
  927. }).Entity;
  928. db.SaveChanges();
  929. }
  930. // 推荐王逻辑(下单)
  931. public void RecommendMethod(int UserId, string TradeMonth)
  932. {
  933. string SendData = "{\"Kind\":\"1\",\"Data\":{\"UserId\":\"" + UserId + "\",\"TradeMonth\":\"" + TradeMonth + "\"}}";
  934. RedisDbconn.Instance.AddList("RecommendActStatQueue", SendData);
  935. }
  936. //操作运营中心额度
  937. public void OperateReserveBackFor(WebCMSEntities maindb, int UserId, decimal Money)
  938. {
  939. OpModels.WebCMSEntities db = new OpModels.WebCMSEntities();
  940. while(UserId > 0)
  941. {
  942. Users user = maindb.Users.FirstOrDefault(m => m.Id == UserId);
  943. if(user != null)
  944. {
  945. bool sys = db.SysAdmin.Any(m => m.UserId == UserId);
  946. if(sys)
  947. {
  948. OpModels.UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId) ?? new OpModels.UserAccount();
  949. if(account.TotalAmt <= 0)
  950. {
  951. return;
  952. }
  953. if(account.TotalAmt < Money)
  954. {
  955. Money = account.TotalAmt;
  956. }
  957. if(Money > 0)
  958. {
  959. OperateAmountChange(db, UserId, Money, 2, 1, "商城购机");
  960. OperateAmountChange(db, UserId, Money, 1, 2, "商城购机");
  961. }
  962. UserId = 0;
  963. }
  964. else
  965. {
  966. UserId = user.ParentUserId;
  967. }
  968. }
  969. else
  970. {
  971. UserId = 0;
  972. }
  973. }
  974. db.Dispose();
  975. }
  976. public void OperateReserveBack(int UserId, decimal Money)
  977. {
  978. OpModels.WebCMSEntities db = new OpModels.WebCMSEntities();
  979. OpModels.UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId) ?? new OpModels.UserAccount();
  980. if(account.TotalAmt <= 0)
  981. {
  982. return;
  983. }
  984. if(account.TotalAmt < Money)
  985. {
  986. Money = account.TotalAmt;
  987. }
  988. if(Money > 0)
  989. {
  990. OperateAmountChange(db, UserId, Money, 2, 1, "商城购机");
  991. OperateAmountChange(db, UserId, Money, 1, 2, "商城购机");
  992. }
  993. db.Dispose();
  994. }
  995. public void ActReserveBack(int UserId, decimal OpReserve1, decimal OpReserve2, decimal OpReserve3)
  996. {
  997. OpModels.WebCMSEntities db = new OpModels.WebCMSEntities();
  998. if(OpReserve1 > 0) OperateAmountChange(db, UserId, OpReserve1, 1, 1, "机具激活", true);
  999. if(OpReserve2 > 0) OperateAmountChange(db, UserId, OpReserve2, 1, 2, "机具激活", true);
  1000. if(OpReserve3 > 0) OperateAmountChange(db, UserId, OpReserve3, 1, 3, "机具激活", true);
  1001. db.Dispose();
  1002. }
  1003. public void OperateAmountChange(OpModels.WebCMSEntities db, int UserId, decimal Money, int OperateType, int AmountType, string Remark = "", bool record = false)
  1004. {
  1005. OpModels.UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  1006. if (account == null)
  1007. {
  1008. account = db.UserAccount.Add(new OpModels.UserAccount()
  1009. {
  1010. Id = UserId,
  1011. UserId = UserId,
  1012. }).Entity;
  1013. db.SaveChanges();
  1014. }
  1015. decimal BeforeAmount = account.ValidAmount + account.TotalAmt + account.ValidForGetAmount; //变更前总金额
  1016. if(OperateType == 1)
  1017. {
  1018. if(AmountType == 1)
  1019. {
  1020. // BeforeAmount = account.TotalAmt;
  1021. account.TotalAmt += Money;
  1022. // AfterAmount = account.TotalAmt;
  1023. }
  1024. else if(AmountType == 2)
  1025. {
  1026. // BeforeAmount = account.ValidForGetAmount;
  1027. account.ValidForGetAmount += Money;
  1028. // AfterAmount = account.ValidForGetAmount;
  1029. }
  1030. else
  1031. {
  1032. // BeforeAmount = account.ValidAmount;
  1033. account.ValidAmount += Money;
  1034. // AfterAmount = account.ValidAmount;
  1035. }
  1036. }
  1037. else
  1038. {
  1039. if(AmountType == 1)
  1040. {
  1041. // BeforeAmount = account.TotalAmt;
  1042. account.TotalAmt -= Money;
  1043. // AfterAmount = account.TotalAmt;
  1044. }
  1045. else if(AmountType == 2)
  1046. {
  1047. // BeforeAmount = account.ValidForGetAmount;
  1048. account.ValidForGetAmount -= Money;
  1049. // AfterAmount = account.ValidForGetAmount;
  1050. }
  1051. else
  1052. {
  1053. // BeforeAmount = account.ValidAmount;
  1054. account.ValidAmount -= Money;
  1055. // AfterAmount = account.ValidAmount;
  1056. }
  1057. }
  1058. decimal AfterAmount = account.ValidAmount + account.TotalAmt + account.ValidForGetAmount; //变更后总金额
  1059. if(record)
  1060. {
  1061. OpModels.AmountRecord add = db.AmountRecord.Add(new OpModels.AmountRecord()
  1062. {
  1063. CreateDate = DateTime.Now,
  1064. UpdateDate = DateTime.Now,
  1065. OperateType = OperateType,
  1066. AfterAmount = AfterAmount,
  1067. BeforeAmount = BeforeAmount,
  1068. UseAmount = Money,
  1069. UserId = UserId,
  1070. SeoDescription = Remark,
  1071. Version = AmountType,
  1072. }).Entity;
  1073. }
  1074. db.SaveChanges();
  1075. }
  1076. #endregion
  1077. #region 盟主推荐奖励
  1078. public void LeaderRecommendPrize(WebCMSEntities db, Orders order, int UserId, decimal Money, int ChangeType)
  1079. {
  1080. int level = 0;
  1081. while(UserId > 0)
  1082. {
  1083. level += 1;
  1084. Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
  1085. if(user != null)
  1086. {
  1087. if((user.UserType == 1 || user.LeaderLevel > 0) && level > 1)
  1088. {
  1089. ChangeAccount(db, order, UserId, Money, ChangeType);
  1090. UserId = 0;
  1091. }
  1092. else
  1093. {
  1094. UserId = user.ParentUserId;
  1095. }
  1096. }
  1097. else
  1098. {
  1099. UserId = 0;
  1100. }
  1101. }
  1102. db.Dispose();
  1103. }
  1104. #endregion
  1105. #region 购买盟主预设职级
  1106. public void LeaderPreUserLevel(WebCMSEntities db, int UserId, int LeaderKind)
  1107. {
  1108. int Month = LeaderKind == 1 ? 6 : 12;
  1109. int Rank = LeaderKind == 1 ? 5 : 7;
  1110. UserRankWhite check = db.UserRankWhite.FirstOrDefault(m => m.Id == UserId);
  1111. if(check == null)
  1112. {
  1113. db.UserRankWhite.Add(new UserRankWhite()
  1114. {
  1115. CreateDate = DateTime.Now, //设置时间
  1116. UpdateDate = DateTime.Now.AddMonths(Month), //过期时间
  1117. Rank = Rank,
  1118. UserId = UserId, //用户
  1119. Id = UserId,
  1120. });
  1121. }
  1122. else
  1123. {
  1124. if(check.Rank < Rank)
  1125. {
  1126. check.Rank = Rank;
  1127. check.UpdateDate = DateTime.Now.AddMonths(Month);
  1128. }
  1129. else if(check.Rank == Rank && check.UpdateDate < DateTime.Now.AddMonths(Month))
  1130. {
  1131. check.UpdateDate = DateTime.Now.AddMonths(Month);
  1132. }
  1133. }
  1134. db.SaveChanges();
  1135. }
  1136. #endregion
  1137. #region 记录盟主
  1138. public void AddLeader(WebCMSEntities db, int UserId, int LeaderLevel)
  1139. {
  1140. Leaders leader = db.Leaders.FirstOrDefault(m => m.Id == UserId);
  1141. if(leader == null)
  1142. {
  1143. db.Leaders.Add(new Leaders()
  1144. {
  1145. CreateDate = DateTime.Now, //设置时间
  1146. UserId = UserId, //用户
  1147. LeaderLevel = LeaderLevel,
  1148. });
  1149. }
  1150. else
  1151. {
  1152. leader.LeaderLevel = LeaderLevel;
  1153. }
  1154. db.SaveChanges();
  1155. }
  1156. #endregion
  1157. }
  1158. }