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