SycnProfitServiceV2.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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 add = 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. PublicFunction.SplitUserAccountRecord(add);
  171. }
  172. catch (Exception ex)
  173. {
  174. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitAmount + "\n" + ex.ToString(), "同步到余额异常");
  175. tran.Rollback();
  176. }
  177. startid = int.Parse(dr["Id"].ToString());
  178. function.WriteLog(index.ToString(), "同步分润数据");
  179. }
  180. foreach(int UserId in UserIds)
  181. {
  182. RedisDbconn.Instance.AddList("UserMonthFeeQueue", UserId.ToString());
  183. }
  184. }
  185. else
  186. {
  187. op = false;
  188. }
  189. }
  190. CustomerSqlConn.op("update ProfitRecord set Version=1 where BrandId=" + BrandId + " and SeoTitle='" + date + "' and Version=0 and UserId>0", conn);
  191. db.Dispose();
  192. }
  193. //分润补贴
  194. private void DoSubsidyProfit(int BrandId, string date)
  195. {
  196. WebCMSEntities db = new WebCMSEntities();
  197. string conn = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  198. string StartId = function.ReadInstance("/PublicParams/ProfitSubsidyDetailId" + date + ".txt");
  199. if(string.IsNullOrEmpty(StartId))
  200. {
  201. StartId = "0";
  202. }
  203. 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);
  204. 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);
  205. function.WriteLog("补贴:" + BrandId + ":" + dt.Rows.Count, "同步补贴数据");
  206. int index = 0;
  207. foreach (DataRow dr in dt.Rows)
  208. {
  209. index += 1;
  210. int UserId = int.Parse(dr["SubsidyUserId"].ToString());
  211. decimal ProfitMoney = decimal.Parse(dr[1].ToString());
  212. try
  213. {
  214. string content = UserId + "|" + BrandId + "|" + ProfitMoney + "|" + index;
  215. RedisDbconn.Instance.AddList("DoSubsidyProfitQueue", content);
  216. }
  217. catch (Exception ex)
  218. {
  219. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步到补贴异常");
  220. }
  221. }
  222. CustomerSqlConn.op("update ProfitSubsidyDetail set Status=1 where Id>=" + StartId + " and Status=0 and BrandId=" + BrandId + " and TradeMonth='" + date + "'", conn);
  223. db.Dispose();
  224. dt.Dispose();
  225. RedisDbconn.Instance.AddList("DoSubsidyProfitQueue", "end");
  226. SycnProfitServiceV2.Instance.StartSubsidy();
  227. }
  228. private void DoSubsidyProfit2(int BrandId, string date)
  229. {
  230. WebCMSEntities db = new WebCMSEntities();
  231. string conn = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  232. int startid = 0;
  233. bool op = true;
  234. while(op)
  235. {
  236. 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);
  237. if(dt.Rows.Count > 0)
  238. {
  239. function.WriteLog("补贴:" + BrandId + ":" + dt.Rows.Count, "同步补贴数据");
  240. int index = 0;
  241. foreach (DataRow dr in dt.Rows)
  242. {
  243. index += 1;
  244. int UserId = int.Parse(dr["UserId"].ToString());
  245. decimal ProfitMoney = decimal.Parse(dr["ProfitAmount"].ToString());
  246. var tran = db.Database.BeginTransaction();
  247. try
  248. {
  249. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  250. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  251. if (account == null)
  252. {
  253. account = db.UserAccount.Add(new UserAccount()
  254. {
  255. Id = UserId,
  256. UserId = UserId,
  257. }).Entity;
  258. db.SaveChanges();
  259. }
  260. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  261. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  262. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  263. decimal AfterTotalAmount = account.TotalAmount + ProfitMoney; //变更后总金额
  264. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  265. decimal AfterBalanceAmount = account.BalanceAmount + ProfitMoney; //变更后余额
  266. UserAccountRecord add = db.UserAccountRecord.Add(new UserAccountRecord()
  267. {
  268. CreateDate = DateTime.Now,
  269. UpdateDate = DateTime.Now,
  270. UserId = UserId, //创客
  271. ProductType = BrandId,
  272. ChangeType = 111, //变动类型
  273. ChangeAmount = ProfitMoney, //变更金额
  274. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  275. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  276. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  277. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  278. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  279. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  280. Remark = "直拓商户补贴",
  281. }).Entity;
  282. UserProfit profit = db.UserProfit.FirstOrDefault(m => m.Id == UserId);
  283. if (profit == null)
  284. {
  285. profit = db.UserProfit.Add(new UserProfit()
  286. {
  287. Id = UserId,
  288. UserId = UserId,
  289. }).Entity;
  290. db.SaveChanges();
  291. }
  292. profit.ProfitRate += ProfitMoney;
  293. db.SaveChanges();
  294. tran.Commit();
  295. PublicFunction.SplitUserAccountRecord(add);
  296. }
  297. catch (Exception ex)
  298. {
  299. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步到补贴异常");
  300. tran.Rollback();
  301. }
  302. startid = int.Parse(dr["Id"].ToString());
  303. function.WriteLog(index.ToString(), "同步补贴数据");
  304. }
  305. }
  306. else
  307. {
  308. op = false;
  309. }
  310. }
  311. CustomerSqlConn.op("update SubsidyRecord set Status=1 where BrandId=" + BrandId + " and SeoTitle='" + date + "' and Status=0 and UserId>0", conn);
  312. db.Dispose();
  313. }
  314. #region 助力宝分润展示
  315. public void DoHelpProfit(string month)
  316. {
  317. string conn = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  318. CustomerSqlConn.dtable("update HelpProfitReward set Status=1 where Status=0 and TradeMonth='" + month + "'", conn);
  319. }
  320. #endregion
  321. #region 助力宝分润到账
  322. public void DoHelpProfit2(string month)
  323. {
  324. WebCMSEntities db = new WebCMSEntities();
  325. string conn = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  326. DataTable dt = CustomerSqlConn.dtable("select UserId,RewardType,sum(CreditRewardAmount),sum(CreditTradeAmt) from HelpProfitReward where Status=1 and TradeMonth='" + month + "' GROUP BY UserId,RewardType", conn);
  327. function.WriteLog("数量:" + dt.Rows.Count, "同步助力宝分润数据");
  328. int index = 0;
  329. foreach (DataRow dr in dt.Rows)
  330. {
  331. index += 1;
  332. int UserId = int.Parse(dr["UserId"].ToString());
  333. int RewardType = int.Parse(dr["RewardType"].ToString());
  334. decimal ProfitMoney = decimal.Parse(dr[2].ToString());
  335. decimal TradeAmt = decimal.Parse(dr[3].ToString());
  336. var tran = db.Database.BeginTransaction();
  337. try
  338. {
  339. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  340. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  341. if (account == null)
  342. {
  343. account = db.UserAccount.Add(new UserAccount()
  344. {
  345. Id = UserId,
  346. UserId = UserId,
  347. }).Entity;
  348. db.SaveChanges();
  349. }
  350. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  351. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  352. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  353. account.BalanceAmount += ProfitMoney;
  354. account.TotalAmount += ProfitMoney;
  355. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  356. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  357. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  358. UserAccountRecord add = db.UserAccountRecord.Add(new UserAccountRecord()
  359. {
  360. CreateDate = DateTime.Now,
  361. UpdateDate = DateTime.Now,
  362. UserId = UserId, //创客
  363. ProductType = 101,
  364. ChangeType = RewardType, //变动类型
  365. ChangeAmount = ProfitMoney, //变更金额
  366. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  367. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  368. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  369. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  370. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  371. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  372. Remark = user.RealName.Substring(0, 1) + "**:" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + "交易" + TradeAmt.ToString("f2"),
  373. }).Entity;
  374. db.SaveChanges();
  375. tran.Commit();
  376. PublicFunction.SplitUserAccountRecord(add);
  377. }
  378. catch (Exception ex)
  379. {
  380. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步助利宝分润异常");
  381. tran.Rollback();
  382. }
  383. function.WriteLog(index.ToString(), "同步补贴数据");
  384. }
  385. OtherMySqlConn.dtable("update HelpProfitReward set Status=2 where Status=1 and TradeMonth='" + month + "'");
  386. db.Dispose();
  387. }
  388. #endregion
  389. #region 扣创客服务费
  390. private void DoUserServiceFee()
  391. {
  392. WebCMSEntities db = new WebCMSEntities();
  393. int startId = 0;
  394. bool op = true;
  395. while(op)
  396. {
  397. var list = db.UserProfit.Select(m => new { m.Id, m.ProfitRate }).Where(m => m.Id > startId).OrderBy(m => m.Id).Take(50).ToList();
  398. if(list.Count > 0)
  399. {
  400. foreach(var item in list)
  401. {
  402. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == item.Id);
  403. if (account == null)
  404. {
  405. account = db.UserAccount.Add(new UserAccount()
  406. {
  407. Id = item.Id,
  408. UserId = item.Id,
  409. }).Entity;
  410. db.SaveChanges();
  411. }
  412. account.BalanceAmount += item.ProfitRate;
  413. account.TotalAmount += item.ProfitRate;
  414. db.SaveChanges();
  415. if(CheckUser(db, item.Id))
  416. {
  417. RedisDbconn.Instance.AddList("UserMonthFeeQueue", item.Id.ToString());
  418. }
  419. else
  420. {
  421. RedisDbconn.Instance.AddList("PosExpiredPayQueue", item.Id.ToString());
  422. }
  423. startId = item.Id;
  424. }
  425. list.Clear();
  426. Thread.Sleep(100);
  427. }
  428. else
  429. {
  430. op = false;
  431. }
  432. }
  433. db.Dispose();
  434. }
  435. #endregion
  436. public void StartProfit()
  437. {
  438. Thread th = new Thread(StartProfitDo);
  439. th.IsBackground = true;
  440. th.Start();
  441. }
  442. public void StartProfitDo()
  443. {
  444. WebCMSEntities db = new WebCMSEntities();
  445. bool op = true;
  446. while (op)
  447. {
  448. string content = RedisDbconn.Instance.RPop<string>("DoTradeProfitQueue");
  449. if (!string.IsNullOrEmpty(content))
  450. {
  451. if(content == "end")
  452. {
  453. op = false;
  454. }
  455. else
  456. {
  457. try
  458. {
  459. string[] data = content.Split('|');
  460. int UserId = int.Parse(data[0]);
  461. int BrandId = int.Parse(data[1]);
  462. decimal ProfitMoney = decimal.Parse(data[2]);
  463. int index = int.Parse(data[3]);
  464. string IdBrand = UserId + "_" + BrandId;
  465. UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  466. if (MachineData == null)
  467. {
  468. MachineData = db.UserMachineData.Add(new UserMachineData()
  469. {
  470. IdBrand = IdBrand,
  471. }).Entity;
  472. db.SaveChanges();
  473. }
  474. MachineData.TradeProfit += ProfitMoney;
  475. db.SaveChanges();
  476. function.WriteLog(index.ToString() + "--" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "同步分润队列数据");
  477. }
  478. catch (Exception ex)
  479. {
  480. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "同步分润队列数据异常");
  481. }
  482. }
  483. }
  484. else
  485. {
  486. Thread.Sleep(1000);
  487. }
  488. }
  489. db.Dispose();
  490. }
  491. public void StartSubsidy()
  492. {
  493. Thread th = new Thread(StartSubsidyDo);
  494. th.IsBackground = true;
  495. th.Start();
  496. }
  497. public void StartSubsidyDo()
  498. {
  499. WebCMSEntities db = new WebCMSEntities();
  500. bool op = true;
  501. while (op)
  502. {
  503. string content = RedisDbconn.Instance.RPop<string>("DoSubsidyProfitQueue");
  504. if (!string.IsNullOrEmpty(content))
  505. {
  506. if(content == "end")
  507. {
  508. op = false;
  509. }
  510. else
  511. {
  512. try
  513. {
  514. string[] data = content.Split('|');
  515. int UserId = int.Parse(data[0]);
  516. int BrandId = int.Parse(data[1]);
  517. decimal ProfitMoney = decimal.Parse(data[2]);
  518. int index = int.Parse(data[3]);
  519. string IdBrand = UserId + "_" + BrandId;
  520. UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  521. if (MachineData == null)
  522. {
  523. MachineData = db.UserMachineData.Add(new UserMachineData()
  524. {
  525. IdBrand = IdBrand,
  526. }).Entity;
  527. db.SaveChanges();
  528. }
  529. MachineData.OtherProfit += ProfitMoney;
  530. db.SaveChanges();
  531. function.WriteLog(index.ToString() + "--" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "同步补贴队列数据");
  532. }
  533. catch (Exception ex)
  534. {
  535. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "同步补贴队列数据异常");
  536. }
  537. }
  538. }
  539. else
  540. {
  541. Thread.Sleep(1000);
  542. }
  543. }
  544. db.Dispose();
  545. }
  546. #region
  547. private bool CheckUser(WebCMSEntities db, int Id)
  548. {
  549. DateTime ExpireDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00").AddDays(-90); //创客认证超过90天比对时间
  550. //超过90天创客判断
  551. return db.Users.Any(m => m.Id == Id && m.AuthFlag == 1 && m.AuthDate < ExpireDate);
  552. }
  553. #endregion
  554. }
  555. }