SycnProfitServiceV2.cs 24 KB

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