SycnProfitServiceV2.cs 25 KB

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