AlipayPayBack2Service.cs 58 KB

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