AlipayPayBack2Service.cs 51 KB

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