| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- using System;
- using System.Collections.Generic;
- using Library;
- using LitJson;
- using System.Linq;
- using System.Data;
- using System.Threading;
- using MySystem.PxcModels;
- namespace MySystem
- {
- public class StatHelpProfitService
- {
- public readonly static StatHelpProfitService Instance = new StatHelpProfitService();
- private StatHelpProfitService()
- { }
- // 统计交易额V2
- public void StartEverDayV2()
- {
- Thread th = new Thread(StartEverDayV2Do);
- th.IsBackground = true;
- th.Start();
- }
- public void StartEverDayV2Do()
- {
- while (true)
- {
- if (DateTime.Now.Hour >= 3)
- {
- StatTradeAmountEverDayV2();
- }
- Thread.Sleep(30000);
- }
- }
- public void StatTradeAmountEverDayV2()
- {
- OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
- function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "实时执行助利宝交易额日志");
- WebCMSEntities db = new WebCMSEntities();
- using (var tran = db.Database.BeginTransaction())
- {
- try
- {
- string startId = function.ReadInstance("/TradeRecord/Id3.txt");
- if (string.IsNullOrEmpty(startId))
- {
- startId = "2750000";
- }
- 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");
- if (idsDt.Rows.Count > 0)
- {
- string ids = "";
- foreach (DataRow idsDr in idsDt.Rows)
- {
- ids += idsDr["Id"].ToString() + ",";
- startId = idsDr["Id"].ToString();
- }
- 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')");
- if (selfDt.Rows.Count > 0)
- {
- function.WriteLog("统计人数:" + selfDt.Rows.Count + "\n\n", "实时执行助利宝交易额日志");
- foreach (DataRow selfDr in selfDt.Rows)
- {
- int MerchantId = int.Parse(selfDr["MerchantId"].ToString());
- string TradeDate = selfDr[1].ToString();
- string TradeMonth = TradeDate.Substring(0, 6);
- decimal TradeAmount = decimal.Parse(selfDr[2].ToString());
- HelpProfitMerIds merIds = db.HelpProfitMerIds.FirstOrDefault(m => m.MerchantId == MerchantId) ?? new HelpProfitMerIds();
- bool Check = CheckAmount(db, MerchantId, merIds.UserId, TradeMonth);
- if (Check)
- {
- HelpProfitMerTradeSummay selfStat = db.HelpProfitMerTradeSummay.FirstOrDefault(m => m.MerchantId == MerchantId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate);
- if (selfStat == null)
- {
- selfStat = db.HelpProfitMerTradeSummay.Add(new HelpProfitMerTradeSummay()
- {
- TradeMonth = TradeMonth,
- TradeDate = TradeDate,
- MerchantId = MerchantId,
- }).Entity;
- db.SaveChanges();
- }
- decimal MoreAmount = TradeAmount * 0.01M;
- int random = function.get_Random(0, 10000);
- // if(MoreAmount > 1 && random < 5000)
- // {
- MoreAmount = decimal.Parse(function.CheckInt(MoreAmount.ToString().Split(".")[0]));
- // }
- selfStat.TradeAmount += TradeAmount + MoreAmount;
- }
- }
- OtherMySqlConn.op("update TradeRecord set QueryCount=3 where Id in (" + ids.TrimEnd(',') + ")");
- function.WritePage("/TradeRecord/", "Id3.txt", startId);
- }
- db.SaveChanges();
- }
- tran.Commit();
- }
- catch (Exception ex)
- {
- tran.Rollback();
- function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "实时执行助利宝交易额异常");
- }
- }
- db.Dispose();
- function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "实时执行助利宝交易额日志");
- }
- public void StatTradeAmountTmp()
- {
- OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
- function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "实时执行助利宝交易额日志");
- WebCMSEntities db = new WebCMSEntities();
- using (var tran = db.Database.BeginTransaction())
- {
- try
- {
- 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')");
- if (selfDt.Rows.Count > 0)
- {
- function.WriteLog("统计人数:" + selfDt.Rows.Count + "\n\n", "实时执行助利宝交易额日志");
- foreach (DataRow selfDr in selfDt.Rows)
- {
- int MerchantId = int.Parse(selfDr["MerchantId"].ToString());
- string TradeMonth = selfDr[1].ToString();
- decimal TradeAmount = decimal.Parse(selfDr[2].ToString());
- HelpProfitMerIds merIds = db.HelpProfitMerIds.FirstOrDefault(m => m.MerchantId == MerchantId) ?? new HelpProfitMerIds();
- HelpProfitAmountSummary profitStat = db.HelpProfitAmountSummary.FirstOrDefault(m => m.UserId == merIds.UserId && m.TradeMonth == TradeMonth);
- if (profitStat == null)
- {
- int Month = db.HelpProfitAmountSummary.Count(m => m.UserId == merIds.UserId && Convert.ToInt32(m.TradeMonth) <= Convert.ToInt32(TradeMonth));
- profitStat = db.HelpProfitAmountSummary.Add(new HelpProfitAmountSummary()
- {
- TradeMonth = TradeMonth,
- UserId = merIds.UserId,
- QueryCount = Month + 1,
- }).Entity;
- db.SaveChanges();
- }
- profitStat.TradeAmount += TradeAmount;
- }
- }
- db.SaveChanges();
- tran.Commit();
- }
- catch (Exception ex)
- {
- tran.Rollback();
- function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "实时执行助利宝交易额2异常");
- }
- }
- db.Dispose();
- }
- // 统计创客商机台数
- public void StartUserTrade()
- {
- Thread th = new Thread(StartUserTradeDo);
- th.IsBackground = true;
- th.Start();
- }
- public void StartUserTradeDo()
- {
- while (true)
- {
- // if (DateTime.Now.Hour >= 3)
- // {
- StartUserTradeGo();
- // }
- Thread.Sleep(3000);
- }
- }
- public void StartUserTradeGo()
- {
- OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
- function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "实时统计助利宝创客交易额日志");
- WebCMSEntities db = new WebCMSEntities();
- using (var tran = db.Database.BeginTransaction())
- {
- try
- {
- string startId = function.ReadInstance("/TradeRecord/HelpProfitMerchantForUserId.txt");
- if (string.IsNullOrEmpty(startId))
- {
- startId = "0";
- }
- DataTable idsDt = OtherMySqlConn.dtable("select Id from HelpProfitMerchantForUser where Id>=" + startId + " and Status=0 order by Id limit 50");
- if (idsDt.Rows.Count > 0)
- {
- string ids = "";
- foreach (DataRow idsDr in idsDt.Rows)
- {
- ids += idsDr["Id"].ToString() + ",";
- startId = idsDr["Id"].ToString();
- }
- 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')");
- if (selfDt.Rows.Count > 0)
- {
- function.WriteLog("统计人数:" + selfDt.Rows.Count + "\n\n", "实时统计助利宝创客交易额日志");
- foreach (DataRow selfDr in selfDt.Rows)
- {
- int UserId = int.Parse(selfDr["UserId"].ToString());
- string TradeMonth = selfDr[1].ToString();
- int MerCount = int.Parse(selfDr[2].ToString());
- HelpProfitAmountSummary selfStat = db.HelpProfitAmountSummary.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth);
- if (selfStat == null)
- {
- int CheckMonth = db.HelpProfitAmountSummary.Count(m => m.UserId == UserId && Convert.ToInt32(m.TradeMonth) <= Convert.ToInt32(TradeMonth));
- selfStat = db.HelpProfitAmountSummary.Add(new HelpProfitAmountSummary()
- {
- TradeMonth = TradeMonth,
- UserId = UserId,
- QueryCount = CheckMonth + 1,
- }).Entity;
- db.SaveChanges();
- }
- selfStat.MerCount += MerCount;
- int Month = selfStat.QueryCount;
- if(Month > 24)
- {
- Month = 24;
- }
- selfStat.MaxAmount += EveryMonthFixedVal()[Month] * MerCount;
- }
- OtherMySqlConn.op("update HelpProfitMerchantForUser set Status=1 where Id in (" + ids.TrimEnd(',') + ")");
- function.WritePage("/TradeRecord/", "HelpProfitMerchantForUserId.txt", startId);
- }
- db.SaveChanges();
- }
- tran.Commit();
- }
- catch (Exception ex)
- {
- tran.Rollback();
- function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "实时统计助利宝创客交易额异常");
- }
- }
- db.Dispose();
- function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "实时统计助利宝创客交易额日志");
- }
- //计算商户交易额是否入账
- private bool CheckAmount(WebCMSEntities db, int MerchantId, int UserId, string TradeMonth)
- {
- HelpProfitAmountSummary Summary = db.HelpProfitAmountSummary.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth) ?? new HelpProfitAmountSummary();
- decimal MaxTradeAmount = Summary.MaxAmount; //当月最大交易总额
- decimal CheckAmount = 500000 < MaxTradeAmount * 0.3M ? 500000 : MaxTradeAmount * 0.3M;
- //当月单台交易额 <= 50万或当月最大交易总额的30%(取小值),则入账
- decimal SingleTradeAmount = 0;
- bool MerCheck = db.HelpProfitMerTradeSummay.Any(m => m.MerchantId == MerchantId && m.TradeMonth == TradeMonth);
- if(MerCheck)
- {
- SingleTradeAmount = db.HelpProfitMerTradeSummay.Where(m => m.MerchantId == MerchantId && m.TradeMonth == TradeMonth).Sum(m => m.TradeAmount);
- }
- if (SingleTradeAmount < CheckAmount)
- {
- //当月总交易额不超过当月最大交易总额,则入账
- if (Summary.TradeAmount < CheckAmount)
- {
- return true;
- }
- }
- return false;
- }
- //商户24个月,每月固定额度
- private Dictionary<int, decimal> EveryMonthFixedVal()
- {
- Dictionary<int, decimal> obj = new Dictionary<int, decimal>();
- obj.Add(0, 0M);
- obj.Add(1, 70000M);
- obj.Add(2, 60000M);
- obj.Add(3, 50000M);
- obj.Add(4, 40000M);
- obj.Add(5, 30000M);
- obj.Add(6, 30000M);
- obj.Add(7, 27000M);
- obj.Add(8, 25000M);
- obj.Add(9, 20000M);
- obj.Add(10, 17000M);
- obj.Add(11, 14000M);
- obj.Add(12, 14000M);
- obj.Add(13, 13000M);
- obj.Add(14, 12000M);
- obj.Add(15, 12000M);
- obj.Add(16, 10000M);
- obj.Add(17, 8000M);
- obj.Add(18, 7000M);
- obj.Add(19, 6000M);
- obj.Add(20, 5000M);
- obj.Add(21, 4000M);
- obj.Add(22, 3000M);
- obj.Add(23, 2000M);
- obj.Add(24, 1000M);
- return obj;
- }
- #region 每月重置最大交易总额
- public void ResetMaxTradeAmount()
- {
- Thread th = new Thread(ResetMaxTradeAmountDo);
- th.IsBackground = true;
- th.Start();
- }
- public void ResetMaxTradeAmountDo()
- {
- while (true)
- {
- if (DateTime.Now.Hour > 0 && DateTime.Now.Hour < 3)
- {
- string TradeMonth = DateTime.Now.AddMonths(-1).ToString("yyyyMM");
- ResetMaxTradeAmountGo(TradeMonth);
- }
- Thread.Sleep(30000);
- }
- }
- public void ResetMaxTradeAmountGo(string TradeMonth)
- {
- try
- {
- string CheckExist = function.ReadInstance("/HelpProfitAmountSummary/" + TradeMonth + ".txt");
- if(string.IsNullOrEmpty(CheckExist))
- {
- function.WritePage("/HelpProfitAmountSummary/", TradeMonth + ".txt", DateTime.Now.ToString());
- WebCMSEntities db = new WebCMSEntities();
- OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
- DataTable userlist = OtherMySqlConn.dtable("select * from HelpProfitAmountSummary where TradeMonth='" + TradeMonth + "'");
- foreach (DataRow user in userlist.Rows)
- {
- int Id = int.Parse(user["Id"].ToString());
- int UserId = int.Parse(user["UserId"].ToString());
- int Month = int.Parse(user["QueryCount"].ToString());
- if(Month > 24)
- {
- Month = 24;
- }
- decimal MonthAmount1 = 0; //本月额度
- decimal MonthAmount2 = 0; //上月额度
- decimal MonthAmount3 = 0; //上上月额度
- if(Month > 0)
- {
- MonthAmount1 = EveryMonthFixedVal()[Month]; //上月额度
- }
- if(Month > 1)
- {
- MonthAmount2 = EveryMonthFixedVal()[Month - 1]; //上月额度
- }
- if(Month > 2)
- {
- MonthAmount3 = EveryMonthFixedVal()[Month - 2]; //上上月额度
- }
- List<int> MerCounts = new List<int>(); //近2月月台数,不包含当月
- int MerCount1 = 0; //本月台数
- int MerCount2 = 0; //上月台数
- int MerCount3 = 0; //上上月台数
- List<HelpProfitAmountSummary> Summarys = db.HelpProfitAmountSummary.Where(m => m.UserId == UserId && Convert.ToInt32(m.TradeMonth) <= Convert.ToInt32(TradeMonth)).OrderByDescending(m => m.TradeMonth).Take(3).ToList();
- foreach(HelpProfitAmountSummary Summary in Summarys)
- {
- MerCounts.Add(Summary.MerCount);
- }
- if(MerCounts.Count > 0)
- {
- MerCount1 = MerCounts[0];
- }
- if(MerCounts.Count > 1)
- {
- MerCount2 = MerCounts[1];
- }
- if(MerCounts.Count > 2)
- {
- MerCount3 = MerCounts[2];
- }
- decimal MaxTradeAmount = MerCount1 * MonthAmount1 + MerCount2 * MonthAmount2 + MerCount3 * MonthAmount3; //当月最大交易总额
- HelpProfitAmountSummary selfStat = db.HelpProfitAmountSummary.FirstOrDefault(m => m.Id == Id);
- if(selfStat != null)
- {
- selfStat.MaxAmount = MaxTradeAmount;
- }
- }
- db.SaveChanges();
- db.Dispose();
- }
- }
- catch(Exception ex)
- {
- function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "每月重置最大交易总额异常");
- }
- }
- #endregion
- }
- }
|