Exemplo n.º 1
0
        public void DownloadFile()
        {
            //log.Info("Start download file from ftp according to FtpControl table.");
            IList<FtpControl> ftpControlList = this.genericMgr.FindAll<FtpControl>("from FtpControl fc where IOType=?", BusinessConstants.IO_TYPE_IN);
            //dssFtpControlMgr.GetDssFtpControl(BusinessConstants.IO_TYPE_IN);

            if (ftpControlList != null && ftpControlList.Count > 0)
            {
                foreach (FtpControl ftpControl in ftpControlList)
                {
                    string ftpServer = string.Empty;
                    int ftpPort = 21;
                    string[] ftpInboundFolders;
                    string ftpUser = string.Empty;
                    string ftpPass = string.Empty;
                    string filePattern = string.Empty;
                    string localTempFolder = string.Empty;
                    string localFolder = string.Empty;
                    string ftpBackupFolder = string.Empty;
                    string vaildFilePattern = string.Empty;
                    try
                    {
                        #region 获取ftp参数
                        ftpServer = ftpControl.FtpServer;
                        ftpPort = ftpControl.FtpPort.HasValue ? ftpControl.FtpPort.Value : 21;
                        ftpInboundFolders = ftpControl.FtpFolder.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                        ftpBackupFolder = ftpControl.FtpTempFolder;
                        ftpUser = ftpControl.FtpUser;
                        ftpPass = ftpControl.FtpPassword;
                        filePattern = ftpControl.FilePattern;
                        vaildFilePattern = ftpControl.VaildFilePattern;
                        #endregion

                        #region 初始化本地目录
                        localTempFolder = ftpControl.LocalTempFolder;
                        localTempFolder = localTempFolder.Replace("\\", "/");
                        if (!localTempFolder.EndsWith("/"))
                        {
                            localTempFolder += "/";
                        }
                        if (!Directory.Exists(localTempFolder))
                        {
                            Directory.CreateDirectory(localTempFolder);
                        }

                        localFolder = ftpControl.LocalFolder;
                        localFolder = localFolder.Replace("\\", "/");
                        if (!localFolder.EndsWith("/"))
                        {
                            localFolder += "/";
                        }
                        if (!Directory.Exists(localFolder))
                        {
                            Directory.CreateDirectory(localFolder);
                        }
                        #endregion

                        IList<InboundControl> inboundCtrlList = this.genericMgr.FindAll<InboundControl>();

                        #region 下载文件
                        foreach (var ftpInboundFolder in ftpInboundFolders)
                        {
                            InboundControl inboundCtrl = this.genericMgr.FindAll<InboundControl>("from InboundControl ic where ic.FtpFolder=?", ftpInboundFolder).SingleOrDefault();
                            FtpHelper ftp = new FtpHelper(ftpServer, ftpPort, ftpInboundFolder, ftpUser, ftpPass);
                            foreach (string vaildFileName in ftp.GetFileList(vaildFilePattern))
                            {
                                string fileName = filePattern.Replace("*", vaildFileName.Substring(0, vaildFileName.IndexOf(".")));
                                string inFolder = inboundCtrl.InFloder;
                                inFolder = inFolder.Replace("\\", "/");
                                if (!inFolder.EndsWith("/"))
                                {
                                    inFolder += "/";
                                }
                                if (!Directory.Exists(inFolder))
                                {
                                    Directory.CreateDirectory(inFolder);
                                }
                                try
                                {
                                    ftp.Download(inFolder, vaildFileName);//CTL
                                    ftp.Download(inFolder, fileName);//DAT
                                    
                                    //log.Info("Move file from folder: " + localTempFolder + vaildFileName + " to folder: " + localFolder + vaildFileName);
                                    File.Copy(inFolder + fileName, localTempFolder + fileName);
                                    //if (ftpBackupFolder != null && ftpBackupFolder.Length > 0)
                                    //{
                                    //    ftp.MovieFile(vaildFileName, ftpBackupFolder);
                                    //    ftp.MovieFile(fileName, ftpBackupFolder);
                                    //    //ftp.MovieFile(fileName, ftpControl.FtpBackUp);
                                    //    //ftp.MovieFile(fileName.Substring(0, fileName.IndexOf("."))+".CTL", ftpControl.FtpBackUp);
                                    //}
                                    //else
                                    //{
                                        ftp.Delete(vaildFileName);
                                        ftp.Delete(fileName);
                                    //}
                                    
                                }
                                catch (Exception ex)
                                {
                                    log.Error("Download file:" + vaildFileName, ex);
                                    //ftp.MovieFile(fileName, ftpControl.FtpErrorFolder);
                                    //ftp.MovieFile(fileName.Substring(0, fileName.IndexOf(".")) + ".CTL", ftpControl.FtpErrorFolder);
                                }
                            }
                        }

                        #endregion
                    }
                    catch (Exception ex)
                    {
                        log.Error("Download files from ftpServer:" + ftpServer, ex);
                    }
                }
            }
            else
            {
                log.Info("No record found in FtpControl table.");
            }

            //log.Info("End download file from ftp according to FtpControl table.");
        }
        private void DownloadFile()
        {
            log.Info("Start download file from ftp according to FtpControl table.");
            IList<DssFtpControl> dssFtpControlList = this.dssFtpControlMgr.GetDssFtpControl(BusinessConstants.IO_TYPE_IN);

            if (dssFtpControlList != null && dssFtpControlList.Count > 0)
            {
                foreach (DssFtpControl dssFtpControl in dssFtpControlList)
                {
                    string ftpServer = string.Empty;
                    int ftpPort = 21;
                    string ftpInboundFolder = string.Empty;
                    string ftpUser = string.Empty;
                    string ftpPass = string.Empty;
                    string filePattern = string.Empty;
                    string localTempFolder = string.Empty;
                    string localFolder = string.Empty;
                    string ftpBackupFolder = string.Empty;
                    try
                    {
                        #region 获取ftp参数
                        ftpServer = dssFtpControl.FtpServer;
                        ftpPort = dssFtpControl.FtpPort.HasValue ? dssFtpControl.FtpPort.Value : 21;
                        ftpInboundFolder = dssFtpControl.FtpFolder;
                        ftpBackupFolder = dssFtpControl.FtpTempFolder;
                        ftpUser = dssFtpControl.FtpUser;
                        ftpPass = dssFtpControl.FtpPassword;
                        filePattern = dssFtpControl.FilePattern;
                        #endregion

                        #region 初始化本地目录
                        localTempFolder = dssFtpControl.LocalTempFolder;
                        localTempFolder = localTempFolder.Replace("\\", "/");
                        if (!localTempFolder.EndsWith("/"))
                        {
                            localTempFolder += "/";
                        }
                        if (!Directory.Exists(localTempFolder))
                        {
                            Directory.CreateDirectory(localTempFolder);
                        }

                        localFolder = dssFtpControl.LocalFolder;
                        localFolder = localFolder.Replace("\\", "/");
                        if (!localFolder.EndsWith("/"))
                        {
                            localFolder += "/";
                        }
                        if (!Directory.Exists(localFolder))
                        {
                            Directory.CreateDirectory(localFolder);
                        }
                        #endregion

                        #region 下载文件
                        FtpHelper ftp = new FtpHelper(ftpServer, ftpPort, ftpInboundFolder, ftpUser, ftpPass);
                        foreach (string fileName in ftp.GetFileList(filePattern))
                        {
                            try
                            {
                                ftp.Download(localTempFolder, fileName);
                                log.Info("Move file from folder: " + localTempFolder + fileName + " to folder: " + localFolder + fileName);
                                File.Move(localTempFolder + fileName, localFolder + fileName);
                                if (ftpBackupFolder != null && ftpBackupFolder.Length > 0)
                                {
                                    ftp.MovieFile(fileName, ftpBackupFolder);
                                }
                                else
                                {
                                    ftp.Delete(fileName);
                                }
                            }
                            catch (Exception ex)
                            {
                                log.Error("Download file:" + fileName, ex);
                            }
                        }
                        #endregion
                    }
                    catch (Exception ex)
                    {
                        log.Error("Download files from ftpServer:" + ftpServer, ex);
                    }
                }
            }
            else
            {
                log.Info("No record found in FtpControl table.");
            }

            log.Info("End download file from ftp according to FtpControl table.");
        }
        public void Execute(JobRunContext context)
        {
            #region 获取参数
            JobDataMap jobDataMap = context.JobDataMap;
            string inboundDirectoryName = string.Empty;
            string archiveDirectoryName = string.Empty;
            string errorDirectoryName = string.Empty;
            string ftpServer = string.Empty;
            int ftpPort = 21;
            string ftpInboundFolder = string.Empty;
            string ftpUser = string.Empty;
            string ftpPass = string.Empty;
            string localTempFolder = string.Empty;

            if (jobDataMap.ContainKey(INBOUND_DIRECTORY))
            {
                inboundDirectoryName = jobDataMap.GetStringValue(INBOUND_DIRECTORY);
            }
            else
            {
                throw new BusinessErrorException("not specify inbound directory.");
            }

            if (jobDataMap.ContainKey(ARCHIVE_DIRECTORY))
            {
                archiveDirectoryName = jobDataMap.GetStringValue(ARCHIVE_DIRECTORY);
            }
            else
            {
                throw new BusinessErrorException("not specify archive directory.");
            }

            if (jobDataMap.ContainKey(ERROR_DIRECTORY))
            {
                errorDirectoryName = jobDataMap.GetStringValue(ERROR_DIRECTORY);
            }
            else
            {
                throw new BusinessErrorException("not specify error directory.");
            }

            if (jobDataMap.ContainKey(FTP_SERVER))
            {
                ftpServer = jobDataMap.GetStringValue(FTP_SERVER);
            }
            else
            {
                throw new BusinessErrorException("not ftp server address.");
            }

            if (jobDataMap.ContainKey(FTP_PORT))
            {
                ftpPort = jobDataMap.GetIntValue(FTP_PORT);
            }
            else
            {
                log.Info("using default ftp port 21.");
            }

            if (jobDataMap.ContainKey(FTP_INBOUND_FOLDER))
            {
                ftpInboundFolder = jobDataMap.GetStringValue(FTP_INBOUND_FOLDER);
            }
            else
            {
                throw new BusinessErrorException("not specify ftp inbound folder.");
            }

            if (jobDataMap.ContainKey(FTP_USER))
            {
                ftpUser = jobDataMap.GetStringValue(FTP_USER);
            }
            else
            {
                throw new BusinessErrorException("not specify ftp user.");
            }

            if (jobDataMap.ContainKey(FTP_PASSWORD))
            {
                ftpPass = jobDataMap.GetStringValue(FTP_PASSWORD);
            }
            else
            {
                throw new BusinessErrorException("not specify ftp password.");
            }

            if (jobDataMap.ContainKey(LOCAL_TEMP_FOLDER))
            {
                localTempFolder = jobDataMap.GetStringValue(LOCAL_TEMP_FOLDER);
            }
            else
            {
                throw new BusinessErrorException("not specify local temp folder.");
            }
            #endregion

            #region 下载WO文件
            try
            {
                #region 初始化本地目录
                localTempFolder = localTempFolder.Replace("\\", "/");
                if (!localTempFolder.EndsWith("/"))
                {
                    localTempFolder += "/";
                }
                if (!Directory.Exists(localTempFolder))
                {
                    Directory.CreateDirectory(localTempFolder);
                }
         
                inboundDirectoryName = inboundDirectoryName.Replace("\\", "/");
                if (!inboundDirectoryName.EndsWith("/"))
                {
                    inboundDirectoryName += "/";
                }
                if (!Directory.Exists(inboundDirectoryName))
                {
                    Directory.CreateDirectory(inboundDirectoryName);
                }
                #endregion

                #region 下载文件
                FtpHelper ftp = new FtpHelper(ftpServer, ftpPort, ftpInboundFolder, ftpUser, ftpPass);
                string[] fileList = ftp.GetFileList();
                foreach (string fileName in fileList)
                {
                    try
                    {
                        ftp.Download(localTempFolder, fileName);
                        log.Info("Move file from folder: " + localTempFolder + fileName + " to folder: " + inboundDirectoryName + fileName);
                        File.Move(localTempFolder + fileName, inboundDirectoryName + fileName);
                        ftp.Delete(fileName);
                    }
                    catch (Exception ex)
                    {
                        log.Error("Download file:" + fileName, ex);
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                log.Error("Download files from ftpServer:" + ftpServer, ex);
            }
            #endregion

            string[] inboundFiles = Directory.GetFiles(inboundDirectoryName);
            if (inboundFiles != null && inboundFiles.Length > 0)
            {
                foreach (string inboundFile in inboundFiles)
                {
                    string fileName = inboundFile.Substring(inboundFile.LastIndexOf("/"));

                    
                    try
                    {
                        ProcessSingleFile(inboundDirectoryName, inboundFile);

                        if (!Directory.Exists(archiveDirectoryName))
                        {
                            Directory.CreateDirectory(archiveDirectoryName);
                        }

                        if (File.Exists(archiveDirectoryName + fileName))
                        {
                            File.Delete(archiveDirectoryName + fileName);
                        }

                        File.Move(inboundFile, archiveDirectoryName + fileName);
                    }
                    catch (Exception ex)
                    {
                        log.Error("Unexpected error occour when processing inbound file " + inboundFile, ex);
                        if (!Directory.Exists(errorDirectoryName))
                        {
                            Directory.CreateDirectory(errorDirectoryName);
                        }

                        if (File.Exists(errorDirectoryName + fileName))
                        {
                            File.Delete(errorDirectoryName + fileName);
                        }

                        File.Move(inboundFile, errorDirectoryName + fileName);
                    }
                }
            }
        }
Exemplo n.º 4
0
        private void UploadFile()
        {
            log.Info("Start upload file to ftp according to FtpControl table.");
            IList<DssFtpControl> dssFtpControlList = this.dssFtpControlMgr.GetDssFtpControl(BusinessConstants.IO_TYPE_OUT);

            if (dssFtpControlList != null && dssFtpControlList.Count > 0)
            {
                foreach (DssFtpControl dssFtpControl in dssFtpControlList)
                {
                    string ftpServer = string.Empty;
                    int ftpPort = 21;
                    string ftpTempFolder = string.Empty;
                    string ftpFolder = string.Empty;
                    string ftpUser = string.Empty;
                    string ftpPass = string.Empty;
                    string filePattern = string.Empty;
                    string localFolder = string.Empty;
                    try
                    {
                        #region 获取参数
                        ftpServer = dssFtpControl.FtpServer;
                        ftpPort = dssFtpControl.FtpPort.HasValue ? dssFtpControl.FtpPort.Value : 21;
                        ftpTempFolder = dssFtpControl.FtpTempFolder;
                        ftpFolder = dssFtpControl.FtpFolder;
                        ftpUser = dssFtpControl.FtpUser;
                        ftpPass = dssFtpControl.FtpPassword;
                        filePattern = dssFtpControl.FilePattern;
                        localFolder = dssFtpControl.LocalFolder;
                        #endregion

                        #region 初始化远程目录
                        FtpHelper ftp = new FtpHelper(ftpServer, ftpPort, ftpTempFolder, ftpUser, ftpPass);
                        //ftp.Login();
                        //ftp.ChangeDir(ftpTempFolder);
                        //ftp.BinaryMode = true;

                        ftpTempFolder = ftpTempFolder.Replace("\\", "/");
                        if (!ftpTempFolder.EndsWith("/"))
                        {
                            ftpTempFolder += "/";
                        }

                        try
                        {
                            //清空Temp目录
                            foreach (string fileName in ftp.GetFileList(filePattern))
                            {

                                ftp.Delete(fileName);

                            }
                        }
                        catch (Exception)
                        {
                        }
                        //if (!ftp.DirectoryExist(ftpTempFolder))
                        //{
                        //    ftp.MakeDir(ftpTempFolder);
                        //}

                        ftpFolder = ftpFolder.Replace("\\", "/");
                        if (!ftpFolder.EndsWith("/"))
                        {
                            ftpFolder += "/";
                        }
                        //if (!ftp.DirectoryExist(ftpFolder))
                        //{
                        //    ftp.MakeDir(ftpFolder);
                        //}
                        #endregion

                        #region 获取本地上传文件列表
                        string[] files = null;
                        if (filePattern != null)
                        {
                            files = Directory.GetFiles(localFolder, filePattern);
                        }
                        else
                        {
                            files = Directory.GetFiles(localFolder);
                        }
                        #endregion

                        #region 上传文件
                        if (files != null && files.Length > 0)
                        {
                            foreach (string fileFullPath in files)
                            {
                                try
                                {
                                    string fomatedFileFullPath = fileFullPath.Replace("\\", "/");
                                    string fileName = fomatedFileFullPath.Substring(fomatedFileFullPath.LastIndexOf("/") + 1);
                                    log.Info("Upload file: " + fomatedFileFullPath);
                                    ftp.Upload(fomatedFileFullPath);
                                }
                                catch (Exception ex)
                                {
                                    log.Error("Upload file:" + fileFullPath, ex);
                                }
                            }

                            foreach (string fileFullPath in files)
                            {
                                try
                                {
                                    string fomatedFileFullPath = fileFullPath.Replace("\\", "/");
                                    string fileName = fomatedFileFullPath.Substring(fomatedFileFullPath.LastIndexOf("/") + 1);
                                    log.Info("Move file: " + fomatedFileFullPath);
                                    ftp.Rename(fileName, ftpFolder + fileName);
                                    log.Info("Delete file: " + fomatedFileFullPath);
                                    File.Delete(fomatedFileFullPath);
                                }
                                catch (Exception ex)
                                {
                                    log.Error("Upload file:" + fileFullPath, ex);
                                }
                            }
                        }
                        #endregion
                    }
                    catch (Exception ex)
                    {
                        log.Error("Upload files from ftpServer:" + ftpServer, ex);
                    }
                }
            }
            else
            {
                log.Info("No record found in FtpControl table.");
            }

            log.Info("End upload file to ftp according to FtpControl table.");
        }
Exemplo n.º 5
0
        public void UploadFile()
        {
            //log.Info("Start upload file to ftp according to FtpControl table.");
            IList<FtpControl> ftpControlList = this.genericMgr.FindAll<FtpControl>("from FtpControl fc where IOType=? ", BusinessConstants.IO_TYPE_OUT);

            if (ftpControlList != null && ftpControlList.Count > 0)
            {
                foreach (FtpControl ftpControl in ftpControlList)
                {
                    string ftpServer = string.Empty;
                    int ftpPort = 21;
                    string ftpTempFolder = string.Empty;
                    string ftpFolder = string.Empty;
                    string ftpUser = string.Empty;
                    string ftpPass = string.Empty;
                    string filePattern = string.Empty;
                    string localFolder = string.Empty;
                    string localTempFolder = string.Empty;
                    try
                    {
                        #region 获取参数
                        ftpServer = ftpControl.FtpServer;
                        ftpPort = ftpControl.FtpPort.HasValue ? ftpControl.FtpPort.Value : 21;
                        ftpTempFolder = ftpControl.FtpTempFolder;
                        ftpFolder = ftpControl.FtpFolder;
                        ftpUser = ftpControl.FtpUser;
                        ftpPass = ftpControl.FtpPassword;
                        filePattern = ftpControl.FilePattern;
                        localFolder = ftpControl.LocalFolder;
                        localTempFolder = ftpControl.LocalTempFolder;
                        #endregion

                        #region 初始化远程目录
                        FtpHelper ftp = new FtpHelper(ftpServer, ftpPort, ftpFolder, ftpUser, ftpPass);
                        
                        //ftpTempFolder = ftpTempFolder.Replace("\\", "/");
                        //if (!ftpTempFolder.EndsWith("/"))
                        //{
                        //    ftpTempFolder += "/";
                        //}

                        //try
                        //{
                        //    //清空Temp目录
                        //    foreach (string fileName in ftp.GetFileList(filePattern))
                        //    {

                        //        ftp.Delete(fileName);

                        //    }
                        //}
                        //catch (Exception)
                        //{
                        //}
                        //if (!ftp.DirectoryExist(ftpTempFolder))
                        //{
                        //    ftp.MakeDir(ftpTempFolder);
                        //}

                        ftpFolder = ftpFolder.Replace("\\", "/");
                        if (!ftpFolder.EndsWith("/"))
                        {
                            ftpFolder += "/";
                        }
                        //if (!ftp.DirectoryExist(ftpFolder))
                        //{
                        //    ftp.MakeDir(ftpFolder);
                        //}
                        #endregion

                        #region 获取本地上传文件列表
                        string[] files = null;
                        if (filePattern != null)
                        {
                            files = Directory.GetFiles(localFolder, filePattern);
                        }
                        else
                        {
                            files = Directory.GetFiles(localFolder);
                        }
                        #endregion

                        #region 上传文件
                        if (files != null && files.Length > 0)
                        {
                            foreach (string fileFullPath in files)
                            {
                                string fomatedFileFullPath = fileFullPath.Replace("\\", "/");
                                string fileName = fomatedFileFullPath.Substring(fomatedFileFullPath.LastIndexOf("/") + 1);
                                try
                                {
                                    ftp.Upload(fomatedFileFullPath); //上传
                                    if (ftp.FileExist(fileName))//检查是否已经存在FTP
                                    {
                                        if (ftpControl.VaildFilePattern != "BACKUP")//上传成功 写入ctl文件 表示完成 
                                        {
                                            string CTLName = fomatedFileFullPath.Substring(0, fomatedFileFullPath.LastIndexOf("/") + 1) + fileName.Replace(".DAT", ".CTL");
                                            this.CreateCTL(CTLName);
                                            ftp.Upload(CTLName);
                                            File.Delete(CTLName);
                                        }
                                        //else
                                        //{
                                        //    localTempFolder = localTempFolder.Replace("\\", "/");
                                        //    File.Copy(fomatedFileFullPath, localTempFolder+"/" + fileName);  //备份目录
                                        //}
                                        //log.Info("Delete file: " + fomatedFileFullPath);
                                        File.Delete(fomatedFileFullPath);
                                    }
                                    else
                                    {
                                        throw new Exception();
                                    }
                                   
                                }
                                catch (Exception ex)
                                {
                                    if (fileName.Substring(0, 5) == "ASNLE")
                                    {
                                        this.genericMgr.Update("update CreateIpDAT set IsCreateDat=?,FileName=? where FileName=?", new object[] { false, ex.Message.Length>50?ex.Message.Substring(0,49):ex.Message, fileName });
                                    }
                                    else if (fileName.Substring(0, 6) == "SEQ1LE")
                                    {
                                        this.genericMgr.Update("update CreateOrderDAT set IsCreateDat=?,FileName=? where FileName=?", new object[] { false, ex.Message.Length > 50 ? ex.Message.Substring(0, 49) : ex.Message, fileName });
                                    }
                                    else if (fileName.Substring(0, 4) == "SHIP")
                                    {
                                        this.genericMgr.Update("update CreateProcurementOrderDAT set IsCreateDat=?,FileName=? where FileName=?", new object[] { false, ex.Message.Length > 50 ? ex.Message.Substring(0, 49) : ex.Message, fileName });
                                    }
                                    else if (fileName.Substring(0, 5) == "LOGLE")
                                    {
                                        this.genericMgr.Update("update LesINLog set IsCreateDat=0 where HandTime>?", new object[] { System.DateTime.Now.AddHours(-1) });
                                    }
                                    else if (fileName.Substring(0, 4) == "CANC")
                                    {
                                        this.genericMgr.Update("update CancelReceiptMasterDAT set IsCreateDat =0, CreateDATDate=?,DATFileName=? where DATFileName=?", new object[] { System.DateTime.Now, ex.Message.Length > 50 ? ex.Message.Substring(0, 49) : ex.Message, fileName });
                                    }
                                    else if (fileName.Substring(0, 4) == "HUID")
                                    {
                                        this.genericMgr.Update("update CreateBarCode set DATFileName=?,CreateDATDate=?,IsCreateDat=0 where DATFileName=?", new object[] { ex.Message.Length > 50 ? ex.Message.Substring(0, 49) : ex.Message, System.DateTime.Now, fileName });
                                    }
                                    else if (fileName.Substring(0, 3) == "SEQ" && fileName.Substring(0, 6) != "SEQ1LE")
                                    {
                                        this.genericMgr.Update("update CreateSeqOrderDAT set IsCreateDat=?,FileName=? where FileName=? ", new object[] { false, ex.Message.Length > 50 ? ex.Message.Substring(0, 49) : ex.Message, fileName });
                                    }
                                    else if (fileName.Substring(0, 4) == "ITEM")
                                    {
                                        this.genericMgr.Update("update ItemStandardPackDAT set IsCreateDat=0, DATFileName=?,CreateDATDate=? where DATFileName=?", new object[] { ex.Message.Length > 50 ? ex.Message.Substring(0, 49) : ex.Message, System.DateTime.Now, fileName });
                                    }
                                    else if (fileName.Substring(0, 4) == "WARE")
                                    {
                                    }
                                    else if (fileName.Substring(0, 4) == "FLOW")
                                    {
                                    }
                                    else if (fileName.Substring(0, 4) == "RETU")
                                    {
                                        this.genericMgr.Update("update YieldReturn set IsCreateDat=0, DATFileName=?,CreateDATDate=? where DATFileName=?", new object[] { ex.Message.Length > 50 ? ex.Message.Substring(0, 49) : ex.Message, System.DateTime.Now, fileName });
                                    }
                                    log.Error("Upload file:" + fileFullPath, ex);
                                }
                            }
                        }
                        #endregion
                    }
                    catch (Exception ex)
                    {
                        log.Error("Upload files from ftpServer:" + ftpServer, ex);
                    }
                }
            }
            else
            {
                log.Info("No record found in FtpControl table.");
            }

            //log.Info("End upload file to ftp according to FtpControl table.");
        }
        private void Process(string ftpServer, int ftpPort, string ftpUser, string ftpPass, string ftpFolder,
                               string localFolder, string localTempFolder, IList<ErrorMessage> errorMessageList)
        {
            try
            {
                StringBuilder sql = new StringBuilder();
                sql.Append(
                    " select r.Plant,l.SAPLocation,det.Item,");
                sql.Append(
                    " CASE WHEN det.IsCS = 1 THEN 'K' ELSE '' END AS IsCS, ");
                sql.Append("det.CSSupplier, ");
                sql.Append("SUM(CASE WHEN det.QualityType = 0 THEN det.Qty ELSE 0 END) AS QualifyQty,");
                sql.Append("SUM(CASE WHEN det.QualityType = 1 THEN det.Qty ELSE 0 END) AS InspectQty,");
                sql.Append("SUM(CASE WHEN det.QualityType = 2 THEN det.Qty ELSE 0 END) AS RejectQty, ");
                sql.Append("i.Uom ");
                sql.Append(" from VIEW_LocationLotDet det,MD_Location l,MD_Region r,MD_Item i ");
                sql.Append(" where det.Location = l.Code and l.Region = r.Code and det.Item=i.Code ");
                sql.Append(" group by r.Plant,l.SAPLocation,det.Item,i.Uom,det.CSSupplier,det.IsCS ");
                var locLotDetList = this.genericMgr.FindAllWithNativeSql<object[]>(sql.ToString());
                if (locLotDetList == null || locLotDetList.Count == 0)
                {
                    log.Warn("没有库存数据");
                    return;
                }
                string ymd = DateTime.Now.AddDays(-1).ToString("yyyyMMdd");
                IWorkbook workbook = new HSSFWorkbook();

                ISheet sheet = workbook.CreateSheet(ymd);
                sheet.ForceFormulaRecalculation = true;

                ICellStyle headStyle = workbook.CreateCellStyle();
                IFont font = workbook.CreateFont();
                font.Boldweight = (short)FontBoldWeight.BOLD;
                font.FontHeightInPoints = (short)10;
                headStyle.SetFont(font);
                int rownum = 0;
                int colnum = 0;

                #region 列头

                XlsHelper.SetRowCell(sheet, rownum, colnum++, "工厂代码", headStyle);
                XlsHelper.SetRowCell(sheet, rownum, colnum++, "库存地点", headStyle);
                XlsHelper.SetRowCell(sheet, rownum, colnum++, "物料代码", headStyle);
                XlsHelper.SetRowCell(sheet, rownum, colnum++, "特殊标志", headStyle);
                XlsHelper.SetRowCell(sheet, rownum, colnum++, "供应厂商", headStyle);
                XlsHelper.SetRowCell(sheet, rownum, colnum++, "正常库存", headStyle);
                XlsHelper.SetRowCell(sheet, rownum, colnum++, "质检库存", headStyle);
                XlsHelper.SetRowCell(sheet, rownum, colnum++, "冻结库存", headStyle);
                XlsHelper.SetRowCell(sheet, rownum, colnum, "库存单位", headStyle);

                rownum++;

                #endregion

                #region 列表

                foreach (var locLotDet in locLotDetList)
                {
                    //工厂代码	库存地点	物料代码	特殊标志	供应厂商	正常库存	冻结库存	质检库存	库存单位
                    try
                    {
                        colnum = 0;
                        if (locLotDet[0] != null)
                            XlsHelper.SetRowCell(sheet, rownum, colnum, locLotDet[0].ToString());
                        colnum++;
                        if (locLotDet[1] != null)
                            XlsHelper.SetRowCell(sheet, rownum, colnum, locLotDet[1].ToString());
                        colnum++;
                        if (locLotDet[2] != null)
                            XlsHelper.SetRowCell(sheet, rownum, colnum, locLotDet[2].ToString());
                        colnum++;
                        if (locLotDet[3] != null)
                            XlsHelper.SetRowCell(sheet, rownum, colnum, locLotDet[3].ToString());
                        colnum++;
                        if (locLotDet[4] != null)
                            XlsHelper.SetRowCell(sheet, rownum, colnum, locLotDet[4].ToString());
                        colnum++;
                        if (locLotDet[5] != null)
                            XlsHelper.SetRowCell(sheet, rownum, colnum, decimal.Parse(locLotDet[5].ToString()).ToString("0.########"));
                        colnum++;
                        if (locLotDet[6] != null)
                            XlsHelper.SetRowCell(sheet, rownum, colnum, decimal.Parse(locLotDet[6].ToString()).ToString("0.########"));
                        colnum++;
                        if (locLotDet[7] != null)
                            XlsHelper.SetRowCell(sheet, rownum, colnum, decimal.Parse(locLotDet[7].ToString()).ToString("0.########"));
                        colnum++;
                        if (locLotDet[8] != null)
                            XlsHelper.SetRowCell(sheet, rownum, colnum, locLotDet[8].ToString());
                        rownum++;
                    }
                    catch (Exception ex)
                    {

                        log.Error(NVelocityTemplateRepository.TemplateEnum.GenerateInvRepFail, ex);
                        string errorMessage = locLotDet[0].ToString() + " " + locLotDet[1].ToString() + locLotDet[2].ToString() + " " +
                                              locLotDet[3].ToString() + " " + decimal.Parse(locLotDet[4].ToString()).ToString("0.########") + " " +
                                              decimal.Parse(locLotDet[5].ToString()).ToString("0.########") + " " +
                                              decimal.Parse(locLotDet[7].ToString()).ToString("0.########") + " " + locLotDet[7].ToString() +
                                              locLotDet[8].ToString() + " 生成库存报表失败。";
                        errorMessageList.Add(new ErrorMessage
                            {
                                Template = NVelocityTemplateRepository.TemplateEnum.GenerateInvRepFail,
                                Message = errorMessage,
                                Exception = ex
                            });
                    }
                }

                #endregion

                XlsHelper.WriteToFile(localFolder, "LESMB52" + ymd + ".xls", workbook);

                if (!Directory.Exists(localTempFolder))
                {
                    Directory.CreateDirectory(localTempFolder);
                }

                string localTempFolderFileName = localTempFolder + "/" + "LESMB52" + ymd + ".xls";
                string localFolderFileName = localFolder + "/" + "LESMB52" + ymd + ".xls";
                File.Copy(localFolderFileName, localTempFolderFileName, true); //备份目录

                log.Info("文件生成成功,记录数:" + locLotDetList.Count());

                #region 上传

                try
                {
                    FtpHelper ftp = new FtpHelper(ftpServer, ftpPort, ftpFolder, ftpUser, ftpPass);
                    ftp.Upload(localTempFolderFileName);
                    File.Delete(localTempFolderFileName);

                    log.Info(localTempFolderFileName + "上传成功");
                }
                catch (Exception ex)
                {
                    log.Error("Upload file:" + localTempFolderFileName, ex);

                    log.Error(NVelocityTemplateRepository.TemplateEnum.GenerateInvRepFail, ex);
                    string errorMessage = "生成库存报表上传失败。";
                    errorMessageList.Add(new ErrorMessage
                    {
                        Template = NVelocityTemplateRepository.TemplateEnum.GenerateInvRepFail,
                        Message = errorMessage,
                        Exception = ex
                    });
                }

                #endregion
            }
            catch (Exception ex)
            {
                log.Error(NVelocityTemplateRepository.TemplateEnum.GenerateInvRepFail, ex);
                string errorMessage = "生成库存报表失败。";
                errorMessageList.Add(new ErrorMessage
                    {
                        Template = NVelocityTemplateRepository.TemplateEnum.GenerateInvRepFail,
                        Message = errorMessage,
                        Exception = ex
                    });
            }
        }