using RGD.DBUtility;
using System;
using System.Data;
using System.Windows.Forms;

namespace RGD.WCS
{
    public partial class FrmDeviceErrorLog : Form
    {
        private static FrmDeviceErrorLog _formInstance;

        public static FrmDeviceErrorLog FormInstance
        {
            get
            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmDeviceErrorLog();
                }
                return _formInstance;
            }
            set { _formInstance = value; }
        }

        public FrmDeviceErrorLog()
        {
            InitializeComponent();
            this.dateTimePickerBegin.Value = DateTime.Today;
            this.dateTimePickerEnd.Value = DateTime.Today;
            _formInstance = this;//20101028
        }

        private void FrmDeviceErrorLog_Load(object sender, EventArgs e)
        {
            DataView dvs = DbHelperSQL.Query("Select name from syscolumns Where ID=OBJECT_ID('V_Device_Error_log') ORDER BY colorder").Tables[0].DefaultView;
            //cbstartposition
            cbField.ValueMember = "name";
            cbField.DisplayMember = "name";
            cbField.DataSource = dvs;
            cbField.SelectedIndex = -1;
        }

        private void btQuery_Click(object sender, EventArgs e)
        {
            string searchstr = string.Empty;

            if (this.cbField.Text.Trim().Length != 0 && this.tbContent.Text.Trim().Length != 0)
            {
                searchstr = string.Format(" And {0} like '%{1}%'", cbField.Text.Trim(), tbContent.Text.Trim());
            }

            string sql = string.Format("SELECT * FROM V_Device_Error_log WHERE (发生时间 >= '{0}') AND (发生时间 <= '{1}' {2})", dateTimePickerBegin.Value.ToString("yyyy-MM-dd HH:mm:ss"), dateTimePickerEnd.Value.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss"), searchstr);

            DataView dv = DbHelperSQL.Query(sql).Tables[0].DefaultView;

            this.dataGridView1.DataSource = dv;
        }

        private void btNullQuery_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确认要清空数据库内记录的所有设备的历史报警吗？", "操作提示：", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }
            DbHelperSQL.ExecuteSql("delete from T_Base_Device_Error_Log");
        }
    }
}