AlipayPayBack2Service.cs 57 KB

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