AlipayPayBack2Service.cs 64 KB

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