AlipayPayBack2Service.cs 64 KB

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