SycnProfitServiceV2.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Threading;
  6. using MySystem.Models;
  7. using Library;
  8. namespace MySystem
  9. {
  10. public class SycnProfitServiceV2
  11. {
  12. public readonly static SycnProfitServiceV2 Instance = new SycnProfitServiceV2();
  13. private SycnProfitServiceV2()
  14. { }
  15. public void Start()
  16. {
  17. Thread th = new Thread(doSomething);
  18. th.IsBackground = true;
  19. th.Start();
  20. }
  21. public void doSomething()
  22. {
  23. while (true)
  24. {
  25. string content = RedisDbconn.Instance.RPop<string>("SycnProfitQueue2");
  26. if (!string.IsNullOrEmpty(content))
  27. {
  28. try
  29. {
  30. function.WriteLog(DateTime.Now.ToString() + "\r\n\r\n", "同步分润数据");
  31. string[] data = content.Split(new string[] { "#cut#" }, StringSplitOptions.None);
  32. int BrandId = int.Parse(data[0]);
  33. string date = data[1];
  34. int OpType = int.Parse(data[2]);
  35. string SysUserName = data[3];
  36. if (OpType == 0)
  37. {
  38. DoTradeProfit(BrandId, date, SysUserName);
  39. DoSubsidyProfit(BrandId, date);
  40. }
  41. else if (OpType == 1)
  42. {
  43. DoTradeProfit2(BrandId, date, SysUserName);
  44. DoSubsidyProfit2(BrandId, date);
  45. if(BrandId == 1)
  46. {
  47. DoUserServiceFee();
  48. }
  49. }
  50. function.WriteLog(DateTime.Now.ToString() + "\r\n\r\n", "同步分润数据");
  51. }
  52. catch (Exception ex)
  53. {
  54. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "同步分润数据异常");
  55. }
  56. }
  57. else
  58. {
  59. Thread.Sleep(60000);
  60. }
  61. }
  62. }
  63. private void DoTradeProfit(int BrandId, string date, string SysUserName)
  64. {
  65. WebCMSEntities db = new WebCMSEntities();
  66. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  67. string StartId = function.ReadInstance("/PublicParams/ProfitRewardRecordId" + date + ".txt");
  68. if(string.IsNullOrEmpty(StartId))
  69. {
  70. StartId = "0";
  71. }
  72. OtherMySqlConn.op("insert into ProfitRecord (CreateDate,CreateMan,SeoTitle,ParentNav,BrandId,UserId,DirectFlag,ProfitAmount) select now(),'root','" + date + "',(select ParentNav from Users where Id=p.UserId)," + BrandId + ",UserId,ProfitType,sum(CreditTradeProfit+DebitNonTradeCapProfit+DebitTradeCapProfit) from ProfitRewardRecord p where Id>=" + StartId + " and CheckStatus=0 and BrandId=" + BrandId + " and UserId>0 and TradeMonth='" + date + "' group by UserId,ProfitType order by UserId");
  73. DataTable dt = OtherMySqlConn.dtable("select UserId,sum(CreditTradeProfit+DebitNonTradeCapProfit+DebitTradeCapProfit) from ProfitRewardRecord where Id>=" + StartId + " and CheckStatus=0 and BrandId=" + BrandId + " and UserId>0 and TradeMonth='" + date + "' group by UserId");
  74. function.WriteLog("分润:" + BrandId + ":" + dt.Rows.Count, "同步分润数据");
  75. int index = 0;
  76. foreach (DataRow dr in dt.Rows)
  77. {
  78. index += 1;
  79. int UserId = int.Parse(dr["UserId"].ToString());
  80. decimal ProfitMoney = decimal.Parse(dr[1].ToString());
  81. try
  82. {
  83. string content = UserId + "|" + BrandId + "|" + ProfitMoney + "|" + index;
  84. RedisDbconn.Instance.AddList("DoTradeProfitQueue", content);
  85. }
  86. catch (Exception ex)
  87. {
  88. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步到余额异常");
  89. }
  90. function.WriteLog(index.ToString(), "同步分润数据");
  91. }
  92. OtherMySqlConn.op("update ProfitRewardRecord set CheckStatus=1 where Id>=" + StartId + " and CheckStatus=0 and BrandId=" + BrandId + " and UserId>0 and TradeMonth='" + date + "'");
  93. db.Dispose();
  94. dt.Dispose();
  95. RedisDbconn.Instance.AddList("DoTradeProfitQueue", "end");
  96. SycnProfitServiceV2.Instance.StartProfit();
  97. }
  98. private void DoTradeProfit2(int BrandId, string date, string SysUserName)
  99. {
  100. WebCMSEntities db = new WebCMSEntities();
  101. string conn = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  102. int startid = 0;
  103. bool op = true;
  104. while(op)
  105. {
  106. DataTable dt = CustomerSqlConn.dtable("select Id,UserId,DirectFlag,ProfitAmount from ProfitRecord where Id>" + startid + " and BrandId=" + BrandId + " and SeoTitle='" + date + "' and Version=0 and UserId>0 order by Id limit 200", conn);
  107. if(dt.Rows.Count > 0)
  108. {
  109. function.WriteLog("分润:" + BrandId + ":" + dt.Rows.Count, "同步分润数据");
  110. int index = 0;
  111. List<int> UserIds = new List<int>();
  112. foreach (DataRow dr in dt.Rows)
  113. {
  114. index += 1;
  115. int UserId = int.Parse(dr["UserId"].ToString());
  116. int DirectFlag = int.Parse(dr["DirectFlag"].ToString());
  117. decimal ProfitAmount = decimal.Parse(dr["ProfitAmount"].ToString());
  118. var tran = db.Database.BeginTransaction();
  119. try
  120. {
  121. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  122. if (account == null)
  123. {
  124. account = db.UserAccount.Add(new UserAccount()
  125. {
  126. Id = UserId,
  127. UserId = UserId,
  128. }).Entity;
  129. db.SaveChanges();
  130. }
  131. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  132. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  133. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  134. decimal AfterTotalAmount = account.TotalAmount + ProfitAmount; //变更后总金额
  135. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  136. decimal AfterBalanceAmount = account.BalanceAmount + ProfitAmount; //变更后余额
  137. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  138. {
  139. CreateDate = DateTime.Now,
  140. UpdateDate = DateTime.Now,
  141. UserId = UserId, //创客
  142. ProductType = BrandId,
  143. ChangeType = 1, //变动类型
  144. ChangeAmount = ProfitAmount, //变更金额
  145. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  146. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  147. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  148. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  149. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  150. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  151. Remark = DirectFlag == 1 ? "直拓商户分润" : "品牌推广服务费",
  152. }).Entity;
  153. UserProfit profit = db.UserProfit.FirstOrDefault(m => m.Id == UserId);
  154. if (profit == null)
  155. {
  156. profit = db.UserProfit.Add(new UserProfit()
  157. {
  158. Id = UserId,
  159. UserId = UserId,
  160. }).Entity;
  161. db.SaveChanges();
  162. }
  163. profit.ProfitRate += ProfitAmount;
  164. db.SaveChanges();
  165. if(!UserIds.Contains(UserId))
  166. {
  167. UserIds.Add(UserId);
  168. }
  169. tran.Commit();
  170. }
  171. catch (Exception ex)
  172. {
  173. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitAmount + "\n" + ex.ToString(), "同步到余额异常");
  174. tran.Rollback();
  175. }
  176. startid = int.Parse(dr["Id"].ToString());
  177. function.WriteLog(index.ToString(), "同步分润数据");
  178. }
  179. foreach(int UserId in UserIds)
  180. {
  181. RedisDbconn.Instance.AddList("UserMonthFeeQueue", UserId.ToString());
  182. }
  183. }
  184. else
  185. {
  186. op = false;
  187. }
  188. }
  189. CustomerSqlConn.op("update ProfitRecord set Version=1 where BrandId=" + BrandId + " and SeoTitle='" + date + "' and Version=0 and UserId>0", conn);
  190. db.Dispose();
  191. }
  192. //分润补贴
  193. private void DoSubsidyProfit(int BrandId, string date)
  194. {
  195. WebCMSEntities db = new WebCMSEntities();
  196. string conn = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  197. string StartId = function.ReadInstance("/PublicParams/ProfitSubsidyDetailId" + date + ".txt");
  198. if(string.IsNullOrEmpty(StartId))
  199. {
  200. StartId = "0";
  201. }
  202. CustomerSqlConn.op("insert into SubsidyRecord (CreateDate,SeoKeyword,SeoTitle,ParentNav,BrandId,UserId,ProfitAmount) select now(),'root','" + date + "',(select ParentNav from Users where Id=p.SubsidyUserId)," + BrandId + ",SubsidyUserId,sum(SubsidyProfitRate) from ProfitSubsidyDetail p where Id>=" + StartId + " and Status=0 and BrandId=" + BrandId + " and TradeMonth='" + date + "' GROUP BY SubsidyUserId", conn);
  203. DataTable dt = CustomerSqlConn.dtable("select SubsidyUserId,sum(SubsidyProfitRate) from ProfitSubsidyDetail where Id>=" + StartId + " and Status=0 and BrandId=" + BrandId + " and TradeMonth='" + date + "' GROUP BY SubsidyUserId", conn);
  204. function.WriteLog("补贴:" + BrandId + ":" + dt.Rows.Count, "同步补贴数据");
  205. int index = 0;
  206. foreach (DataRow dr in dt.Rows)
  207. {
  208. index += 1;
  209. int UserId = int.Parse(dr["SubsidyUserId"].ToString());
  210. decimal ProfitMoney = decimal.Parse(dr[1].ToString());
  211. try
  212. {
  213. string content = UserId + "|" + BrandId + "|" + ProfitMoney + "|" + index;
  214. RedisDbconn.Instance.AddList("DoSubsidyProfitQueue", content);
  215. }
  216. catch (Exception ex)
  217. {
  218. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步到补贴异常");
  219. }
  220. }
  221. CustomerSqlConn.op("update ProfitSubsidyDetail set Status=1 where Id>=" + StartId + " and Status=0 and BrandId=" + BrandId + " and TradeMonth='" + date + "'", conn);
  222. db.Dispose();
  223. dt.Dispose();
  224. RedisDbconn.Instance.AddList("DoSubsidyProfitQueue", "end");
  225. SycnProfitServiceV2.Instance.StartSubsidy();
  226. }
  227. private void DoSubsidyProfit2(int BrandId, string date)
  228. {
  229. WebCMSEntities db = new WebCMSEntities();
  230. string conn = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  231. int startid = 0;
  232. bool op = true;
  233. while(op)
  234. {
  235. DataTable dt = CustomerSqlConn.dtable("select Id,UserId,ProfitAmount from SubsidyRecord where Id>" + startid + " and BrandId=" + BrandId + " and SeoTitle='" + date + "' and Status=0 and UserId>0 order by Id limit 200", conn);
  236. if(dt.Rows.Count > 0)
  237. {
  238. function.WriteLog("补贴:" + BrandId + ":" + dt.Rows.Count, "同步补贴数据");
  239. int index = 0;
  240. foreach (DataRow dr in dt.Rows)
  241. {
  242. index += 1;
  243. int UserId = int.Parse(dr["UserId"].ToString());
  244. decimal ProfitMoney = decimal.Parse(dr["ProfitAmount"].ToString());
  245. var tran = db.Database.BeginTransaction();
  246. try
  247. {
  248. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  249. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  250. if (account == null)
  251. {
  252. account = db.UserAccount.Add(new UserAccount()
  253. {
  254. Id = UserId,
  255. UserId = UserId,
  256. }).Entity;
  257. db.SaveChanges();
  258. }
  259. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  260. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  261. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  262. decimal AfterTotalAmount = account.TotalAmount + ProfitMoney; //变更后总金额
  263. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  264. decimal AfterBalanceAmount = account.BalanceAmount + ProfitMoney; //变更后余额
  265. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  266. {
  267. CreateDate = DateTime.Now,
  268. UpdateDate = DateTime.Now,
  269. UserId = UserId, //创客
  270. ProductType = BrandId,
  271. ChangeType = 111, //变动类型
  272. ChangeAmount = ProfitMoney, //变更金额
  273. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  274. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  275. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  276. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  277. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  278. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  279. Remark = "直拓商户补贴",
  280. }).Entity;
  281. UserProfit profit = db.UserProfit.FirstOrDefault(m => m.Id == UserId);
  282. if (profit == null)
  283. {
  284. profit = db.UserProfit.Add(new UserProfit()
  285. {
  286. Id = UserId,
  287. UserId = UserId,
  288. }).Entity;
  289. db.SaveChanges();
  290. }
  291. profit.ProfitRate += ProfitMoney;
  292. db.SaveChanges();
  293. tran.Commit();
  294. }
  295. catch (Exception ex)
  296. {
  297. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步到补贴异常");
  298. tran.Rollback();
  299. }
  300. startid = int.Parse(dr["Id"].ToString());
  301. function.WriteLog(index.ToString(), "同步补贴数据");
  302. }
  303. }
  304. else
  305. {
  306. op = false;
  307. }
  308. }
  309. CustomerSqlConn.op("update SubsidyRecord set Status=1 where BrandId=" + BrandId + " and SeoTitle='" + date + "' and Status=0 and UserId>0", conn);
  310. db.Dispose();
  311. }
  312. #region 助力宝分润展示
  313. public void DoHelpProfit(string month)
  314. {
  315. string conn = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  316. CustomerSqlConn.dtable("update HelpProfitReward set Status=1 where Status=0 and TradeMonth='" + month + "'", conn);
  317. }
  318. #endregion
  319. #region 助力宝分润到账
  320. public void DoHelpProfit2(string month)
  321. {
  322. WebCMSEntities db = new WebCMSEntities();
  323. string conn = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  324. DataTable dt = CustomerSqlConn.dtable("select UserId,RewardType,sum(CreditRewardAmount),sum(CreditTradeAmt) from HelpProfitReward where Status=1 and TradeMonth='" + month + "' GROUP BY UserId,RewardType", conn);
  325. function.WriteLog("数量:" + dt.Rows.Count, "同步助力宝分润数据");
  326. int index = 0;
  327. foreach (DataRow dr in dt.Rows)
  328. {
  329. index += 1;
  330. int UserId = int.Parse(dr["UserId"].ToString());
  331. int RewardType = int.Parse(dr["RewardType"].ToString());
  332. decimal ProfitMoney = decimal.Parse(dr[2].ToString());
  333. decimal TradeAmt = decimal.Parse(dr[3].ToString());
  334. var tran = db.Database.BeginTransaction();
  335. try
  336. {
  337. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  338. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  339. if (account == null)
  340. {
  341. account = db.UserAccount.Add(new UserAccount()
  342. {
  343. Id = UserId,
  344. UserId = UserId,
  345. }).Entity;
  346. db.SaveChanges();
  347. }
  348. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  349. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  350. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  351. account.BalanceAmount += ProfitMoney;
  352. account.TotalAmount += ProfitMoney;
  353. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  354. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  355. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  356. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  357. {
  358. CreateDate = DateTime.Now,
  359. UpdateDate = DateTime.Now,
  360. UserId = UserId, //创客
  361. ProductType = 101,
  362. ChangeType = RewardType, //变动类型
  363. ChangeAmount = ProfitMoney, //变更金额
  364. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  365. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  366. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  367. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  368. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  369. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  370. Remark = user.RealName.Substring(0, 1) + "**:" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + "交易" + TradeAmt.ToString("f2"),
  371. }).Entity;
  372. db.SaveChanges();
  373. tran.Commit();
  374. }
  375. catch (Exception ex)
  376. {
  377. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步助利宝分润异常");
  378. tran.Rollback();
  379. }
  380. function.WriteLog(index.ToString(), "同步补贴数据");
  381. }
  382. OtherMySqlConn.dtable("update HelpProfitReward set Status=2 where Status=1 and TradeMonth='" + month + "'");
  383. db.Dispose();
  384. }
  385. #endregion
  386. #region 扣创客服务费
  387. private void DoUserServiceFee()
  388. {
  389. WebCMSEntities db = new WebCMSEntities();
  390. int startId = 0;
  391. bool op = true;
  392. while(op)
  393. {
  394. var list = db.UserProfit.Select(m => new { m.Id, m.ProfitRate }).Where(m => m.Id > startId).OrderBy(m => m.Id).Take(50).ToList();
  395. if(list.Count > 0)
  396. {
  397. foreach(var item in list)
  398. {
  399. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == item.Id);
  400. if (account == null)
  401. {
  402. account = db.UserAccount.Add(new UserAccount()
  403. {
  404. Id = item.Id,
  405. UserId = item.Id,
  406. }).Entity;
  407. db.SaveChanges();
  408. }
  409. account.BalanceAmount += item.ProfitRate;
  410. account.TotalAmount += item.ProfitRate;
  411. db.SaveChanges();
  412. if(CheckUser(db, item.Id))
  413. {
  414. RedisDbconn.Instance.AddList("UserMonthFeeQueue", item.Id.ToString());
  415. }
  416. else
  417. {
  418. RedisDbconn.Instance.AddList("PosExpiredPayQueue", item.Id.ToString());
  419. }
  420. startId = item.Id;
  421. }
  422. list.Clear();
  423. Thread.Sleep(100);
  424. }
  425. else
  426. {
  427. op = false;
  428. }
  429. }
  430. db.Dispose();
  431. }
  432. #endregion
  433. public void StartProfit()
  434. {
  435. Thread th = new Thread(StartProfitDo);
  436. th.IsBackground = true;
  437. th.Start();
  438. }
  439. public void StartProfitDo()
  440. {
  441. WebCMSEntities db = new WebCMSEntities();
  442. bool op = true;
  443. while (op)
  444. {
  445. string content = RedisDbconn.Instance.RPop<string>("DoTradeProfitQueue");
  446. if (!string.IsNullOrEmpty(content))
  447. {
  448. if(content == "end")
  449. {
  450. op = false;
  451. }
  452. else
  453. {
  454. try
  455. {
  456. string[] data = content.Split('|');
  457. int UserId = int.Parse(data[0]);
  458. int BrandId = int.Parse(data[1]);
  459. decimal ProfitMoney = decimal.Parse(data[2]);
  460. int index = int.Parse(data[3]);
  461. string IdBrand = UserId + "_" + BrandId;
  462. UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  463. if (MachineData == null)
  464. {
  465. MachineData = db.UserMachineData.Add(new UserMachineData()
  466. {
  467. IdBrand = IdBrand,
  468. }).Entity;
  469. db.SaveChanges();
  470. }
  471. MachineData.TradeProfit += ProfitMoney;
  472. db.SaveChanges();
  473. function.WriteLog(index.ToString() + "--" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "同步分润队列数据");
  474. }
  475. catch (Exception ex)
  476. {
  477. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "同步分润队列数据异常");
  478. }
  479. }
  480. }
  481. else
  482. {
  483. Thread.Sleep(1000);
  484. }
  485. }
  486. db.Dispose();
  487. }
  488. public void StartSubsidy()
  489. {
  490. Thread th = new Thread(StartSubsidyDo);
  491. th.IsBackground = true;
  492. th.Start();
  493. }
  494. public void StartSubsidyDo()
  495. {
  496. WebCMSEntities db = new WebCMSEntities();
  497. bool op = true;
  498. while (op)
  499. {
  500. string content = RedisDbconn.Instance.RPop<string>("DoSubsidyProfitQueue");
  501. if (!string.IsNullOrEmpty(content))
  502. {
  503. if(content == "end")
  504. {
  505. op = false;
  506. }
  507. else
  508. {
  509. try
  510. {
  511. string[] data = content.Split('|');
  512. int UserId = int.Parse(data[0]);
  513. int BrandId = int.Parse(data[1]);
  514. decimal ProfitMoney = decimal.Parse(data[2]);
  515. int index = int.Parse(data[3]);
  516. string IdBrand = UserId + "_" + BrandId;
  517. UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  518. if (MachineData == null)
  519. {
  520. MachineData = db.UserMachineData.Add(new UserMachineData()
  521. {
  522. IdBrand = IdBrand,
  523. }).Entity;
  524. db.SaveChanges();
  525. }
  526. MachineData.OtherProfit += ProfitMoney;
  527. db.SaveChanges();
  528. function.WriteLog(index.ToString() + "--" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "同步补贴队列数据");
  529. }
  530. catch (Exception ex)
  531. {
  532. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "同步补贴队列数据异常");
  533. }
  534. }
  535. }
  536. else
  537. {
  538. Thread.Sleep(1000);
  539. }
  540. }
  541. db.Dispose();
  542. }
  543. #region
  544. private bool CheckUser(WebCMSEntities db, int Id)
  545. {
  546. DateTime ExpireDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00").AddDays(-90); //创客认证超过90天比对时间
  547. //超过90天创客判断
  548. return db.Users.Any(m => m.Id == Id && m.AuthFlag == 1 && m.AuthDate < ExpireDate);
  549. }
  550. #endregion
  551. }
  552. }