using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using DBFactory;
namespace wcfControlMonitorClient
{
    /// <summary>
    /// Creator:Richard.liu
    /// 设备故障查询
    /// </summary>
    public partial class FrmBrowseApplyLog : Form
    {
        private static FrmBrowseApplyLog _formInstance;

        public static FrmBrowseApplyLog FormInstance
        {
            get 

            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmBrowseApplyLog();
                    isFirstShow = false;
                }
                return _formInstance; 
            }
            set { _formInstance = value; }
        }
        DBOperator dbo =CStaticClass.dbo;
        static bool isFirstShow = false;//20130117
        public FrmBrowseApplyLog()
        {
            InitializeComponent();
            this.dateTimePicker1.Value = DateTime.Today;
            this.dateTimePicker2.Value = DateTime.Today;
            _formInstance = this;
        }

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

                //if (this.comboBox_SearchTable.Text.Trim().Length != 0 && this.textBox_SearchKeyWord.Text.Trim().Length != 0)
                //{
                //    searchstr = string.Format(" And {0} like '%{1}%'", comboBox_SearchTable.Text.Trim(), textBox_SearchKeyWord.Text.Trim());
                //}
                if (this.comboBox_SearchTable.Text.Trim().Length == 0)
                {
                    MessageBox.Show("请选择要查询的任务表名字!");
                    return;
                }
                //string sql = string.Format("SELECT * FROM V_IO_CONTROL_APPLY_WCS WHERE (申请时间 >= '{0}') AND (申请时间 <= '{1}' {2}) and 仓库编号 like '%{3}%'  order by 申请时间 ", dateTimePicker1.Value.ToString("yyyy-MM-dd HH:mm:ss"), dateTimePicker2.Value.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss"), searchstr, CStaticClass.UserArea);
                string sql = string.Format("SELECT * FROM {0} ",this.comboBox_SearchTable.Text.Trim());

                DataView dv = dbo.ExceSQL(sql).Tables[0].DefaultView;

                this.dataGridView1.DataSource = dv;
                this.labelCounts.Text = string.Format("共{0}行", dv.Count);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "异常提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }

        private void btNullQuery_Click(object sender, EventArgs e)
        {
            if (this.comboBox_SearchTable.Text.Trim().Length == 0)
            {
                MessageBox.Show("请选择要查询的任务表名字!");
                return;
            }
            if (MessageBox.Show("您确认要清空" + this.comboBox_SearchTable.Text.Trim() + "表当前所有记录吗?", "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }
            dbo.ExceSQL("delete from " + this.comboBox_SearchTable.Text.Trim());
          
            string sql = string.Format("SELECT * FROM {0} ", this.comboBox_SearchTable.Text.Trim());

            DataView dv = dbo.ExceSQL(sql).Tables[0].DefaultView;

            this.dataGridView1.DataSource = dv;
            this.labelCounts.Text = string.Format("共{0}行", dv.Count);
        }

        private void FrmBrowseApplyLog_Load(object sender, EventArgs e)
        {
            if (isFirstShow == false)
            {
                try
                {
                    DataView dvs = dbo.ExceSQL("Select name from syscolumns Where ID=OBJECT_ID('V_IO_CONTROL_APPLY_WCS') ORDER BY colorder").Tables[0].DefaultView;
                    //cbstartposition
                    //comboBox_SearchTable.ValueMember = "name";
                    //comboBox_SearchTable.DisplayMember = "name";
                    //comboBox_SearchTable.DataSource = dvs;
                    //comboBox_SearchTable.SelectedIndex = -1;

                    //int savedays = 60;
                    //string sql = string.Format("Delete  from IO_CONTROL_APPLY_WCS where CREATE_TIME ='-' or ABS(datediff(day,CREATE_TIME,getdate()))>{0}", savedays);
                    //dbo.ExceSQL(sql);
                    ////sql = string.Format("Delete  from T_Base_Device_Error_Log where ABS(datediff(day,F_DateTime,getdate()))>{0}", CStaticClass.SaveDays);
                    ////dbo.ExceSQL(sql);
                    isFirstShow = true;
                    //int b = 0;
                    //int a = 8 / b;
                }
                catch(Exception ex)
                {
                    throw ex;
                }
            }
        }

       
    }
}