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;

namespace SiaSun.LMS.LED
{
    public partial class TABLE_CLEAR : Form
    {
        public TABLE_CLEAR()
        {
            InitializeComponent();

            //设置格式
            this.dtpFrom.CustomFormat = "yyyy-MM-dd";
            this.dtpFrom.Value = DateTime.Now.AddYears(-1).AddMonths(-1);
            this.dtpTo.CustomFormat = "yyyy-MM-dd";
            this.dtpTo.Value = DateTime.Now.AddYears(-1);
        }

        /// <summary>
        /// 清理数据
        /// </summary>
        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;

            #region 校验日期是否为一年前

            if (this.dtpTo.Value.AddYears(1) > DateTime.Now)
            {
                MessageBox.Show("一年内的数据禁止清理");

                return;
            }

            #endregion

            foreach (CheckBox chk in this.panelControl.Controls)
            {
                if (chk.Checked)
                {
                    if (chk == this.chkPlan)
                    {
                        ClearPlanData();
                    }

                    if (chk == this.chkRecord)
                    {
                        ClearRecordData();
                    }

                    if (chk == this.chkSYS_LOG)
                    {
                        ClearSysLogData();
                    }

                    if (chk == this.chkINOUT_NOPLAN)
                    {
                        ClearInOutNoPlanData();
                    }

                    if (chk == this.chkAPPLY_HIS)
                    {
                        ClearApplyHisData();
                    }
                }
            }

            this.Cursor = Cursors.Default;
        }

        /// <summary>
        /// 关闭窗体
        /// </summary>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 清理计划数据
        /// </summary>
        private void ClearPlanData()
        {
            try
            {
                //是否选中
                if (chkPlan.Checked)
                {
                    string strDateWhere = string.Format("PLAN_STATUS IN('Complete') AND PLAN_BEGIN_TIME>='{0}'", this.dtpFrom.Value.ToString("yyyy-MM-dd")) + string.Format("AND PLAN_END_TIME<'{0}'", this.dtpTo.Value.AddDays(1).ToString("yyyy-MM-dd"));
                    string strPLAN_LIST = string.Format("DELETE FROM PLAN_LIST WHERE PLAN_ID IN(SELECT PLAN_ID FROM PLAN_MAIN WHERE {0})", strDateWhere);
                    string strPLAN_MAIN = string.Format("DELETE FROM PLAN_MAIN WHERE {0}", strDateWhere);

                    //先清除计划列表
                    int intAffect = Program._I_BaseService.ExecuteNonQuery_ReturnInt(strPLAN_LIST);
                    if (intAffect > 0)
                    {
                        intAffect = Program._I_BaseService.ExecuteNonQuery_ReturnInt(strPLAN_MAIN);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("清理计划数据失败!" + ex.Message, "系统提示");
            }
            finally
            {
                MessageBox.Show("操作成功!");
            }
        }

        /// <summary>
        /// 清理入出库记录数据
        /// </summary>
        private void ClearRecordData()
        {
            try
            {
                //是否选中
                if (this.chkRecord.Checked)
                {
                    string strDateWhere = string.Format("MANAGE_BEGIN_TIME>='{0}'", this.dtpFrom.Value.ToString("yyyy-MM-dd")) + string.Format("AND MANAGE_END_TIME<'{0}'", this.dtpTo.Value.AddDays(1).ToString("yyyy-MM-dd"));
                    string strRECORD_LIST = string.Format("DELETE FROM RECORD_LIST WHERE RECORD_ID IN(SELECT RECORD_ID FROM RECORD_MAIN WHERE {0})", strDateWhere);
                    string strRECORD_MAIN = string.Format("DELETE FROM RECORD_MAIN WHERE {0}", strDateWhere);

                    //先清除计划列表
                    int intAffect = Program._I_BaseService.ExecuteNonQuery_ReturnInt(strRECORD_LIST);
                    if (intAffect > 0)
                    {
                        intAffect = Program._I_BaseService.ExecuteNonQuery_ReturnInt(strRECORD_MAIN);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("清理入出库记录数据失败!" + ex.Message, "系统提示");
            }
            finally
            {
                MessageBox.Show("操作成功!");
            }
        }

        /// <summary>
        /// 清理系统日志数据
        /// </summary>
        private void ClearSysLogData()
        {
            try
            {
                //是否选中
                if (this.chkSYS_LOG.Checked)
                {
                    string strDateWhere = string.Format("LOG_DATE>='{0}'", this.dtpFrom.Value.ToString("yyyy-MM-dd")) + string.Format("AND LOG_DATE<'{0}'", this.dtpTo.Value.AddDays(1).ToString("yyyy-MM-dd"));
                    string strSYS_LOG = string.Format("DELETE FROM SYS_LOG WHERE {0}", strDateWhere);
                    Program._I_BaseService.ExecuteNonQuery_ReturnVoid(strSYS_LOG);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("清理系统日志数据失败!" + ex.Message, "系统提示");
            }
            finally
            {
                MessageBox.Show("操作成功!");
            }
        }

        /// <summary>
        /// 清理补填EAM单据提醒数据
        /// </summary>
        private void ClearInOutNoPlanData()
        {
            try
            {
                //是否选中
                if (this.chkINOUT_NOPLAN.Checked)
                {
                    string strDateWhere = string.Format("MANAGE_BEGIN_TIME>='{0}'", this.dtpFrom.Value.ToString("yyyy-MM-dd")) + string.Format("AND MANAGE_END_TIME<'{0}'", this.dtpTo.Value.AddDays(1).ToString("yyyy-MM-dd"));
                    string strINOUT_NOPLAN = string.Format("DELETE FROM WMS_INOUT_NOPLAN WHERE {0}", strDateWhere);
                    Program._I_BaseService.ExecuteNonQuery_ReturnVoid(strINOUT_NOPLAN);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("清理补填EAM单据提醒数据失败!" + ex.Message, "系统提示");
            }
            finally
            {
                MessageBox.Show("操作成功!");
            }
        }

        /// <summary>
        /// 清理任务申请历史数据
        /// </summary>
        private void ClearApplyHisData()
        {
            try
            {
                //是否选中
                if (this.chkAPPLY_HIS.Checked)
                {
                    string strDateWhere = string.Format("CREATE_TIME>='{0}'", this.dtpFrom.Value.ToString("yyyy-MM-dd")) + string.Format("AND CREATE_TIME<'{0}'", this.dtpTo.Value.AddDays(1).ToString("yyyy-MM-dd"));
                    string strAPPLY_HIS = string.Format("DELETE FROM IO_CONTROL_APPLY_HIS WHERE {0}", strDateWhere);
                    Program._I_BaseService.ExecuteNonQuery_ReturnVoid(strAPPLY_HIS);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("清理任务申请历史数据失败!" + ex.Message, "系统提示");
            }
            finally
            {
                MessageBox.Show("操作成功!");
            }
        }

    }
}