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

304 lines
12 KiB
C#

using log4net;
using Newtonsoft.Json;
using Quartz;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
namespace JobReqWebData
{
public class JobGetMskFtp : IJob
{
private ILog log = LogManager.GetLogger(typeof(JobGetMskFtp));
public void Execute(IJobExecutionContext context)
{
log.Debug($"开始运行ftp");
try
{
string connStr = context.JobDetail.JobDataMap.GetString("ConnectString");
var FTPADDRESS = context.JobDetail.JobDataMap.GetString("FTPADDRESS");
var FTPPATH = context.JobDetail.JobDataMap.GetString("FTPPATH");
var FTPUSERNAME = context.JobDetail.JobDataMap.GetString("FTPUSERNAME");
var FTPPWD = context.JobDetail.JobDataMap.GetString("FTPPWD");
var FTPPORT = context.JobDetail.JobDataMap.GetString("FTPPORT");
//// 配置会话选项
//SessionOptions sessionOptions = new SessionOptions
//{
// Protocol = Protocol.Ftp,
// HostName = FTPADDRESS,
// UserName = FTPUSERNAME,
// Password= FTPPWD
//};
//try
//{
// log.Debug($"FTP连接:{FTPADDRESS},{FTPPATH},{FTPUSERNAME},{FTPPWD}");
// var m_session = new Session();
// m_session.Open(sessionOptions);
// log.Debug($"FTP已连接:{FTPADDRESS},{FTPPATH},{FTPUSERNAME},{FTPPWD}");
// //this.propUpdateDownloadStatus("已连接");
// var ftpfilelist = m_session.ListDirectory(FTPPATH);
// var filepath = AppDomain.CurrentDomain.BaseDirectory + @"\FTPDOWNLOADFILE\" + DateTime.Now.ToString("yyyy-MM-dd") + @"\";
// if (Directory.Exists(filepath) == false)
// {
// Directory.CreateDirectory(filepath);
// log.Debug($"服务器本地建立文件夹:{filepath}");
// }
// for (var i = 0; i <= ftpfilelist.Files.Count - 1; i++)
// {
// if (!ftpfilelist.Files[i].IsDirectory && Path.GetFileName(ftpfilelist.Files[i].FullName) != "..")
// {
// var finf = new fileprop();
// finf.fdate = ftpfilelist.Files[i].LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
// finf.fname = Path.GetFileName(ftpfilelist.Files[i].FullName);
// finf.fullname = ftpfilelist.Files[i].FullName;
// finf.fsize = ftpfilelist.Files[i].Length.ToString();
// finf.ftype = "文件";
// m_filelist.Add(finf);
// }
// else
// {
// if (ftpfilelist.Files[i].Name != "..")
// {
// var ftpfilelist2 = m_session.ListDirectory(ftpfilelist.Files[i].FullName);
// for (var j = 0; j <= ftpfilelist2.Files.Count - 1; j++)
// {
// if (!ftpfilelist2.Files[j].IsDirectory && ftpfilelist2.Files[j].Name != "..")
// {
// var finf = new fileprop();
// finf.fdate = ftpfilelist2.Files[j].LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
// finf.fname = ftpfilelist2.Files[j].Name;
// finf.fullname = ftpfilelist2.Files[j].FullName;
// finf.fsize = ftpfilelist2.Files[j].Length.ToString();
// finf.ftype = "文件";
// m_filelist.Add(finf);
// }
// }
// }
// }
// }
//}
//catch (Exception e)
//{
//}
FTPDA FTPDA = new FTPDA();
try
{
FTPDA.ErrorMsg = "";
FTPDA.Uri = new Uri("ftp://" + FTPADDRESS + "/");
FTPDA.DirectoryPath = FTPPATH.Replace("/", "//");
FTPDA.UserName = FTPUSERNAME;
FTPDA.Password = FTPPWD;
log.Debug($"FTP连接:{FTPADDRESS},{FTPPATH},{FTPUSERNAME},{FTPPWD}");
var filearray = FTPDA.ListFiles();
var errmsg = FTPDA.ErrorMsg;
log.Debug($"FTP错误日志"+errmsg);
if (filearray == null || filearray.Count() == 0)
{
log.Debug($"没有可读取的文件,进程结束");
return;
}
log.Debug($"发现文件数:{filearray.Count()}");
var filepath = AppDomain.CurrentDomain.BaseDirectory + @"\FTPDOWNLOADFILE\" + DateTime.Now.ToString("yyyy-MM-dd") + @"\";
if (Directory.Exists(filepath) == false)
{
Directory.CreateDirectory(filepath);
log.Debug($"服务器本地建立文件夹:{filepath}");
}
foreach (FileStruct file in filearray)
{
if (!file.IsDirectory)
{
string LocalFullPath = Path.Combine(filepath, file.Name);
if (File.Exists(LocalFullPath))
{
log.Debug($"当前路径下已经存在同名文件:{LocalFullPath}");
FTPDA.DeleteFile(file.Name);
}
else
if (FTPDA.DownloadFile(file.Name, filepath))
{
log.Debug($"下载文件:{LocalFullPath}");
var tmpfile = file.Name;
if (tmpfile.IndexOf(".") > 0)
{
var tmpfilelist = tmpfile.Split('.');
if (tmpfilelist.Length >= 3 && tmpfilelist[2] == "CONTRL")
{
var filename = filepath + file.Name;
var headList = "";
var custno = "";
try
{
FileStream fs = new FileStream(filename, FileMode.Open);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.GetEncoding("gb2312"));
string line;
while ((line = sr.ReadLine()) != null && line != "")
{
headList = headList + line;
}
headList = headList.Replace("?'", "し");
string[] StrList = headList.Split('\'');
for (var i = 0; i <= StrList.Length - 1; i++)
{
line = StrList[i];
if (line.Length > 3)
{
var head = line.Substring(0, 3);
if (head == "UCM")
{
custno = GetPosStringplus(line, 2);
if (!string.IsNullOrEmpty(custno))
{
using (SqlConnection dbcon = new SqlConnection(connStr))
{
dbcon.Open();
string sql = " insert op_status (ST_ID,BSNO,STATUS,ISCOMP,COMPTIME,COMPOP,INPUTTIME,STTYPE) Select NEWID(),BSNO,'订舱已接收',1,GETDATE(),'ADMIN',GETDATE(),'1' FROM OP_SEAE WHERE CUSTNO='" + custno + "'";
SqlCommand cmd = new SqlCommand(sql, dbcon);
cmd.ExecuteNonQuery();
dbcon.Close();
}
}
}
}
}
}
catch
{
}
}
}
FTPDA.DeleteFile(file.Name);
}
}
}
log.Debug($"连接字符串:{connStr}");
}
catch (Exception ex)
{
log.Error(ex.Message);
}
}
catch (Exception ex)
{
log.Error(ex.Message);
log.Error(ex.StackTrace);
}
}
public static string GetPosStringplus(string str, int pos, bool isnum = false)
{
var result = "";
if (isnum) result = "0";
var j = 0;
if (str.IndexOf("+") == -1) return result;
str = str.Replace("??", "?").Replace("?'", "'").Replace("?+", "し");
for (int i = 1; i < pos; i++)
{
if (str.IndexOf("+") == -1) return result;
if (str.IndexOf("+") != -1)
{
j = str.IndexOf("+");
str = str.Substring(j + 1, str.Length - j - 1);
}
}
j = str.IndexOf("+");
if (j != -1)
{
str = str.Substring(0, j);
}
else
{
if (str.Length != 0)
str = str.Substring(0, str.Length);
}
result = str.Replace("し", "+");
if (isnum)
{
if (result == "")
result = "0";
}
return result;
}
static public string GETBSNO(string MBLNO, SqlConnection dbcon)
{
var strSql = new StringBuilder();
strSql.Append("SELECT BSNO from V_OP_BS where MBLNO='" + MBLNO + "'");
SqlDataAdapter adapter = new SqlDataAdapter(strSql.ToString(), dbcon);
DataTable table = new DataTable();
adapter.Fill(table);
var BSNO = "";
if (table.Rows.Count > 0)
{
foreach (DataRow row in table.Rows)
{
BSNO = row["BSNO"].ToString();
}
}
return BSNO;
}
}
}