SycnProfitServiceV2.cs 27 KB

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