AlipayPayBack2Service.cs 60 KB

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