AlipayPayBack2Service.cs 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  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. string Codes = "";
  166. 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();
  167. int RecordId = 0;
  168. if(coupons.Count > 0 && (pro.ProductId == 34 || pro.ProductId == -2))
  169. {
  170. var adds = db.HelpProfitExchange.Add(new HelpProfitExchange()
  171. {
  172. CreateDate = DateTime.Now, //创建时间
  173. UserId = order.UserId,
  174. ExchangeCount = coupons.Count,
  175. }).Entity;
  176. db.SaveChanges();
  177. RecordId = adds.Id;
  178. }
  179. foreach (var coupon in coupons) // TODO: 数量多的话,会慢
  180. {
  181. PosCoupons item = db.PosCoupons.FirstOrDefault(m => m.Id == coupon.Id);
  182. if (item != null)
  183. {
  184. item.CreateDate = DateTime.Now;
  185. item.UserId = order.UserId;
  186. item.UpdateDate = DateTime.Now.AddDays(180);
  187. if(pro.ProductId == 34 || pro.ProductId == -2)
  188. {
  189. item.HelpProfitFlag = 1;
  190. db.HelpProfitExchangeDetail.Add(new HelpProfitExchangeDetail()
  191. {
  192. CreateDate = DateTime.Now, //创建时间
  193. UserId = order.UserId,
  194. PosCouponId = item.Id,
  195. ExchangeCode = item.ExchangeCode,
  196. RecordId = RecordId,
  197. });
  198. }
  199. Codes += item.ExchangeCode + ",";
  200. couponIds.Add(coupon.Id);
  201. }
  202. }
  203. order.SnNos = Codes.TrimEnd(',');
  204. }
  205. db.SaveChanges();
  206. if (pro.ProductId == 27 || pro.ProductId == 28)
  207. {
  208. //充值盟主储备金
  209. decimal TotalPrice = 10000;
  210. if(pro.ProductId == 27)
  211. {
  212. TotalPrice = 40000;
  213. }
  214. OpReserve(db, order, order.UserId, TotalPrice, 1);
  215. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId) ?? new Users();
  216. if(pro.ProductId == 27)
  217. {
  218. OperateReserveBackFor(db, user.Id, 40000);
  219. //预设大盟主职级
  220. LeaderPreUserLevel(db, order.UserId, 2);
  221. AddLeader(db, order.UserId, 2);
  222. decimal Prize = decimal.Parse(function.CheckNum(PublicFunction.GetPublicParam(db, "BigLeaderPrize")));
  223. if(Prize > 0 && BeforeLeaderLevel < 2 && user.UserType == 0)
  224. {
  225. LeaderRecommendPrize(db, order, user.Id, Prize, 122);
  226. }
  227. }
  228. }
  229. if (pro.ProductId == 28) //购买小盟主,给上级大盟主返储备金
  230. {
  231. bool check = LeaderBack(db, order);
  232. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId) ?? new Users();
  233. if (check) OperateReserveBackFor(db, user.Id, 10000);
  234. //预设小盟主职级
  235. LeaderPreUserLevel(db, order.UserId, 1);
  236. AddLeader(db, order.UserId, 1);
  237. decimal Prize = decimal.Parse(function.CheckNum(PublicFunction.GetPublicParam(db, "SmallLeaderPrize")));
  238. if(Prize > 0 && BeforeLeaderLevel < 1 && user.UserType == 0)
  239. {
  240. LeaderRecommendPrize(db, order, user.Id, Prize, 123);
  241. }
  242. }
  243. }
  244. // 购买盟主储蓄金
  245. if (pro.ProductId == 39 || pro.ProductId == 40)
  246. {
  247. order.Status = 2;
  248. //充值盟主储备金
  249. decimal TotalPrice = 10000;
  250. if(pro.ProductId == 39)
  251. {
  252. TotalPrice = 40000;
  253. }
  254. OpReserve(db, order, order.UserId, TotalPrice, 1);
  255. if(pro.ProductId == 39) //购买大盟主储蓄金,给上级运营中心返可提现金额
  256. {
  257. OperateReserveBackFor(db, order.UserId, 40000);
  258. AddLeader(db, order.UserId, 2);
  259. LeaderPreUserLevel(db, order.UserId, 2);
  260. }
  261. else if(pro.ProductId == 40) //购买小盟主储蓄金,给上级大盟主返储备金
  262. {
  263. LeaderBack(db, order);
  264. AddLeader(db, order.UserId, 1);
  265. LeaderPreUserLevel(db, order.UserId, 1);
  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. db.SaveChanges();
  409. //寻找最近储蓄金充足的大盟主
  410. int PUserId = user.Id;
  411. bool LeaderFlag = true;
  412. bool OncePrizeFlag2 = true;
  413. while(PUserId > 0)
  414. {
  415. Users puser = db.Users.FirstOrDefault(m => m.Id == PUserId);
  416. if(puser != null)
  417. {
  418. if(puser.LeaderLevel == 2 && LeaderFlag == true) //大盟主
  419. {
  420. if(couponIds.Count > 0)
  421. {
  422. UserAccount pacccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
  423. foreach(int couponId in couponIds)
  424. {
  425. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  426. if(coupon != null && pacccount.LeaderReserve >= 400 * pro.ProductCount)
  427. {
  428. coupon.LeaderUserId = puser.Id;
  429. }
  430. }
  431. db.SaveChanges();
  432. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == order.UserId) ?? new UserAccount();
  433. if(acccount.LeaderReserve >= 400 * pro.ProductCount && OncePrizeFlag2)
  434. {
  435. //扣减备用金
  436. OpReserve(db, order, order.UserId, 400 * pro.ProductCount, 2, 0, "商城购机");
  437. //返回到余额
  438. OpLeaderAccount(db, order, order.UserId, 400, pro.ProductCount);
  439. OncePrizeFlag2 = false;
  440. }
  441. LeaderFlag = false;
  442. }
  443. }
  444. PUserId = puser.ParentUserId;
  445. }
  446. else
  447. {
  448. PUserId = 0;
  449. }
  450. }
  451. }
  452. else
  453. {
  454. //寻找最近储蓄金充足的大盟主及最近运营中心额度大于0的运营中心(含自身)
  455. int PUserId = user.Id;
  456. int LeaderFlag = 0;
  457. bool OperateFlag = true;
  458. bool OncePrizeFlag3 = true;
  459. while(PUserId > 0)
  460. {
  461. Users puser = db.Users.FirstOrDefault(m => m.Id == PUserId);
  462. if(puser != null)
  463. {
  464. if(puser.LeaderLevel > 0 && puser.LeaderLevel > LeaderFlag && LeaderFlag < 2) //大盟主
  465. {
  466. if(couponIds.Count > 0)
  467. {
  468. if(puser.LeaderLevel == 2)
  469. {
  470. UserAccount pacccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
  471. foreach(int couponId in couponIds)
  472. {
  473. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  474. if(coupon != null && pacccount.LeaderReserve >= 400 * pro.ProductCount)
  475. {
  476. coupon.LeaderUserId = puser.Id;
  477. }
  478. }
  479. db.SaveChanges();
  480. }
  481. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
  482. if(acccount.LeaderReserve >= 400 * pro.ProductCount && OncePrizeFlag3)
  483. {
  484. //扣减备用金
  485. OpReserve(db, order, puser.Id, 400 * pro.ProductCount, 2, 0, "商城购机");
  486. //返回到余额
  487. OpLeaderAccount(db, order, puser.Id, 400, pro.ProductCount);
  488. LeaderFlag = puser.LeaderLevel;
  489. OncePrizeFlag3 = false;
  490. }
  491. }
  492. }
  493. if(puser.UserType == 1 && OperateFlag == true) //运营中心
  494. {
  495. if(couponIds.Count > 0)
  496. {
  497. foreach(int couponId in couponIds)
  498. {
  499. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  500. if(coupon != null)
  501. {
  502. coupon.OpId = puser.Id;
  503. }
  504. }
  505. db.SaveChanges();
  506. OperateFlag = false;
  507. }
  508. }
  509. PUserId = puser.ParentUserId;
  510. }
  511. else
  512. {
  513. PUserId = 0;
  514. }
  515. }
  516. //运营中心返额度(下单人自己是运营中心)
  517. // OperateReserveBackFor(db, order.UserId, order.TotalPrice);
  518. }
  519. return;
  520. }
  521. int ParentUserId = user.ParentUserId;
  522. // List<int> proids = new List<int>();
  523. // proids.Add(10);
  524. // proids.Add(11);
  525. // 普通创客购买600一组的机具券
  526. bool OncePrizeFlag4 = true;
  527. while(ParentUserId > 0)
  528. {
  529. Users puser = db.Users.FirstOrDefault(m => m.Id == ParentUserId) ?? new Users();
  530. int machineCount = db.PosMachinesTwo.Count(m => m.BuyUserId == ParentUserId && m.PosSnType == 0 && m.ActivationState == 0); //判断是否拥有3台兑换机
  531. int ActiveCount = db.PosMachinesTwo.Count(m => m.BuyUserId == ParentUserId && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  532. int couponCount = db.PosCoupons.Count(m => m.UserId == ParentUserId && m.IsUse == 0); //判断是否拥有3张券
  533. function.WriteLog("MakerCode:" + puser.MakerCode, "推荐下单奖励监控日志");
  534. function.WriteLog("machineCount:" + machineCount, "推荐下单奖励监控日志");
  535. function.WriteLog("ActiveCount:" + ActiveCount, "推荐下单奖励监控日志");
  536. if ((machineCount + ActiveCount + couponCount >= 3 || puser.LeaderLevel > 0) && !directPrize)
  537. {
  538. // function.WriteLog("满足条件", "推荐下单奖励监控日志");
  539. // if(puser.LeaderLevel == 0) // 非盟主直推奖励,每个创客第一次下单,上级可得
  540. // {
  541. // List<int> oids = new List<int>();
  542. // var orderpros = db.OrderProduct.Select(m => new { m.OrderId,m.UserId,m.ProductId }).Where(m => m.UserId == order.UserId && proids.Contains(m.ProductId)).ToList();
  543. // foreach(var orderpro in orderpros)
  544. // {
  545. // oids.Add(orderpro.OrderId);
  546. // }
  547. // bool check = db.Orders.Any(m => oids.Contains(m.Id) && m.Status > 0);
  548. // if(!check)
  549. // {
  550. // DirectPrize(db, order.Id, ParentUserId, pro.ProductCount);
  551. // directPrize = true;
  552. // }
  553. // }
  554. // else
  555. // {
  556. // 盟主直推奖励,可以每次下单获得
  557. DirectPrize(db, order.Id, ParentUserId, pro.ProductCount);
  558. directPrize = true;
  559. // }
  560. //推荐下单上级获得30天的机具循环天数
  561. 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();
  562. foreach (var subPos in posList)
  563. {
  564. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == subPos.Id);
  565. if (pos != null)
  566. {
  567. pos.RecycEndDate = pos.RecycEndDate.Value.AddDays(30);
  568. }
  569. }
  570. db.SaveChanges();
  571. }
  572. if(puser.LeaderLevel > 0)
  573. {
  574. function.WriteLog("OrderId:" + order.Id, "监控大盟主购机奖励");
  575. function.WriteLog("UserId:" + puser.Id, "监控大盟主购机奖励");
  576. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
  577. function.WriteLog("LeaderReserve:" + acccount.LeaderReserve, "监控大盟主购机奖励");
  578. function.WriteLog("buyPrize:" + buyPrize, "监控大盟主购机奖励");
  579. if(acccount.LeaderReserve >= 400 * pro.ProductCount && !buyPrize)
  580. {
  581. //购机奖励
  582. OpAccount(db, order, puser.Id, 100, pro.ProductCount);
  583. buyPrize = true;
  584. }
  585. function.WriteLog("LeaderReserve:" + acccount.LeaderReserve, "监控大盟主购机奖励");
  586. function.WriteLog("leaderFlag:" + leaderFlag, "监控大盟主购机奖励");
  587. function.WriteLog("OncePrizeFlag4:" + OncePrizeFlag4, "监控大盟主购机奖励");
  588. function.WriteLog("-----------------------------", "监控大盟主购机奖励");
  589. if(acccount.LeaderReserve >= 400 * pro.ProductCount && puser.LeaderLevel > leaderFlag && leaderFlag < 2 && OncePrizeFlag4)
  590. {
  591. //扣减备用金
  592. OpReserve(db, order, puser.Id, 400 * pro.ProductCount, 2, order.UserId, "购机奖励");
  593. //返回到余额
  594. OpLeaderAccount(db, order, puser.Id, 400, pro.ProductCount);
  595. leaderFlag = puser.LeaderLevel;
  596. OncePrizeFlag4 = false;
  597. }
  598. //如果是大盟主,则标记大盟主标签
  599. if(couponIds.Count > 0 && puser.LeaderLevel == 2 && acccount.LeaderReserve >= 400 * pro.ProductCount && !bigLeaderPrize)
  600. {
  601. foreach(int couponId in couponIds)
  602. {
  603. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  604. if(coupon != null)
  605. {
  606. coupon.LeaderUserId = puser.Id;
  607. }
  608. }
  609. db.SaveChanges();
  610. bigLeaderPrize = true;
  611. }
  612. }
  613. if(puser.UserType == 1)
  614. {
  615. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
  616. if(!buyPrize)
  617. {
  618. //购机奖励
  619. ChangeAccount(db, order, puser.Id, 100 * pro.ProductCount, 120);
  620. buyPrize = true;
  621. }
  622. //标记标签
  623. if(couponIds.Count > 0 && !operateFlag)
  624. {
  625. foreach(int couponId in couponIds)
  626. {
  627. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  628. if(coupon != null)
  629. {
  630. coupon.OpId= puser.Id;
  631. }
  632. }
  633. db.SaveChanges();
  634. operateFlag = true;
  635. }
  636. }
  637. ParentUserId = puser.ParentUserId;
  638. }
  639. //运营中心返额度
  640. // OperateReserveBackFor(db, order.UserId, order.TotalPrice);
  641. }
  642. }
  643. function.WriteLog("\r\n\r\n", "推荐下单奖励监控日志");
  644. }
  645. //推荐王
  646. if(pro.ProductId == 29)
  647. {
  648. RecommendMethod(order.UserId, order.CreateDate.Value.ToString("yyyyMM"));
  649. }
  650. //购买分仓临时额度
  651. if(pro.ProductId == 30 || pro.ProductId == 31 || pro.ProductId == 32)
  652. {
  653. string SendData = "{\"Kind\":\"1\",\"Data\":{\"OrderId\":\"" + order.Id + "\"}}";
  654. RedisDbconn.Instance.AddList("StoreApplyQueue", SendData);
  655. }
  656. //购买小分仓临时额度
  657. if(pro.ProductId == 45 || pro.ProductId == 46 || pro.ProductId == 47)
  658. {
  659. string SendData = "{\"Kind\":\"1\",\"Data\":{\"OrderId\":\"" + order.Id + "\"}}";
  660. RedisDbconn.Instance.AddList("PreStoreApplyQueue", SendData);
  661. }
  662. }
  663. else
  664. {
  665. if(order.ProductId > 0 && order.ErpMode > 0)
  666. {
  667. //盟主储备金-升级/购买 ErpMode:1-升级,2-购买
  668. if(order.ErpMode > 0)
  669. {
  670. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  671. if(user != null)
  672. {
  673. if(order.ErpMode == 1)
  674. {
  675. user.LeaderLevel = 2;
  676. db.SaveChanges();
  677. }
  678. OpReserve(db, order, order.UserId, order.TotalPrice, 1);
  679. }
  680. }
  681. }
  682. }
  683. }
  684. }
  685. //小盟主购买逻辑
  686. public bool LeaderBack(WebCMSEntities db, Orders order)
  687. {
  688. int LeaderUserId = order.UserId;
  689. int level = 0;
  690. bool result = true;
  691. while(LeaderUserId > 0)
  692. {
  693. level += 1;
  694. Users user = db.Users.FirstOrDefault(m => m.Id == LeaderUserId);
  695. if(user != null)
  696. {
  697. if(user.LeaderLevel == 2 && level > 1)
  698. {
  699. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == LeaderUserId);
  700. if(account != null)
  701. {
  702. decimal TotalPrice = 10000;
  703. if(account.LeaderReserve >= TotalPrice)
  704. {
  705. OpReserve(db, order, LeaderUserId, TotalPrice, 2, 0, "推荐小盟主");
  706. OpLeaderAccount(db, order, LeaderUserId, TotalPrice);
  707. LeaderUserId = 0;
  708. result = false;
  709. }
  710. else
  711. {
  712. LeaderUserId = user.ParentUserId;
  713. // decimal LeaderReserve = account.LeaderReserve;
  714. // OpReserve(db, order, LeaderUserId, LeaderReserve, 2, 0, "推荐小盟主");
  715. // OpLeaderAccount(db, order, LeaderUserId, LeaderReserve);
  716. }
  717. }
  718. }
  719. else
  720. {
  721. LeaderUserId = user.ParentUserId;
  722. }
  723. }
  724. else
  725. {
  726. LeaderUserId = 0;
  727. }
  728. }
  729. return result;
  730. }
  731. //操作储备金
  732. public void OpReserve(WebCMSEntities db, Orders order, int UserId, decimal Money, int ChangeType, int SourceUserId = 0, string Remark = "储备金购买")
  733. {
  734. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  735. if (account == null)
  736. {
  737. account = db.UserAccount.Add(new UserAccount()
  738. {
  739. Id = UserId,
  740. UserId = UserId,
  741. }).Entity;
  742. db.SaveChanges();
  743. }
  744. decimal BeforeAmount = account.LeaderReserve; //变更前总金额
  745. if(ChangeType == 1)
  746. {
  747. account.LeaderReserve += Money;
  748. }
  749. else
  750. {
  751. account.LeaderReserve -= Money;
  752. }
  753. decimal AfterAmount = account.LeaderReserve; //变更后总金额
  754. LeaderReserveRecord add = db.LeaderReserveRecord.Add(new LeaderReserveRecord()
  755. {
  756. CreateDate = DateTime.Now,
  757. ChangeType = ChangeType,
  758. OrderId = order.Id,
  759. Remark = Remark,
  760. AfterAmt = AfterAmount,
  761. BeforeAmt = BeforeAmount,
  762. ChangeAmt = Money,
  763. TradeDate = DateTime.Now.ToString("yyyyMMdd"),
  764. TradeMonth = DateTime.Now.ToString("yyyyMM"),
  765. UserId = UserId,
  766. SourceUserId = SourceUserId,
  767. }).Entity;
  768. db.SaveChanges();
  769. }
  770. //操作余额
  771. public void OpAccount(WebCMSEntities db, Orders order, int UserId, decimal Money, int Count = 1)
  772. {
  773. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  774. if (account == null)
  775. {
  776. account = db.UserAccount.Add(new UserAccount()
  777. {
  778. Id = UserId,
  779. UserId = UserId,
  780. }).Entity;
  781. db.SaveChanges();
  782. }
  783. int ChangeType = 0;
  784. if(Money == 600)
  785. {
  786. ChangeType = 117;
  787. }
  788. else if(Money == 100)
  789. {
  790. ChangeType = 118;
  791. }
  792. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  793. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  794. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  795. account.BalanceAmount += Money * Count;
  796. account.TotalAmount += Money * Count;
  797. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  798. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  799. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  800. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  801. {
  802. CreateDate = DateTime.Now,
  803. UpdateDate = DateTime.Now,
  804. UserId = UserId, //创客
  805. ChangeType = ChangeType, //变动类型
  806. ChangeAmount = Money * Count, //变更金额
  807. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  808. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  809. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  810. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  811. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  812. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  813. QueryCount = order.Id,
  814. }).Entity;
  815. db.SaveChanges();
  816. }
  817. public void ChangeAccount(WebCMSEntities db, Orders order, int UserId, decimal Money, int ChangeType = 0, string Remark = "")
  818. {
  819. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  820. if (account == null)
  821. {
  822. account = db.UserAccount.Add(new UserAccount()
  823. {
  824. Id = UserId,
  825. UserId = UserId,
  826. }).Entity;
  827. db.SaveChanges();
  828. }
  829. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  830. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  831. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  832. account.BalanceAmount += Money;
  833. if(Money > 0)
  834. {
  835. account.TotalAmount += Money;
  836. }
  837. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  838. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  839. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  840. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  841. {
  842. CreateDate = DateTime.Now,
  843. UpdateDate = DateTime.Now,
  844. UserId = UserId, //创客
  845. ChangeType = ChangeType, //变动类型
  846. ChangeAmount = Math.Abs(Money), //变更金额
  847. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  848. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  849. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  850. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  851. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  852. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  853. QueryCount = order.Id,
  854. Remark = Remark,
  855. }).Entity;
  856. db.SaveChanges();
  857. }
  858. //操作盟主可提现余额
  859. public void OpLeaderAccount(WebCMSEntities db, Orders order, int UserId, decimal Money, int Count = 1)
  860. {
  861. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  862. if (account == null)
  863. {
  864. account = db.UserAccount.Add(new UserAccount()
  865. {
  866. Id = UserId,
  867. UserId = UserId,
  868. }).Entity;
  869. db.SaveChanges();
  870. }
  871. int ChangeType = 0;
  872. if(Money == 400)
  873. {
  874. ChangeType = 117;
  875. }
  876. decimal BeforeLeaderBalanceAmount = account.LeaderBalanceAmount; //变更前余额
  877. account.LeaderBalanceAmount += Money * Count;
  878. decimal AfterLeaderBalanceAmount = account.LeaderBalanceAmount; //变更后余额
  879. LeaderAccountRecord leaderAccountRecord = db.LeaderAccountRecord.Add(new LeaderAccountRecord()
  880. {
  881. CreateDate = DateTime.Now,
  882. UpdateDate = DateTime.Now,
  883. UserId = UserId, //创客
  884. ChangeType = ChangeType, //变动类型
  885. ChangeAmount = Money * Count, //变更金额
  886. BeforeBalanceAmount = BeforeLeaderBalanceAmount, //变更前余额
  887. AfterBalanceAmount = AfterLeaderBalanceAmount, //变更后余额
  888. QueryCount = order.Id,
  889. }).Entity;
  890. db.SaveChanges();
  891. }
  892. public void DirectPrize(WebCMSEntities db, int OrderId, int UserId, int Count = 1)
  893. {
  894. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  895. if (account == null)
  896. {
  897. account = db.UserAccount.Add(new UserAccount()
  898. {
  899. Id = UserId,
  900. UserId = UserId,
  901. }).Entity;
  902. db.SaveChanges();
  903. }
  904. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  905. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  906. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  907. account.BalanceAmount += 100 * Count;
  908. account.TotalAmount += 100 * Count;
  909. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  910. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  911. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  912. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  913. {
  914. CreateDate = DateTime.Now,
  915. UpdateDate = DateTime.Now,
  916. UserId = UserId, //创客
  917. ChangeType = 112, //变动类型
  918. ChangeAmount = 100 * Count, //变更金额
  919. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  920. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  921. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  922. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  923. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  924. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  925. QueryCount = OrderId,
  926. }).Entity;
  927. db.SaveChanges();
  928. }
  929. // 推荐王逻辑(下单)
  930. public void RecommendMethod(int UserId, string TradeMonth)
  931. {
  932. string SendData = "{\"Kind\":\"1\",\"Data\":{\"UserId\":\"" + UserId + "\",\"TradeMonth\":\"" + TradeMonth + "\"}}";
  933. RedisDbconn.Instance.AddList("RecommendActStatQueue", SendData);
  934. }
  935. //操作运营中心额度
  936. public void OperateReserveBackFor(WebCMSEntities maindb, int UserId, decimal Money)
  937. {
  938. OpModels.WebCMSEntities db = new OpModels.WebCMSEntities();
  939. while(UserId > 0)
  940. {
  941. Users user = maindb.Users.FirstOrDefault(m => m.Id == UserId);
  942. if(user != null)
  943. {
  944. bool sys = db.SysAdmin.Any(m => m.UserId == UserId);
  945. if(sys)
  946. {
  947. OpModels.UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId) ?? new OpModels.UserAccount();
  948. if(account.TotalAmt <= 0)
  949. {
  950. return;
  951. }
  952. if(account.TotalAmt < Money)
  953. {
  954. Money = account.TotalAmt;
  955. }
  956. if(Money > 0)
  957. {
  958. OperateAmountChange(db, UserId, Money, 2, 1, "商城购机");
  959. OperateAmountChange(db, UserId, Money, 1, 2, "商城购机");
  960. }
  961. UserId = 0;
  962. }
  963. else
  964. {
  965. UserId = user.ParentUserId;
  966. }
  967. }
  968. else
  969. {
  970. UserId = 0;
  971. }
  972. }
  973. db.Dispose();
  974. }
  975. public void OperateReserveBack(int UserId, decimal Money)
  976. {
  977. OpModels.WebCMSEntities db = new OpModels.WebCMSEntities();
  978. OpModels.UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId) ?? new OpModels.UserAccount();
  979. if(account.TotalAmt <= 0)
  980. {
  981. return;
  982. }
  983. if(account.TotalAmt < Money)
  984. {
  985. Money = account.TotalAmt;
  986. }
  987. if(Money > 0)
  988. {
  989. OperateAmountChange(db, UserId, Money, 2, 1, "商城购机");
  990. OperateAmountChange(db, UserId, Money, 1, 2, "商城购机");
  991. }
  992. db.Dispose();
  993. }
  994. public void ActReserveBack(int UserId, decimal OpReserve1, decimal OpReserve2, decimal OpReserve3)
  995. {
  996. OpModels.WebCMSEntities db = new OpModels.WebCMSEntities();
  997. if(OpReserve1 > 0) OperateAmountChange(db, UserId, OpReserve1, 1, 1, "机具激活", true);
  998. if(OpReserve2 > 0) OperateAmountChange(db, UserId, OpReserve2, 1, 2, "机具激活", true);
  999. if(OpReserve3 > 0) OperateAmountChange(db, UserId, OpReserve3, 1, 3, "机具激活", true);
  1000. db.Dispose();
  1001. }
  1002. public void OperateAmountChange(OpModels.WebCMSEntities db, int UserId, decimal Money, int OperateType, int AmountType, string Remark = "", bool record = false)
  1003. {
  1004. OpModels.UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  1005. if (account == null)
  1006. {
  1007. account = db.UserAccount.Add(new OpModels.UserAccount()
  1008. {
  1009. Id = UserId,
  1010. UserId = UserId,
  1011. }).Entity;
  1012. db.SaveChanges();
  1013. }
  1014. decimal BeforeAmount = account.ValidAmount + account.TotalAmt + account.ValidForGetAmount; //变更前总金额
  1015. if(OperateType == 1)
  1016. {
  1017. if(AmountType == 1)
  1018. {
  1019. // BeforeAmount = account.TotalAmt;
  1020. account.TotalAmt += Money;
  1021. // AfterAmount = account.TotalAmt;
  1022. }
  1023. else if(AmountType == 2)
  1024. {
  1025. // BeforeAmount = account.ValidForGetAmount;
  1026. account.ValidForGetAmount += Money;
  1027. // AfterAmount = account.ValidForGetAmount;
  1028. }
  1029. else
  1030. {
  1031. // BeforeAmount = account.ValidAmount;
  1032. account.ValidAmount += Money;
  1033. // AfterAmount = account.ValidAmount;
  1034. }
  1035. }
  1036. else
  1037. {
  1038. if(AmountType == 1)
  1039. {
  1040. // BeforeAmount = account.TotalAmt;
  1041. account.TotalAmt -= Money;
  1042. // AfterAmount = account.TotalAmt;
  1043. }
  1044. else if(AmountType == 2)
  1045. {
  1046. // BeforeAmount = account.ValidForGetAmount;
  1047. account.ValidForGetAmount -= Money;
  1048. // AfterAmount = account.ValidForGetAmount;
  1049. }
  1050. else
  1051. {
  1052. // BeforeAmount = account.ValidAmount;
  1053. account.ValidAmount -= Money;
  1054. // AfterAmount = account.ValidAmount;
  1055. }
  1056. }
  1057. decimal AfterAmount = account.ValidAmount + account.TotalAmt + account.ValidForGetAmount; //变更后总金额
  1058. if(record)
  1059. {
  1060. OpModels.AmountRecord add = db.AmountRecord.Add(new OpModels.AmountRecord()
  1061. {
  1062. CreateDate = DateTime.Now,
  1063. UpdateDate = DateTime.Now,
  1064. OperateType = OperateType,
  1065. AfterAmount = AfterAmount,
  1066. BeforeAmount = BeforeAmount,
  1067. UseAmount = Money,
  1068. UserId = UserId,
  1069. SeoDescription = Remark,
  1070. Version = AmountType,
  1071. }).Entity;
  1072. }
  1073. db.SaveChanges();
  1074. }
  1075. #endregion
  1076. #region 盟主推荐奖励
  1077. public void LeaderRecommendPrize(WebCMSEntities db, Orders order, int UserId, decimal Money, int ChangeType)
  1078. {
  1079. int level = 0;
  1080. while(UserId > 0)
  1081. {
  1082. level += 1;
  1083. Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
  1084. if(user != null)
  1085. {
  1086. if((user.UserType == 1 || user.LeaderLevel > 0) && level > 1)
  1087. {
  1088. ChangeAccount(db, order, UserId, Money, ChangeType);
  1089. UserId = 0;
  1090. }
  1091. else
  1092. {
  1093. UserId = user.ParentUserId;
  1094. }
  1095. }
  1096. else
  1097. {
  1098. UserId = 0;
  1099. }
  1100. }
  1101. db.Dispose();
  1102. }
  1103. #endregion
  1104. #region 购买盟主预设职级
  1105. public void LeaderPreUserLevel(WebCMSEntities db, int UserId, int LeaderKind)
  1106. {
  1107. int Month = LeaderKind == 1 ? 6 : 12;
  1108. int Rank = LeaderKind == 1 ? 5 : 7;
  1109. UserRankWhite check = db.UserRankWhite.FirstOrDefault(m => m.Id == UserId);
  1110. if(check == null)
  1111. {
  1112. db.UserRankWhite.Add(new UserRankWhite()
  1113. {
  1114. CreateDate = DateTime.Now, //设置时间
  1115. UpdateDate = DateTime.Now.AddMonths(Month), //过期时间
  1116. Rank = Rank,
  1117. UserId = UserId, //用户
  1118. Id = UserId,
  1119. });
  1120. }
  1121. else
  1122. {
  1123. if(check.Rank < Rank)
  1124. {
  1125. check.Rank = Rank;
  1126. check.UpdateDate = DateTime.Now.AddMonths(Month);
  1127. }
  1128. else if(check.Rank == Rank && check.UpdateDate < DateTime.Now.AddMonths(Month))
  1129. {
  1130. check.UpdateDate = DateTime.Now.AddMonths(Month);
  1131. }
  1132. }
  1133. db.SaveChanges();
  1134. }
  1135. #endregion
  1136. #region 记录盟主
  1137. public void AddLeader(WebCMSEntities db, int UserId, int LeaderLevel)
  1138. {
  1139. int Month = LeaderLevel == 1 ? 6 : 12;
  1140. Leaders leader = db.Leaders.FirstOrDefault(m => m.Id == UserId);
  1141. if(leader == null)
  1142. {
  1143. db.Leaders.Add(new Leaders()
  1144. {
  1145. Id = UserId,
  1146. CreateDate = DateTime.Now, //设置时间
  1147. UserId = UserId, //用户
  1148. LeaderLevel = LeaderLevel,
  1149. LastBuyDate = DateTime.Now,
  1150. ExpiredDate = DateTime.Now.AddMonths(Month),
  1151. });
  1152. }
  1153. else
  1154. {
  1155. leader.LeaderLevel = LeaderLevel;
  1156. leader.LastBuyDate = DateTime.Now;
  1157. leader.ExpiredDate = DateTime.Now.AddMonths(Month);
  1158. }
  1159. db.SaveChanges();
  1160. SendLeaderMsg(db, UserId);
  1161. }
  1162. #endregion
  1163. #region 盟主续费通知
  1164. public void SendLeaderMsg(WebCMSEntities db, int UserId)
  1165. {
  1166. string msg = "";
  1167. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  1168. DateTime now = DateTime.Now;
  1169. Leaders leader = db.Leaders.FirstOrDefault(m => m.Id == UserId);
  1170. if(leader != null)
  1171. {
  1172. if(leader.ExpiredDate >= now)
  1173. {
  1174. msg = "尊敬的" + user.RealName + "盟主,恭喜您的盟主已成功开通";
  1175. }
  1176. else
  1177. {
  1178. msg = "尊敬的" + user.RealName + "盟主,恭喜您重新成为盟主";
  1179. }
  1180. }
  1181. else
  1182. {
  1183. msg = "尊敬的" + user.RealName + "盟主,恭喜您的盟主已成功开通";
  1184. }
  1185. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  1186. {
  1187. UserId = Convert.ToInt32(UserId), //创客
  1188. Title = "开通盟主提醒", //标题
  1189. Content = msg, //内容
  1190. Summary = msg,
  1191. CreateDate = DateTime.Now,
  1192. }));
  1193. }
  1194. #endregion
  1195. }
  1196. }