StatHelpProfitService.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. using System;
  2. using System.Collections.Generic;
  3. using Library;
  4. using LitJson;
  5. using System.Linq;
  6. using System.Data;
  7. using System.Threading;
  8. using MySystem.PxcModels;
  9. namespace MySystem
  10. {
  11. public class StatHelpProfitService
  12. {
  13. public readonly static StatHelpProfitService Instance = new StatHelpProfitService();
  14. private StatHelpProfitService()
  15. { }
  16. // 统计交易额V2
  17. public void StartEverDayV2()
  18. {
  19. Thread th = new Thread(StartEverDayV2Do);
  20. th.IsBackground = true;
  21. th.Start();
  22. }
  23. public void StartEverDayV2Do()
  24. {
  25. while (true)
  26. {
  27. if (DateTime.Now.Hour >= 3)
  28. {
  29. StatTradeAmountEverDayV2();
  30. }
  31. Thread.Sleep(30000);
  32. }
  33. }
  34. public void StatTradeAmountEverDayV2()
  35. {
  36. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  37. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "实时执行助利宝交易额日志");
  38. WebCMSEntities db = new WebCMSEntities();
  39. using (var tran = db.Database.BeginTransaction())
  40. {
  41. try
  42. {
  43. string startId = function.ReadInstance("/TradeRecord/Id3.txt");
  44. if (string.IsNullOrEmpty(startId))
  45. {
  46. startId = "2750000";
  47. }
  48. DataTable idsDt = OtherMySqlConn.dtable("select Id from TradeRecord where Id>=" + startId + " and CreateDate>='2022-09-05 00:00:00' and MerchantId in (select MerchantId from HelpProfitMerIds) and SeoTitle='HelpProfit' and QueryCount>0 and QueryCount<3 order by Id limit 50");
  49. if (idsDt.Rows.Count > 0)
  50. {
  51. string ids = "";
  52. foreach (DataRow idsDr in idsDt.Rows)
  53. {
  54. ids += idsDr["Id"].ToString() + ",";
  55. startId = idsDr["Id"].ToString();
  56. }
  57. DataTable selfDt = OtherMySqlConn.dtable("select MerchantId,DATE_FORMAT(CreateDate,'%Y%m%d'),sum(TradeAmount) from TradeRecord where Id in (" + ids.TrimEnd(',') + ") group by MerchantId,DATE_FORMAT(CreateDate,'%Y%m%d')");
  58. if (selfDt.Rows.Count > 0)
  59. {
  60. function.WriteLog("统计人数:" + selfDt.Rows.Count + "\n\n", "实时执行助利宝交易额日志");
  61. foreach (DataRow selfDr in selfDt.Rows)
  62. {
  63. int MerchantId = int.Parse(selfDr["MerchantId"].ToString());
  64. string TradeDate = selfDr[1].ToString();
  65. string TradeMonth = TradeDate.Substring(0, 6);
  66. decimal TradeAmount = decimal.Parse(selfDr[2].ToString());
  67. HelpProfitMerIds merIds = db.HelpProfitMerIds.FirstOrDefault(m => m.MerchantId == MerchantId) ?? new HelpProfitMerIds();
  68. bool Check = CheckAmount(db, MerchantId, merIds.UserId, TradeMonth);
  69. if (Check)
  70. {
  71. HelpProfitMerTradeSummay selfStat = db.HelpProfitMerTradeSummay.FirstOrDefault(m => m.MerchantId == MerchantId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate);
  72. if (selfStat == null)
  73. {
  74. selfStat = db.HelpProfitMerTradeSummay.Add(new HelpProfitMerTradeSummay()
  75. {
  76. TradeMonth = TradeMonth,
  77. TradeDate = TradeDate,
  78. MerchantId = MerchantId,
  79. }).Entity;
  80. db.SaveChanges();
  81. }
  82. decimal MoreAmount = TradeAmount * 0.01M;
  83. int random = function.get_Random(0, 10000);
  84. // if(MoreAmount > 1 && random < 5000)
  85. // {
  86. MoreAmount = decimal.Parse(function.CheckInt(MoreAmount.ToString().Split(".")[0]));
  87. // }
  88. selfStat.TradeAmount += TradeAmount + MoreAmount;
  89. }
  90. }
  91. OtherMySqlConn.op("update TradeRecord set QueryCount=3 where Id in (" + ids.TrimEnd(',') + ")");
  92. function.WritePage("/TradeRecord/", "Id3.txt", startId);
  93. }
  94. db.SaveChanges();
  95. }
  96. tran.Commit();
  97. }
  98. catch (Exception ex)
  99. {
  100. tran.Rollback();
  101. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "实时执行助利宝交易额异常");
  102. }
  103. }
  104. db.Dispose();
  105. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "实时执行助利宝交易额日志");
  106. }
  107. public void StatTradeAmountTmp()
  108. {
  109. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  110. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "实时执行助利宝交易额日志");
  111. WebCMSEntities db = new WebCMSEntities();
  112. using (var tran = db.Database.BeginTransaction())
  113. {
  114. try
  115. {
  116. DataTable selfDt = OtherMySqlConn.dtable("select MerchantId,DATE_FORMAT(CreateDate,'%Y%m'),sum(TradeAmount) from TradeRecord where CreateDate>='2022-09-05 00:00:00' and MerchantId in (select MerchantId from HelpProfitMerIds) and SeoTitle='HelpProfit' group by MerchantId,DATE_FORMAT(CreateDate,'%Y%m') order by MerchantId,DATE_FORMAT(CreateDate,'%Y%m')");
  117. if (selfDt.Rows.Count > 0)
  118. {
  119. function.WriteLog("统计人数:" + selfDt.Rows.Count + "\n\n", "实时执行助利宝交易额日志");
  120. foreach (DataRow selfDr in selfDt.Rows)
  121. {
  122. int MerchantId = int.Parse(selfDr["MerchantId"].ToString());
  123. string TradeMonth = selfDr[1].ToString();
  124. decimal TradeAmount = decimal.Parse(selfDr[2].ToString());
  125. HelpProfitMerIds merIds = db.HelpProfitMerIds.FirstOrDefault(m => m.MerchantId == MerchantId) ?? new HelpProfitMerIds();
  126. HelpProfitAmountSummary profitStat = db.HelpProfitAmountSummary.FirstOrDefault(m => m.UserId == merIds.UserId && m.TradeMonth == TradeMonth);
  127. if (profitStat == null)
  128. {
  129. int Month = db.HelpProfitAmountSummary.Count(m => m.UserId == merIds.UserId && Convert.ToInt32(m.TradeMonth) <= Convert.ToInt32(TradeMonth));
  130. profitStat = db.HelpProfitAmountSummary.Add(new HelpProfitAmountSummary()
  131. {
  132. TradeMonth = TradeMonth,
  133. UserId = merIds.UserId,
  134. QueryCount = Month + 1,
  135. }).Entity;
  136. db.SaveChanges();
  137. }
  138. profitStat.TradeAmount += TradeAmount;
  139. }
  140. }
  141. db.SaveChanges();
  142. tran.Commit();
  143. }
  144. catch (Exception ex)
  145. {
  146. tran.Rollback();
  147. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "实时执行助利宝交易额2异常");
  148. }
  149. }
  150. db.Dispose();
  151. }
  152. // 统计创客商机台数
  153. public void StartUserTrade()
  154. {
  155. Thread th = new Thread(StartUserTradeDo);
  156. th.IsBackground = true;
  157. th.Start();
  158. }
  159. public void StartUserTradeDo()
  160. {
  161. while (true)
  162. {
  163. // if (DateTime.Now.Hour >= 3)
  164. // {
  165. StartUserTradeGo();
  166. // }
  167. Thread.Sleep(3000);
  168. }
  169. }
  170. public void StartUserTradeGo()
  171. {
  172. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  173. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "实时统计助利宝创客交易额日志");
  174. WebCMSEntities db = new WebCMSEntities();
  175. using (var tran = db.Database.BeginTransaction())
  176. {
  177. try
  178. {
  179. string startId = function.ReadInstance("/TradeRecord/HelpProfitMerchantForUserId.txt");
  180. if (string.IsNullOrEmpty(startId))
  181. {
  182. startId = "0";
  183. }
  184. DataTable idsDt = OtherMySqlConn.dtable("select Id from HelpProfitMerchantForUser where Id>=" + startId + " and Status=0 order by Id limit 50");
  185. if (idsDt.Rows.Count > 0)
  186. {
  187. string ids = "";
  188. foreach (DataRow idsDr in idsDt.Rows)
  189. {
  190. ids += idsDr["Id"].ToString() + ",";
  191. startId = idsDr["Id"].ToString();
  192. }
  193. DataTable selfDt = OtherMySqlConn.dtable("select UserId,DATE_FORMAT(CreateDate,'%Y%m'),count(Id) from HelpProfitMerchantForUser where Id in (" + ids.TrimEnd(',') + ") group by UserId,DATE_FORMAT(CreateDate,'%Y%m')");
  194. if (selfDt.Rows.Count > 0)
  195. {
  196. function.WriteLog("统计人数:" + selfDt.Rows.Count + "\n\n", "实时统计助利宝创客交易额日志");
  197. foreach (DataRow selfDr in selfDt.Rows)
  198. {
  199. int UserId = int.Parse(selfDr["UserId"].ToString());
  200. string TradeMonth = selfDr[1].ToString();
  201. int MerCount = int.Parse(selfDr[2].ToString());
  202. HelpProfitAmountSummary selfStat = db.HelpProfitAmountSummary.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth);
  203. if (selfStat == null)
  204. {
  205. int CheckMonth = db.HelpProfitAmountSummary.Count(m => m.UserId == UserId && Convert.ToInt32(m.TradeMonth) <= Convert.ToInt32(TradeMonth));
  206. selfStat = db.HelpProfitAmountSummary.Add(new HelpProfitAmountSummary()
  207. {
  208. TradeMonth = TradeMonth,
  209. UserId = UserId,
  210. QueryCount = CheckMonth + 1,
  211. }).Entity;
  212. db.SaveChanges();
  213. }
  214. selfStat.MerCount += MerCount;
  215. int Month = selfStat.QueryCount;
  216. if(Month > 24)
  217. {
  218. Month = 24;
  219. }
  220. selfStat.MaxAmount += EveryMonthFixedVal()[Month] * MerCount;
  221. }
  222. OtherMySqlConn.op("update HelpProfitMerchantForUser set Status=1 where Id in (" + ids.TrimEnd(',') + ")");
  223. function.WritePage("/TradeRecord/", "HelpProfitMerchantForUserId.txt", startId);
  224. }
  225. db.SaveChanges();
  226. }
  227. tran.Commit();
  228. }
  229. catch (Exception ex)
  230. {
  231. tran.Rollback();
  232. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "实时统计助利宝创客交易额异常");
  233. }
  234. }
  235. db.Dispose();
  236. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "实时统计助利宝创客交易额日志");
  237. }
  238. //计算商户交易额是否入账
  239. private bool CheckAmount(WebCMSEntities db, int MerchantId, int UserId, string TradeMonth)
  240. {
  241. HelpProfitAmountSummary Summary = db.HelpProfitAmountSummary.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth) ?? new HelpProfitAmountSummary();
  242. decimal MaxTradeAmount = Summary.MaxAmount; //当月最大交易总额
  243. decimal CheckAmount = 500000 < MaxTradeAmount * 0.3M ? 500000 : MaxTradeAmount * 0.3M;
  244. //当月单台交易额 <= 50万或当月最大交易总额的30%(取小值),则入账
  245. decimal SingleTradeAmount = 0;
  246. bool MerCheck = db.HelpProfitMerTradeSummay.Any(m => m.MerchantId == MerchantId && m.TradeMonth == TradeMonth);
  247. if(MerCheck)
  248. {
  249. SingleTradeAmount = db.HelpProfitMerTradeSummay.Where(m => m.MerchantId == MerchantId && m.TradeMonth == TradeMonth).Sum(m => m.TradeAmount);
  250. }
  251. if (SingleTradeAmount < CheckAmount)
  252. {
  253. //当月总交易额不超过当月最大交易总额,则入账
  254. if (Summary.TradeAmount < CheckAmount)
  255. {
  256. return true;
  257. }
  258. }
  259. return false;
  260. }
  261. //商户24个月,每月固定额度
  262. private Dictionary<int, decimal> EveryMonthFixedVal()
  263. {
  264. Dictionary<int, decimal> obj = new Dictionary<int, decimal>();
  265. obj.Add(0, 0M);
  266. obj.Add(1, 70000M);
  267. obj.Add(2, 60000M);
  268. obj.Add(3, 50000M);
  269. obj.Add(4, 40000M);
  270. obj.Add(5, 30000M);
  271. obj.Add(6, 30000M);
  272. obj.Add(7, 27000M);
  273. obj.Add(8, 25000M);
  274. obj.Add(9, 20000M);
  275. obj.Add(10, 17000M);
  276. obj.Add(11, 14000M);
  277. obj.Add(12, 14000M);
  278. obj.Add(13, 13000M);
  279. obj.Add(14, 12000M);
  280. obj.Add(15, 12000M);
  281. obj.Add(16, 10000M);
  282. obj.Add(17, 8000M);
  283. obj.Add(18, 7000M);
  284. obj.Add(19, 6000M);
  285. obj.Add(20, 5000M);
  286. obj.Add(21, 4000M);
  287. obj.Add(22, 3000M);
  288. obj.Add(23, 2000M);
  289. obj.Add(24, 1000M);
  290. return obj;
  291. }
  292. #region 每月重置最大交易总额
  293. public void ResetMaxTradeAmount()
  294. {
  295. Thread th = new Thread(ResetMaxTradeAmountDo);
  296. th.IsBackground = true;
  297. th.Start();
  298. }
  299. public void ResetMaxTradeAmountDo()
  300. {
  301. while (true)
  302. {
  303. if (DateTime.Now.Hour > 0 && DateTime.Now.Hour < 3)
  304. {
  305. string TradeMonth = DateTime.Now.AddMonths(-1).ToString("yyyyMM");
  306. ResetMaxTradeAmountGo(TradeMonth);
  307. }
  308. Thread.Sleep(30000);
  309. }
  310. }
  311. public void ResetMaxTradeAmountGo(string TradeMonth)
  312. {
  313. try
  314. {
  315. string CheckExist = function.ReadInstance("/HelpProfitAmountSummary/" + TradeMonth + ".txt");
  316. if(string.IsNullOrEmpty(CheckExist))
  317. {
  318. function.WritePage("/HelpProfitAmountSummary/", TradeMonth + ".txt", DateTime.Now.ToString());
  319. WebCMSEntities db = new WebCMSEntities();
  320. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  321. DataTable userlist = OtherMySqlConn.dtable("select * from HelpProfitAmountSummary where TradeMonth='" + TradeMonth + "'");
  322. foreach (DataRow user in userlist.Rows)
  323. {
  324. int Id = int.Parse(user["Id"].ToString());
  325. int UserId = int.Parse(user["UserId"].ToString());
  326. int Month = int.Parse(user["QueryCount"].ToString());
  327. if(Month > 24)
  328. {
  329. Month = 24;
  330. }
  331. decimal MonthAmount1 = 0; //本月额度
  332. decimal MonthAmount2 = 0; //上月额度
  333. decimal MonthAmount3 = 0; //上上月额度
  334. if(Month > 0)
  335. {
  336. MonthAmount1 = EveryMonthFixedVal()[Month]; //上月额度
  337. }
  338. if(Month > 1)
  339. {
  340. MonthAmount2 = EveryMonthFixedVal()[Month - 1]; //上月额度
  341. }
  342. if(Month > 2)
  343. {
  344. MonthAmount3 = EveryMonthFixedVal()[Month - 2]; //上上月额度
  345. }
  346. List<int> MerCounts = new List<int>(); //近2月月台数,不包含当月
  347. int MerCount1 = 0; //本月台数
  348. int MerCount2 = 0; //上月台数
  349. int MerCount3 = 0; //上上月台数
  350. List<HelpProfitAmountSummary> Summarys = db.HelpProfitAmountSummary.Where(m => m.UserId == UserId && Convert.ToInt32(m.TradeMonth) <= Convert.ToInt32(TradeMonth)).OrderByDescending(m => m.TradeMonth).Take(3).ToList();
  351. foreach(HelpProfitAmountSummary Summary in Summarys)
  352. {
  353. MerCounts.Add(Summary.MerCount);
  354. }
  355. if(MerCounts.Count > 0)
  356. {
  357. MerCount1 = MerCounts[0];
  358. }
  359. if(MerCounts.Count > 1)
  360. {
  361. MerCount2 = MerCounts[1];
  362. }
  363. if(MerCounts.Count > 2)
  364. {
  365. MerCount3 = MerCounts[2];
  366. }
  367. decimal MaxTradeAmount = MerCount1 * MonthAmount1 + MerCount2 * MonthAmount2 + MerCount3 * MonthAmount3; //当月最大交易总额
  368. HelpProfitAmountSummary selfStat = db.HelpProfitAmountSummary.FirstOrDefault(m => m.Id == Id);
  369. if(selfStat != null)
  370. {
  371. selfStat.MaxAmount = MaxTradeAmount;
  372. }
  373. }
  374. db.SaveChanges();
  375. db.Dispose();
  376. }
  377. }
  378. catch(Exception ex)
  379. {
  380. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "每月重置最大交易总额异常");
  381. }
  382. }
  383. #endregion
  384. }
  385. }