AlipayPayBack2Service.cs 64 KB

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