SycnProfitServiceV2.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 IdBrand = UserId + "_" + BrandId;
  75. UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  76. if (MachineData == null)
  77. {
  78. MachineData = db.UserMachineData.Add(new UserMachineData()
  79. {
  80. IdBrand = IdBrand,
  81. }).Entity;
  82. db.SaveChanges();
  83. }
  84. MachineData.TradeProfit += ProfitMoney;
  85. db.SaveChanges();
  86. }
  87. catch (Exception ex)
  88. {
  89. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步到余额异常");
  90. }
  91. function.WriteLog(index.ToString(), "同步分润数据");
  92. }
  93. OtherMySqlConn.op("update ProfitRewardRecord set CheckStatus=1 where CheckStatus=0 and BrandId=" + BrandId + " and UserId>0 and TradeMonth='" + date + "'");
  94. db.Dispose();
  95. }
  96. private void DoTradeProfit2(int BrandId, string date, string SysUserName)
  97. {
  98. WebCMSEntities db = new WebCMSEntities();
  99. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  100. int startid = 0;
  101. bool op = true;
  102. while(op)
  103. {
  104. 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");
  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. OtherMySqlConn.dtable("update ProfitRecord set Version=1 where BrandId=" + BrandId + " and SeoTitle='" + date + "' and Version=0 and UserId>0");
  170. db.Dispose();
  171. }
  172. //分润补贴
  173. private void DoSubsidyProfit(int BrandId, string date)
  174. {
  175. WebCMSEntities db = new WebCMSEntities();
  176. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  177. 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");
  178. DataTable dt = OtherMySqlConn.dtable("select SubsidyUserId,sum(SubsidyProfitRate) from ProfitSubsidyDetail where Status=0 and BrandId=" + BrandId + " and TradeMonth='" + date + "' GROUP BY SubsidyUserId");
  179. function.WriteLog("补贴:" + BrandId + ":" + dt.Rows.Count, "同步补贴数据");
  180. int index = 0;
  181. foreach (DataRow dr in dt.Rows)
  182. {
  183. index += 1;
  184. int UserId = int.Parse(dr["SubsidyUserId"].ToString());
  185. decimal ProfitMoney = decimal.Parse(dr[1].ToString());
  186. try
  187. {
  188. string content = UserId + "|" + BrandId + "|" + ProfitMoney + "|" + index;
  189. RedisDbconn.Instance.AddList("DoSubsidyProfitQueue", content);
  190. }
  191. catch (Exception ex)
  192. {
  193. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步到补贴异常");
  194. }
  195. }
  196. OtherMySqlConn.dtable("update ProfitSubsidyDetail set Status=1 where Status=0 and BrandId=" + BrandId + " and TradeMonth='" + date + "'");
  197. db.Dispose();
  198. }
  199. private void DoSubsidyProfit2(int BrandId, string date)
  200. {
  201. WebCMSEntities db = new WebCMSEntities();
  202. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  203. int startid = 0;
  204. bool op = true;
  205. while(op)
  206. {
  207. DataTable dt = OtherMySqlConn.dtable("select Id,UserId,ProfitAmount from SubsidyRecord where Id>" + startid + " and BrandId=" + BrandId + " and SeoTitle='" + date + "' and Version=0 and UserId>0 order by Id limit 200");
  208. if(dt.Rows.Count > 0)
  209. {
  210. function.WriteLog("补贴:" + BrandId + ":" + dt.Rows.Count, "同步补贴数据");
  211. int index = 0;
  212. foreach (DataRow dr in dt.Rows)
  213. {
  214. index += 1;
  215. int UserId = int.Parse(dr["UserId"].ToString());
  216. decimal ProfitMoney = decimal.Parse(dr["ProfitAmount"].ToString());
  217. var tran = db.Database.BeginTransaction();
  218. try
  219. {
  220. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  221. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  222. if (account == null)
  223. {
  224. account = db.UserAccount.Add(new UserAccount()
  225. {
  226. Id = UserId,
  227. UserId = UserId,
  228. }).Entity;
  229. db.SaveChanges();
  230. }
  231. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  232. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  233. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  234. account.BalanceAmount += ProfitMoney;
  235. account.TotalAmount += ProfitMoney;
  236. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  237. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  238. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  239. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  240. {
  241. CreateDate = DateTime.Now,
  242. UpdateDate = DateTime.Now,
  243. UserId = UserId, //创客
  244. ProductType = BrandId,
  245. ChangeType = 111, //变动类型
  246. ChangeAmount = ProfitMoney, //变更金额
  247. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  248. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  249. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  250. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  251. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  252. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  253. Remark = "直拓商户补贴",
  254. }).Entity;
  255. db.SaveChanges();
  256. tran.Commit();
  257. }
  258. catch (Exception ex)
  259. {
  260. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步到补贴异常");
  261. tran.Rollback();
  262. }
  263. startid = int.Parse(dr["Id"].ToString());
  264. function.WriteLog(index.ToString(), "同步补贴数据");
  265. }
  266. }
  267. else
  268. {
  269. op = false;
  270. }
  271. }
  272. OtherMySqlConn.dtable("update SubsidyRecord set Version=1 where BrandId=" + BrandId + " and SeoTitle='" + date + "' and Version=0 and UserId>0");
  273. db.Dispose();
  274. }
  275. #region 助力宝分润展示
  276. public void DoHelpProfit(string month)
  277. {
  278. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  279. OtherMySqlConn.dtable("update HelpProfitReward set Status=1 where Status=0 and TradeMonth='" + month + "'");
  280. }
  281. #endregion
  282. #region 助力宝分润到账
  283. public void DoHelpProfit2(string month)
  284. {
  285. WebCMSEntities db = new WebCMSEntities();
  286. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  287. DataTable dt = OtherMySqlConn.dtable("select UserId,RewardType,sum(CreditRewardAmount),sum(CreditTradeAmt) from HelpProfitReward where Status=1 and TradeMonth='" + month + "' GROUP BY UserId,RewardType");
  288. function.WriteLog("数量:" + dt.Rows.Count, "同步助力宝分润数据");
  289. int index = 0;
  290. foreach (DataRow dr in dt.Rows)
  291. {
  292. index += 1;
  293. int UserId = int.Parse(dr["UserId"].ToString());
  294. int RewardType = int.Parse(dr["RewardType"].ToString());
  295. decimal ProfitMoney = decimal.Parse(dr[2].ToString());
  296. decimal TradeAmt = decimal.Parse(dr[3].ToString());
  297. var tran = db.Database.BeginTransaction();
  298. try
  299. {
  300. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  301. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  302. if (account == null)
  303. {
  304. account = db.UserAccount.Add(new UserAccount()
  305. {
  306. Id = UserId,
  307. UserId = UserId,
  308. }).Entity;
  309. db.SaveChanges();
  310. }
  311. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  312. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  313. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  314. account.BalanceAmount += ProfitMoney;
  315. account.TotalAmount += ProfitMoney;
  316. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  317. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  318. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  319. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  320. {
  321. CreateDate = DateTime.Now,
  322. UpdateDate = DateTime.Now,
  323. UserId = UserId, //创客
  324. ProductType = 101,
  325. ChangeType = RewardType, //变动类型
  326. ChangeAmount = ProfitMoney, //变更金额
  327. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  328. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  329. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  330. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  331. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  332. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  333. Remark = user.RealName.Substring(0, 1) + "**:" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + "交易" + TradeAmt.ToString("f2"),
  334. }).Entity;
  335. db.SaveChanges();
  336. tran.Commit();
  337. }
  338. catch (Exception ex)
  339. {
  340. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步助利宝分润异常");
  341. tran.Rollback();
  342. }
  343. function.WriteLog(index.ToString(), "同步补贴数据");
  344. }
  345. OtherMySqlConn.dtable("update HelpProfitReward set Status=2 where Status=1 and TradeMonth='" + month + "'");
  346. db.Dispose();
  347. }
  348. #endregion
  349. public void StartSubsidy()
  350. {
  351. Thread th = new Thread(StartSubsidyDo);
  352. th.IsBackground = true;
  353. th.Start();
  354. }
  355. public void StartSubsidyDo()
  356. {
  357. while (true)
  358. {
  359. string content = RedisDbconn.Instance.RPop<string>("DoSubsidyProfitQueue");
  360. if (!string.IsNullOrEmpty(content))
  361. {
  362. try
  363. {
  364. string[] data = content.Split('|');
  365. int UserId = int.Parse(data[0]);
  366. int BrandId = int.Parse(data[1]);
  367. decimal ProfitMoney = decimal.Parse(data[2]);
  368. int index = int.Parse(data[3]);
  369. WebCMSEntities db = new WebCMSEntities();
  370. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  371. string IdBrand = UserId + "_" + BrandId;
  372. UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  373. if (MachineData == null)
  374. {
  375. MachineData = db.UserMachineData.Add(new UserMachineData()
  376. {
  377. IdBrand = IdBrand,
  378. }).Entity;
  379. db.SaveChanges();
  380. }
  381. MachineData.OtherProfit += ProfitMoney;
  382. db.SaveChanges();
  383. function.WriteLog(index.ToString() + "--" + DateTime.Now.ToString(), "同步补贴数据");
  384. }
  385. catch (Exception ex)
  386. {
  387. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "同步补贴队列数据异常");
  388. }
  389. }
  390. else
  391. {
  392. Thread.Sleep(1000);
  393. }
  394. }
  395. }
  396. }
  397. }