SycnProfitServiceV2.cs 28 KB

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