using ICSharpCode.Core;
using Mesnac.Action.Base;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Mesnac.Action.ChemicalWeighing.Report.MaterialStatisticsReport
{
    /// <summary>
    /// 导出物料统计报表业务
    /// </summary>
    class ExportAction : ChemicalWeighingAction, IAction
    {
        private DbMCControl _clientDGV = null;
        public void Run(RuntimeParameter runtime)
        {
            base.RunIni(runtime);        //必须要调用的
            ICSharpCode.Core.LoggingService<ExportAction>.Debug("物料统计-导出...");

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "xls files(*.xls)|*.xls";
            sfd.FileName = String.Format("物料统计报表_{0:yyyyMMdd}", DateTime.Now);
            sfd.AddExtension = true;
            DialogResult result = sfd.ShowDialog();
            if (result == DialogResult.OK)
            {
                this._clientDGV = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "LR_weigh").FirstOrDefault();
                if (_clientDGV == null || !(_clientDGV.BaseControl is DataGridView))
                {
                    ICSharpCode.Core.LoggingService<SelectWeightAction>.Warn("{物料统计报表-导出} 缺少DataGridView控件...");
                    runtime.IsReturn = false;
                    return;
                }
                DataTable dt = _clientDGV.BaseControl.BindDataSource as DataTable;
                dt.Columns["Recipe_Name"].ColumnName = "配方名";
                dt.Columns["Material_Code"].ColumnName = "物料代码";
                dt.Columns["Material_name"].ColumnName = "物料名";
                dt.Columns["realWeight"].ColumnName = "重量(kg)";
                string fileName = sfd.FileName;
                if (!String.IsNullOrEmpty(fileName))
                {
                    try
                    {
                        System.IO.Stream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite);
                        Mesnac.Basic.DataToFileHandler.Instance.ToExcel(dt, ref fs);
                        fs.Close();
                        string msg1 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Report_MaterialStatisticsReport_ExportAction_msg1"));      //导出物料统计数据至Excel成功!
                        ICSharpCode.Core.LoggingService<ExportAction>.Info(msg1);

                        #region 记录操作日志

                        base.DBLog(msg1);

                        #endregion

                        MessageBox.Show(msg1, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch (Exception ex)
                    {
                        string msg2 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Report_MaterialStatisticsReport_ExportAction_msg2"));      //导出物料统计数据至Excel失败:{0}!
                        msg2 = String.Format(msg2, ex.Message);
                        ICSharpCode.Core.LoggingService<ExportAction>.Error(msg2);

                        #region 记录操作日志

                        base.DBLog(msg2);

                        #endregion

                        MessageBox.Show(msg2, Mesnac.Basic.LanguageHelper.WarnCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        runtime.IsReturn = true;
                        return;
                    }
                }
            }
        }
    }
}