SycnProfitServiceV2.cs 25 KB

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