示例#1
0
        public bool Start(string schedule_code)
        {
            bool result = false;
            FileManagement _fileHelper = new FileManagement();
           
            try
            {
                if (string.IsNullOrEmpty(schedule_code))
                {
                    return result;
                }
                string GroupCode = string.Empty;
                string MailTitle = string.Empty;

                StringBuilder strbody = new StringBuilder();
                MailModel mailModel = new MailModel();
                mailModel.MysqlConnectionString = mySqlConnectionString;
                //獲取該排程參數
                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("MailFromAddress"))
                    {
                        mailModel.MailFromAddress = item.value;
                    }
                    else if (item.parameterCode.Equals("MailHost"))
                    {
                        mailModel.MailHost = item.value;
                    }
                    else if (item.parameterCode.Equals("MailPort"))
                    {
                        mailModel.MailPort = item.value;
                    }
                    else if (item.parameterCode.Equals("MailFromUser"))
                    {
                        mailModel.MailFromUser = item.value;
                    }
                    else if (item.parameterCode.Equals("EmailPassWord"))
                    {
                        mailModel.MailFormPwd = item.value;
                    }
                    else if (item.parameterCode.Equals("GroupCode"))
                    {
                        GroupCode = item.value;
                    }
                    else if (item.parameterCode.Equals("MailTitle"))
                    {
                        MailTitle = item.value;
                    }
                    else if (item.parameterCode.Equals("MailTitle"))
                    {
                        MailTitle = item.value;
                    }
                    else if (item.parameterCode.Equals("filepath"))
                    {
                        stockExcelsavePath = item.value.Trim().ToLower();;
                    }
                }
                stockExcelsavePathSuccess = Path.Combine(stockExcelsavePath, "success");
                stockExcelsavePathFail = Path.Combine(stockExcelsavePath, "fail");
                stockExcelsavePathIgnore = Path.Combine(stockExcelsavePath, "ignore");
                #endregion

                MailHelper mail = new MailHelper(mailModel);
                string[] files = _fileHelper.GetAllFiles(stockExcelsavePath, @"*.*");
                string consoleFile = String.Empty;
                string lastFile = files.Max<String>();
                if (files.Length < 1)
                {
                    mail.SendToGroup(GroupCode, MailTitle, "今日沒有文件倒入", false, true);//發送郵件給群組
                }
                #region 創建四個Datata來保存數據
                DataTable _dtSucess = new DataTable();//更新成功的數據
                _dtSucess.Columns.Add("商品編號", typeof(string));
                _dtSucess.Columns.Add("商品細項編號", typeof(string));
                _dtSucess.Columns.Add("商品ERP編號", typeof(string));
                _dtSucess.Columns.Add("商品名稱", typeof(string));
                _dtSucess.Columns.Add("規格", typeof(string));
                _dtSucess.Columns.Add("庫存", typeof(string));
                DataTable _dtFail = new DataTable();//更新失敗的數據
                _dtFail.Columns.Add("商品編號", typeof(string));
                _dtFail.Columns.Add("商品細項編號", typeof(string));
                _dtFail.Columns.Add("商品ERP編號", typeof(string));
                _dtFail.Columns.Add("商品名稱", typeof(string));
                _dtFail.Columns.Add("規格", typeof(string));
                _dtFail.Columns.Add("庫存", typeof(string));
                DataTable _dtIgnore = new DataTable();//跳過更新的數據
                _dtIgnore.Columns.Add("商品ERP編號", typeof(string));
                DataTable _dtErrorTable = new DataTable();//打不開Excel文件的數據
                _dtErrorTable.Columns.Add("路徑", typeof(string));
                _dtErrorTable.Columns.Add("描述", typeof(string));
                #endregion
                foreach (string file in files)
                {
                    if (lastFile == file)//只操作最新的文件
                    {
                      
                        int num = 0;
                        StringBuilder errorLog = GetStockMessageFromFile(file, ref _dtSucess, ref _dtFail, ref _dtIgnore, ref _dtIgnore);
                        if (String.IsNullOrEmpty(errorLog.ToString()))
                        {
                            string newFileName = file.Substring(file.LastIndexOf("\\"));
                            _fileHelper.MoveOneFile(file, stockExcelsavePathSuccess, newFileName);
                            MailTitle = "ERP庫存更新提醒";
                            mail.SendToGroup(GroupCode, MailTitle, "更新成功數量:" + num + "文件名称:" + newFileName, false, true);//發送郵件給群組
                        }
                        else
                        {
                            string newFileName = file.Substring(file.LastIndexOf("\\"));
                            string errorNewFileName = newFileName.Substring(0, newFileName.LastIndexOf(".")) + "_err.xls";
                            string errorlogNewFileName = stockExcelsavePathFail + newFileName.Substring(0, newFileName.LastIndexOf(".")) + "_err.txt";
                            _fileHelper.MoveOneFile(file, stockExcelsavePathFail, errorNewFileName);
                            SaveErrorLog(errorLog, errorlogNewFileName, newFileName);
                            MailTitle = "ERP庫存更新異常提醒";
                            StringBuilder sbMailBody = new StringBuilder();
                            if (_dtSucess.Rows.Count > 0)
                            {
                                sbMailBody.AppendLine("更新成功商品");
                                sbMailBody.AppendLine(GetHtmlByDataTable(_dtSucess));
                            }
                            if (_dtFail.Rows.Count > 0)
                            {
                                sbMailBody.AppendLine("更新失敗商品");
                                sbMailBody.AppendLine(GetHtmlByDataTable(_dtFail));
                            }
                            if (_dtIgnore.Rows.Count > 0)
                            {
                                sbMailBody.AppendLine("跳過更新商品");
                                sbMailBody.AppendLine(GetHtmlByDataTable(_dtIgnore));
                            }
                            if (_dtErrorTable.Rows.Count > 0)
                            {
                                sbMailBody.AppendLine("打不開Excel文件的數據");
                                sbMailBody.AppendLine(GetHtmlByDataTable(_dtErrorTable));
                            }
                            mail.SendToGroup(GroupCode, MailTitle, sbMailBody.ToString(), false, true);//發送郵件給群組

                        }
                            

                        
                    }
                    else//忽略過時文件
                    {
                        string newFileName = file.Substring(file.LastIndexOf("\\"));
                        _fileHelper.MoveOneFile(file, stockExcelsavePathIgnore, newFileName);
                    }

                }

               
               // StringBuilder sbMailBody = new StringBuilder();

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