コード例 #1
0
 public int InsertReceiveFromTcatFTP(LogisticsTcatSod model)
 {
     model.Replace4MySQL();
     StringBuilder InsertSql = new StringBuilder("");
     InsertSql.AppendFormat(@" INSERT INTO logistics_tcat_sod (delivery_number,order_id,station_name,delivery_status_time,customer_id,status_id,
                            status_note,specification,create_date) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}'); ",
                              model.delivery_number, model.order_id, model.station_name, model.delivery_status_time, model.customer_id, 
                              model.status_id,model.status_note,model.specification ,DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
     return _accessMySql.execCommand(InsertSql.ToString());
 }
コード例 #2
0
        public bool Start(string schedule_code)
        {
            if (string.IsNullOrEmpty(schedule_code))
            {
                return false;
            }
            try
            {
                string ftpServerIP = string.Empty;
                string filename = string.Empty;
                string ftpUserID = string.Empty;
                string ftpPassword = string.Empty;
                string localPath_1 = string.Empty;
                string localPath_2 = string.Empty;

                //獲取該排程參數
                List<ScheduleConfigQuery> store_config = new List<ScheduleConfigQuery>();
                ScheduleConfigQuery query_config = new ScheduleConfigQuery();
                query_config.schedule_code = schedule_code;
                _secheduleServiceMgr = new ScheduleServiceMgr(mySqlConnectionString);
                store_config = _secheduleServiceMgr.GetScheduleConfig(query_config);

                #region FTP參數賦值
                foreach (ScheduleConfigQuery item in store_config)
                {
                    if (item.parameterCode.Equals("FtpSite"))
                    {
                        ftpServerIP = item.value;
                    }
                    else if (item.parameterCode.Equals("ftpuser"))
                    {
                        ftpUserID = item.value;
                    }
                    else if (item.parameterCode.Equals("ftppassword"))
                    {
                        ftpPassword = item.value;
                    }
                    else if (item.parameterCode.Equals("localPath_1"))//下載的文件保存路徑(本地)
                    {
                        localPath_1 = item.value;
                    }
                    else if (item.parameterCode.Equals("localPath_2"))//下載的文件保存路徑(本地)
                    {
                        localPath_2 = item.value;
                    } 
                }             
                #endregion

                bool localPath1Bool = true; 
                try
                {
                    if (!Directory.Exists(localPath_1))
                    {
                        Directory.CreateDirectory(localPath_1);
                    }
                }
                catch(Exception ex)
                {
                    localPath1Bool = false;
                    string str1 = " 參數 localPath_1 有問題,創建路徑(保存下載文件)失敗,失敗的原因:" + ex.Message;
                    SendMail(schedule_code, str1);
                    throw new Exception(ex.Message);
                }
              

                #region FTP下載
                /// <summary>
                /// FTP下載
                /// </summary>
                string fileContentStr = string.Empty;
                bool result = false;
                if (localPath1Bool)
                {
                    try
                    {
                        string filePath = localPath_1;//下載保存文件路徑
                        result = DownloadFTP(filePath, ftpServerIP, ftpUserID, ftpPassword);
                    }
                    catch (Exception ex)
                    {
                        int index = ex.Message.LastIndexOf("-->");
                        int subStrLeng = index + 3;
                        string errorMessage = ex.Message.Substring(subStrLeng, ex.Message.Length - subStrLeng);
                        string str = "sod文件下載失敗,失敗的原因:" + errorMessage;
                        SendMail(schedule_code, str);
                        throw new Exception(ex.Message);
                    }
                }
               
               

                if (result)//下載成功后,插入數據,轉移文件,發送郵件
                {               
                    string[] filenames = Directory.GetFiles(localPath_1, "*.sod");


                    #region 循環讀取文件,插入數據
                    /// <summary>
                    /// 循環讀取文件,插入數據
                    /// </summary>
                    foreach (string file in filenames)
                    {
                        string localFilePath_1 = file;
                        if (File.Exists(localFilePath_1))
                        {
                            FileStream fs = new FileStream(localFilePath_1, FileMode.Open, FileAccess.Read);//创建写入文件                        
                            StreamReader readSr = new StreamReader(fs, Encoding.GetEncoding("big5"));

                            fileContentStr = readSr.ReadToEnd();//开始讀取值
                            readSr.Close();
                            fs.Close();
                        }
                        string[] array1 = fileContentStr.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);//數據數組
                        for (int i = 0; i < array1.Length; i++)//循環插入數據
                        {
                            string[] array2 = new string[] { };
                            array2 = array1[i].Split(new string[] { "|" }, StringSplitOptions.None);//欄位數組

                            string year = array2[3].ToString().Substring(0, 4);
                            string month = array2[3].ToString().Substring(4, 2);
                            string day = array2[3].ToString().Substring(6, 2);
                            string hour = array2[3].ToString().Substring(8, 2);
                            string minute = array2[3].ToString().Substring(10, 2);
                            string second = array2[3].ToString().Substring(12, 2);
                            string delivery_status_time = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;

                            
                            LogisticsTcatSodQuery query = new LogisticsTcatSodQuery ();                            
                            query.delivery_number = array2[0];
                            query.order_id = Convert.ToInt32(array2[1]);
                            query.delivery_status_time = Convert.ToDateTime(delivery_status_time);
                            query.status_id = array2[5];
                            DataTable selDT = logisticsTcatSodDao.GetRepeatInfo(query);

                            if (selDT.Rows.Count == 0)//如果數據已經存在,不插入
                            {
                                LogisticsTcatSod model = new LogisticsTcatSod();
                                model.delivery_number = array2[0];
                                model.order_id = Convert.ToInt32(array2[1]);
                                model.station_name = array2[2];
                                model.delivery_status_time = Convert.ToDateTime(delivery_status_time);
                                model.customer_id = array2[4];
                                model.status_id = array2[5];
                                model.status_note = array2[6];
                                model.specification = array2[7];
                                logisticsTcatSodDao.InsertReceiveFromTcatFTP(model);                               
                            }
                        }
                    } 
                    #endregion


                    #region 循環轉移文件
                    /// <summary>
                    /// 循環轉移文件
                    /// </summary>
                    if (filenames.Length > 0)
                    {
                        bool localPath2Bool = true;
                        foreach (string file in filenames)
                        {
                            int index = file.LastIndexOfAny(new char[] { '/', '\\' });
                            int subStrLeng = index + 1;
                            string localFilePath_1 = file;
                            string localFilePath_2 = localPath_2 + "\\" + DateTime.Now.ToString("yyyyMMdd") + "\\" + file.Substring(subStrLeng, file.Length - subStrLeng).Replace(".", DateTime.Now.ToString("mmss") + ".");

                            
                            try
                            {
                                //轉移文件
                                if (!Directory.Exists(localPath_2 + "\\" + DateTime.Now.ToString("yyyyMMdd")))
                                {
                                    Directory.CreateDirectory(localPath_2 + "\\" + DateTime.Now.ToString("yyyyMMdd"));
                                }
                                if (File.Exists(localFilePath_1))
                                {
                                    File.Move(localFilePath_1, localFilePath_2);
                                }
                                //if (!File.Exists(localFilePath_2))//本地文件(处理过的文件保存)存在
                                //{
                                //    FileStream fs2 = new FileStream(localFilePath_2, FileMode.Create, FileAccess.Write);//创建写入文件 
                                //    FileStream fs1 = new FileStream(localFilePath_1, FileMode.Open);
                                //    StreamReader sr = new StreamReader(fs1, Encoding.GetEncoding("big5"));
                                //    StreamWriter sw = new StreamWriter(fs2, Encoding.GetEncoding("big5"));
                                //    sw.Write(sr.ReadToEnd());//开始写入值
                                //    sw.Close();
                                //    fs1.Close();
                                //}
                                //else
                                //{
                                //    FileStream fs2 = new FileStream(localFilePath_2, FileMode.Truncate, FileAccess.Write);//创建写入文件 
                                //    FileStream fs1 = new FileStream(localFilePath_1, FileMode.Open);
                                //    StreamReader sr = new StreamReader(fs1, Encoding.GetEncoding("big5"));
                                //    StreamWriter sw = new StreamWriter(fs2, Encoding.GetEncoding("big5"));//Big5
                                //    sw.Write(sr.ReadToEnd());//开始写入值
                                //    sw.Close();
                                //    fs1.Close();
                                //}
                            }
                            catch (Exception ex)
                            {
                                localPath2Bool = false;
                                string str1 = " sod文件下載成功,數據庫更新成功。" + "但是該文件在本地保存失敗,失敗的原因:" + ex.Message;                                
                                SendMail(schedule_code, str1);
                                throw new Exception(ex.Message);
                            }
                        }

                        if (localPath2Bool)
                        {
                            //所有操作都執行成功
                            string downLoadSuccess = "sod文件下載成功,所有操作都執行成功";
                            SendMail(schedule_code, downLoadSuccess);
                        }                       
                    }
                    else
                    {
                        string downLoadNull = "沒有要下載的sod文件";
                        SendMail(schedule_code, downLoadNull);
                    }
                     
                    #endregion                  
                }
                #endregion

            }
            catch (Exception ex)
            {
                throw new Exception("ReceiveStatusFromTCatMgr-->Start-->" + ex.Message);
            }
            return true;
        }