Newer
Older
RbFreqStand / RbFreqStandMeasure / info / InfoCtrlForm.cs
yangqianqian on 8 Apr 2021 21 KB detail-setting-databackup complete
using Casic.Birmm.RbFreqStandMeasure.R_DataBase.Dto;
using Casic.Birmm.RbFreqStandMeasure.R_DataBase.Model;
using Casic.Birmm.RbFreqStandMeasure.R_DataBase.Service;
using Casic.Birmm.RbFreqStandMeasure.R_DataBase.Service.Impl;
using DevComponents.DotNetBar.Controls;
using DevComponents.Editors;
using DevComponents.Editors.DateTimeAdv;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace Casic.Birmm.RbFreqStandMeasure.info
{
    public partial class InfoCtrlForm : UserControl
    {
        private DataTable devList = null;

        private int totalCount = 0; // 总行数
        private int currentPage = 1; // 当前页码
        private int pageCount = 0; // 总页数
        private readonly int limitCount = 10; // 每页数据条数,当前页面的表格固定为10条

        JObject condition = new JObject(); // 查询条件

        DeviceService deviceService = new DeviceServiceImpl();

        #region 样式常量定义
        private readonly Color titleBackColor = Color.FromArgb(63, 132, 215); // #3F64D7
        private readonly Color tableForeColor = Color.White;
        private readonly Font titleFont = new Font("微软雅黑", 14F, FontStyle.Regular, GraphicsUnit.Pixel, 134);

        private readonly Color inputBlack = Color.FromArgb(51, 51, 51); // #333333
        private readonly Color inputDefault = Color.FromArgb(204, 204, 204); // #CCCCCC
        #endregion

        public InfoCtrlForm()
        {
            InitializeComponent();

            this.comboBox_DevStatus.SelectedIndex = 0; // 仪器状态默认选择“全部”

            // 进入页面时默认查询所有的数据
            totalCount = GetTotalCount(condition);

            // 初始化表格并加载数据
            InitTableDevList();
            LoadDevList(condition);
        }

        private void InitTableDevList()
        {
            dataGridView_DevList.Columns.Clear();
            dataGridView_DevList.Rows.Clear();

            // 添加标题栏
            Panel tableHeader = new Panel
            {
                BackColor = tableForeColor,
                Size = new Size(964, 40),
                Location = new Point(0, 0)
            };
            tableHeader.BackColor = titleBackColor;
            dataGridView_DevList.Controls.Add(tableHeader);
            // 序号
            Label colNo = new Label
            {
                Font = titleFont,
                ForeColor = tableForeColor,
                TextAlign = ContentAlignment.MiddleCenter,
                BackColor = titleBackColor,
                AutoSize = false,
                Text = "序号",
                Location = new Point(0, 0),
                Size = new Size(80, 40)
            };
            dataGridView_DevList.Controls.Add(colNo);
            colNo.BringToFront();

            // 登记时间
            Label colRegTime = new Label
            {
                Font = titleFont,
                ForeColor = tableForeColor,
                TextAlign = ContentAlignment.MiddleCenter,
                BackColor = titleBackColor,
                AutoSize = false,
                Text = "登记时间",
                Location = new Point(80, 0),
                Size = new Size(164, 40)
            };
            dataGridView_DevList.Controls.Add(colRegTime);
            colRegTime.BringToFront();

            // 仪器名称
            Label colDevName = new Label
            {
                Font = titleFont,
                ForeColor = tableForeColor,
                TextAlign = ContentAlignment.MiddleCenter,
                BackColor = titleBackColor,
                AutoSize = false,
                Text = "仪器名称",
                Location = new Point(244, 0),
                Size = new Size(120, 40)
            };
            dataGridView_DevList.Controls.Add(colDevName);
            colDevName.BringToFront();

            // 仪器型号
            Label colDevType = new Label
            {
                Font = titleFont,
                ForeColor = tableForeColor,
                TextAlign = ContentAlignment.MiddleCenter,
                BackColor = titleBackColor,
                AutoSize = false,
                Text = "仪器型号",
                Location = new Point(364, 0),
                Size = new Size(120, 40)
            };
            dataGridView_DevList.Controls.Add(colDevType);
            colDevType.BringToFront();

            // 仪器编号
            Label colDevNo = new Label
            {
                Font = titleFont,
                ForeColor = tableForeColor,
                TextAlign = ContentAlignment.MiddleCenter,
                BackColor = titleBackColor,
                AutoSize = false,
                Text = "仪器编号",
                Location = new Point(484, 0),
                Size = new Size(120, 40)
            };
            dataGridView_DevList.Controls.Add(colDevNo);
            colDevNo.BringToFront();

            // 客户名称
            Label colCustomName = new Label
            {
                Font = titleFont,
                ForeColor = tableForeColor,
                TextAlign = ContentAlignment.MiddleCenter,
                BackColor = titleBackColor,
                AutoSize = false,
                Text = "客户名称",
                Location = new Point(604, 0),
                Size = new Size(120, 40)
            };
            dataGridView_DevList.Controls.Add(colCustomName);
            colCustomName.BringToFront();

            // 设备状态
            Label colDevStatus = new Label
            {
                Font = titleFont,
                ForeColor = tableForeColor,
                TextAlign = ContentAlignment.MiddleCenter,
                BackColor = titleBackColor,
                AutoSize = false,
                Text = "设备状态",
                Location = new Point(724, 0),
                Size = new Size(120, 40)
            };
            dataGridView_DevList.Controls.Add(colDevStatus);
            colDevStatus.BringToFront();

            // 通道号
            Label colChannelNo = new Label
            {
                Font = titleFont,
                ForeColor = tableForeColor,
                TextAlign = ContentAlignment.MiddleCenter,
                BackColor = titleBackColor,
                AutoSize = false,
                Text = "通道号",
                Location = new Point(844, 0),
                Size = new Size(120, 40)
            };
            dataGridView_DevList.Controls.Add(colChannelNo);
            colChannelNo.BringToFront();
        }

        private void LoadDevList(JObject condition)
        {
            devList = new DataTable();

            // 设置表格的列
            devList.Columns.Add("index", Type.GetType("System.String"));
            devList.Columns.Add("regTime", Type.GetType("System.String"));
            devList.Columns.Add("devName", Type.GetType("System.String"));
            devList.Columns.Add("devModel", Type.GetType("System.String"));
            devList.Columns.Add("devCode", Type.GetType("System.String"));
            devList.Columns.Add("customName", Type.GetType("System.String"));
            devList.Columns.Add("status", Type.GetType("System.String"));
            devList.Columns.Add("channelNo", Type.GetType("System.String"));
            devList.Columns.Add("id", Type.GetType("System.Int64"));
            devList.Columns.Add("customerDev", Type.GetType("System.String"));

            // 分页查询数据库
            string devName = "";
            string devModel = "";
            string devCode = "";
            string devStatus = "";
            string customComp = "";
            string customName = "";
            int page = 1;
            if (condition.Property("devName") != null)
            {
                devName = condition.Property("devName").Value.ToString();
            }
            if (condition.Property("devModel") != null)
            {
                devModel = condition.Property("devModel").Value.ToString();
            }
            if (condition.Property("devCode") != null)
            {
                devCode = condition.Property("devCode").Value.ToString();
            }
            if (condition.Property("devStatus") != null)
            {
                devStatus = condition.Property("devStatus").Value.ToString();
            }
            if (condition.Property("customComp") != null)
            {
                customComp = condition.Property("customComp").Value.ToString();
            }
            if (condition.Property("customName") != null)
            {
                customName = condition.Property("customName").Value.ToString();
            }
            if (condition.Property("page") != null)
            {
                page = Convert.ToInt32(condition.Property("page").Value.ToString());
            }

            // 获取总条数
            totalCount = deviceService.getTotalCount(devName, devCode, devModel, customComp, customName, devStatus);

            // 获取当前页的数据
            List<DeviceDto> queryList = deviceService.searchPage(devName, devCode, devModel, customComp, customName, devStatus, page, limitCount);
            if (null != queryList && queryList.Count > 0)
            {
                // 重新计算页码数据
                pageCount = Decimal.ToInt32(Math.Ceiling((decimal) totalCount / limitCount));
                ((Label)this.Controls.Find("labelPageCount", true)[0]).Text = "共 " + totalCount + " 条,每页 " + limitCount + " 条,共 " + pageCount + " 页";
                ((Label)this.Controls.Find("labelCurrentPage", true)[0]).Text = currentPage.ToString();

                int i = 0;
                foreach ( DeviceDto device in queryList )
                {
                    int index = (page-1) * limitCount + i + 1;
                    devList.Rows.Add(index, device.RegTime, device.DevName, device.DevModel, device.DevCode, device.CustomerName, device.StatusName, device.Channel, device.Id, device.CustomerDev);
                    i++;
                }

                this.Controls.Find("panelNodata", true)[0].Hide();
                this.Controls.Find("panelPage", true)[0].Show();
            }
            else
            {
                this.Controls.Find("panelNodata", true)[0].Show();
                this.Controls.Find("panelPage", true)[0].Hide();
            }

            dataGridView_DevList.DataSource = devList;

            // width=964px
            dataGridView_DevList.Columns[0].Width = 80;
            dataGridView_DevList.Columns[1].Width = 164;
            dataGridView_DevList.Columns[2].Width = 120;
            dataGridView_DevList.Columns[3].Width = 120;
            dataGridView_DevList.Columns[4].Width = 120;
            dataGridView_DevList.Columns[5].Width = 120;
            dataGridView_DevList.Columns[6].Width = 120;
            dataGridView_DevList.Columns[7].Width = 120;
            dataGridView_DevList.Columns[8].Width = 0;

            dataGridView_DevList.Columns[8].Visible = false; // id不显示
            dataGridView_DevList.Columns[9].Visible = false; // id不显示

            dataGridView_DevList.Columns[0].ReadOnly = true;
            dataGridView_DevList.Columns[1].ReadOnly = true;
            dataGridView_DevList.Columns[2].ReadOnly = true;
            dataGridView_DevList.Columns[3].ReadOnly = true;
            dataGridView_DevList.Columns[4].ReadOnly = true;
            dataGridView_DevList.Columns[5].ReadOnly = true;
            dataGridView_DevList.Columns[6].ReadOnly = true;
            dataGridView_DevList.Columns[7].ReadOnly = true;
            dataGridView_DevList.Columns[8].ReadOnly = true;
        }

        private int GetTotalCount(JObject condition)
        {
            string devName = "";
            string devModel = "";
            string devCode = "";
            string devStatus = "";
            string customComp = "";
            string customName = "";
            if (condition.Property("devName") != null)
            {
                devName = condition.Property("devName").Value.ToString();
            }
            if (condition.Property("devModel") != null)
            {
                devModel = condition.Property("devModel").Value.ToString();
            }
            if (condition.Property("devCode") != null)
            {
                devCode = condition.Property("devCode").Value.ToString();
            }
            if (condition.Property("devStatus") != null)
            {
                devStatus = condition.Property("devStatus").Value.ToString();
            }
            if (condition.Property("customComp") != null)
            {
                customComp = condition.Property("customComp").Value.ToString();
            }
            if (condition.Property("customName") != null)
            {
                customName = condition.Property("customName").Value.ToString();
            }

            int total = deviceService.getTotalCount(devName, devCode, devModel, customComp, customName, devStatus);

            return total;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddDevDlg addDevDlg = new AddDevDlg();
            addDevDlg.HideMaskAction += () =>
            {
                this.HideMask();
            };
            this.ShowMask();
            addDevDlg.StartPosition = FormStartPosition.CenterParent; // 指定窗口弹出在父窗口的中间位置

            // 设置默认值
            DateTimeInput startTimeInput = (DateTimeInput) addDevDlg.Controls.Find("startTime", true)[0];
            startTimeInput.Value = DateTime.Now;

            DateTimeInput endTimeInput = (DateTimeInput)addDevDlg.Controls.Find("endTime", true)[0];
            endTimeInput.Value = DateTime.Now.AddDays(1);

            addDevDlg.ShowDialog();
        }

        private MaskForm maskForm;

        private void ShowMask()
        {
            maskForm = new MaskForm(this.Parent.Parent.Location, this.Parent.Parent.Size);
            maskForm.Show();
        }

        private void HideMask()
        {
            if (maskForm != null)
            {
                maskForm.Close();
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            string devName = inputDevName.Text;
            string devModel = inputDevModel.Text;
            string devCode = inputDevCode.Text;
            string customComp = inputCustomComp.Text;
            string customName = inputCustomName.Text;
            ComboBoxEx statusSelect = (ComboBoxEx)this.Controls.Find("comboBox_DevStatus", true)[0];
            string devStatus = (string)((ComboItem)statusSelect.SelectedItem).Value;

            condition.RemoveAll();
            condition.Add("devName", devName);
            condition.Add("devModel", devModel);
            condition.Add("devCode", devCode);
            condition.Add("devStatus", devStatus);
            condition.Add("customComp", customComp);
            condition.Add("customName", customName);
            condition.Add("page", 1);

            // 查询数据
            LoadDevList(condition);
        }

        private string CheckInputText(string inputName, string defaultText)
        {
            string name = ((TextBoxX)this.Controls.Find(inputName, true)[0]).Text;
            if (name == defaultText)
            {
                return "";
            }
            else
            {
                return name;
            }
        }
        
        private void inputClick(string inputName, string defaultText, object sender)
        {
            string name = CheckInputText(inputName, defaultText);

            ((TextBoxX)sender).Text = name;
            ((TextBoxX)sender).ForeColor = inputBlack;
        }

        private void inputLeave(string inputName, string defaultText, object sender)
        {
            string name = CheckInputText(inputName, defaultText);
            if (name == "")
            {
                ((TextBoxX)sender).Text = defaultText;
                ((TextBoxX)sender).ForeColor = inputDefault;
            }
        }       
        private void btnPre_Click(object sender, EventArgs e)
        {
            if (currentPage > 1)
            {
                currentPage -= 1;
                condition.Remove("page");
                condition.Add("page", currentPage);
                LoadDevList(condition);
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (currentPage < pageCount)
            {
                currentPage += 1;
                condition.Remove("page");
                condition.Add("page", currentPage);
                LoadDevList(condition);
            }
        }

        private void btnDetail_Click(object sender, EventArgs e)
        {
            int rowIndex = dataGridView_DevList.CurrentRow.Index;
            long id = (long)dataGridView_DevList.Rows[rowIndex].Cells[8].Value;

            DetailDlg detailDlg = new DetailDlg(id);
            detailDlg.Controls.Find("label_devModel", true)[0].Text = dataGridView_DevList.Rows[rowIndex].Cells[3].Value.ToString();
            detailDlg.Controls.Find("label_devCode", true)[0].Text = dataGridView_DevList.Rows[rowIndex].Cells[4].Value.ToString();
            detailDlg.Controls.Find("label_customerComp", true)[0].Text = dataGridView_DevList.Rows[rowIndex].Cells[9].Value.ToString();
            detailDlg.Controls.Find("label_customerName", true)[0].Text = dataGridView_DevList.Rows[rowIndex].Cells[5].Value.ToString();

            detailDlg.HideMaskAction += () =>
            {
                this.HideMask();
            };
            this.ShowMask();
            detailDlg.StartPosition = FormStartPosition.CenterParent; // 指定窗口弹出在父窗口的中间位置
            detailDlg.ShowDialog();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            int rowIndex = dataGridView_DevList.CurrentRow.Index;
            long id = (long)dataGridView_DevList.Rows[rowIndex].Cells[8].Value;
            string devName = (string)dataGridView_DevList.Rows[rowIndex].Cells[2].Value;
            string devModel = (string)dataGridView_DevList.Rows[rowIndex].Cells[3].Value;
            string devCode = (string)dataGridView_DevList.Rows[rowIndex].Cells[4].Value;

            AddDevDlg editDlg = new AddDevDlg();
            editDlg.Controls.Find("labelTitle", true)[0].Text = "修改";
            editDlg.Controls.Find("inputDevName", true)[0].Text = devName;
            editDlg.Controls.Find("inputDevModel", true)[0].Text = devModel;
            editDlg.Controls.Find("inputDevCode", true)[0].Text = devCode;
            editDlg.text_devType.Text = (string)dataGridView_DevList.Rows[rowIndex].Cells[2].Value;
            editDlg.inputCustComp.Text = (string)dataGridView_DevList.Rows[rowIndex].Cells[9].Value;
            editDlg.inputCustName.Text = (string)dataGridView_DevList.Rows[rowIndex].Cells[5].Value;
            editDlg.text_channelNo.Text = (string)dataGridView_DevList.Rows[rowIndex].Cells[7].Value;
            editDlg.btnSave.Text = "保存";
            if (((string)dataGridView_DevList.Rows[rowIndex].Cells[6].Value).Equals("检测中"))
            {
                editDlg.btn_downChannelList.Enabled = false;
                editDlg.text_channelNo.ReadOnly = true;
                DetectionItemService detectionItemService = new DetectionItemServiceImpl();
                DetectionItem detectionItem = (detectionItemService.search(id, true))[0];
                editDlg.groupBox1.Enabled = false;
                editDlg.groupBox2.Enabled = false;
                if (!detectionItem.Accuracy.Equals("-3")) editDlg.checkBox_accuracy.Checked = true;
              
                if (!detectionItem.Stability.Equals("-3"))
                {
                    editDlg.checkBox_stability.Checked = true;
                    ((RadioButton)editDlg.Controls.Find("radio_" + detectionItem.Interval + "s", true)[0]).Checked = true;
                }
                if (!detectionItem.BootFeature.Equals("-3")) editDlg.checkBox_bootFeature.Checked = true;
                if (!detectionItem.AgeRate.Equals("-3")) editDlg.checkBox_ageRate.Checked = true;
            }
            editDlg.HideMaskAction += () =>
            {
                this.HideMask();
            };
            this.ShowMask();
            editDlg.StartPosition = FormStartPosition.CenterParent; // 指定窗口弹出在父窗口的中间位置
            editDlg.ShowDialog();
        }

        private void btn_downDevStatus_Click(object sender, EventArgs e)
        {
            comboBox_DevStatus.DroppedDown = true;
        }

        private void comboBox_DevStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            inputDevStatus.Text = comboBox_DevStatus.SelectedItem.ToString();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (((string)dataGridView_DevList.Rows[dataGridView_DevList.CurrentRow.Index].Cells[6].Value).Equals("检测中"))
            {
                MessageBox.Show("该仪器在检测中,无法删除!");
                return;
            }
            if (MessageBox.Show("确认删除选中仪器?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                    == DialogResult.Yes)
            {
                deviceService.delete(Convert.ToInt32(dataGridView_DevList.Rows[dataGridView_DevList.CurrentRow.Index].Cells[8].Value));
                MessageBox.Show("删除成功", "提示");
                LoadDevList(condition);
            }
        }
    }
}