using ICSharpCode.Core;
using Mesnac.Action.Base;
using Mesnac.Codd.Session;
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.Alarm.PmtAlarm
{
    class DeleteAction : ChemicalWeighingAction, IAction
    {
        private string caption = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_Caption"));    //提示
        public void Run(RuntimeParameter runtime)
        {
            string msg1 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_Default_SynchroData_Delete_msg1"));   //您确认要删除当前信息吗?
            if (MessageBox.Show(msg1, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
            {
                return;
            }
            base.RunIni(runtime); //必须调用

            #region 限定只对相同DataSource的数据表进行操作

            string mcDataSourceID = String.Empty;
            if (runtime.Sender is Mesnac.Controls.Base.IBaseControl)
            {
                mcDataSourceID = (runtime.Sender as Mesnac.Controls.Base.IBaseControl).MCDataSourceID;
            }

            #endregion

            ShowMsg(String.Empty);  //清除消息显示标签内容

            foreach (DbMCSource dbsource in GetAllDbMCSources())
            {
                if (string.IsNullOrWhiteSpace(dbsource.DesignSource) ||
                    string.IsNullOrWhiteSpace(dbsource.DataTable.ToString()))
                {
                    continue;
                }
                if (!String.IsNullOrEmpty(mcDataSourceID) && dbsource.MCDataSource.Site.Name != mcDataSourceID)
                {
                    continue;
                }
                string source = dbsource.DesignSource;
                string table = dbsource.DataTable;
                DbHelper dbHelper = NewDbHelper(source);
                if (dbHelper == null)
                {
                    continue;
                }
                dbHelper.ClearParameter();
                dbHelper.CommandType = CommandType.Text;

                DbMCControlDesignKeyConfig config = new DbMCControlDesignKeyConfig();
                List<DbMCControl> objidcontrols = GetDbMCControlByKey(config.ToString(new string[] { source, table, "Alarm_ID" }));
                foreach (DbMCControl control in objidcontrols)
                {
                    if (control.BaseControl.MCValue == null ||
                        string.IsNullOrWhiteSpace(control.BaseControl.MCValue.ToString()))
                    {
                        continue;
                    }
                    if ((base.DbOptionTypesIsModify(control.BaseControl)))
                    {
                        string id = control.BaseControl.MCValue.ToString();
                        string sqlstr = "DELETE FROM [" + table + "] WHERE Alarm_ID='" + id + "'";
                        if (dbHelper.DbSession.ProviderFactory.GetType().FullName.Contains("OracleClientFactory"))
                        {
                            sqlstr = "DELETE FROM " + table + " WHERE Alarm_ID='" + id + "'";
                        }
                        dbHelper.ClearParameter();
                        dbHelper.CommandText = sqlstr;
                        dbHelper.ExecuteNonQuery();
                        string msg2 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_Default_SynchroData_Delete_msg2"));   //信息删除成功!
                        ShowMsg(msg2);

                        #region 记录操作日志

                        base.DBLog(msg2);

                        #endregion
                    }
                }
            }
        }
    }
}