AlipayPayBack2Service.cs 62 KB

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