AlipayPayBack2Service.cs 64 KB

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