StatHelpProfitService.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. HelpProfitAmountSummary profitStat = db.HelpProfitAmountSummary.FirstOrDefault(m => m.UserId == merIds.UserId && m.TradeMonth == TradeMonth);
  90. if (profitStat == null)
  91. {
  92. profitStat = db.HelpProfitAmountSummary.Add(new HelpProfitAmountSummary()
  93. {
  94. TradeMonth = TradeMonth,
  95. UserId = merIds.UserId,
  96. }).Entity;
  97. db.SaveChanges();
  98. }
  99. profitStat.TradeAmount += TradeAmount;
  100. }
  101. }
  102. OtherMySqlConn.op("update TradeRecord set QueryCount=3 where Id in (" + ids.TrimEnd(',') + ")");
  103. function.WritePage("/TradeRecord/", "Id3.txt", startId);
  104. }
  105. db.SaveChanges();
  106. }
  107. tran.Commit();
  108. }
  109. catch (Exception ex)
  110. {
  111. tran.Rollback();
  112. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "实时执行助利宝交易额异常");
  113. }
  114. }
  115. db.Dispose();
  116. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "实时执行助利宝交易额日志");
  117. }
  118. // 统计创客商机台数
  119. public void StartUserTrade()
  120. {
  121. Thread th = new Thread(StartUserTradeDo);
  122. th.IsBackground = true;
  123. th.Start();
  124. }
  125. public void StartUserTradeDo()
  126. {
  127. while (true)
  128. {
  129. if (DateTime.Now.Hour >= 3)
  130. {
  131. StartUserTradeGo();
  132. }
  133. Thread.Sleep(30000);
  134. }
  135. }
  136. public void StartUserTradeGo()
  137. {
  138. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  139. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "实时统计助利宝创客交易额日志");
  140. WebCMSEntities db = new WebCMSEntities();
  141. using (var tran = db.Database.BeginTransaction())
  142. {
  143. try
  144. {
  145. string startId = function.ReadInstance("/TradeRecord/HelpProfitMerchantForUserId.txt");
  146. if (string.IsNullOrEmpty(startId))
  147. {
  148. startId = "0";
  149. }
  150. DataTable idsDt = OtherMySqlConn.dtable("select Id from HelpProfitMerchantForUser where Id>=" + startId + " and Status=0 order by Id limit 50");
  151. if (idsDt.Rows.Count > 0)
  152. {
  153. string ids = "";
  154. foreach (DataRow idsDr in idsDt.Rows)
  155. {
  156. ids += idsDr["Id"].ToString() + ",";
  157. startId = idsDr["Id"].ToString();
  158. }
  159. 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')");
  160. if (selfDt.Rows.Count > 0)
  161. {
  162. function.WriteLog("统计人数:" + selfDt.Rows.Count + "\n\n", "实时统计助利宝创客交易额日志");
  163. foreach (DataRow selfDr in selfDt.Rows)
  164. {
  165. int UserId = int.Parse(selfDr["UserId"].ToString());
  166. string TradeMonth = selfDr[1].ToString();
  167. int MerCount = int.Parse(selfDr[2].ToString());
  168. HelpProfitAmountSummary selfStat = db.HelpProfitAmountSummary.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth);
  169. if (selfStat == null)
  170. {
  171. selfStat = db.HelpProfitAmountSummary.Add(new HelpProfitAmountSummary()
  172. {
  173. TradeMonth = TradeMonth,
  174. UserId = UserId,
  175. }).Entity;
  176. db.SaveChanges();
  177. }
  178. selfStat.MerCount += MerCount;
  179. selfStat.MaxAmount += EveryMonthFixedVal()[selfStat.QueryCount] * MerCount;
  180. }
  181. OtherMySqlConn.op("update HelpProfitMerchantForUser set Status=1 where Id in (" + ids.TrimEnd(',') + ")");
  182. function.WritePage("/TradeRecord/", "HelpProfitMerchantForUserId.txt", startId);
  183. }
  184. db.SaveChanges();
  185. }
  186. tran.Commit();
  187. }
  188. catch (Exception ex)
  189. {
  190. tran.Rollback();
  191. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "实时统计助利宝创客交易额异常");
  192. }
  193. }
  194. db.Dispose();
  195. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "实时统计助利宝创客交易额日志");
  196. }
  197. //计算商户交易额是否入账
  198. private bool CheckAmount(WebCMSEntities db, int MerchantId, int UserId, string TradeMonth)
  199. {
  200. HelpProfitAmountSummary Summary = db.HelpProfitAmountSummary.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth) ?? new HelpProfitAmountSummary();
  201. decimal MaxTradeAmount = Summary.MaxAmount; //当月最大交易总额
  202. decimal CheckAmount = 500000 < MaxTradeAmount * 0.3M ? 500000 : MaxTradeAmount * 0.3M;
  203. //当月单台交易额 <= 50万或当月最大交易总额的30%(取小值),则入账
  204. decimal SingleTradeAmount = 0;
  205. bool MerCheck = db.HelpProfitMerTradeSummay.Any(m => m.MerchantId == MerchantId && m.TradeMonth == TradeMonth);
  206. if(MerCheck)
  207. {
  208. SingleTradeAmount = db.HelpProfitMerTradeSummay.Where(m => m.MerchantId == MerchantId && m.TradeMonth == TradeMonth).Sum(m => m.TradeAmount);
  209. }
  210. if (SingleTradeAmount < CheckAmount)
  211. {
  212. //当月总交易额不超过当月最大交易总额,则入账
  213. if (Summary.TradeAmount < CheckAmount)
  214. {
  215. return true;
  216. }
  217. }
  218. return false;
  219. }
  220. //商户24个月,每月固定额度
  221. private Dictionary<int, decimal> EveryMonthFixedVal()
  222. {
  223. Dictionary<int, decimal> obj = new Dictionary<int, decimal>();
  224. obj.Add(0, 0M);
  225. obj.Add(1, 7M);
  226. obj.Add(2, 6M);
  227. obj.Add(3, 5M);
  228. obj.Add(4, 4M);
  229. obj.Add(5, 3M);
  230. obj.Add(6, 3M);
  231. obj.Add(7, 2.7M);
  232. obj.Add(8, 2.5M);
  233. obj.Add(9, 2M);
  234. obj.Add(10, 1.7M);
  235. obj.Add(11, 1.4M);
  236. obj.Add(12, 1.4M);
  237. obj.Add(13, 1.3M);
  238. obj.Add(14, 1.2M);
  239. obj.Add(15, 1.2M);
  240. obj.Add(16, 1M);
  241. obj.Add(17, 0.8M);
  242. obj.Add(18, 0.7M);
  243. obj.Add(19, 0.6M);
  244. obj.Add(20, 0.5M);
  245. obj.Add(21, 0.4M);
  246. obj.Add(22, 0.3M);
  247. obj.Add(23, 0.2M);
  248. obj.Add(24, 0.1M);
  249. return obj;
  250. }
  251. #region 每月重置最大交易总额
  252. public void ResetMaxTradeAmount()
  253. {
  254. Thread th = new Thread(ResetMaxTradeAmountDo);
  255. th.IsBackground = true;
  256. th.Start();
  257. }
  258. public void ResetMaxTradeAmountDo()
  259. {
  260. while (true)
  261. {
  262. if (DateTime.Now.Hour > 0 && DateTime.Now.Hour < 3)
  263. {
  264. ResetMaxTradeAmountGo();
  265. }
  266. Thread.Sleep(30000);
  267. }
  268. }
  269. public void ResetMaxTradeAmountGo()
  270. {
  271. while(true)
  272. {
  273. try
  274. {
  275. string TradeMonth = DateTime.Now.ToString("yyyyMM");
  276. string CheckExist = function.ReadInstance("/HelpProfitAmountSummary/" + TradeMonth + ".txt");
  277. if(string.IsNullOrEmpty(CheckExist))
  278. {
  279. function.WritePage("/HelpProfitAmountSummary/", TradeMonth + ".txt", DateTime.Now.ToString());
  280. string StartDate = DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00";
  281. string EndDate = DateTime.Now.AddMonths(1).ToString("yyyy-MM") + "-01 00:00:00";
  282. WebCMSEntities db = new WebCMSEntities();
  283. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  284. DataTable userlist = OtherMySqlConn.dtable("select DISTINCT UserId from HelpProfitMerchantForUser order by UserId desc");
  285. foreach (DataRow user in userlist.Rows)
  286. {
  287. int UserId = int.Parse(user[0].ToString());
  288. int Month = db.HelpProfitAmountSummary.Count(m => m.UserId == UserId);
  289. if(Month > 24)
  290. {
  291. Month = 24;
  292. }
  293. decimal MonthAmount2 = 0; //上月额度
  294. decimal MonthAmount3 = 0; //上上月额度
  295. if(Month > 1)
  296. {
  297. MonthAmount2 = EveryMonthFixedVal()[Month - 1]; //上月额度
  298. }
  299. if(Month > 2)
  300. {
  301. MonthAmount3 = EveryMonthFixedVal()[Month - 2]; //上上月额度
  302. }
  303. List<int> MerCounts = new List<int>(); //近2月月台数,不包含当月
  304. int MerCount2 = 0; //上月台数
  305. int MerCount3 = 0; //上上月台数
  306. List<HelpProfitAmountSummary> Summarys = db.HelpProfitAmountSummary.Where(m => m.UserId == UserId).OrderByDescending(m => m.TradeMonth).ToList();
  307. foreach(HelpProfitAmountSummary Summary in Summarys)
  308. {
  309. if(Summary.MerCount > 0 && MerCounts.Count < 3)
  310. {
  311. MerCounts.Add(Summary.MerCount);
  312. }
  313. if(MerCounts.Count >= 3)
  314. {
  315. break;
  316. }
  317. }
  318. if(MerCounts.Count > 0)
  319. {
  320. MerCount2 = MerCounts[0];
  321. }
  322. if(MerCounts.Count > 1)
  323. {
  324. MerCount3 = MerCounts[1];
  325. }
  326. decimal MaxTradeAmount = MerCount2 * MonthAmount2 + MerCount3 * MonthAmount3; //当月最大交易总额
  327. HelpProfitAmountSummary selfStat = db.HelpProfitAmountSummary.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth);
  328. if (selfStat == null)
  329. {
  330. selfStat = db.HelpProfitAmountSummary.Add(new HelpProfitAmountSummary()
  331. {
  332. TradeMonth = TradeMonth,
  333. UserId = UserId,
  334. QueryCount = Month,
  335. }).Entity;
  336. db.SaveChanges();
  337. }
  338. selfStat.MaxAmount = MaxTradeAmount;
  339. }
  340. db.SaveChanges();
  341. db.Dispose();
  342. }
  343. }
  344. catch(Exception ex)
  345. {
  346. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "每月重置最大交易总额异常");
  347. }
  348. Thread.Sleep(600000);
  349. }
  350. }
  351. #endregion
  352. }
  353. }