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

namespace wcfControlMonitorClient
{
    public partial class FrmStorageCheck : Form
    {
        DBOperator dbo = CStaticClass.dbo;
        DBOperator dboM = CStaticClass.dboM;
        private static FrmStorageCheck _formInstance;

        public static FrmStorageCheck FormInstance
        {
            get
            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmStorageCheck();
                }
                return _formInstance;
            }
            set { _formInstance = value; }
        }
        public FrmStorageCheck()
        {
            InitializeComponent();
            btnRefresh_Click(null, EventArgs.Empty);
        }

        private void FrmStorageCheck_Load(object sender, EventArgs e)
        {
            btnRefresh_Click(null, EventArgs.Empty);
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确定要开始上传库存信息吗?", "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }
            DataView dv = new DataView();
            try
            {
                dv = dbo.ExceSQL("SELECT * from ST_CELL  where fpalletbarcode != '-'").Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    this.btnUpload.Text = "正在上传中";
                    for (int i = 0; i < dv.Count; i++)
                    {
                        dboM.ExecuteSql("insert into IDX_ASRS_INV (LocationID,Palletid,Addtime,READFLAG) VALUES ('"
                            + dv[i]["FCELLCODE"].ToString() + "','" + dv[i]["fpalletbarcode"].ToString() + "',sysdate,'N')");
                        //Thread.Sleep(2000);
                    }
                    this.btnUpload.Text = "开始上传数据";
                    MessageBox.Show("上传成功!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("上传失败!" + ex.Message, "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                throw ex;
            }
            finally
            {
                dv = null;
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            DataView dv = new DataView();
            try
            {
                dv = dbo.ExceSQL("SELECT count(*) from ST_CELL  where fpalletbarcode != '-'").Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    this.uploadCount.Text = dv[0][0].ToString();
                }
                dv = dbo.ExceSQL("SELECT F_IF_CHECK,F_LAST_CHECK_DATE from T_STORAGE_CHECK").Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    if (dv[0]["F_IF_CHECK"].ToString() == "1")
                    {
                        this.btnUpdateCheck.Text = "自动上传数据已开启";
                    }
                    else
                    {
                        this.btnUpdateCheck.Text = "自动上传数据已关闭";
                    }
                    this.tbDate.Text = dv[0]["F_LAST_CHECK_DATE"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dv = null;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmStorageCheck_FormClosing(object sender, FormClosingEventArgs e)
        {
            _formInstance = null;
        }

        private void btnUpdateCheck_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确定要更改是否自动上传数据吗?", "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }
            DataView dv = dbo.ExceSQL("SELECT F_IF_CHECK from T_STORAGE_CHECK").Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                if (dv[0]["F_IF_CHECK"].ToString() == "1")
                {
                    dbo.ExecuteSql("update T_STORAGE_CHECK set F_IF_CHECK = 0");
                    this.btnUpdateCheck.Text = "自动上传数据已关闭";
                }
                else
                {
                    dbo.ExecuteSql("update T_STORAGE_CHECK set F_IF_CHECK = 1");
                    this.btnUpdateCheck.Text = "自动上传数据已开启";
                }
                btnRefresh_Click(null, EventArgs.Empty);
            }
        }
    }
}