AlipayPayBack2Service.cs 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using System.Threading;
  6. using MySystem.PxcModels;
  7. using Library;
  8. using LitJson;
  9. namespace MySystem
  10. {
  11. public class AlipayPayBack2Service
  12. {
  13. public readonly static AlipayPayBack2Service Instance = new AlipayPayBack2Service();
  14. private AlipayPayBack2Service()
  15. { }
  16. public void Start()
  17. {
  18. Thread th = new Thread(dosomething);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. private void dosomething()
  23. {
  24. bool op = true;
  25. while (op)
  26. {
  27. string content = RedisDbconn.Instance.RPop<string>("PayCallBack2");
  28. if (!string.IsNullOrEmpty(content))
  29. {
  30. try
  31. {
  32. sloveAlipayCallBack(content);
  33. }
  34. catch(Exception ex)
  35. {
  36. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "支付宝支付回调异常");
  37. }
  38. }
  39. else
  40. {
  41. Thread.Sleep(2000);
  42. }
  43. }
  44. }
  45. public void sloveAlipayCallBack(string content)
  46. {
  47. JsonData jsonObj = JsonMapper.ToObject(content);
  48. string OrderNo = jsonObj["out_trade_no"].ToString();
  49. string TradeNo = jsonObj["transaction_id"].ToString();
  50. decimal TotalFee = decimal.Parse(function.CheckNum(jsonObj["total_fee"].ToString()));
  51. WebCMSEntities db = new WebCMSEntities();
  52. OrderForNo forNo = db.OrderForNo.FirstOrDefault(m => m.OrderNo == OrderNo);
  53. if (forNo != null)
  54. {
  55. string[] ids = forNo.OrderIds.Split(',');
  56. foreach (string idString in ids)
  57. {
  58. int OrderId = int.Parse(idString);
  59. DoOrderV2(db, OrderId);
  60. Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId && m.Status > 0);
  61. if(order != null)
  62. {
  63. Products product = db.Products.FirstOrDefault(m => m.Id == order.ProductId) ?? new Products();
  64. if(product.ProductKind == 2)
  65. {
  66. order.Status = 2;
  67. order.SendStatus = 1;
  68. order.SendDate = DateTime.Now;
  69. db.SaveChanges();
  70. }
  71. }
  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 || Kind == 3)
  158. {
  159. CouponCount = 3 * pro.ProductCount;
  160. }
  161. else if(Kind == 2)
  162. {
  163. CouponCount = 2 * pro.ProductCount;
  164. }
  165. if(pro.ProductId == 34) //助利宝商机券购买,50张起购
  166. {
  167. CouponCount = 50 * pro.ProductCount;
  168. }
  169. string Codes = "";
  170. var coupons = db.PosCoupons.Where(m => m.IsLock == 0 && m.IsUse == 0 && m.UserId == 0 && m.QueryCount == Kind).OrderBy(m => m.Id).Take(CouponCount).ToList();
  171. int RecordId = 0;
  172. if(coupons.Count > 0 && (pro.ProductId == 34 || pro.ProductId == -2))
  173. {
  174. var adds = db.HelpProfitExchange.Add(new HelpProfitExchange()
  175. {
  176. CreateDate = DateTime.Now, //创建时间
  177. UserId = order.UserId,
  178. ExchangeCount = coupons.Count,
  179. }).Entity;
  180. db.SaveChanges();
  181. RecordId = adds.Id;
  182. }
  183. foreach (var coupon in coupons) // TODO: 数量多的话,会慢
  184. {
  185. PosCoupons item = db.PosCoupons.FirstOrDefault(m => m.Id == coupon.Id);
  186. if (item != null)
  187. {
  188. item.CreateDate = DateTime.Now;
  189. item.UserId = order.UserId;
  190. item.UpdateDate = DateTime.Now.AddDays(180);
  191. if(pro.ProductId == 34 || pro.ProductId == -2)
  192. {
  193. item.HelpProfitFlag = 1;
  194. db.HelpProfitExchangeDetail.Add(new HelpProfitExchangeDetail()
  195. {
  196. CreateDate = DateTime.Now, //创建时间
  197. UserId = order.UserId,
  198. PosCouponId = item.Id,
  199. ExchangeCode = item.ExchangeCode,
  200. RecordId = RecordId,
  201. });
  202. }
  203. Codes += item.ExchangeCode + ",";
  204. couponIds.Add(coupon.Id);
  205. }
  206. }
  207. order.SnNos = Codes.TrimEnd(',');
  208. }
  209. db.SaveChanges();
  210. if (pro.ProductId == 27 || pro.ProductId == 28)
  211. {
  212. //充值盟主储备金
  213. decimal TotalPrice = 10000;
  214. if(pro.ProductId == 27)
  215. {
  216. TotalPrice = 40000;
  217. }
  218. OpReserve(db, order, order.UserId, TotalPrice, 1);
  219. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId) ?? new Users();
  220. if(pro.ProductId == 27)
  221. {
  222. OperateReserveBackFor(db, user.Id, 40000);
  223. //自留券数量清除
  224. CancelStayCoupon(db, order.UserId, 7);
  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. //自留券数量清除
  240. CancelStayCoupon(db, order.UserId, 5);
  241. // if (check) OperateReserveBackFor(db, user.Id, 10000);
  242. //预设小盟主职级
  243. LeaderPreUserLevel(db, order.UserId, 1);
  244. AddLeader(db, order.UserId, 1);
  245. decimal Prize = decimal.Parse(function.CheckNum(PublicFunction.GetPublicParam(db, "SmallLeaderPrize")));
  246. if(Prize > 0 && BeforeLeaderLevel < 1 && user.UserType == 0)
  247. {
  248. LeaderRecommendPrize(db, order, user.Id, Prize, 123);
  249. }
  250. }
  251. }
  252. // 购买盟主储蓄金
  253. if (pro.ProductId == 39 || pro.ProductId == 40)
  254. {
  255. order.Status = 2;
  256. //充值盟主储备金
  257. decimal TotalPrice = 10000;
  258. if(pro.ProductId == 39)
  259. {
  260. TotalPrice = 40000;
  261. }
  262. OpReserve(db, order, order.UserId, TotalPrice, 1);
  263. if(pro.ProductId == 39) //购买大盟主储蓄金,给上级运营中心返可提现金额
  264. {
  265. OperateReserveBackFor(db, order.UserId, 40000);
  266. AddLeader(db, order.UserId, 2);
  267. LeaderPreUserLevel(db, order.UserId, 2);
  268. }
  269. else if(pro.ProductId == 40) //购买小盟主储蓄金,给上级大盟主返储备金
  270. {
  271. LeaderBack(db, order);
  272. AddLeader(db, order.UserId, 1);
  273. LeaderPreUserLevel(db, order.UserId, 1);
  274. }
  275. }
  276. //推荐下单奖励
  277. List<int> LkbIds = new List<int>(); //来客吧组合包
  278. LkbIds.Add(77);
  279. LkbIds.Add(78);
  280. LkbIds.Add(79);
  281. if (pro.ProductId == 10 || pro.ProductId == 11 || LkbIds.Contains(pro.ProductId))
  282. {
  283. bool checkPrize = db.UserAccountRecord.Any(m => m.QueryCount == OrderId && m.ChangeType == 112);
  284. function.WriteLog("OrderId:" + OrderId, "推荐下单奖励监控日志");
  285. function.WriteLog("checkPrize:" + checkPrize, "推荐下单奖励监控日志");
  286. if (checkPrize)
  287. {
  288. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  289. if (user != null)
  290. {
  291. if(user.LeaderLevel > 0 || user.UserType == 1) //盟主或运营中心
  292. {
  293. //获得100元奖励
  294. OpAccount(db, order, order.UserId, 100, pro.ProductCount);
  295. }
  296. }
  297. }
  298. else
  299. {
  300. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  301. if (user != null)
  302. {
  303. bool directPrize = false; //直推奖标记
  304. bool bigLeaderPrize = false; //大盟主券标记
  305. bool operateFlag = false; //运营中心标记
  306. bool buyPrize = false; //返100购机奖励标记或返100购机奖励标记-运营中心
  307. int leaderFlag = 0; //返600备用金标记
  308. if(user.LeaderLevel > 0 || user.UserType == 1) //盟主或运营中心
  309. {
  310. if(user.LeaderLevel > 0)
  311. {
  312. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == order.UserId) ?? new UserAccount();
  313. if(acccount.LeaderReserve >= order.TotalPrice)
  314. {
  315. if(order.PayMode == 4)
  316. {
  317. //扣减备用金
  318. OpReserve(db, order, order.UserId, order.TotalPrice, 0, 0, "商城购机(储备金支付)");
  319. }
  320. }
  321. string parentString = user.ParentNav + "," + order.UserId + ",";
  322. string[] puids = parentString.Replace(",,", ",").Trim(',').Split(',');
  323. Array.Reverse(puids);
  324. bool getFlag = true;
  325. foreach(string puid in puids)
  326. {
  327. int puidNum = int.Parse(function.CheckInt(puid));
  328. Users puser = db.Users.FirstOrDefault(m => m.Id == puidNum) ?? new Users();
  329. if(puser.LeaderLevel > 0 && getFlag)
  330. {
  331. UserAccount pacccount = db.UserAccount.FirstOrDefault(m => m.Id == puidNum) ?? new UserAccount();
  332. if(pacccount.LeaderReserve >= 400 * pro.ProductCount)
  333. {
  334. //获得100元奖励
  335. OpAccount(db, order, puidNum, 100, pro.ProductCount);
  336. getFlag = false;
  337. break;
  338. }
  339. }
  340. }
  341. //小盟主在有效期内购券后预设职级,仍为K5,但增加一条预设为K1的预设记录
  342. if(user.LeaderLevel == 1)
  343. {
  344. DateTime now = DateTime.Now;
  345. bool checkLeader = db.Leaders.Any(m => m.Id == user.Id && m.ExpiredDate > now);
  346. if(checkLeader)
  347. {
  348. PreUserLevel(db, user.Id, 1, DateTime.Parse("2025-01-01 00:00:00"));
  349. }
  350. }
  351. }
  352. else if(user.UserType == 1)
  353. {
  354. //获得100元奖励
  355. OpAccount(db, order, order.UserId, 100, pro.ProductCount);
  356. }
  357. //推荐奖励
  358. DirectPrize(db, order.Id, order.UserId, pro.ProductCount);
  359. //推荐下单上级获得30天的机具循环天数(盟主自己得)
  360. 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();
  361. foreach (var subPos in posList)
  362. {
  363. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == subPos.Id);
  364. if (pos != null)
  365. {
  366. pos.RecycEndDate = pos.RecycEndDate.Value.AddDays(30);
  367. }
  368. }
  369. db.SaveChanges();
  370. if(order.PayMode == 4 && user.LeaderLevel > 0) //使用盟主储蓄金,并且是盟主
  371. {
  372. if(user.UserType == 1)
  373. {
  374. if(couponIds.Count > 0)
  375. {
  376. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == user.Id) ?? new UserAccount();
  377. foreach(int couponId in couponIds)
  378. {
  379. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  380. if(coupon != null)
  381. {
  382. if(acccount.LeaderReserve >= 400 * pro.ProductCount)
  383. {
  384. coupon.LeaderUserId = user.Id;
  385. }
  386. coupon.OpId = user.Id;
  387. }
  388. }
  389. }
  390. db.SaveChanges();
  391. }
  392. else
  393. {
  394. //寻找最近运营中心额度大于0的运营中心
  395. int PUserId = user.Id;
  396. bool OperateFlag = true;
  397. bool OncePrizeFlag1 = true;
  398. while(PUserId > 0)
  399. {
  400. Users puser = db.Users.FirstOrDefault(m => m.Id == PUserId);
  401. if(puser != null)
  402. {
  403. if(puser.UserType == 1 && OperateFlag == true) //运营中心
  404. {
  405. if(couponIds.Count > 0)
  406. {
  407. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == order.UserId) ?? new UserAccount();
  408. foreach(int couponId in couponIds)
  409. {
  410. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  411. if(coupon != null)
  412. {
  413. if(acccount.LeaderReserve >= 400 * pro.ProductCount)
  414. {
  415. coupon.LeaderUserId = user.Id;
  416. }
  417. coupon.OpId = puser.Id;
  418. }
  419. }
  420. db.SaveChanges();
  421. if(acccount.LeaderReserve >= 400 * pro.ProductCount && OncePrizeFlag1)
  422. {
  423. //扣减备用金
  424. OpReserve(db, order, order.UserId, 400 * pro.ProductCount, 2, 0, "商城购机");
  425. //返回到余额
  426. OpLeaderAccount(db, order, order.UserId, 400, pro.ProductCount);
  427. OncePrizeFlag1 = false;
  428. }
  429. OperateFlag = false;
  430. }
  431. else if(LkbIds.Contains(pro.ProductId)) //来客吧组合包
  432. {
  433. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == order.UserId) ?? new UserAccount();
  434. if(acccount.LeaderReserve >= 400 * pro.ProductCount && OncePrizeFlag1)
  435. {
  436. //扣减备用金
  437. OpReserve(db, order, order.UserId, 400 * pro.ProductCount, 2, 0, "商城购机");
  438. //返回到余额
  439. OpLeaderAccount(db, order, order.UserId, 400, pro.ProductCount);
  440. OncePrizeFlag1 = false;
  441. }
  442. OperateFlag = false;
  443. }
  444. }
  445. PUserId = puser.ParentUserId;
  446. }
  447. else
  448. {
  449. PUserId = 0;
  450. }
  451. }
  452. }
  453. }
  454. else if(order.PayMode == 4 && user.UserType == 1) //使用盟主储蓄金,并且是运营中心
  455. {
  456. if(couponIds.Count > 0)
  457. {
  458. foreach(int couponId in couponIds)
  459. {
  460. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  461. if(coupon != null)
  462. {
  463. coupon.OpId = user.Id;
  464. }
  465. }
  466. db.SaveChanges();
  467. //寻找最近储蓄金充足的大盟主
  468. int PUserId = user.Id;
  469. bool LeaderFlag = true;
  470. bool OncePrizeFlag2 = true;
  471. while(PUserId > 0)
  472. {
  473. Users puser = db.Users.FirstOrDefault(m => m.Id == PUserId);
  474. if(puser != null)
  475. {
  476. if(puser.LeaderLevel == 2 && LeaderFlag == true) //大盟主
  477. {
  478. if(couponIds.Count > 0)
  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. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == order.UserId) ?? new UserAccount();
  491. if(acccount.LeaderReserve >= 400 * pro.ProductCount && OncePrizeFlag2)
  492. {
  493. //扣减备用金
  494. OpReserve(db, order, order.UserId, 400 * pro.ProductCount, 2, 0, "商城购机");
  495. //返回到余额
  496. OpLeaderAccount(db, order, order.UserId, 400, pro.ProductCount);
  497. OncePrizeFlag2 = false;
  498. }
  499. LeaderFlag = false;
  500. }
  501. else if(LkbIds.Contains(pro.ProductId))
  502. {
  503. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == order.UserId) ?? new UserAccount();
  504. if(acccount.LeaderReserve >= 400 * pro.ProductCount && OncePrizeFlag2)
  505. {
  506. //扣减备用金
  507. OpReserve(db, order, order.UserId, 400 * pro.ProductCount, 2, 0, "商城购机");
  508. //返回到余额
  509. OpLeaderAccount(db, order, order.UserId, 400, pro.ProductCount);
  510. OncePrizeFlag2 = false;
  511. }
  512. LeaderFlag = false;
  513. }
  514. }
  515. PUserId = puser.ParentUserId;
  516. }
  517. else
  518. {
  519. PUserId = 0;
  520. }
  521. }
  522. }
  523. else
  524. {
  525. //寻找最近储蓄金充足的大盟主及最近运营中心额度大于0的运营中心(含自身)
  526. int PUserId = user.Id;
  527. int LeaderFlag = 0;
  528. bool OperateFlag = true;
  529. bool OncePrizeFlag3 = true;
  530. while(PUserId > 0)
  531. {
  532. Users puser = db.Users.FirstOrDefault(m => m.Id == PUserId);
  533. if(puser != null)
  534. {
  535. if(puser.LeaderLevel > 0 && puser.LeaderLevel > LeaderFlag && LeaderFlag < 2) //大盟主
  536. {
  537. if(couponIds.Count > 0)
  538. {
  539. if(puser.LeaderLevel == 2)
  540. {
  541. UserAccount pacccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
  542. foreach(int couponId in couponIds)
  543. {
  544. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  545. if(coupon != null && pacccount.LeaderReserve >= 400 * pro.ProductCount)
  546. {
  547. coupon.LeaderUserId = puser.Id;
  548. }
  549. }
  550. db.SaveChanges();
  551. }
  552. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
  553. if(acccount.LeaderReserve >= 400 * pro.ProductCount && OncePrizeFlag3)
  554. {
  555. //扣减备用金
  556. OpReserve(db, order, puser.Id, 400 * pro.ProductCount, 2, 0, "商城购机");
  557. //返回到余额
  558. OpLeaderAccount(db, order, puser.Id, 400, pro.ProductCount);
  559. LeaderFlag = puser.LeaderLevel;
  560. OncePrizeFlag3 = false;
  561. }
  562. }
  563. else if(LkbIds.Contains(pro.ProductId))
  564. {
  565. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
  566. if(acccount.LeaderReserve >= 400 * pro.ProductCount && OncePrizeFlag3)
  567. {
  568. //扣减备用金
  569. OpReserve(db, order, puser.Id, 400 * pro.ProductCount, 2, 0, "商城购机");
  570. //返回到余额
  571. OpLeaderAccount(db, order, puser.Id, 400, pro.ProductCount);
  572. LeaderFlag = puser.LeaderLevel;
  573. OncePrizeFlag3 = false;
  574. }
  575. }
  576. }
  577. if(puser.UserType == 1 && OperateFlag == true) //运营中心
  578. {
  579. if(couponIds.Count > 0)
  580. {
  581. foreach(int couponId in couponIds)
  582. {
  583. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  584. if(coupon != null)
  585. {
  586. coupon.OpId = puser.Id;
  587. }
  588. }
  589. db.SaveChanges();
  590. OperateFlag = false;
  591. }
  592. }
  593. PUserId = puser.ParentUserId;
  594. }
  595. else
  596. {
  597. PUserId = 0;
  598. }
  599. }
  600. //运营中心返额度(下单人自己是运营中心)
  601. // OperateReserveBackFor(db, order.UserId, order.TotalPrice);
  602. }
  603. return;
  604. }
  605. int ParentUserId = user.ParentUserId;
  606. // List<int> proids = new List<int>();
  607. // proids.Add(10);
  608. // proids.Add(11);
  609. // 普通创客购买600一组的机具券
  610. bool OncePrizeFlag4 = true;
  611. while(ParentUserId > 0)
  612. {
  613. Users puser = db.Users.FirstOrDefault(m => m.Id == ParentUserId) ?? new Users();
  614. bool valid = Utils.Instance.IsStandardUser(ParentUserId);
  615. function.WriteLog("是否有效创客:" + valid, "推荐下单奖励监控日志");
  616. if (valid)
  617. {
  618. // 盟主直推奖励,可以每次下单获得
  619. if(!directPrize)
  620. {
  621. DirectPrize(db, order.Id, ParentUserId, pro.ProductCount);
  622. directPrize = true;
  623. }
  624. //推荐下单上级获得30天的机具循环天数
  625. 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();
  626. foreach (var subPos in posList)
  627. {
  628. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == subPos.Id);
  629. if (pos != null)
  630. {
  631. pos.RecycEndDate = pos.RecycEndDate.Value.AddDays(30);
  632. }
  633. }
  634. db.SaveChanges();
  635. }
  636. if(puser.LeaderLevel > 0)
  637. {
  638. function.WriteLog("OrderId:" + order.Id, "监控大盟主购机奖励");
  639. function.WriteLog("UserId:" + puser.Id, "监控大盟主购机奖励");
  640. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
  641. function.WriteLog("LeaderReserve:" + acccount.LeaderReserve, "监控大盟主购机奖励");
  642. function.WriteLog("buyPrize:" + buyPrize, "监控大盟主购机奖励");
  643. if(acccount.LeaderReserve >= 400 * pro.ProductCount && !buyPrize)
  644. {
  645. //购机奖励
  646. OpAccount(db, order, puser.Id, 100, pro.ProductCount);
  647. buyPrize = true;
  648. }
  649. function.WriteLog("LeaderReserve:" + acccount.LeaderReserve, "监控大盟主购机奖励");
  650. function.WriteLog("leaderFlag:" + leaderFlag, "监控大盟主购机奖励");
  651. function.WriteLog("OncePrizeFlag4:" + OncePrizeFlag4, "监控大盟主购机奖励");
  652. function.WriteLog("-----------------------------", "监控大盟主购机奖励");
  653. if(acccount.LeaderReserve >= 400 * pro.ProductCount && puser.LeaderLevel > leaderFlag && leaderFlag < 2 && OncePrizeFlag4)
  654. {
  655. //扣减备用金
  656. OpReserve(db, order, puser.Id, 400 * pro.ProductCount, 2, order.UserId, "购机奖励");
  657. //返回到余额
  658. OpLeaderAccount(db, order, puser.Id, 400, pro.ProductCount);
  659. leaderFlag = puser.LeaderLevel;
  660. OncePrizeFlag4 = false;
  661. }
  662. //如果是小盟主,则查找该小盟主关联的大盟主储蓄金是否充足,是则给券打上关联大盟主标记
  663. if(couponIds.Count > 0 && puser.LeaderLevel == 1 && !bigLeaderPrize)
  664. {
  665. LeaderAccountRecord leaderItem = db.LeaderAccountRecord.FirstOrDefault(m => m.Sort == puser.Id && m.LeaderId > 0 && m.LeaderReserve >= 400 * pro.ProductCount);
  666. if(leaderItem != null)
  667. {
  668. leaderItem.LeaderReserve -= 400 * pro.ProductCount;
  669. foreach(int couponId in couponIds)
  670. {
  671. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  672. if(coupon != null)
  673. {
  674. coupon.LeaderUserId = leaderItem.LeaderId;
  675. }
  676. }
  677. db.SaveChanges();
  678. bigLeaderPrize = true;
  679. }
  680. }
  681. //如果是大盟主,则标记大盟主标签
  682. if(couponIds.Count > 0 && puser.LeaderLevel == 2 && acccount.LeaderReserve >= 400 * pro.ProductCount && !bigLeaderPrize)
  683. {
  684. foreach(int couponId in couponIds)
  685. {
  686. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  687. if(coupon != null)
  688. {
  689. coupon.LeaderUserId = puser.Id;
  690. }
  691. }
  692. db.SaveChanges();
  693. bigLeaderPrize = true;
  694. }
  695. }
  696. if(puser.UserType == 1)
  697. {
  698. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
  699. if(!buyPrize)
  700. {
  701. //购机奖励
  702. ChangeAccount(db, order, puser.Id, 100 * pro.ProductCount, 120);
  703. buyPrize = true;
  704. }
  705. //标记标签
  706. if(couponIds.Count > 0 && !operateFlag)
  707. {
  708. foreach(int couponId in couponIds)
  709. {
  710. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  711. if(coupon != null)
  712. {
  713. coupon.OpId= puser.Id;
  714. }
  715. }
  716. db.SaveChanges();
  717. operateFlag = true;
  718. }
  719. }
  720. ParentUserId = puser.ParentUserId;
  721. }
  722. //运营中心返额度
  723. // OperateReserveBackFor(db, order.UserId, order.TotalPrice);
  724. }
  725. }
  726. function.WriteLog("\r\n\r\n", "推荐下单奖励监控日志");
  727. }
  728. //推荐王
  729. if(pro.ProductId == 29)
  730. {
  731. RecommendMethod(order.UserId, order.CreateDate.Value.ToString("yyyyMM"));
  732. }
  733. //购买分仓临时额度
  734. if(pro.ProductId == 30 || pro.ProductId == 31 || pro.ProductId == 32)
  735. {
  736. string SendData = "{\"Kind\":\"1\",\"Data\":{\"OrderId\":\"" + order.Id + "\"}}";
  737. RedisDbconn.Instance.AddList("StoreApplyQueue", SendData);
  738. }
  739. //购买小分仓临时额度
  740. if(pro.ProductId == 56 || pro.ProductId == 57 || pro.ProductId == 58)
  741. {
  742. string SendData = "{\"Kind\":\"1\",\"Data\":{\"OrderId\":\"" + order.Id + "\"}}";
  743. RedisDbconn.Instance.AddList("PreStoreApplyQueue", SendData);
  744. }
  745. }
  746. else
  747. {
  748. if(order.ProductId > 0 && order.ErpMode > 0)
  749. {
  750. //盟主储备金-升级/购买 ErpMode:1-升级,2-购买
  751. if(order.ErpMode > 0)
  752. {
  753. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  754. if(user != null)
  755. {
  756. if(order.ErpMode == 1)
  757. {
  758. user.LeaderLevel = 2;
  759. db.SaveChanges();
  760. }
  761. OpReserve(db, order, order.UserId, order.TotalPrice, 1);
  762. }
  763. }
  764. }
  765. }
  766. }
  767. }
  768. //小盟主购买逻辑
  769. public bool LeaderBack(WebCMSEntities db, Orders order)
  770. {
  771. int LeaderUserId = order.UserId;
  772. int level = 0;
  773. bool result = true;
  774. while(LeaderUserId > 0)
  775. {
  776. level += 1;
  777. Users user = db.Users.FirstOrDefault(m => m.Id == LeaderUserId);
  778. if(user != null)
  779. {
  780. decimal TotalPrice = 10000;
  781. if((user.LeaderLevel == 2 || user.UserType == 1) && level > 1)
  782. {
  783. if(user.LeaderLevel == 2 && result)
  784. {
  785. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == LeaderUserId);
  786. if(account != null)
  787. {
  788. if(account.LeaderReserve >= TotalPrice)
  789. {
  790. OpReserve(db, order, LeaderUserId, TotalPrice, 2, 0, "推荐小盟主");
  791. OpLeaderAccount(db, order, LeaderUserId, TotalPrice, 1, 1);
  792. LeaderUserId = 0;
  793. result = false;
  794. }
  795. else
  796. {
  797. LeaderUserId = user.ParentUserId;
  798. // decimal LeaderReserve = account.LeaderReserve;
  799. // OpReserve(db, order, LeaderUserId, LeaderReserve, 2, 0, "推荐小盟主");
  800. // OpLeaderAccount(db, order, LeaderUserId, LeaderReserve);
  801. }
  802. }
  803. }
  804. if(user.UserType == 1 && result)
  805. {
  806. OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
  807. OpModels.UserAccount account = opdb.UserAccount.FirstOrDefault(m => m.Id == LeaderUserId) ?? new OpModels.UserAccount();
  808. if(account.TotalAmt > 0)
  809. {
  810. if(account.TotalAmt < TotalPrice)
  811. {
  812. TotalPrice = account.TotalAmt;
  813. }
  814. if(TotalPrice > 0)
  815. {
  816. OperateAmountChange(opdb, LeaderUserId, TotalPrice, 2, 1, "商城购机");
  817. OperateAmountChange(opdb, LeaderUserId, TotalPrice, 1, 2, "商城购机");
  818. LeaderUserId = 0;
  819. result = false;
  820. }
  821. }
  822. }
  823. }
  824. else
  825. {
  826. LeaderUserId = user.ParentUserId;
  827. }
  828. }
  829. else
  830. {
  831. LeaderUserId = 0;
  832. }
  833. }
  834. return result;
  835. }
  836. //操作储备金
  837. private int GetLeaderReserveRecordType(string Remark)
  838. {
  839. Dictionary<int, string> data = new Dictionary<int, string>();
  840. data.Add(11, "兑换机具券,机具券兑换,系统增加(盟主储蓄金),系统扣减(盟主储蓄金),储备金购买");
  841. data.Add(12, "系统增加(可提现余额),系统扣减(可提现余额)");
  842. data.Add(13, "推荐大盟主,推荐小盟主,商城购机,购机奖励");
  843. var item = data.FirstOrDefault(m => m.Value.Contains(Remark));
  844. if(item.Key > 0)
  845. {
  846. return item.Key;
  847. }
  848. return 0;
  849. }
  850. public void OpReserve(WebCMSEntities db, Orders order, int UserId, decimal Money, int ChangeType, int SourceUserId = 0, string Remark = "储备金购买")
  851. {
  852. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  853. if (account == null)
  854. {
  855. account = db.UserAccount.Add(new UserAccount()
  856. {
  857. Id = UserId,
  858. UserId = UserId,
  859. }).Entity;
  860. db.SaveChanges();
  861. }
  862. decimal BeforeAmount = account.LeaderReserve; //变更前总金额
  863. if(ChangeType == 1)
  864. {
  865. account.LeaderReserve += Money;
  866. }
  867. else
  868. {
  869. account.LeaderReserve -= Money;
  870. }
  871. decimal AfterAmount = account.LeaderReserve; //变更后总金额
  872. LeaderReserveRecord add = db.LeaderReserveRecord.Add(new LeaderReserveRecord()
  873. {
  874. CreateDate = DateTime.Now,
  875. ChangeType = ChangeType,
  876. OrderId = order.Id,
  877. Remark = Remark,
  878. AfterAmt = AfterAmount,
  879. BeforeAmt = BeforeAmount,
  880. ChangeAmt = Money,
  881. TradeDate = DateTime.Now.ToString("yyyyMMdd"),
  882. TradeMonth = DateTime.Now.ToString("yyyyMM"),
  883. UserId = UserId,
  884. SourceUserId = SourceUserId,
  885. Sort = GetLeaderReserveRecordType(Remark),
  886. }).Entity;
  887. db.SaveChanges();
  888. }
  889. //操作余额
  890. public void OpAccount(WebCMSEntities db, Orders order, int UserId, decimal Money, int Count = 1)
  891. {
  892. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  893. if (account == null)
  894. {
  895. account = db.UserAccount.Add(new UserAccount()
  896. {
  897. Id = UserId,
  898. UserId = UserId,
  899. }).Entity;
  900. db.SaveChanges();
  901. }
  902. int ChangeType = 0;
  903. if(Money == 600)
  904. {
  905. ChangeType = 117;
  906. }
  907. else if(Money == 100)
  908. {
  909. ChangeType = 118;
  910. }
  911. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  912. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  913. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  914. account.BalanceAmount += Money * Count;
  915. account.TotalAmount += Money * Count;
  916. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  917. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  918. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  919. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  920. {
  921. CreateDate = DateTime.Now,
  922. UpdateDate = DateTime.Now,
  923. UserId = UserId, //创客
  924. ChangeType = ChangeType, //变动类型
  925. ChangeAmount = Money * Count, //变更金额
  926. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  927. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  928. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  929. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  930. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  931. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  932. QueryCount = order.Id,
  933. }).Entity;
  934. db.SaveChanges();
  935. }
  936. public void ChangeAccount(WebCMSEntities db, Orders order, int UserId, decimal Money, int ChangeType = 0, string Remark = "")
  937. {
  938. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  939. if (account == null)
  940. {
  941. account = db.UserAccount.Add(new UserAccount()
  942. {
  943. Id = UserId,
  944. UserId = UserId,
  945. }).Entity;
  946. db.SaveChanges();
  947. }
  948. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  949. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  950. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  951. account.BalanceAmount += Money;
  952. if(Money > 0)
  953. {
  954. account.TotalAmount += Money;
  955. }
  956. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  957. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  958. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  959. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  960. {
  961. CreateDate = DateTime.Now,
  962. UpdateDate = DateTime.Now,
  963. UserId = UserId, //创客
  964. ChangeType = ChangeType, //变动类型
  965. ChangeAmount = Math.Abs(Money), //变更金额
  966. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  967. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  968. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  969. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  970. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  971. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  972. QueryCount = order.Id,
  973. Remark = Remark,
  974. }).Entity;
  975. db.SaveChanges();
  976. }
  977. //操作盟主可提现余额
  978. public void OpLeaderAccount(WebCMSEntities db, Orders order, int UserId, decimal Money, int Count = 1, int Kind = 0)
  979. {
  980. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  981. if (account == null)
  982. {
  983. account = db.UserAccount.Add(new UserAccount()
  984. {
  985. Id = UserId,
  986. UserId = UserId,
  987. }).Entity;
  988. db.SaveChanges();
  989. }
  990. int ChangeType = 0;
  991. if(Money == 400)
  992. {
  993. ChangeType = 117;
  994. }
  995. int LeaderId = 0;
  996. decimal LeaderReserve = 0;
  997. if(Kind == 1)
  998. {
  999. LeaderId = UserId;
  1000. LeaderReserve = Money * Count;
  1001. }
  1002. decimal BeforeLeaderBalanceAmount = account.LeaderBalanceAmount; //变更前余额
  1003. account.LeaderBalanceAmount += Money * Count;
  1004. decimal AfterLeaderBalanceAmount = account.LeaderBalanceAmount; //变更后余额
  1005. LeaderAccountRecord leaderAccountRecord = db.LeaderAccountRecord.Add(new LeaderAccountRecord()
  1006. {
  1007. CreateDate = DateTime.Now,
  1008. UpdateDate = DateTime.Now,
  1009. UserId = UserId, //创客
  1010. ChangeType = ChangeType, //变动类型
  1011. ChangeAmount = Money * Count, //变更金额
  1012. BeforeBalanceAmount = BeforeLeaderBalanceAmount, //变更前余额
  1013. AfterBalanceAmount = AfterLeaderBalanceAmount, //变更后余额
  1014. QueryCount = order.Id,
  1015. Sort = order.UserId,
  1016. LeaderId = LeaderId,
  1017. LeaderReserve = LeaderReserve,
  1018. }).Entity;
  1019. db.SaveChanges();
  1020. }
  1021. public void DirectPrize(WebCMSEntities db, int OrderId, int UserId, int Count = 1)
  1022. {
  1023. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  1024. if (account == null)
  1025. {
  1026. account = db.UserAccount.Add(new UserAccount()
  1027. {
  1028. Id = UserId,
  1029. UserId = UserId,
  1030. }).Entity;
  1031. db.SaveChanges();
  1032. }
  1033. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  1034. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  1035. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  1036. account.BalanceAmount += 100 * Count;
  1037. account.TotalAmount += 100 * Count;
  1038. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  1039. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  1040. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  1041. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  1042. {
  1043. CreateDate = DateTime.Now,
  1044. UpdateDate = DateTime.Now,
  1045. UserId = UserId, //创客
  1046. ChangeType = 112, //变动类型
  1047. ChangeAmount = 100 * Count, //变更金额
  1048. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  1049. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  1050. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  1051. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  1052. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  1053. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  1054. QueryCount = OrderId,
  1055. }).Entity;
  1056. db.SaveChanges();
  1057. }
  1058. // 推荐王逻辑(下单)
  1059. public void RecommendMethod(int UserId, string TradeMonth)
  1060. {
  1061. string SendData = "{\"Kind\":\"1\",\"Data\":{\"UserId\":\"" + UserId + "\",\"TradeMonth\":\"" + TradeMonth + "\"}}";
  1062. RedisDbconn.Instance.AddList("RecommendActStatQueue", SendData);
  1063. }
  1064. //操作运营中心额度
  1065. public void OperateReserveBackFor(WebCMSEntities maindb, int UserId, decimal Money)
  1066. {
  1067. OpModels.WebCMSEntities db = new OpModels.WebCMSEntities();
  1068. while(UserId > 0)
  1069. {
  1070. Users user = maindb.Users.FirstOrDefault(m => m.Id == UserId);
  1071. if(user != null)
  1072. {
  1073. bool sys = db.SysAdmin.Any(m => m.UserId == UserId);
  1074. if(sys)
  1075. {
  1076. OpModels.UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId) ?? new OpModels.UserAccount();
  1077. if(account.TotalAmt <= 0)
  1078. {
  1079. return;
  1080. }
  1081. if(account.TotalAmt < Money)
  1082. {
  1083. Money = account.TotalAmt;
  1084. }
  1085. if(Money > 0)
  1086. {
  1087. OperateAmountChange(db, UserId, Money, 2, 1, "商城购机");
  1088. OperateAmountChange(db, UserId, Money, 1, 2, "商城购机");
  1089. }
  1090. UserId = 0;
  1091. }
  1092. else
  1093. {
  1094. UserId = user.ParentUserId;
  1095. }
  1096. }
  1097. else
  1098. {
  1099. UserId = 0;
  1100. }
  1101. }
  1102. db.Dispose();
  1103. }
  1104. public void OperateReserveBack(int UserId, decimal Money)
  1105. {
  1106. OpModels.WebCMSEntities db = new OpModels.WebCMSEntities();
  1107. OpModels.UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId) ?? new OpModels.UserAccount();
  1108. if(account.TotalAmt <= 0)
  1109. {
  1110. return;
  1111. }
  1112. if(account.TotalAmt < Money)
  1113. {
  1114. Money = account.TotalAmt;
  1115. }
  1116. if(Money > 0)
  1117. {
  1118. OperateAmountChange(db, UserId, Money, 2, 1, "商城购机");
  1119. OperateAmountChange(db, UserId, Money, 1, 2, "商城购机");
  1120. }
  1121. db.Dispose();
  1122. }
  1123. public void ActReserveBack(int UserId, decimal OpReserve1, decimal OpReserve2, decimal OpReserve3)
  1124. {
  1125. OpModels.WebCMSEntities db = new OpModels.WebCMSEntities();
  1126. if(OpReserve1 > 0) OperateAmountChange(db, UserId, OpReserve1, 1, 1, "机具激活", true);
  1127. if(OpReserve2 > 0) OperateAmountChange(db, UserId, OpReserve2, 1, 2, "机具激活", true);
  1128. if(OpReserve3 > 0) OperateAmountChange(db, UserId, OpReserve3, 1, 3, "机具激活", true);
  1129. db.Dispose();
  1130. }
  1131. public void OperateAmountChange(OpModels.WebCMSEntities db, int UserId, decimal Money, int OperateType, int AmountType, string Remark = "", bool record = false)
  1132. {
  1133. OpModels.UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  1134. if (account == null)
  1135. {
  1136. account = db.UserAccount.Add(new OpModels.UserAccount()
  1137. {
  1138. Id = UserId,
  1139. UserId = UserId,
  1140. }).Entity;
  1141. db.SaveChanges();
  1142. }
  1143. decimal BeforeAmount = account.ValidAmount + account.TotalAmt + account.ValidForGetAmount; //变更前总金额
  1144. if(OperateType == 1)
  1145. {
  1146. if(AmountType == 1)
  1147. {
  1148. // BeforeAmount = account.TotalAmt;
  1149. account.TotalAmt += Money;
  1150. // AfterAmount = account.TotalAmt;
  1151. }
  1152. else if(AmountType == 2)
  1153. {
  1154. // BeforeAmount = account.ValidForGetAmount;
  1155. account.ValidForGetAmount += Money;
  1156. // AfterAmount = account.ValidForGetAmount;
  1157. }
  1158. else
  1159. {
  1160. // BeforeAmount = account.ValidAmount;
  1161. account.ValidAmount += Money;
  1162. // AfterAmount = account.ValidAmount;
  1163. }
  1164. }
  1165. else
  1166. {
  1167. if(AmountType == 1)
  1168. {
  1169. // BeforeAmount = account.TotalAmt;
  1170. account.TotalAmt -= Money;
  1171. // AfterAmount = account.TotalAmt;
  1172. }
  1173. else if(AmountType == 2)
  1174. {
  1175. // BeforeAmount = account.ValidForGetAmount;
  1176. account.ValidForGetAmount -= Money;
  1177. // AfterAmount = account.ValidForGetAmount;
  1178. }
  1179. else
  1180. {
  1181. // BeforeAmount = account.ValidAmount;
  1182. account.ValidAmount -= Money;
  1183. // AfterAmount = account.ValidAmount;
  1184. }
  1185. }
  1186. decimal AfterAmount = account.ValidAmount + account.TotalAmt + account.ValidForGetAmount; //变更后总金额
  1187. if(record)
  1188. {
  1189. OpModels.AmountRecord add = db.AmountRecord.Add(new OpModels.AmountRecord()
  1190. {
  1191. CreateDate = DateTime.Now,
  1192. UpdateDate = DateTime.Now,
  1193. OperateType = OperateType,
  1194. AfterAmount = AfterAmount,
  1195. BeforeAmount = BeforeAmount,
  1196. UseAmount = Money,
  1197. UserId = UserId,
  1198. SeoDescription = Remark,
  1199. Version = AmountType,
  1200. }).Entity;
  1201. }
  1202. db.SaveChanges();
  1203. }
  1204. #endregion
  1205. #region 盟主推荐奖励
  1206. public void LeaderRecommendPrize(WebCMSEntities db, Orders order, int UserId, decimal Money, int ChangeType)
  1207. {
  1208. int level = 0;
  1209. while(UserId > 0)
  1210. {
  1211. level += 1;
  1212. Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
  1213. if(user != null)
  1214. {
  1215. if((user.UserType == 1 || user.LeaderLevel > 0) && level > 1)
  1216. {
  1217. ChangeAccount(db, order, UserId, Money, ChangeType);
  1218. UserId = 0;
  1219. }
  1220. else
  1221. {
  1222. UserId = user.ParentUserId;
  1223. }
  1224. }
  1225. else
  1226. {
  1227. UserId = 0;
  1228. }
  1229. }
  1230. db.Dispose();
  1231. }
  1232. #endregion
  1233. #region 购买盟主预设职级
  1234. public void LeaderPreUserLevel(WebCMSEntities db, int UserId, int LeaderKind)
  1235. {
  1236. int Month = LeaderKind == 1 ? 6 : 9;
  1237. int Rank = LeaderKind == 1 ? 5 : 7;
  1238. LeaderRankWhite check = db.LeaderRankWhite.FirstOrDefault(m => m.Id == UserId);
  1239. if(check == null)
  1240. {
  1241. db.LeaderRankWhite.Add(new LeaderRankWhite()
  1242. {
  1243. CreateDate = DateTime.Now, //设置时间
  1244. UpdateDate = DateTime.Now.AddMonths(Month), //过期时间
  1245. Rank = Rank,
  1246. UserId = UserId, //用户
  1247. Id = UserId,
  1248. });
  1249. }
  1250. else
  1251. {
  1252. if(check.Rank < Rank)
  1253. {
  1254. check.Rank = Rank;
  1255. check.UpdateDate = DateTime.Now.AddMonths(Month);
  1256. }
  1257. else if(check.Rank == Rank && check.UpdateDate < DateTime.Now.AddMonths(Month))
  1258. {
  1259. check.UpdateDate = DateTime.Now.AddMonths(Month);
  1260. }
  1261. }
  1262. db.SaveChanges();
  1263. }
  1264. public void PreUserLevel(WebCMSEntities db, int UserId, int Rank, DateTime ExpiredDate)
  1265. {
  1266. LeaderRankWhite rank = db.LeaderRankWhite.FirstOrDefault(m => m.Id == UserId);
  1267. if(rank == null)
  1268. {
  1269. db.LeaderRankWhite.Add(new LeaderRankWhite()
  1270. {
  1271. CreateDate = DateTime.Now, //设置时间
  1272. UserId = UserId, //用户
  1273. Id = UserId,
  1274. });
  1275. db.SaveChanges();
  1276. }
  1277. rank.Rank = Rank;
  1278. rank.UpdateDate = ExpiredDate;
  1279. db.SaveChanges();
  1280. }
  1281. //取消自留券数量
  1282. public void CancelStayCoupon(WebCMSEntities db, int UserId, int CheckLevel = 5)
  1283. {
  1284. int myLevel = Utils.Instance.GetUserLevel(UserId);
  1285. if(myLevel <= CheckLevel)
  1286. {
  1287. Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
  1288. if(user != null)
  1289. {
  1290. user.Version = 0;
  1291. db.SaveChanges();
  1292. }
  1293. }
  1294. }
  1295. #endregion
  1296. #region 记录盟主
  1297. public void AddLeader(WebCMSEntities db, int UserId, int LeaderLevel)
  1298. {
  1299. int Month = LeaderLevel == 1 ? 6 : 9;
  1300. Leaders leader = db.Leaders.FirstOrDefault(m => m.Id == UserId);
  1301. if(leader == null)
  1302. {
  1303. db.Leaders.Add(new Leaders()
  1304. {
  1305. Id = UserId,
  1306. CreateDate = DateTime.Now, //设置时间
  1307. UserId = UserId, //用户
  1308. LeaderLevel = LeaderLevel,
  1309. LastBuyDate = DateTime.Now,
  1310. ExpiredDate = DateTime.Now.AddMonths(Month),
  1311. });
  1312. }
  1313. else
  1314. {
  1315. leader.LeaderLevel = LeaderLevel;
  1316. leader.LastBuyDate = DateTime.Now;
  1317. leader.ExpiredDate = DateTime.Now.AddMonths(Month);
  1318. }
  1319. db.SaveChanges();
  1320. SendLeaderMsg(db, UserId);
  1321. }
  1322. #endregion
  1323. #region 盟主续费通知
  1324. public void SendLeaderMsg(WebCMSEntities db, int UserId)
  1325. {
  1326. string msg = "";
  1327. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  1328. DateTime now = DateTime.Now;
  1329. Leaders leader = db.Leaders.FirstOrDefault(m => m.Id == UserId);
  1330. if(leader != null)
  1331. {
  1332. if(leader.ExpiredDate >= now)
  1333. {
  1334. msg = "尊敬的" + user.RealName + "盟主,恭喜您的盟主已成功开通";
  1335. }
  1336. else
  1337. {
  1338. msg = "尊敬的" + user.RealName + "盟主,恭喜您重新成为盟主";
  1339. }
  1340. }
  1341. else
  1342. {
  1343. msg = "尊敬的" + user.RealName + "盟主,恭喜您的盟主已成功开通";
  1344. }
  1345. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  1346. {
  1347. UserId = Convert.ToInt32(UserId), //创客
  1348. Title = "开通盟主提醒", //标题
  1349. Content = msg, //内容
  1350. Summary = msg,
  1351. CreateDate = DateTime.Now,
  1352. }));
  1353. }
  1354. #endregion
  1355. }
  1356. }