SycnProfitServiceV2.cs 25 KB

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