AlipayPayBack2Service.cs 59 KB

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