SycnProfitServiceV3.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 SycnProfitServiceV3
  11. {
  12. public readonly static SycnProfitServiceV3 Instance = new SycnProfitServiceV3();
  13. private SycnProfitServiceV3()
  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>("SycnProfitQueue2");
  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. if(BrandId == 1)
  46. {
  47. DoUserProfit(date);
  48. DoUserServiceFee();
  49. //扣过期机具费
  50. // RedisDbconn.Instance.AddList("DoChargeAmountQueue", "0");
  51. }
  52. }
  53. function.WriteLog(DateTime.Now.ToString() + "\r\n\r\n", "同步分润数据");
  54. }
  55. catch (Exception ex)
  56. {
  57. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "同步分润数据异常");
  58. }
  59. }
  60. else
  61. {
  62. Thread.Sleep(60000);
  63. }
  64. }
  65. }
  66. private void DoTradeProfit(int BrandId, string date, string SysUserName)
  67. {
  68. WebCMSEntities db = new WebCMSEntities();
  69. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  70. string StartId = function.ReadInstance("/PublicParams/ProfitRewardRecordId" + date + ".txt");
  71. if(string.IsNullOrEmpty(StartId))
  72. {
  73. StartId = "0";
  74. }
  75. 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");
  76. 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");
  77. function.WriteLog("分润:" + BrandId + ":" + dt.Rows.Count, "同步分润数据");
  78. int index = 0;
  79. foreach (DataRow dr in dt.Rows)
  80. {
  81. index += 1;
  82. int UserId = int.Parse(dr["UserId"].ToString());
  83. decimal ProfitMoney = decimal.Parse(dr[1].ToString());
  84. try
  85. {
  86. string content = UserId + "|" + BrandId + "|" + ProfitMoney + "|" + index;
  87. RedisDbconn.Instance.AddList("DoTradeProfitQueue", content);
  88. }
  89. catch (Exception ex)
  90. {
  91. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步到余额异常");
  92. }
  93. function.WriteLog(index.ToString(), "同步分润数据");
  94. }
  95. OtherMySqlConn.op("update ProfitRewardRecord set CheckStatus=1 where Id>=" + StartId + " and CheckStatus=0 and BrandId=" + BrandId + " and UserId>0 and TradeMonth='" + date + "'");
  96. db.Dispose();
  97. dt.Dispose();
  98. RedisDbconn.Instance.AddList("DoTradeProfitQueue", "end");
  99. SycnProfitServiceV2.Instance.StartProfit();
  100. }
  101. private void DoTradeProfit2(int BrandId, string date, string SysUserName)
  102. {
  103. WebCMSEntities db = new WebCMSEntities();
  104. string conn = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  105. CustomerSqlConn.op("insert into UserAccountRecord (CreateDate,UpdateDate,UserId,ProductType,ChangeType,ChangeAmount,BeforeTotalAmount,AfterTotalAmount,BeforeFreezeAmount,AfterFreezeAmount,BeforeBalanceAmount,AfterBalanceAmount,Remark) select tb.CreateDate,tb.UpdateDate,tb.UserId,ProductType,ChangeType,ProfitAmount,a.TotalAmount BeforeTotalAmount,a.TotalAmount+ProfitAmount AfterTotalAmount,a.FreezeAmount BeforeFreezeAmount,a.FreezeAmount AfterFreezeAmount,a.BalanceAmount BeforeBalanceAmount,a.BalanceAmount+ProfitAmount AfterBalanceAmount,Remark from (select now() CreateDate,now() UpdateDate,UserId," + BrandId + " ProductType,1 ChangeType,ProfitAmount,(case when DirectFlag=1 then '直拓商户分润' else '品牌推广服务费' end) Remark from ProfitRecord where BrandId=" + BrandId + " and SeoTitle='" + date + "' and Version=0 and UserId>0 order by Id) tb left join UserAccount a on tb.UserId=a.Id", conn);
  106. CustomerSqlConn.op("update ProfitRecord set Version=1 where BrandId=" + BrandId + " and SeoTitle='" + date + "' and Version=0 and UserId>0", conn);
  107. db.Dispose();
  108. }
  109. //分润补贴
  110. private void DoSubsidyProfit(int BrandId, string date)
  111. {
  112. WebCMSEntities db = new WebCMSEntities();
  113. string conn = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  114. string StartId = function.ReadInstance("/PublicParams/ProfitSubsidyDetailId" + date + ".txt");
  115. if(string.IsNullOrEmpty(StartId))
  116. {
  117. StartId = "0";
  118. }
  119. 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);
  120. 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);
  121. function.WriteLog("补贴:" + BrandId + ":" + dt.Rows.Count, "同步补贴数据");
  122. int index = 0;
  123. foreach (DataRow dr in dt.Rows)
  124. {
  125. index += 1;
  126. int UserId = int.Parse(dr["SubsidyUserId"].ToString());
  127. decimal ProfitMoney = decimal.Parse(dr[1].ToString());
  128. try
  129. {
  130. string content = UserId + "|" + BrandId + "|" + ProfitMoney + "|" + index;
  131. RedisDbconn.Instance.AddList("DoSubsidyProfitQueue", content);
  132. }
  133. catch (Exception ex)
  134. {
  135. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步到补贴异常");
  136. }
  137. }
  138. CustomerSqlConn.op("update ProfitSubsidyDetail set Status=1 where Id>=" + StartId + " and Status=0 and BrandId=" + BrandId + " and TradeMonth='" + date + "'", conn);
  139. db.Dispose();
  140. dt.Dispose();
  141. RedisDbconn.Instance.AddList("DoSubsidyProfitQueue", "end");
  142. SycnProfitServiceV2.Instance.StartSubsidy();
  143. }
  144. private void DoSubsidyProfit2(int BrandId, string date)
  145. {
  146. WebCMSEntities db = new WebCMSEntities();
  147. string conn = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  148. CustomerSqlConn.op("insert into UserAccountRecord (CreateDate,UpdateDate,UserId,ProductType,ChangeType,ChangeAmount,BeforeTotalAmount,AfterTotalAmount,BeforeFreezeAmount,AfterFreezeAmount,BeforeBalanceAmount,AfterBalanceAmount,Remark) select tb.CreateDate,tb.UpdateDate,tb.UserId,ProductType,ChangeType,ProfitAmount,a.TotalAmount BeforeTotalAmount,a.TotalAmount+ProfitAmount AfterTotalAmount,a.FreezeAmount BeforeFreezeAmount,a.FreezeAmount AfterFreezeAmount,a.BalanceAmount BeforeBalanceAmount,a.BalanceAmount+ProfitAmount AfterBalanceAmount,Remark from (select now() CreateDate,now() UpdateDate,UserId," + BrandId + " ProductType,111 ChangeType,ProfitAmount,'直拓商户补贴' Remark from SubsidyRecord where BrandId=" + BrandId + " and SeoTitle='" + date + "' and Status=0 and UserId>0 order by Id) tb left join UserAccount a on tb.UserId=a.Id", conn);
  149. CustomerSqlConn.op("update SubsidyRecord set Status=1 where BrandId=" + BrandId + " and SeoTitle='" + date + "' and Status=0 and UserId>0", conn);
  150. db.Dispose();
  151. }
  152. #region 助力宝分润展示
  153. public void DoHelpProfit(string month)
  154. {
  155. string conn = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  156. CustomerSqlConn.dtable("update HelpProfitReward set Status=1 where Status=0 and TradeMonth='" + month + "'", conn);
  157. }
  158. #endregion
  159. #region 助力宝分润到账
  160. public void DoHelpProfit2(string month)
  161. {
  162. WebCMSEntities db = new WebCMSEntities();
  163. string conn = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  164. DataTable dt = CustomerSqlConn.dtable("select UserId,RewardType,sum(CreditRewardAmount),sum(CreditTradeAmt) from HelpProfitReward where Status=1 and TradeMonth='" + month + "' GROUP BY UserId,RewardType", conn);
  165. function.WriteLog("数量:" + dt.Rows.Count, "同步助力宝分润数据");
  166. int index = 0;
  167. foreach (DataRow dr in dt.Rows)
  168. {
  169. index += 1;
  170. int UserId = int.Parse(dr["UserId"].ToString());
  171. int RewardType = int.Parse(dr["RewardType"].ToString());
  172. decimal ProfitMoney = decimal.Parse(dr[2].ToString());
  173. decimal TradeAmt = decimal.Parse(dr[3].ToString());
  174. var tran = db.Database.BeginTransaction();
  175. try
  176. {
  177. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  178. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  179. if (account == null)
  180. {
  181. account = db.UserAccount.Add(new UserAccount()
  182. {
  183. Id = UserId,
  184. UserId = UserId,
  185. }).Entity;
  186. db.SaveChanges();
  187. }
  188. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  189. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  190. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  191. account.BalanceAmount += ProfitMoney;
  192. account.TotalAmount += ProfitMoney;
  193. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  194. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  195. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  196. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  197. {
  198. CreateDate = DateTime.Now,
  199. UpdateDate = DateTime.Now,
  200. UserId = UserId, //创客
  201. ProductType = 101,
  202. ChangeType = RewardType, //变动类型
  203. ChangeAmount = ProfitMoney, //变更金额
  204. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  205. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  206. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  207. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  208. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  209. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  210. Remark = user.RealName.Substring(0, 1) + "**:" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + "交易" + TradeAmt.ToString("f2"),
  211. }).Entity;
  212. db.SaveChanges();
  213. tran.Commit();
  214. }
  215. catch (Exception ex)
  216. {
  217. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步助利宝分润异常");
  218. tran.Rollback();
  219. }
  220. function.WriteLog(index.ToString(), "同步补贴数据");
  221. }
  222. OtherMySqlConn.dtable("update HelpProfitReward set Status=2 where Status=1 and TradeMonth='" + month + "'");
  223. db.Dispose();
  224. }
  225. #endregion
  226. #region 分润结果临时数据
  227. private void DoUserProfit(string date)
  228. {
  229. string conn = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  230. CustomerSqlConn.op("delete from UserProfit;insert into UserProfit (Id,UserId,ProfitRate) select UserId,UserId,sum(ProfitAmount) from (select UserId,ProfitAmount from ProfitRecord where SeoTitle='" + date + "' and Version=1 and UserId>0 union all select UserId,ProfitAmount from SubsidyRecord where SeoTitle='" + date + "' and Status=1 and UserId>0) tb group by UserId", conn);
  231. }
  232. #endregion
  233. #region 扣创客服务费
  234. private void DoUserServiceFee()
  235. {
  236. string conn = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  237. CustomerSqlConn.op("insert into UserAccount (Id,UserId) select Id,Id from UserProfit where Id not in (select Id from UserAccount)", conn);
  238. CustomerSqlConn.op("update UserAccount a set BalanceAmount=BalanceAmount+(select ProfitRate from UserProfit where Id=a.Id),TotalAmount=TotalAmount+(select ProfitRate from UserProfit where Id=a.Id) where Id in (select Id from UserProfit)", conn);
  239. string Month = DateTime.Now.AddDays(-90).ToString("yyyy-MM");
  240. CustomerSqlConn.op("insert into UserAccountRecord (CreateDate,UpdateDate,UserId,ChangeType,ChangeAmount,BeforeTotalAmount,AfterTotalAmount,BeforeFreezeAmount,AfterFreezeAmount,BeforeBalanceAmount,AfterBalanceAmount) select now(),now(),Id,125,BalanceAmount,TotalAmount BeforeTotalAmount,TotalAmount AfterTotalAmount,FreezeAmount,FreezeAmount,BalanceAmount BeforeBalanceAmount,BalanceAmount-BalanceAmount AfterBalanceAmount from UserAccount where Id in (select Id from Users where AuthFlag=1 and AuthDate<'" + Month + "-01 00:00:00') and BalanceAmount>0 and BalanceAmount<10", conn);
  241. CustomerSqlConn.op("insert into UserAccountRecord (CreateDate,UpdateDate,UserId,ChangeType,ChangeAmount,BeforeTotalAmount,AfterTotalAmount,BeforeFreezeAmount,AfterFreezeAmount,BeforeBalanceAmount,AfterBalanceAmount) select now(),now(),Id,125,10,TotalAmount BeforeTotalAmount,TotalAmount AfterTotalAmount,FreezeAmount,FreezeAmount,BalanceAmount BeforeBalanceAmount,BalanceAmount-10 AfterBalanceAmount from UserAccount where Id in (select Id from Users where AuthFlag=1 and AuthDate<'" + Month + "-01 00:00:00') and BalanceAmount>=10;", conn);
  242. CustomerSqlConn.op("update UserAccount a set BalanceAmount=0 where Id in (select Id from Users where AuthFlag=1 and AuthDate<'" + Month + "-01 00:00:00') and BalanceAmount>0 and BalanceAmount<10", conn);
  243. CustomerSqlConn.op("update UserAccount a set BalanceAmount=BalanceAmount-10 where Id in (select Id from Users where AuthFlag=1 and AuthDate<'" + Month + "-01 00:00:00') and BalanceAmount>=10", conn);
  244. }
  245. #endregion
  246. #region 扣过期机具费
  247. private void DoExpiredPosFee()
  248. {
  249. string conn = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  250. string checck = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00").AddDays(-60).ToString("yyyy-MM-dd HH:mm:ss");
  251. DataTable dt = CustomerSqlConn.dtable("select DISTINCT BuyUserId from PosMachinesTwo where RecycEndDate<'" + checck + "' and Status>-1 and BindingState=0 and ScanQrTrade<999", conn);
  252. foreach(DataRow dr in dt.Rows)
  253. {
  254. RedisDbconn.Instance.AddList("PosExpiredPayQueue", dr["BuyUserId"].ToString());
  255. }
  256. }
  257. #endregion
  258. public void StartProfit()
  259. {
  260. Thread th = new Thread(StartProfitDo);
  261. th.IsBackground = true;
  262. th.Start();
  263. }
  264. public void StartProfitDo()
  265. {
  266. WebCMSEntities db = new WebCMSEntities();
  267. bool op = true;
  268. while (op)
  269. {
  270. string content = RedisDbconn.Instance.RPop<string>("DoTradeProfitQueue");
  271. if (!string.IsNullOrEmpty(content))
  272. {
  273. if(content == "end")
  274. {
  275. op = false;
  276. }
  277. else
  278. {
  279. try
  280. {
  281. string[] data = content.Split('|');
  282. int UserId = int.Parse(data[0]);
  283. int BrandId = int.Parse(data[1]);
  284. decimal ProfitMoney = decimal.Parse(data[2]);
  285. int index = int.Parse(data[3]);
  286. string IdBrand = UserId + "_" + BrandId;
  287. UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  288. if (MachineData == null)
  289. {
  290. MachineData = db.UserMachineData.Add(new UserMachineData()
  291. {
  292. IdBrand = IdBrand,
  293. }).Entity;
  294. db.SaveChanges();
  295. }
  296. MachineData.TradeProfit += ProfitMoney;
  297. db.SaveChanges();
  298. function.WriteLog(index.ToString() + "--" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "同步分润队列数据");
  299. }
  300. catch (Exception ex)
  301. {
  302. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "同步分润队列数据异常");
  303. }
  304. }
  305. }
  306. else
  307. {
  308. Thread.Sleep(1000);
  309. }
  310. }
  311. db.Dispose();
  312. }
  313. public void StartSubsidy()
  314. {
  315. Thread th = new Thread(StartSubsidyDo);
  316. th.IsBackground = true;
  317. th.Start();
  318. }
  319. public void StartSubsidyDo()
  320. {
  321. WebCMSEntities db = new WebCMSEntities();
  322. bool op = true;
  323. while (op)
  324. {
  325. string content = RedisDbconn.Instance.RPop<string>("DoSubsidyProfitQueue");
  326. if (!string.IsNullOrEmpty(content))
  327. {
  328. if(content == "end")
  329. {
  330. op = false;
  331. }
  332. else
  333. {
  334. try
  335. {
  336. string[] data = content.Split('|');
  337. int UserId = int.Parse(data[0]);
  338. int BrandId = int.Parse(data[1]);
  339. decimal ProfitMoney = decimal.Parse(data[2]);
  340. int index = int.Parse(data[3]);
  341. string IdBrand = UserId + "_" + BrandId;
  342. UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  343. if (MachineData == null)
  344. {
  345. MachineData = db.UserMachineData.Add(new UserMachineData()
  346. {
  347. IdBrand = IdBrand,
  348. }).Entity;
  349. db.SaveChanges();
  350. }
  351. MachineData.OtherProfit += ProfitMoney;
  352. db.SaveChanges();
  353. function.WriteLog(index.ToString() + "--" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "同步补贴队列数据");
  354. }
  355. catch (Exception ex)
  356. {
  357. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "同步补贴队列数据异常");
  358. }
  359. }
  360. }
  361. else
  362. {
  363. Thread.Sleep(1000);
  364. }
  365. }
  366. db.Dispose();
  367. }
  368. #region
  369. private bool CheckUser(WebCMSEntities db, int Id)
  370. {
  371. DateTime ExpireDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00").AddDays(-90); //创客认证超过90天比对时间
  372. //超过90天创客判断
  373. return db.Users.Any(m => m.Id == Id && m.AuthFlag == 1 && m.AuthDate < ExpireDate);
  374. }
  375. #endregion
  376. }
  377. }