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/Job_BoschEdi_FTP/Job_BoschEdi_FTP.cs

219 lines
6.8 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using log4net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Quartz;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using HcUtility.Core;
using HcUtility.Comm;
using System.Data.Entity;
using System.Text.RegularExpressions;
using DSWeb.Common.DB;
using Terminal_CDMA;
using DSWeb.MvcShipping.Models.MsOpApply;
using DSWeb.SoftMng.Controllers;
using DSWeb.SoftMng.DAL;
using DSWeb.Areas.Mobile.DAL;
using Renci.SshNet;
namespace Job_BoschEdi_FTP
{
public class Job_Bosch_FTP : IJob
{
private ILog log = LogManager.GetLogger(typeof(Job_Bosch_FTP));
//private string connStr { get; set; }
private const string admin_userid = "1BEC90E1-9780-472F-90C2-0C6390C044A4";
private const string admin_username = "系统管理员";
private string connStr = "";
private string EDINAME = "";
private string LocalDS7 = "";
public void Execute(IJobExecutionContext context)
{
log.Debug($"Execute开始");
connStr = context.JobDetail.JobDataMap.GetString("ConnectString");
EDINAME = context.JobDetail.JobDataMap.GetString("EDINAME");
//1连接到数据库 查找code_ftpset 当中 ediname包含 BOSCH 的
//使用其中的 serverip sftpport username password tmpfoldername(要列表的文件夹)
//2打开sftp连接 列表文件
//3遍历列表
//3.1 下载文件 如果下载成功则
//
//3.1.1用ds7主项目的文件解析服务导入文件
//3.1.1.1如果成功则删除文件
//4遍历完成 关闭连接
try
{
CommonDataContext cdc = new CommonDataContext(connStr);
var ftpinfo = new code_FtpSet_md();
var ftpinfoList = cdc.code_FtpSet.Where(x => x.EDINAME == EDINAME).ToList();
if (ftpinfoList == null || ftpinfoList.Count == 0)
{
log.Error("没有找到BOSCH的FTP信息");
return;
}
else
{
ftpinfo = ftpinfoList[0];
}
var localpath = AppDomain.CurrentDomain.BaseDirectory + @"\XMLFILE\" + DateTime.Now.ToString("yyyy-MM-dd") + @"\";
if (Directory.Exists(localpath) == false)
{
Directory.CreateDirectory(localpath);
log.Debug($"服务器本地建立文件夹:{localpath}");
}
var remoatPath = ftpinfo.TMPFOLDERNAME.Replace(@"\", "/").EndsWith("/") ? ftpinfo.TMPFOLDERNAME : ftpinfo.TMPFOLDERNAME + "/";
//2打开sftp连接 列表文件
SFTPHelper FTPDA = new SFTPHelper(ftpinfo);
FTPDA.Connect();
try
{
var filenameList = FTPDA.sftp.ListDirectory(remoatPath);
if (filenameList != null && filenameList.Count() > 0)
{
foreach (var filename in filenameList)
{
if (filename.Length == 0) continue;
var byt = FTPDA.sftp.ReadAllBytes(filename.FullName);
var saveFileName = filename.Name;
if (Directory.Exists(localpath + saveFileName) == false)
{
File.WriteAllBytes(localpath + saveFileName, byt);
}
DBResult _r = DecHeadDAL.Deal381XML(localpath + saveFileName, admin_userid, admin_username, ftpinfo.CORPID, "DongShengDB");
if (_r.Success)
{
FTPDA.sftp.DeleteFile(filename.FullName);
log.Debug($"{filename}解析完成 删除文件");
}
else
{
log.Error($"{filename}解析失败:" + _r.Message);
}
}
}
}
catch (Exception ex)
{
log.Error("错误中断:" + ex.Message);
}
finally
{
FTPDA.Disconnect();
}
}
catch (Exception ex) {
log.Error("错误中断0:" + ex.Message);
}
}
private void DoSendMessage(string mblno) {
}
}
public class Job_SFTP_Upload : IJob
{
private ILog log = LogManager.GetLogger(typeof(Job_SFTP_Upload));
private string LocalPath = "";
private string RemotePath = "";
private string FTPADDRESS = "";
private string FTPPORT = "";
private string FTPUSERNAME = "";
private string FTPPASSWORD = "";
public void Execute(IJobExecutionContext context)
{
log.Debug($"Execute开始");
LocalPath = context.JobDetail.JobDataMap.GetString("LocalPath");
RemotePath = context.JobDetail.JobDataMap.GetString("RemotePath").Replace(@"\", "/");
FTPADDRESS = context.JobDetail.JobDataMap.GetString("FTPADDRESS");
FTPPORT = context.JobDetail.JobDataMap.GetString("FTPPORT");
FTPUSERNAME = context.JobDetail.JobDataMap.GetString("FTPUSERNAME");
FTPPASSWORD = context.JobDetail.JobDataMap.GetString("FTPPASSWORD");
try
{
//列表本地文件夹内的文件
string[] files = Directory.GetFiles(LocalPath, "*.*");
SFTPHelper FTPDA = new SFTPHelper(FTPADDRESS, FTPPORT, FTPUSERNAME, FTPPASSWORD);
FTPDA.Connect();
foreach (var item in files)
{
using (var file = System.IO.File.OpenRead(item))
{
var Filename = Path.GetFileName(item);
var remotefullpath = (RemotePath.EndsWith("/") ? RemotePath : RemotePath + "/") + Filename;
FTPDA.sftp.UploadFile(file, remotefullpath);
}
}
FTPDA.Disconnect();
//删除文件
foreach (var item in files)
{
File.Delete(item);
}
}
catch (Exception ex)
{
log.Error("错误中断:" + ex.Message);
}
}
private void DoSendMessage(string mblno)
{
}
}
}