SycnProfitServiceV2.cs 30 KB

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