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 DepositReturnStatService { public readonly static DepositReturnStatService Instance = new DepositReturnStatService(); private DepositReturnStatService() { } public void Start() { Thread th = new Thread(StartEverDay); th.IsBackground = true; th.Start(); } public void StartEverDay() { while(true) { if(DateTime.Now.Day <= 15 && DateTime.Now.Hour > 1 && DateTime.Now.Hour < 13) { try { StartEverDayDo(); } catch(Exception ex) { function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "每月统计押金达标数据异常"); } } Thread.Sleep(60000); } } public void StartEverDayDo() { string Month = DateTime.Now.ToString("yyyyMM"); string chk = function.ReadInstance("/DepositReturn/" + Month + ".txt"); if(!string.IsNullOrEmpty(chk)) { return; } function.WritePage("/DepositReturn/", Month + ".txt", DateTime.Now.ToString()); WebCMSEntities db = new WebCMSEntities(); int startid = int.Parse(function.CheckInt(function.ReadInstance("/DepositReturn/" + Month + "MerchantId.txt"))); bool op = true; while(op) { List merchants = db.PosMerchantInfo.Where(m => m.Id > startid && m.ActiveStatus == 1 && m.StandardStatus == 0).OrderBy(m => m.Id).Take(50).ToList(); if(merchants.Count > 0) { foreach(PosMerchantInfo merchant in merchants) { DoSomething(db, merchant); startid = merchant.Id; } } else { op = false; } function.WritePage("/DepositReturn/", Month + "MerchantId.txt", startid.ToString()); } db.Dispose(); } public void StartEverTime() { Thread th = new Thread(StartEverTimeDo); th.IsBackground = true; th.Start(); } public void StartEverTimeDo() { while(true) { try { string content = RedisDbconn.Instance.RPop("DepositReturnStatQueue"); if(!string.IsNullOrEmpty(content)) { int MerchantId = int.Parse(function.CheckInt(content)); WebCMSEntities db = new WebCMSEntities(); PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == MerchantId); if(merchant != null) { DoSomething(db, merchant); } db.Dispose(); } else { Thread.Sleep(60000); } } catch(Exception ex) { function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "统计单个商户押金达标数据异常"); } } } public void DoSomething(WebCMSEntities db, PosMerchantInfo merchant) { function.WriteLog("商户ID:" + merchant.Id, "每月统计押金达标情况"); // string start = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); int StandardMonths = merchant.StandardMonths; int StandardStatus = merchant.StandardStatus; function.WriteLog("StandardMonths:" + StandardMonths, "每月统计押金达标情况"); function.WriteLog("StandardStatus:" + StandardStatus, "每月统计押金达标情况"); DateTime ActDate = merchant.CreateDate.Value; if(merchant.MerStandardDate == null) { PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == merchant.KqSnNo && m.ActivationState == 1 && m.ActivationTime != null); if(pos != null) { ActDate = pos.ActivationTime.Value; } } else { ActDate = merchant.MerStandardDate.Value; } int MonthNum = StandardMonths; for (int i = StandardMonths + 1; i <= 10; i++) { string TradeMonth = ActDate.AddMonths(i).ToString("yyyyMM"); if(int.Parse(TradeMonth) < int.Parse(DateTime.Now.ToString("yyyyMM"))) { decimal TradeAmount = 0; bool CheckAmount = db.PosMerchantTradeSummay.Any(m => m.MerchantId == merchant.Id && m.TradeMonth == TradeMonth); if(CheckAmount) { TradeAmount = db.PosMerchantTradeSummay.Where(m => m.MerchantId == merchant.Id && m.TradeMonth == TradeMonth).Sum(m => m.TradeAmount); } if(TradeAmount >= 10000) { MonthNum = i; } else { StandardStatus = -1; break; } } } function.WriteLog("MonthNum:" + MonthNum, "每月统计押金达标情况"); if(MonthNum >= 10) { StandardStatus = 4; } function.WriteLog("StandardStatus:" + StandardStatus + "\n\n", "每月统计押金达标情况"); PosMerchantInfo edit = db.PosMerchantInfo.FirstOrDefault(m => m.Id == merchant.Id); if(edit != null) { edit.StandardMonths = MonthNum; edit.StandardStatus = StandardStatus; db.SaveChanges(); } } } }