AlipayPayBack2Service.cs 62 KB

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