using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Diagnostics;
using log4net;
using JobCreateFee;
namespace MailAnalyzeTools.Common
{
public class GuadanTool
{
private static ILog LogHelper = LogManager.GetLogger("GuanDan");
public static string GetNassServerPath()
{
//naspath:=get_parameters_value(175,'\\223.223.95.160\data');
// v_id:integer;default:string):string;
int vid = 175;
string defaultpath = @"\\223.223.95.160\data";
string sql = @"select 参数值 from t_sys_parameters_value where VL_ID=175";
string nassServerPath = DbHelperSQL.GetNassPath(sql);
if (!string.IsNullOrEmpty(nassServerPath))
{
defaultpath = nassServerPath.Trim();
}
return defaultpath;
}
///
///
///
///
///
///
///
public static bool TaskFileToNass(string realfilepath, string newreportid, int fromway)
{
bool isok = true;
return isok;
}
///
/// 挂单之前要验证委托的业务是否存在
///
///
///
///
///
///
/// 是否将提单号作为目标文件前缀
///
public static bool Guadan(string mainNo, string bsno, string filetype, string filename_src, out string guadanPath, bool isAddMblNoAsFilePre = false)
{
bool result;
double fsize;
int numunzipped;
DataTable qry_op_file;
int fid;
string naspath;
guadanPath = "";
filename_src = filename_src.Replace("/", "\\");
// 解决文件锁定的问题
try
{
result = true;
string insertsql;
//@ Undeclared identifier(3): 'get_parameters_value'
naspath = GetNassServerPath();
string strsql = string.Format("select * from [t_op_file] where 编号='{0}'", bsno);
//
qry_op_file = DbHelperSQL.QueryTable(strsql);
//
if (qry_op_file.Rows.Count <= 0)
{
insertsql = string.Format(@"INSERT INTO [t_op_file] ([编号],[文件类型],[备注]) VALUES('{0}','托书','是');
INSERT INTO [t_op_file] ([编号],[文件类型],[备注]) VALUES('{0}','入货通知','否');
INSERT INTO [t_op_file] ([编号],[文件类型],[备注]) VALUES('{0}','提单OK件','是');
INSERT INTO [t_op_file] ([编号],[文件类型],[备注]) VALUES('{0}','费用确认','是');
INSERT INTO [t_op_file] ([编号],[文件类型],[备注]) VALUES('{0}','提单留底','否');
INSERT INTO [t_op_file] ([编号],[文件类型],[备注]) VALUES('{0}','提单确认','否');
INSERT INTO [t_op_file] ([编号],[文件类型],[备注]) VALUES('{0}','舱单','否');
INSERT INTO [t_op_file] ([编号],[文件类型],[备注]) VALUES('{0}','报关资料','否');
INSERT INTO [t_op_file] ([编号],[文件类型],[备注]) VALUES('{0}','提单COPY','否');
INSERT INTO [t_op_file] ([编号],[文件类型],[备注]) VALUES('{0}','船公司账单','否');
INSERT INTO [t_op_file] ([编号],[文件类型],[备注]) VALUES('{0}','正本提单交回回执条','否');
INSERT INTO [t_op_file] ([编号],[文件类型],[备注]) VALUES('{0}','保函','否');
INSERT INTO [t_op_file] ([编号],[文件类型],[备注]) VALUES('{0}','船公司挂单','否');", bsno);
int i = DbHelperSQL.ExecuteSql(insertsql);
if (i < 1)
{
LogHelper.Debug("初始化挂单类型出错(GuadanTool):"+ insertsql);
}
}
strsql = string.Format("select * from [t_op_file] where 编号='{0}' and 文件类型='{1}'", bsno, filetype);
// 刷新取得FID
qry_op_file = DbHelperSQL.QueryTable(strsql);
if (qry_op_file.Rows.Count < 1)
{
insertsql = string.Format(@"INSERT INTO [t_op_file] ([编号],[文件类型],[备注]) VALUES('{0}','{1}','否');", bsno, filetype);
int i = DbHelperSQL.ExecuteSql(insertsql);
if (i < 1)
{
LogHelper.Debug("初始化挂单类型出错(GuadanTool):"+ insertsql);
}
}
// 刷新取得FID
qry_op_file = DbHelperSQL.QueryTable(strsql);
if (qry_op_file.Rows.Count < 1)
{
LogHelper.Debug( "初始化挂单类型出错(GuadanTool):");
return false;
}
// 上传附件
if (File.Exists(filename_src))
{
//// 拷贝到临时目录
////@ Undeclared identifier(3): 'GetWinTempPath'
//filename = GetWinTempPath() + Path.GetFileName(filename_src);
//if ((File.Exists(filename)))
//{
// File.Delete(filename);
//}
//File.Copy((filename_src as string), (filename as string), false);
FileInfo flinfo = new FileInfo(filename_src);
fsize = flinfo.Length;
//@ Unsupported property or method(A): 'fieldbyname'
//@ Unsupported property or method(D): 'AsInteger'
fid = int.Parse(qry_op_file.Rows[0]["FID"].ToString());
try
{
if (UpLoadProFile(filename_src, naspath, "user", "user", mainNo, bsno, fid, filetype, out guadanPath, isAddMblNoAsFilePre))
{
// 更新数量及任务路径
}
updateGuadanFilecount(fid, bsno);
}
finally
{
}
}
else
{
LogHelper.Debug("文件不存在或者不能访问 !"+filename_src);
result = false;
return result;
}
}
catch (Exception ex)
{
result = false;
LogHelper.Debug("文件不存在或者不能访问 !" + filename_src+ex.Message);
}
return result;
}
///
/// 电子文档挂单
///
///
///
///
///
/// 主提单号
///
///
///
///
/// 是否在目标文件名前加[mblNo]前缀:true表示加,反之不加
///
public static bool UpLoadProFile(string filename_src, string RemotePathName, string Users, string pw, string mblNo, string bsno, int fid, string filetype, out string newpath, bool isAddMblNoAsFilePre = false)
{
string path;
string filePath;
string filepath_pre;
string filename;
string fileExt;
// 上传文件
newpath = "";
filename = Path.GetFileName(filename_src);
fileExt = Path.GetExtension(filename);
bool status = true;
//连接共享文件夹
//status = connectState(RemotePathName, Users, pw);
if (status)
{
//共享文件夹的目录
DirectoryInfo theFolder = new DirectoryInfo(RemotePathName);
//相对共享文件夹的路径
//string fielpath = @"\123\456\";
// filePath = FormatDateTime("YYYYMM",DateTime.Today);
filePath = DateTime.Today.ToString("yyyyMM");
if (!Directory.Exists(RemotePathName + '\\' + filePath))
{
Directory.CreateDirectory(RemotePathName + '\\' + filePath);
}
if (!Directory.Exists(RemotePathName + '\\' + filePath + '\\' + mblNo))
{
Directory.CreateDirectory(RemotePathName + '\\' + filePath + '\\' + mblNo);
}
// 增加类型
filepath_pre = RemotePathName + '\\' + filePath + '\\' + mblNo.Trim() + '\\' + filetype;
if (!Directory.Exists(filepath_pre))
{
Directory.CreateDirectory(filepath_pre);
}
///newpath = filepath_pre + '\\' + filename;
if (isAddMblNoAsFilePre)
{
filename = mblNo.Trim() + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + filename;
}
newpath = filepath_pre + '\\' + filename;
//获取保存文件的路径
string filePathTarget = filepath_pre;// theFolder.ToString() + fielpath;
//执行方法
Transport(filename_src, filePathTarget, filename);
}
else
{
return false;
}
FileInfo flinfo = new FileInfo(filename_src);
long fsize = flinfo.Length;
string insertsql_dbfile_pre = "INSERT INTO [t_op_file_items]([编号],[名称], [大小],[上传日期],[所有者],[类型],[文件类型],[Pid],[文件存储])"
+ "VALUES('{0}','{1}','{2}',GETDATE(),'DEMO-SA','{3}','{4}','{5}',1);";
string insertsql_dbfile = string.Format(insertsql_dbfile_pre, new object[] { bsno, newpath, fsize, fileExt, filetype, fid });
int i = DbHelperSQL.ExecuteSql_NassDb(insertsql_dbfile);
return i >= 0;
}
public static bool updateGuadanFilecount(int fid, string bsno)
{
string sqlCount;
sqlCount = string.Format("select count(fid) as ncount from t_op_file_items where PID='{0}' and 编号='{1}';", fid, bsno);
string ncount = DbHelperSQL.GetNassPath(sqlCount);
string updatecount = string.Format("Update t_op_file set 大小='{0}' where FID='{1}'", ncount, fid);
int i = DbHelperSQL.ExecuteSql(updatecount);
return i > 0;
}
///
/// 任务文件路径复制到Nass
///
///
///
///
///
///
///
/// 是否使用主提单号[mblNo]作为上传文件名的前缀
///
public static bool UpLoadFileToNass(string extPath, string filename_src, string mblNo, string filetype, out string newpath, string bsno = null, bool isAddMblNoAsFilePre = false)
{
newpath = "";
string filePath;
string filepath_pre;
string filename;
string fileExt;
string RemotePathName, Users, pw;
bool isok = true;
filename_src = filename_src.Replace("/", "\\");
try
{
// 解决文件锁定的问题
string insertsql;
//@ Undeclared identifier(3): 'get_parameters_value'
RemotePathName = GetNassServerPath();
Users = "user";
pw = "user";
// 上传文件
newpath = "";
filename = Path.GetFileName(filename_src);
fileExt = Path.GetExtension(filename);
bool status = false;
//连接共享文件夹
status = connectState(RemotePathName, Users, pw);
if (status)
{
//共享文件夹的目录
DirectoryInfo theFolder = new DirectoryInfo(RemotePathName);
//相对共享文件夹的路径:Task任务
if (!string.IsNullOrEmpty(extPath))
{
RemotePathName += '\\' + extPath;
if (!Directory.Exists(RemotePathName))
{
Directory.CreateDirectory(RemotePathName);
}
}
filePath = DateTime.Today.ToString("yyyyMM");
if (!Directory.Exists(RemotePathName + '\\' + filePath))
{
Directory.CreateDirectory(RemotePathName + '\\' + filePath);
}
if (string.IsNullOrEmpty(mblNo))
{
if (!string.IsNullOrEmpty(bsno))
mblNo = bsno.Trim();
else mblNo = "tmp" + DateTime.Today.ToString("yyyyMM");
}
if (!Directory.Exists(RemotePathName + '\\' + filePath + '\\' + mblNo))
{
Directory.CreateDirectory(RemotePathName + '\\' + filePath + '\\' + mblNo);
}
// 增加类型
if (string.IsNullOrEmpty(filetype))
{
filetype = "未指定";
}
filepath_pre = RemotePathName + '\\' + filePath + '\\' + mblNo.Trim() + '\\' + filetype;
if (!Directory.Exists(filepath_pre))
{
Directory.CreateDirectory(filepath_pre);
}
///newpath = filepath_pre + '\\' + filename;
if (isAddMblNoAsFilePre)
{
//filename = mblNo.Trim() + "_" + filename;
filename = mblNo.Trim() + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + filename;
}
// out 参数,目标文件名
newpath = filepath_pre + '\\' + filename;
//获取保存文件的路径
string filePathTarget = filepath_pre;// theFolder.ToString() + fielpath;
//执行方法
Transport(filename_src, filePathTarget, filename);
isok = true;
}
else
{
isok = false;
}
}
catch (Exception ex)
{
LogHelper.Debug("复制文件到Nass服务器发生错误"+ex.Message);
isok = false;
}
return isok;
}
///
/// 匿名连接
///
///
///
public static bool connectState(string path)
{
return connectState(path, "", "");
}
///
/// 连接远程共享文件夹
///
/// 远程共享文件夹的路径
/// 用户名
/// 密码
///
public static bool connectState(string path, string userName, string passWord)
{
bool Flag = false;
Process proc = new Process();
try
{
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
string dosLine = "net use " + path + " " + passWord + " /user:" + userName;
proc.StandardInput.WriteLine(dosLine);
proc.StandardInput.WriteLine("exit");
while (!proc.HasExited)
{
proc.WaitForExit(1000);
}
string errormsg = proc.StandardError.ReadToEnd();
proc.StandardError.Close();
if (string.IsNullOrEmpty(errormsg))
{
Flag = true;
}
else
{
throw new Exception(errormsg);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
proc.Close();
proc.Dispose();
}
return Flag;
}
///
/// 向远程文件夹保存本地内容,或者从远程文件夹下载文件到本地
///
/// 要保存的文件的路径,如果保存文件到共享文件夹,这个路径就是本地文件路径如:@"D:\1.avi"
/// 保存文件的路径,不含名称及扩展名
/// 保存文件的名称以及扩展名
public static void Transport(string src, string dst1, string fileName)
{
//FileStream inFileStream = new FileStream(src, FileMode.Open);
try
{
if (!Directory.Exists(dst1))
{
Directory.CreateDirectory(dst1);
}
string dst = Path.Combine(dst1, fileName);
if (File.Exists(dst))
{
try
{
File.Delete(dst);
}
catch (Exception)
{
File.Move(dst, dst + ".bak");
}
}
File.Copy(src, dst);
}
catch (Exception ex) {
LogHelper.Debug("复制文件到Nass服务器发生错误" + ex.Message);
}
//FileStream outFileStream = new FileStream(dst, FileMode.OpenOrCreate);
//byte[] buf = new byte[inFileStream.Length];
//int byteCount;
//while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0)
//{
// outFileStream.Write(buf, 0, byteCount);
//}
//inFileStream.Flush();
//inFileStream.Close();
//outFileStream.Flush();
//outFileStream.Close();
}
}
}