AlipayPayBack2Service.cs 64 KB

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