Newer
Older
RbFreqStand / RbFreqStandMeasure / info / InfoCtrlForm.cs
yangqianqian on 30 Apr 2021 26 KB pull counter
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 Casic.Birmm.RbFreqStandMeasure.Tools;
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.IO;
using System.Reflection;
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条
        DetectionService detectionService = new DetectionServiceImpl();
        DetectionItemService detectionItemService = new DetectionItemServiceImpl();

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

        DeviceService deviceService = new DeviceServiceImpl();
        public static InfoCtrlForm infoCtrlForm;

        #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();
            infoCtrlForm = this;
            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(60, 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(60, 0),
                Size = new Size(154, 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(214, 0),
                Size = new Size(110, 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(324, 0),
                Size = new Size(110, 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(434, 0),
                Size = new Size(110, 40)
            };
            dataGridView_DevList.Controls.Add(colDevNo);
            colDevNo.BringToFront();

            // 送检单位
            Label colCustomComp = new Label
            {
                Font = titleFont,
                ForeColor = tableForeColor,
                TextAlign = ContentAlignment.MiddleCenter,
                BackColor = titleBackColor,
                AutoSize = false,
                Text = "送检单位",
                Location = new Point(544, 0),
                Size = new Size(110, 40)
            };
            dataGridView_DevList.Controls.Add(colCustomComp);
            colCustomComp.BringToFront();

            // 联系人
            Label colCustomName = new Label
            {
                Font = titleFont,
                ForeColor = tableForeColor,
                TextAlign = ContentAlignment.MiddleCenter,
                BackColor = titleBackColor,
                AutoSize = false,
                Text = "联系人",
                Location = new Point(654, 0),
                Size = new Size(110, 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(764, 0),
                Size = new Size(100, 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(864, 0),
                Size = new Size(100, 40)
            };
            dataGridView_DevList.Controls.Add(colChannelNo);
            colChannelNo.BringToFront();
        }

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

            // 设置表格的列
            devList.Columns.Add("index", Type.GetType("System.String"));//0
            devList.Columns.Add("regTime", Type.GetType("System.String"));//1
            devList.Columns.Add("devName", Type.GetType("System.String"));//2
            devList.Columns.Add("devModel", Type.GetType("System.String"));//3
            devList.Columns.Add("devCode", Type.GetType("System.String"));//4
            devList.Columns.Add("customComp", Type.GetType("System.String"));//5
            devList.Columns.Add("customName", Type.GetType("System.String"));//6
            devList.Columns.Add("status", Type.GetType("System.String"));//7
            devList.Columns.Add("channelNo", Type.GetType("System.String"));//8
            devList.Columns.Add("id", Type.GetType("System.Int64"));//9
            devList.Columns.Add("devTypeName", Type.GetType("System.String"));//10

            // 分页查询数据库
            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.CustomerDev, device.CustomerName, device.StatusName, device.Channel, device.Id, device.DevTypeName);
                    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 = 60;
            dataGridView_DevList.Columns[1].Width = 154;
            dataGridView_DevList.Columns[2].Width = 110;
            dataGridView_DevList.Columns[3].Width = 110;
            dataGridView_DevList.Columns[4].Width = 110;
            dataGridView_DevList.Columns[5].Width = 110;
            dataGridView_DevList.Columns[6].Width = 110;
            dataGridView_DevList.Columns[7].Width = 100;
            dataGridView_DevList.Columns[8].Width = 100;

            dataGridView_DevList.Columns[9].Visible = false; // id不显示
            dataGridView_DevList.Columns[10].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.isInDetection = false;
            addDevDlg.picker_startTime.Value = DateTime.Now;
            addDevDlg.picker_startTime.MinDate = DateTime.Now;
            addDevDlg.picker_endTime.Value = DateTime.Now.AddMinutes(5);
            addDevDlg.picker_endTime.MinDate = DateTime.Now.AddMinutes(5);
            addDevDlg.HideMaskAction += () =>
            {
                this.HideMask();
            };
            this.ShowMask();
            addDevDlg.StartPosition = FormStartPosition.CenterParent; // 指定窗口弹出在父窗口的中间位置


            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)
        {
            if (dataGridView_DevList.SelectedRows.Count == 0)//判断是否选中某行
            {
                MessageBox.Show("无选中仪器!"); return;
            }
            int rowIndex = dataGridView_DevList.CurrentRow.Index;
            long id = (long)dataGridView_DevList.Rows[rowIndex].Cells[9].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[5].Value.ToString();
            detailDlg.Controls.Find("label_customerName", true)[0].Text = dataGridView_DevList.Rows[rowIndex].Cells[6].Value.ToString();

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

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dataGridView_DevList.SelectedRows.Count == 0)//判断是否选中某行
            {
                MessageBox.Show("无选中仪器!");return;
            }
            int rowIndex = dataGridView_DevList.CurrentRow.Index;
            long id = (long)dataGridView_DevList.Rows[rowIndex].Cells[9].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.deviceId = Convert.ToInt64(dataGridView_DevList.Rows[rowIndex].Cells[9].Value);
            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[10].Value;
            editDlg.inputCustComp.Text = (string)dataGridView_DevList.Rows[rowIndex].Cells[5].Value;
            editDlg.inputCustName.Text = (string)dataGridView_DevList.Rows[rowIndex].Cells[6].Value;
            editDlg.text_channelNo.Text = (string)dataGridView_DevList.Rows[rowIndex].Cells[8].Value;
            editDlg.btnSave.Text = "保存";
            editDlg.picker_startTime.Value = DateTime.Now;
            editDlg.picker_endTime.Value = DateTime.Now.AddMinutes(5);
            if (((string)dataGridView_DevList.Rows[rowIndex].Cells[7].Value).Equals("检测中"))
            {
                AddDevDlg.isInDetection = true;
                DetectionItemService detectionItemService = new DetectionItemServiceImpl();
                List<DetectionItem> list = detectionItemService.search(id, true);
                if (list != null && list.Count > 0)
                {
                    DetectionItem detectionItem = list[0];
                    if (!detectionItem.Accuracy.Equals("-3")) editDlg.checkBox_accuracy.Checked = true;

                    if (!detectionItem.Stability.Equals("-3"))
                    {
                        editDlg.checkBox_stability.Checked = true;
                        foreach (string n in detectionItem.Interval.Split(','))
                        {
                            if(!n.Equals(""))((RadioButton)editDlg.Controls.Find("radio_" + n + "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.picker_startTime.Value = Convert.ToDateTime(detectionItem.StartTime);
                    editDlg.picker_endTime.Value = Convert.ToDateTime(detectionItem.EndTime);
                    editDlg.groupBox2.Enabled = false;
                    editDlg.groupBox1.Enabled = false;
                }
            }
            else
            { AddDevDlg.isInDetection = false; }

            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 (dataGridView_DevList.SelectedRows.Count == 0)//判断是否选中某行
            {
                MessageBox.Show("无选中仪器!"); return;
            }
            if (((string)dataGridView_DevList.Rows[dataGridView_DevList.CurrentRow.Index].Cells[7].Value).Equals("检测中"))
            {
                MessageBox.Show("该仪器在检测中,无法删除!");
                return;
            }
            if (MessageBox.Show("确认删除选中仪器?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                    == DialogResult.Yes)
            {
                deviceService.delete(Convert.ToInt64(dataGridView_DevList.Rows[dataGridView_DevList.CurrentRow.Index].Cells[9].Value));
                MessageBox.Show("删除成功", "提示");
                LoadDevList(condition);
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            try
            {
                long devId = Convert.ToInt64(dataGridView_DevList.Rows[dataGridView_DevList.CurrentRow.Index].Cells[9].Value);

                List<DetectionItem> queryList = detectionItemService.search(devId, false);
                if (null != queryList && queryList.Count > 0)
                {
                    string deviceName = dataGridView_DevList.Rows[dataGridView_DevList.CurrentRow.Index].Cells[2].Value.ToString();
                    string deviceType = dataGridView_DevList.Rows[dataGridView_DevList.CurrentRow.Index].Cells[10].Value.ToString();

                    ExportDlg dlg = new ExportDlg(devId);
                    dlg.label_devName.Text = deviceName;
                    dlg.label_devType.Text = deviceType;
                    dlg.HideMaskAction += () =>
                    {
                        this.HideMask();
                    };
                    this.ShowMask();
                    dlg.StartPosition = FormStartPosition.CenterParent; // 指定窗口弹出在父窗口的中间位置
                    dlg.ShowDialog();

                    
                }
                else
                {
                    MessageBox.Show("所选仪器无测试数据可导出!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("导出失败!");
                LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "导出数据失败:" + ex.Message);
            }

        }
        /// <summary>
                /// 导出报表为Csv
                /// </summary>
                /// <param name="dt">DataTable</param>
                /// <param name="strFilePath">物理路径</param>
                /// <param name="tableheader">表头</param>
                /// <param name="columname">字段标题,逗号分隔</param>
        public bool dt2csv(DataTable dt, string strFilePath, string tableheader, string columname)
        {
            try
            {
                string strBufferLine = "";
                StreamWriter strmWriterObj = new StreamWriter(strFilePath, false, System.Text.Encoding.UTF8);
                strmWriterObj.WriteLine(tableheader);
                strmWriterObj.WriteLine(columname);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    strBufferLine = "";
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        if (j > 0)
                            strBufferLine += ",";
                        strBufferLine += dt.Rows[i][j].ToString();
                    }
                    strmWriterObj.WriteLine(strBufferLine);
                }
                strmWriterObj.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

    }
}