using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using TellerSystem.ServiceProxy.Ext.ServiceHelper;
using Platform.Common.RunningParameters;
namespace TellerSystem.Library.Ext.TradeExtension
{
///
/// FTP日志上传备份
///
/// 翟国栋
/// 2013/9/25 9:23:56
public static class LoggerFTP
{
static string FileName = "Logs";
private static byte[] fileData = null;
private static string filePath = "";
private static List localLogPath = new List();
///
/// 判断当前日志文件是否存在没有备份的
///
/// 文件路径
/// 营业日期
/// True-存在,False-不存在
public static bool IsExistLogFile(FileSystemInfo file, string currentDate)
{
if (!file.Exists)
return false;
DirectoryInfo dire = file as DirectoryInfo;
if (dire == null)
return false;
FileSystemInfo[] files = dire.GetFileSystemInfos();
var fileCount = files.TakeWhile(x => Convert.ToDecimal(x.Name) < Convert.ToDecimal(currentDate)).ToList();
if (fileCount.Count() > 0)
return true;
return false;
}
///
/// 递归遍历本地日志路径
///
/// 本地日志路径
/// 日志路径列表
private static List getFiles(string path)
{
string[] strFileNames = Directory.GetFiles(path);
string[] strDirectories = Directory.GetDirectories(path);
foreach (string filename in strFileNames)
{
localLogPath.Add(filename);
}
foreach (string dir in strDirectories)
{
getFiles(dir);
}
return localLogPath;
}
///
/// 遍历子目录,备份当前日期前的日志文件
///
///
public static void GetFileSystemInfos(FileSystemInfo file)
{
localLogPath.Clear();
if (!file.Exists) return;
var list = getFiles(file.FullName);
foreach (var item in list)
{
if (!item.Contains(LoginUserInfo.TradeDate))
{
var upLastName = item.Substring(item.IndexOf(FileName), item.Length - item.IndexOf(FileName)).ToString();
//上传日志文件
string upPath = string.Empty;
string[] upPathArray = upLastName.Split('\\');
for (int i = 0; i < upPathArray.Length; i++)
{
if (i < upPathArray.Length - 1)
{
upPath += upPathArray[i] + "/";
}
}
fileData = File.ReadAllBytes(item);
var result = 0.00;
try
{
result = TradeHandle.FtpUpLoad(upLastName.Split('\\').LastOrDefault(), upPath, fileData);
}
catch (Exception e)
{
}
}
}
}
///
/// 清除本地日志文件
///
public static void ClearFiles(FileSystemInfo file)
{
DirectoryInfo dire = file as DirectoryInfo;
if (dire == null) return;
FileSystemInfo[] files = dire.GetFileSystemInfos();
string date = string.Empty;
if (LoginUserInfo.TradeDate.Length == 8)
date = LoginUserInfo.TradeDate.Substring(0, 4) + "-" + LoginUserInfo.TradeDate.Substring(4, 2) + "-" + LoginUserInfo.TradeDate.Substring(6, 2);
DateTime dt = DateTime.Now;
if (!DateTime.TryParse(date, out dt))
return;
string[] dateList = new string[3];
dateList[0] = LoginUserInfo.TradeDate;
dateList[1] = string.Format("{0:yyyyMMdd}", dt.AddDays(-1));
dateList[2] = string.Format("{0:yyyyMMdd}", dt.AddDays(-2));
for (int i = 0; i < files.Length; i++)
{
bool flag = true;
string finame = files[i].Name;
string fipath = files[i].FullName;
for (int y = 0; y < dateList.Length; y++)
{
if (files[i].Name == dateList[y])
{
flag = false;
continue;
}
}
if (flag)
{
DirectoryInfo dirInfo = new DirectoryInfo(fipath);
//删除目录及其子目录
dirInfo.Delete(true);
}
}
}
}
}