AlipayPayBack2Service.cs 64 KB

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