AlipayPayBack2Service.cs 64 KB

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