AlipayPayBack2Service.cs 47 KB

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