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