AlipayPayBack2Service.cs 55 KB

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