AlipayPayBack2Service.cs 57 KB

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