AlipayPayBack2Service.cs 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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. if(order.ParentOrderId > 0)
  86. {
  87. int total = db.Orders.Count(m => m.ParentOrderId == order.ParentOrderId);
  88. int paycount = db.Orders.Count(m => m.ParentOrderId == order.ParentOrderId && m.Status > 0);
  89. if(paycount + 1 >= total)
  90. {
  91. order = db.Orders.FirstOrDefault(m => m.Id == order.ParentOrderId && m.Status == 0);
  92. if (order != null)
  93. {
  94. order.Status = 2;
  95. order.PayDate = DateTime.Now;
  96. order.PayStatus = 1;
  97. OrderId = order.Id;
  98. }
  99. }
  100. }
  101. db.SaveChanges();
  102. if(order.ParentOrderId > 0)
  103. {
  104. return;
  105. }
  106. OrderProduct pro = db.OrderProduct.FirstOrDefault(m => m.OrderId == OrderId);
  107. if (pro != null)
  108. {
  109. order.ProductId = pro.ProductId;
  110. db.SaveChanges();
  111. if(order.ErpMode > 0)
  112. {
  113. pro.ProductId = order.ProductId;
  114. }
  115. List<int> couponIds = new List<int>();
  116. if (pro.ProductId == 10 || pro.ProductId == 11 || pro.ProductId == 27 || pro.ProductId == 28 || pro.ProductId == 34 || pro.ProductId == -2)
  117. {
  118. order.Status = 2;
  119. int BuyCount = pro.ProductCount;
  120. int Kind = 0;
  121. if (pro.ProductId == 10 || pro.ProductId == 34)
  122. {
  123. Kind = 1;
  124. }
  125. else if (pro.ProductId == 11 || pro.ProductId == -2)
  126. {
  127. Kind = 2;
  128. }
  129. else if (pro.ProductId == 28)
  130. {
  131. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId && m.LeaderLevel == 0);
  132. if(user != null)
  133. {
  134. user.LeaderLevel = 1;
  135. }
  136. }
  137. else if (pro.ProductId == 27)
  138. {
  139. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId && m.LeaderLevel < 2);
  140. if(user != null)
  141. {
  142. user.LeaderLevel = 2;
  143. }
  144. }
  145. if(Kind <= 2 && (pro.ProductId == 10 || pro.ProductId == 11 || pro.ProductId == 34 || pro.ProductId == -2))
  146. {
  147. // 购买600一组机具券,返券
  148. int CouponCount = 0;
  149. if(Kind == 1)
  150. {
  151. CouponCount = 3 * pro.ProductCount;
  152. }
  153. else if(Kind == 2)
  154. {
  155. CouponCount = 2 * pro.ProductCount;
  156. }
  157. string Codes = "";
  158. 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();
  159. int RecordId = 0;
  160. if(coupons.Count > 0 && (pro.ProductId == 34 || pro.ProductId == -2))
  161. {
  162. var adds = db.HelpProfitExchange.Add(new HelpProfitExchange()
  163. {
  164. CreateDate = DateTime.Now, //创建时间
  165. UserId = order.UserId,
  166. ExchangeCount = coupons.Count,
  167. }).Entity;
  168. db.SaveChanges();
  169. RecordId = adds.Id;
  170. }
  171. foreach (var coupon in coupons) // TODO: 数量多的话,会慢
  172. {
  173. PosCoupons item = db.PosCoupons.FirstOrDefault(m => m.Id == coupon.Id);
  174. if (item != null)
  175. {
  176. item.CreateDate = DateTime.Now;
  177. item.UserId = order.UserId;
  178. item.UpdateDate = DateTime.Now.AddDays(180);
  179. if(pro.ProductId == 34 || pro.ProductId == -2)
  180. {
  181. item.HelpProfitFlag = 1;
  182. db.HelpProfitExchangeDetail.Add(new HelpProfitExchangeDetail()
  183. {
  184. CreateDate = DateTime.Now, //创建时间
  185. UserId = order.UserId,
  186. PosCouponId = item.Id,
  187. ExchangeCode = item.ExchangeCode,
  188. RecordId = RecordId,
  189. });
  190. }
  191. Codes += item.ExchangeCode + ",";
  192. couponIds.Add(coupon.Id);
  193. }
  194. }
  195. order.SnNos = Codes.TrimEnd(',');
  196. }
  197. db.SaveChanges();
  198. if (pro.ProductId == 27 || pro.ProductId == 28)
  199. {
  200. //充值盟主储备金
  201. OpReserve(db, order, order.UserId, order.TotalPrice, 1);
  202. }
  203. if (pro.ProductId == 28) //购买小盟主,给上级大盟主返储备金
  204. {
  205. LeaderBack(db, order);
  206. }
  207. }
  208. //推荐下单奖励
  209. if (pro.ProductId == 10 || pro.ProductId == 11)
  210. {
  211. bool checkPrize = db.UserAccountRecord.Any(m => m.QueryCount == OrderId && m.ChangeType == 112);
  212. function.WriteLog("OrderId:" + OrderId, "推荐下单奖励监控日志");
  213. function.WriteLog("checkPrize:" + checkPrize, "推荐下单奖励监控日志");
  214. if (!checkPrize)
  215. {
  216. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  217. if (user != null)
  218. {
  219. bool directPrize = false; //直推奖标记
  220. bool bigLeaderPrize = false; //大盟主券标记
  221. bool operateFlag = false; //运营中心标记
  222. bool buyPrize = false; //返100购机奖励标记
  223. bool operatePrize = false; //返100购机奖励标记-运营中心
  224. int leaderFlag = 0; //返600备用金标记
  225. if(user.LeaderLevel > 0 || user.UserType == 1) //盟主或运营中心
  226. {
  227. if(user.LeaderLevel == 1)
  228. {
  229. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == order.UserId) ?? new UserAccount();
  230. if(acccount.LeaderReserve >= order.TotalPrice)
  231. {
  232. if(order.PayMode == 4)
  233. {
  234. //扣减备用金
  235. OpReserve(db, order, order.UserId, order.TotalPrice, 2, 0, "商城购机(储备金支付)");
  236. //返回到余额
  237. // OpAccount(db, order, order.UserId, order.TotalPrice / pro.ProductCount, pro.ProductCount);
  238. }
  239. else
  240. {
  241. //扣减备用金
  242. OpReserve(db, order, order.UserId, order.TotalPrice, 2, 0, "商城购机");
  243. //返回到余额
  244. OpLeaderAccount(db, order, order.UserId, order.TotalPrice / pro.ProductCount, pro.ProductCount);
  245. }
  246. }
  247. }
  248. //获得100元奖励
  249. OpAccount(db, order, order.UserId, 100, pro.ProductCount);
  250. //推荐奖励
  251. DirectPrize(db, order.Id, order.UserId, pro.ProductCount);
  252. //推荐下单上级获得30天的机具循环天数(盟主自己得)
  253. 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();
  254. foreach (var subPos in posList)
  255. {
  256. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == subPos.Id);
  257. if (pos != null)
  258. {
  259. pos.RecycEndDate = pos.RecycEndDate.Value.AddDays(30);
  260. }
  261. }
  262. db.SaveChanges();
  263. if(order.PayMode == 4 && user.LeaderLevel == 2) //使用盟主储蓄金,并且是大盟主
  264. {
  265. if(user.UserType == 1)
  266. {
  267. if(couponIds.Count > 0)
  268. {
  269. foreach(int couponId in couponIds)
  270. {
  271. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  272. if(coupon != null)
  273. {
  274. coupon.LeaderUserId = user.Id;
  275. coupon.OpId = user.Id;
  276. }
  277. }
  278. }
  279. db.SaveChanges();
  280. }
  281. else
  282. {
  283. //寻找最近运营中心额度大于0的运营中心
  284. int PUserId = user.Id;
  285. bool OperateFlag = true;
  286. while(PUserId > 0)
  287. {
  288. Users puser = db.Users.FirstOrDefault(m => m.Id == PUserId);
  289. if(puser != null)
  290. {
  291. if(puser.UserType == 1 && OperateFlag == true) //运营中心
  292. {
  293. if(couponIds.Count > 0)
  294. {
  295. foreach(int couponId in couponIds)
  296. {
  297. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  298. if(coupon != null)
  299. {
  300. coupon.OpId = puser.Id;
  301. }
  302. }
  303. db.SaveChanges();
  304. OperateFlag = false;
  305. }
  306. }
  307. PUserId = puser.ParentUserId;
  308. }
  309. else
  310. {
  311. PUserId = 0;
  312. }
  313. }
  314. }
  315. }
  316. else if(order.PayMode == 4 && user.UserType == 1) //使用盟主储蓄金,并且是运营中心
  317. {
  318. if(couponIds.Count > 0)
  319. {
  320. foreach(int couponId in couponIds)
  321. {
  322. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  323. if(coupon != null)
  324. {
  325. coupon.OpId = user.Id;
  326. }
  327. }
  328. }
  329. db.SaveChanges();
  330. //寻找最近储蓄金充足的大盟主
  331. int PUserId = user.Id;
  332. bool LeaderFlag = true;
  333. while(PUserId > 0)
  334. {
  335. Users puser = db.Users.FirstOrDefault(m => m.Id == PUserId);
  336. if(puser != null)
  337. {
  338. if(puser.LeaderLevel == 2 && LeaderFlag == true) //大盟主
  339. {
  340. if(couponIds.Count > 0)
  341. {
  342. foreach(int couponId in couponIds)
  343. {
  344. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  345. if(coupon != null)
  346. {
  347. coupon.LeaderUserId = puser.Id;
  348. }
  349. }
  350. db.SaveChanges();
  351. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == order.UserId) ?? new UserAccount();
  352. if(acccount.LeaderReserve >= order.TotalPrice)
  353. {
  354. //扣减备用金
  355. OpReserve(db, order, order.UserId, order.TotalPrice, 2, 0, "商城购机");
  356. //返回到余额
  357. OpLeaderAccount(db, order, order.UserId, order.TotalPrice / pro.ProductCount, pro.ProductCount);
  358. }
  359. LeaderFlag = false;
  360. }
  361. }
  362. PUserId = puser.ParentUserId;
  363. }
  364. else
  365. {
  366. PUserId = 0;
  367. }
  368. }
  369. }
  370. else
  371. {
  372. //寻找最近储蓄金充足的大盟主及最近运营中心额度大于0的运营中心(含自身)
  373. int PUserId = user.Id;
  374. bool LeaderFlag = true;
  375. bool OperateFlag = true;
  376. while(PUserId > 0)
  377. {
  378. Users puser = db.Users.FirstOrDefault(m => m.Id == PUserId);
  379. if(puser != null)
  380. {
  381. if(puser.LeaderLevel == 2 && LeaderFlag == true) //大盟主
  382. {
  383. if(couponIds.Count > 0)
  384. {
  385. foreach(int couponId in couponIds)
  386. {
  387. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  388. if(coupon != null)
  389. {
  390. coupon.LeaderUserId = puser.Id;
  391. }
  392. }
  393. db.SaveChanges();
  394. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == order.UserId) ?? new UserAccount();
  395. if(acccount.LeaderReserve >= order.TotalPrice)
  396. {
  397. //扣减备用金
  398. OpReserve(db, order, order.UserId, order.TotalPrice, 2, 0, "商城购机");
  399. //返回到余额
  400. OpLeaderAccount(db, order, order.UserId, order.TotalPrice / pro.ProductCount, pro.ProductCount);
  401. }
  402. LeaderFlag = false;
  403. }
  404. }
  405. if(puser.UserType == 1 && OperateFlag == true) //运营中心
  406. {
  407. if(couponIds.Count > 0)
  408. {
  409. foreach(int couponId in couponIds)
  410. {
  411. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  412. if(coupon != null)
  413. {
  414. coupon.OpId = puser.Id;
  415. }
  416. }
  417. db.SaveChanges();
  418. OperateFlag = false;
  419. }
  420. }
  421. PUserId = puser.ParentUserId;
  422. }
  423. else
  424. {
  425. PUserId = 0;
  426. }
  427. }
  428. }
  429. return;
  430. }
  431. int ParentUserId = user.ParentUserId;
  432. // List<int> proids = new List<int>();
  433. // proids.Add(10);
  434. // proids.Add(11);
  435. while(ParentUserId > 0)
  436. {
  437. Users puser = db.Users.FirstOrDefault(m => m.Id == ParentUserId) ?? new Users();
  438. int machineCount = db.PosMachinesTwo.Count(m => m.BuyUserId == ParentUserId && m.PosSnType == 0 && m.ActivationState == 0); //判断是否拥有3台购买机
  439. int ActiveCount = db.PosMachinesTwo.Count(m => m.BuyUserId == ParentUserId && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  440. int couponCount = db.PosCoupons.Count(m => m.UserId == ParentUserId && m.IsUse == 0); //判断是否拥有3张券
  441. function.WriteLog("MakerCode:" + puser.MakerCode, "推荐下单奖励监控日志");
  442. function.WriteLog("machineCount:" + machineCount, "推荐下单奖励监控日志");
  443. function.WriteLog("ActiveCount:" + ActiveCount, "推荐下单奖励监控日志");
  444. if ((machineCount + ActiveCount + couponCount >= 3 || puser.LeaderLevel > 0) && !directPrize)
  445. {
  446. // function.WriteLog("满足条件", "推荐下单奖励监控日志");
  447. // if(puser.LeaderLevel == 0) // 非盟主直推奖励,每个创客第一次下单,上级可得
  448. // {
  449. // List<int> oids = new List<int>();
  450. // var orderpros = db.OrderProduct.Select(m => new { m.OrderId,m.UserId,m.ProductId }).Where(m => m.UserId == order.UserId && proids.Contains(m.ProductId)).ToList();
  451. // foreach(var orderpro in orderpros)
  452. // {
  453. // oids.Add(orderpro.OrderId);
  454. // }
  455. // bool check = db.Orders.Any(m => oids.Contains(m.Id) && m.Status > 0);
  456. // if(!check)
  457. // {
  458. // DirectPrize(db, order.Id, ParentUserId, pro.ProductCount);
  459. // directPrize = true;
  460. // }
  461. // }
  462. // else
  463. // {
  464. // 盟主直推奖励,可以每次下单获得
  465. DirectPrize(db, order.Id, ParentUserId, pro.ProductCount);
  466. directPrize = true;
  467. // }
  468. //推荐下单上级获得30天的机具循环天数
  469. 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();
  470. foreach (var subPos in posList)
  471. {
  472. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == subPos.Id);
  473. if (pos != null)
  474. {
  475. pos.RecycEndDate = pos.RecycEndDate.Value.AddDays(30);
  476. }
  477. }
  478. db.SaveChanges();
  479. }
  480. if(puser.LeaderLevel > 0)
  481. {
  482. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
  483. if(acccount.LeaderReserve >= order.TotalPrice && !buyPrize)
  484. {
  485. //购机奖励
  486. OpAccount(db, order, puser.Id, 100, pro.ProductCount);
  487. buyPrize = true;
  488. }
  489. if(acccount.LeaderReserve >= order.TotalPrice && leaderFlag == 0)
  490. {
  491. //扣减备用金
  492. OpReserve(db, order, puser.Id, order.TotalPrice, 2, order.UserId, "购机奖励");
  493. //返回到余额
  494. OpLeaderAccount(db, order, puser.Id, order.TotalPrice / pro.ProductCount, pro.ProductCount);
  495. leaderFlag = puser.LeaderLevel;
  496. }
  497. //如果是大盟主,则标记大盟主标签
  498. if(couponIds.Count > 0 && puser.LeaderLevel == 2 && acccount.LeaderReserve >= order.TotalPrice && !bigLeaderPrize)
  499. {
  500. foreach(int couponId in couponIds)
  501. {
  502. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  503. if(coupon != null)
  504. {
  505. coupon.LeaderUserId = puser.Id;
  506. }
  507. }
  508. db.SaveChanges();
  509. bigLeaderPrize = true;
  510. }
  511. }
  512. if(puser.UserType == 1)
  513. {
  514. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
  515. if(acccount.LeaderReserve >= order.TotalPrice && !operatePrize)
  516. {
  517. //购机奖励
  518. OpAccount(db, order, puser.Id, 100, pro.ProductCount);
  519. operatePrize = true;
  520. }
  521. //标记标签
  522. if(couponIds.Count > 0 && !operateFlag)
  523. {
  524. foreach(int couponId in couponIds)
  525. {
  526. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
  527. if(coupon != null)
  528. {
  529. coupon.OpId= puser.Id;
  530. }
  531. }
  532. db.SaveChanges();
  533. operateFlag = true;
  534. }
  535. }
  536. ParentUserId = puser.ParentUserId;
  537. }
  538. }
  539. }
  540. function.WriteLog("\r\n\r\n", "推荐下单奖励监控日志");
  541. }
  542. //推荐王
  543. if(pro.ProductId == 29)
  544. {
  545. RecommendMethod(order.UserId, order.CreateDate.Value.ToString("yyyyMM"));
  546. }
  547. //购买分仓临时额度
  548. if(pro.ProductId == 30 || pro.ProductId == 31 || pro.ProductId == 32)
  549. {
  550. string SendData = "{\"Kind\":\"1\",\"Data\":{\"OrderId\":\"" + order.Id + "\"}}";
  551. RedisDbconn.Instance.AddList("StoreApplyQueue", SendData);
  552. }
  553. }
  554. else
  555. {
  556. if(order.ProductId > 0 && order.ErpMode > 0)
  557. {
  558. //盟主储备金-升级/购买 ErpMode:1-升级,2-购买
  559. if(order.ErpMode > 0)
  560. {
  561. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  562. if(user != null)
  563. {
  564. if(order.ErpMode == 1)
  565. {
  566. user.LeaderLevel = 2;
  567. db.SaveChanges();
  568. }
  569. OpReserve(db, order, order.UserId, order.TotalPrice, 1);
  570. }
  571. }
  572. }
  573. }
  574. }
  575. }
  576. //小盟主购买逻辑
  577. public void LeaderBack(WebCMSEntities db, Orders order)
  578. {
  579. int LeaderUserId = order.UserId;
  580. int level = 0;
  581. while(LeaderUserId > 0)
  582. {
  583. level += 1;
  584. Users user = db.Users.FirstOrDefault(m => m.Id == LeaderUserId);
  585. if(user != null)
  586. {
  587. if(user.LeaderLevel == 2 && level > 1)
  588. {
  589. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == LeaderUserId);
  590. if(account != null)
  591. {
  592. if(account.LeaderReserve >= order.TotalPrice)
  593. {
  594. OpReserve(db, order, LeaderUserId, order.TotalPrice, 2, 0, "推荐小盟主");
  595. OpLeaderAccount(db, order, LeaderUserId, order.TotalPrice);
  596. }
  597. else
  598. {
  599. decimal LeaderReserve = account.LeaderReserve;
  600. OpReserve(db, order, LeaderUserId, LeaderReserve, 2, 0, "推荐小盟主");
  601. OpLeaderAccount(db, order, LeaderUserId, LeaderReserve);
  602. }
  603. }
  604. LeaderUserId = 0;
  605. }
  606. else
  607. {
  608. LeaderUserId = user.ParentUserId;
  609. }
  610. }
  611. else
  612. {
  613. LeaderUserId = 0;
  614. }
  615. }
  616. }
  617. //操作储备金
  618. public void OpReserve(WebCMSEntities db, Orders order, int UserId, decimal Money, int ChangeType, int SourceUserId = 0, string Remark = "储备金购买")
  619. {
  620. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  621. if (account == null)
  622. {
  623. account = db.UserAccount.Add(new UserAccount()
  624. {
  625. Id = UserId,
  626. UserId = UserId,
  627. }).Entity;
  628. db.SaveChanges();
  629. }
  630. decimal BeforeAmount = account.LeaderReserve; //变更前总金额
  631. if(ChangeType == 1)
  632. {
  633. account.LeaderReserve += Money;
  634. }
  635. else
  636. {
  637. account.LeaderReserve -= Money;
  638. }
  639. decimal AfterAmount = account.LeaderReserve; //变更后总金额
  640. LeaderReserveRecord add = db.LeaderReserveRecord.Add(new LeaderReserveRecord()
  641. {
  642. CreateDate = DateTime.Now,
  643. ChangeType = ChangeType,
  644. OrderId = order.Id,
  645. Remark = Remark,
  646. AfterAmt = AfterAmount,
  647. BeforeAmt = BeforeAmount,
  648. ChangeAmt = Money,
  649. TradeDate = DateTime.Now.ToString("yyyyMMdd"),
  650. TradeMonth = DateTime.Now.ToString("yyyyMM"),
  651. UserId = UserId,
  652. SourceUserId = SourceUserId,
  653. }).Entity;
  654. db.SaveChanges();
  655. }
  656. //操作余额
  657. public void OpAccount(WebCMSEntities db, Orders order, int UserId, decimal Money, int Count = 1)
  658. {
  659. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  660. if (account == null)
  661. {
  662. account = db.UserAccount.Add(new UserAccount()
  663. {
  664. Id = UserId,
  665. UserId = UserId,
  666. }).Entity;
  667. db.SaveChanges();
  668. }
  669. int ChangeType = 0;
  670. if(Money == 600)
  671. {
  672. ChangeType = 117;
  673. }
  674. else if(Money == 100)
  675. {
  676. ChangeType = 118;
  677. }
  678. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  679. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  680. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  681. account.BalanceAmount += Money * Count;
  682. account.TotalAmount += Money * Count;
  683. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  684. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  685. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  686. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  687. {
  688. CreateDate = DateTime.Now,
  689. UpdateDate = DateTime.Now,
  690. UserId = UserId, //创客
  691. ChangeType = ChangeType, //变动类型
  692. ChangeAmount = Money * Count, //变更金额
  693. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  694. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  695. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  696. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  697. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  698. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  699. QueryCount = order.Id,
  700. }).Entity;
  701. db.SaveChanges();
  702. }
  703. public void ChangeAccount(WebCMSEntities db, Orders order, int UserId, decimal Money, int ChangeType = 0)
  704. {
  705. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  706. if (account == null)
  707. {
  708. account = db.UserAccount.Add(new UserAccount()
  709. {
  710. Id = UserId,
  711. UserId = UserId,
  712. }).Entity;
  713. db.SaveChanges();
  714. }
  715. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  716. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  717. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  718. account.BalanceAmount += Money;
  719. account.TotalAmount += Money;
  720. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  721. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  722. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  723. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  724. {
  725. CreateDate = DateTime.Now,
  726. UpdateDate = DateTime.Now,
  727. UserId = UserId, //创客
  728. ChangeType = ChangeType, //变动类型
  729. ChangeAmount = Money, //变更金额
  730. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  731. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  732. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  733. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  734. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  735. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  736. QueryCount = order.Id,
  737. }).Entity;
  738. db.SaveChanges();
  739. }
  740. //操作盟主可提现余额
  741. public void OpLeaderAccount(WebCMSEntities db, Orders order, int UserId, decimal Money, int Count = 1)
  742. {
  743. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  744. if (account == null)
  745. {
  746. account = db.UserAccount.Add(new UserAccount()
  747. {
  748. Id = UserId,
  749. UserId = UserId,
  750. }).Entity;
  751. db.SaveChanges();
  752. }
  753. int ChangeType = 0;
  754. if(Money == 600)
  755. {
  756. ChangeType = 117;
  757. }
  758. decimal BeforeLeaderBalanceAmount = account.LeaderBalanceAmount; //变更前余额
  759. account.LeaderBalanceAmount += Money * Count;
  760. decimal AfterLeaderBalanceAmount = account.LeaderBalanceAmount; //变更后余额
  761. LeaderAccountRecord leaderAccountRecord = db.LeaderAccountRecord.Add(new LeaderAccountRecord()
  762. {
  763. CreateDate = DateTime.Now,
  764. UpdateDate = DateTime.Now,
  765. UserId = UserId, //创客
  766. ChangeType = ChangeType, //变动类型
  767. ChangeAmount = Money * Count, //变更金额
  768. BeforeBalanceAmount = BeforeLeaderBalanceAmount, //变更前余额
  769. AfterBalanceAmount = AfterLeaderBalanceAmount, //变更后余额
  770. QueryCount = order.Id,
  771. }).Entity;
  772. db.SaveChanges();
  773. }
  774. public void DirectPrize(WebCMSEntities db, int OrderId, int UserId, int Count = 1)
  775. {
  776. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  777. if (account == null)
  778. {
  779. account = db.UserAccount.Add(new UserAccount()
  780. {
  781. Id = UserId,
  782. UserId = UserId,
  783. }).Entity;
  784. db.SaveChanges();
  785. }
  786. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  787. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  788. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  789. account.BalanceAmount += 100 * Count;
  790. account.TotalAmount += 100 * Count;
  791. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  792. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  793. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  794. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  795. {
  796. CreateDate = DateTime.Now,
  797. UpdateDate = DateTime.Now,
  798. UserId = UserId, //创客
  799. ChangeType = 112, //变动类型
  800. ChangeAmount = 100 * Count, //变更金额
  801. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  802. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  803. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  804. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  805. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  806. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  807. QueryCount = OrderId,
  808. }).Entity;
  809. db.SaveChanges();
  810. }
  811. // 推荐王逻辑(下单)
  812. public void RecommendMethod(int UserId, string TradeMonth)
  813. {
  814. string SendData = "{\"Kind\":\"1\",\"Data\":{\"UserId\":\"" + UserId + "\",\"TradeMonth\":\"" + TradeMonth + "\"}}";
  815. RedisDbconn.Instance.AddList("RecommendActStatQueue", SendData);
  816. }
  817. //操作运营中心额度
  818. public void OperateAmountChange(OpModels.WebCMSEntities db, Orders order, int UserId, decimal Money, int OperateType, string Remark = "")
  819. {
  820. OpModels.UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  821. if (account == null)
  822. {
  823. account = db.UserAccount.Add(new OpModels.UserAccount()
  824. {
  825. Id = UserId,
  826. UserId = UserId,
  827. }).Entity;
  828. db.SaveChanges();
  829. }
  830. decimal BeforeAmount = account.ValidAmount; //变更前总金额
  831. if(OperateType == 1)
  832. {
  833. account.ValidAmount += Money;
  834. }
  835. else
  836. {
  837. account.ValidAmount -= Money;
  838. }
  839. decimal AfterAmount = account.ValidAmount; //变更后总金额
  840. OpModels.AmountRecord add = db.AmountRecord.Add(new OpModels.AmountRecord()
  841. {
  842. CreateDate = DateTime.Now,
  843. UpdateDate = DateTime.Now,
  844. OperateType = OperateType,
  845. AfterAmount = AfterAmount,
  846. BeforeAmount = BeforeAmount,
  847. UseAmount = Money,
  848. UserId = UserId,
  849. SeoDescription = Remark,
  850. }).Entity;
  851. db.SaveChanges();
  852. }
  853. #endregion
  854. }
  855. }