AlipayPayBack2Service.cs 55 KB

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