You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

251 lines
8.4 KiB

1 month ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.IO;
using System.Text;
using Sog;
namespace bill_statistics
{
public static class LogFileListSvc
{
public static List<string> AllFileName = new List<string>();
public static string strDir;
public static void ReadDataFromFile()
{
strDir = "../log/bill";
if (OSUtils.IsWindows() == false)
{
strDir = "../../publish/log/bill";
}
DirectoryInfo dir = new DirectoryInfo(strDir);
FileInfo[] allFile = dir.GetFiles();
foreach (FileInfo fi in allFile)
{
int iFileNameLength = fi.Name.Length;
if (fi.Name.Contains("bill_") && fi.Name.Contains(".log")
&& fi.Name[iFileNameLength -1] == 'g' && fi.Name[iFileNameLength - 2] == 'o' && fi.Name[iFileNameLength - 3] == 'l')
{
//兼容2种格式 1 "bill_2016_1_1_0.log" 2 bill_2016-11-18_10.log
//说明是老格式
if (fi.Name[9] == '_')
{
string[] splits = fi.Name.Split('_');
for (int i = 2; i < 5; i++)
{
// "1" "1.log"
if (splits[i].Length == 1 || (i == 4 && splits[i].Length == 5))
{
splits[i] = "0" + splits[i];
}
}
string newFileName = string.Format("bill_{0}-{1}-{2}_{3}", splits[1], splits[2], splits[3], splits[4]);
string newFileFullPathName = strDir + "/" + newFileName;
if (File.Exists(newFileFullPathName) == false)
{
fi.MoveTo(newFileFullPathName);
AllFileName.Add(newFileName);
}
else
{
File.Delete(fi.FullName);
}
}
else
{
AllFileName.Add(fi.Name);
}
}
}
//排序
AllFileName.Sort();
DeleteNoUseFileByMode();
}
private static void DeleteNoUseFileByMode()
{
//bill_2016-10-20_15.log
//正常小时数据,导上个小时数据,不计算留存什么的
if (BillMode.Mode == BillModeType.EXPORT_HOUR_LOG)
{
//留最后2个文件
while (AllFileName.Count > 2)
{
AllFileName.RemoveAt(0);
}
//如果有2个,判断最后一个文件是不是这个小时,如果是,删除,保留上个小时
if (AllFileName.Count == 2)
{
string filename = AllFileName[1];
string strhour = filename.Substring(16, 2);
int hour = -1;
if (int.TryParse(strhour, out hour) && hour == DateTime.Now.Hour)
{
AllFileName.RemoveAt(1);
}
}
}
//保留2个月的数据,必须0点开始,否则计算会出错
else if (BillMode.Mode == BillModeType.EXPROT_DAY_STAT)
{
//计算2个月数据,为什么呢,应为要计算月数据,必须完整,少了就不正确
DateTime now = DateTime.Now;
DateTime todayStarTime = now - now.TimeOfDay;
//得考虑跨月的情况
DateTime yestodayStarTime = todayStarTime.AddDays(-1);
DateTime lastMonthTime = yestodayStarTime.AddMonths(-1);
DateTime lastlastMonthEndDayStartTime = lastMonthTime.AddDays(-(lastMonthTime.Day));
//去头
while (AllFileName.Count > 0)
{
string filename = AllFileName[0];
string strYear = filename.Substring(5, 4);
string strMonth = filename.Substring(10, 2);
string strDay = filename.Substring(13,2);
int iYear = 0;
int iMonth = 0;
int iDay = 0;
int.TryParse(strYear, out iYear);
int.TryParse(strMonth, out iMonth);
int.TryParse(strDay,out iDay);
DateTime filenameTime = new DateTime(iYear,iMonth, iDay);
if (filenameTime < lastlastMonthEndDayStartTime)
{
AllFileName.RemoveAt(0);
continue;
}
break;
}
//去尾巴
string strToday = todayStarTime.ToString("u");
strToday = strToday.Substring(0, 10);
while (AllFileName.Count > 0)
{
string lastfilename = AllFileName.Last();
string lastDay = lastfilename.Substring(5, 10);
if (lastDay == strToday)
{
AllFileName.RemoveAt(AllFileName.Count - 1);
}
else
{
break;
}
}
}
else if (BillMode.Mode == BillModeType.EXPROT_TODAY_STAT)
{
//计算2天的数据,昨天和今天
DateTime now = DateTime.Now;
DateTime todayStarTime = now - now.TimeOfDay;
DateTime yestodayStarTime = todayStarTime.AddDays(-1);
//去头
while (AllFileName.Count > 0)
{
string filename = AllFileName[0];
string strYear = filename.Substring(5, 4);
string strMonth = filename.Substring(10, 2);
string strDay = filename.Substring(13, 2);
int iYear = 0;
int iMonth = 0;
int iDay = 0;
int.TryParse(strYear, out iYear);
int.TryParse(strMonth, out iMonth);
int.TryParse(strDay, out iDay);
DateTime filenameTime = new DateTime(iYear, iMonth, iDay);
if (filenameTime < yestodayStarTime)
{
AllFileName.RemoveAt(0);
continue;
}
break;
}
//这个小时理论上才开始几秒到1分钟,所以需要删除
if (AllFileName.Count > 0)
{
string filename = AllFileName[AllFileName.Count - 1];
string strhour = filename.Substring(16, 2);
int hour = -1;
if (int.TryParse(strhour, out hour) && hour == DateTime.Now.Hour)
{
AllFileName.RemoveAt(AllFileName.Count - 1);
}
}
}
else if(BillMode.Mode == BillModeType.LONG_KEEP)
{
//最多200天数据
while (AllFileName.Count > 24*182)
{
AllFileName.RemoveAt(0);
}
}
else
{
//判断最后一个文件是不是这个小时,如果是,删除
if (AllFileName.Count > 0)
{
string filename = AllFileName[AllFileName.Count - 1];
string strhour = filename.Substring(16, 2);
int hour = -1;
if (int.TryParse(strhour, out hour) && hour == DateTime.Now.Hour)
{
AllFileName.RemoveAt(AllFileName.Count - 1);
}
}
}
}
public static void DeleteNoUseFileByOnlyDayNumber(int onlyDayNumber)
{
int hourCount = 24 * onlyDayNumber;
//最多n天
while(AllFileName.Count > hourCount)
{
AllFileName.RemoveAt(0);
}
}
}
}