AlipayPayBack2Service.cs 61 KB

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