SettingControl.cs 6.4 KB
using ConfigHelper;
using DeviceLibrary;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoCountMachine
{
    using crc = OnlineStore.CodeResourceControl;
    public partial class SettingControl : UserControl
    {
        public SettingControl()
        {
            InitializeComponent();
            RoleManger.RoleChange += RoleManger_RoleChange;
            this.VisibleChanged += SettingControl_VisibleChanged;
            label_count.Text = "";
        }

        private void SettingControl_VisibleChanged(object sender, EventArgs e)
        {
            var t = DateTime.Now;
            DateTime d = new DateTime(t.Year, t.Month, t.Day, 0, 0, 0);
            dateTimePicker_start.Value = d;
            dateTimePicker_end.Value = DateTime.Now;
        }

        private void cb_EnableBuzzer_CheckedChanged(object sender, EventArgs e)
        {
            Config.Set("EnableBuzzer", cb_EnableBuzzer.Checked);
            Config.SaveChange();
            AlarmBuzzer.Enable = cb_EnableBuzzer.Checked;
        }

        private void btn_setadminpassword_Click(object sender, EventArgs e)
        {
            FrmPassword frmPassword = new FrmPassword();
            crc.LanguageProcess(frmPassword);
            frmPassword.EditMode = true;
            frmPassword.ShowDialog();
        }
        private void cb_autorun_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_autorun.Checked)
            {
                ConfigAppSettings.SaveValue(Setting_Init.App_AutoRun, 1);
                AutoRun(Application.ExecutablePath, true);
            }
            else
            {
                ConfigAppSettings.SaveValue(Setting_Init.App_AutoRun, 0);
                AutoRun(Application.ExecutablePath, false);
            }
        }
        public static void AutoRun(string strName, bool value)
        {
            try
            {
                //创建启动对象 
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                //设置运行文件
                startInfo.FileName = System.Windows.Forms.Application.StartupPath + "\\AuToRunManager.exe";
                //设置启动参数 
                startInfo.Arguments = String.Join(" ", new string[2] { strName, value.ToString() });
                //设置启动动作,确保以管理员身份运行
                startInfo.Verb = "runas";
                //如果不是管理员,则启动UAC 
                System.Diagnostics.Process.Start(startInfo);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void SettingControl_Load(object sender, EventArgs e)
        {
            if (DesignMode)
                return;


            cb_EnableBuzzer.Checked = Config.Get("EnableBuzzer", true);

            cb_autorun.Checked = Config.Get("App_AutoRun", false);
            this.cb_autorun.CheckedChanged += new System.EventHandler(this.cb_autorun_CheckedChanged);
            rb_datafilter_today.Checked = true;
        }

        private void RoleManger_RoleChange(object sender, Role e)
        {
            tabPage1.Enabled = false;
            if (e != Role.Admin)
                return;
            tabPage1.Enabled = true;
        }

        private void rb_datafilter_today_CheckedChanged(object sender, EventArgs e)
        {
            var t = DateTime.Now;
            DateTime d = new DateTime(t.Year, t.Month, t.Day, 0, 0, 0);
            setData(d, DateTime.Now);
        }

        private void rb_datafilter_thismonth_CheckedChanged(object sender, EventArgs e)
        {
            var t = DateTime.Now;
            DateTime d = new DateTime(t.Year, t.Month, 1, 0, 0, 0);
            setData(d, DateTime.Now);
        }

        private void rb_datafilter_lastMonth_CheckedChanged(object sender, EventArgs e)
        {
            var t = DateTime.Now;
            DateTime d = new DateTime(t.Year, t.Month, 1, 0, 0, 0);
            DateTime ed = new DateTime(t.Year, t.Month, 1, 0, 0, 0);

            setData(d, ed.AddMonths(1));
        }

        private void rb_datafilter_last30_CheckedChanged(object sender, EventArgs e)
        {
            setData(DateTime.Now.AddDays(-30), DateTime.Now);
        }

        void setData(DateTime startData, DateTime endDate)
        {
            DataTable dt = databaseProc.Current.GetDatabydate(startData, endDate);

            dataGridView1.DataSource = dt;

            dataGridView1.Columns[0].HeaderText = "";//crc.GetString("dataGrid_ID", "条码");
            dataGridView1.Columns[1].HeaderText = "PN";//crc.GetString("dataGrid_Qty", "数量");
            dataGridView1.Columns[2].HeaderText = "Reel ID"; //crc.GetString("dataGrid_Slot", "盘位");
            dataGridView1.Columns[3].HeaderText = "Is NG"; //crc.GetString("dataGrid_DateTime", "时间");
            dataGridView1.Columns[4].HeaderText = "Ng Msg";
            dataGridView1.Columns[5].HeaderText = "QTY";
            dataGridView1.Columns[6].HeaderText = "Label";
            dataGridView1.Columns[7].HeaderText = "Orther Msg";
            dataGridView1.Columns[8].HeaderText = "2D BarCode";
            dataGridView1.Columns[9].HeaderText = "Date";
            dataGridView1.Columns[9].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
            dataGridView1.Columns[8].DisplayIndex = 9;

            dataGridView1.Columns[0].Visible = false;
            dataGridView1.Columns[6].Visible = false;
            dataGridView1.Columns[7].Visible = false;

            label_count.Text = crc.GetString("query_count","查询到{0}条结果.",dt.Rows.Count);
        }

        private void dateTimePicker_start_ValueChanged(object sender, EventArgs e)
        {
            if (rb_datafilter_custom.Checked &&  dateTimePicker_end.Value> dateTimePicker_start.Value)
                setData(dateTimePicker_start.Value, dateTimePicker_end.Value);
        }

        private void dateTimePicker_end_ValueChanged(object sender, EventArgs e)
        {
            if (rb_datafilter_custom.Checked && dateTimePicker_end.Value > dateTimePicker_start.Value)
                setData(dateTimePicker_start.Value, dateTimePicker_end.Value);
        }

        private void rb_datafilter_custom_CheckedChanged(object sender, EventArgs e)
        {
            
        }
    }
}