SycnProfitServiceV3.cs 23 KB

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