AlipayPayBack2Service.cs 46 KB

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