Newer
Older
IRIS_COLLECT / IOM_cs / insertForm / collection / CtrlCollectionFrom.cs
yangqianqian on 29 Dec 2020 26 KB first
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using IOM_cs.insertForm.service;
using IOM_cs.insertForm.service.impl;
using IOM_cs.insertForm.data.dto;
using System.Reflection;
using IOM_cs.insertForm.dtGridView;
using IOM_cs.irisDb.service;
//using IOM_cs.irisDb.service.mySqlImpl;
using IOM_cs.irisDb.model;
using IOM_cs.irisDb;
using IOM_cs.tool;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using IOM_cs.insertForm.sysSetting;

namespace IOM_cs.insertForm.collection
{
    public partial class CtrlCollectionFrom : UserControl
    {
        ICollListViewService collListViewService = (ICollListViewService)DbService.getEntity("ICollListViewService");
        IPersonService iPersonService = (IPersonService)DbService.getEntity("IPersonService");
        IDeptService iDeptService = (IDeptService)DbService.getEntity("IDeptService");

        public static CtrlCollectionFrom ctrlCollectionFrom;

        int totalCount = 0;
        int currentPage = 1;
        int pageCount = 0;
        int limitCount = 10;
        CheckBox ckBox;

        string deptId = "";

        public CtrlCollectionFrom()
        {
            InitializeComponent();
                       
            ctrlCollectionFrom = this;

            treeView_dept.Visible = false;
                        
            comboBoxUpLoad.Items.Add("全部");
            comboBoxUpLoad.Items.Add("未上传");
            comboBoxUpLoad.Items.Add("已上传");
            comboBoxUpLoad.Items.Add("上传失败");

            dataGridView1.Rows.Clear();
            dataGridView1.Columns.Clear();

            DataGridViewCheckBoxColumn columncb = new DataGridViewCheckBoxColumn();
            columncb.HeaderText = "";
            columncb.Name = "cb_check";
            columncb.TrueValue = true;
            columncb.FalseValue = false;
            columncb.DataPropertyName = "IsChecked";
            dataGridView1.Columns.Add(columncb);

            Panel panel = new Panel();
            panel.BackColor = Color.White;
            panel.Size = new System.Drawing.Size(848, 40);
            panel.Location = new Point(0,0);
            panel.BackColor = ColorTranslator.FromHtml("#ECEEF2");
            dataGridView1.Controls.Add(panel);

            ckBox = new CheckBox();
            ckBox.Text = "";
            ckBox.Checked = false;
            System.Drawing.Rectangle rect = dataGridView1.GetCellDisplayRectangle(0, -1, true);
            ckBox.Size = new System.Drawing.Size(13, 13);
            ckBox.BackColor = Color.White;
            ckBox.Location = new Point(16, 13);
            ckBox.Click += new EventHandler(ckBox_CheckedChanged);
            dataGridView1.Controls.Add(ckBox);
            ckBox.BringToFront();

            Label col1 = new Label();
            col1.Text = "员工编号";
            col1.Font = new Font("微软雅黑", 10.5f, FontStyle.Bold);
            col1.ForeColor = ColorTranslator.FromHtml("#333333");
            col1.Location = new Point(69, 11);
            dataGridView1.Controls.Add(col1);
            col1.BringToFront();

            Label col2 = new Label();
            col2.Text = "姓名";
            col2.Font = new Font("微软雅黑", 10.5f, FontStyle.Bold);
            col2.ForeColor = ColorTranslator.FromHtml("#333333");
            col2.Location = new Point(197, 11);
            dataGridView1.Controls.Add(col2);
            col2.BringToFront();

            Label col3 = new Label();
            col3.Text = "部门";
            col3.Font = new Font("微软雅黑", 10.5f, FontStyle.Bold);
            col3.ForeColor = ColorTranslator.FromHtml("#333333");
            col3.Location = new Point(334, 11);
            dataGridView1.Controls.Add(col3);
            col3.BringToFront();

            Label col4 = new Label();
            col4.Text = "性别";
            col4.Font = new Font("微软雅黑", 10.5f, FontStyle.Bold);
            col4.ForeColor = ColorTranslator.FromHtml("#333333");
            col4.Location = new Point(459, 11);
            dataGridView1.Controls.Add(col4);
            col4.BringToFront();

            Label col5 = new Label();
            col5.Text = "采集时间";
            col5.Font = new Font("微软雅黑", 10.5f, FontStyle.Bold);
            col5.ForeColor = ColorTranslator.FromHtml("#333333");
            col5.Location = new Point(569, 11);
            dataGridView1.Controls.Add(col5);
            col5.BringToFront();           

            Label col6 = new Label();
            col6.Text = "上传状态";
            col6.Font = new Font("微软雅黑", 10.5f, FontStyle.Bold);
            col6.ForeColor = ColorTranslator.FromHtml("#333333");
            col6.Location = new Point(718, 11);
            dataGridView1.Controls.Add(col6);
            col6.BringToFront();

            totalCount = 0;
            currentPage = 1;
            pageCount = 0;
            limitCount = 10;


            LoadData("");

        }

        private void CtrlCollectionFrom_Load(object sender, EventArgs e)
        {

            this.Width = 900;
            this.Height = 560;

        }

        private void LoadData(string searchStr)
        {
            try
            {  
                DataTable table = collListViewService.GetCollList(limitCount, currentPage, searchStr);

                // 添加部门列
                DataColumn simplenameCol = new DataColumn();
                simplenameCol.ColumnName = "SIMPLENAME";
                table.Columns.Add(simplenameCol);
                table.Columns["SIMPLENAME"].SetOrdinal(3);

                //包装数据,把部门信息填到部门列
                foreach (DataRow row in table.Rows)
                {
                    DataRow[] rows = CtrlDept.sourceTable.Select("ID='" + row["DEPTID"].ToString() + "'");
                    row["SIMPLENAME"] = rows[0]["SIMPLENAME"].ToString().Replace("∨", "").Replace(">", "").Trim();
                }

                totalCount = collListViewService.GetCollCount(searchStr);
                pageCount = totalCount / limitCount;
                int ys = totalCount % limitCount;
                if (ys != 0)
                    pageCount++;
                setPageLabel();

                dataGridView1.DataSource = table;
                initDataGridView();

                ckBox.CheckState = CheckState.Unchecked;
                //CheckBoxClass.dgv = dataGridView1;
                //CheckBoxClass.AddFullSelect();  

                CurrencyManager cm = (CurrencyManager)BindingContext[dataGridView1.DataSource];

                cm.SuspendBinding();

                for (int index = 0; index < dataGridView1.Rows.Count; index++)
                {
                    DataGridViewCheckBoxCell cb = (DataGridViewCheckBoxCell)dataGridView1.Rows[index].Cells[0];
                    if (dataGridView1.Rows[index].Cells[7].Value.ToString() == "已上传")
                    {
                        //dataGridView1.EndEdit();
                        cb.ReadOnly = true;
                    }
                }
                cm.ResumeBinding();
            }
            catch (Exception e)
            {
                MessageBox.Show("加载数据异常!" + e.Message);
            }
        }

        // 全选事件
        private void ckBox_CheckedChanged(object sender, EventArgs e)
        {
            dataGridView1.EndEdit();
            if (((CheckBox)sender).CheckState == CheckState.Checked)
            {
                for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                {
                    //if(dataGridView1.)
                    DataGridViewCheckBoxCell checkBox = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[0];
                    checkBox.Value = true;                    
                }
            }
            else
            {
                for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell checkBox = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[0];
                    checkBox.Value = false;
                }
            }
        }

        private void setPageLabel()
        {
            label3.Text = "共" + totalCount + "条,每页" + limitCount + "条,共" + pageCount + "页";
            textBoxPage.Text = currentPage.ToString();
        }

        private void initDataGridView() 
        {
            try
            {
                dataGridView1.Columns[1].Visible = false;
                dataGridView1.Columns[2].HeaderText = "员工编号";
                dataGridView1.Columns[3].HeaderText = "姓名";
                dataGridView1.Columns[4].HeaderText = "部门";
                dataGridView1.Columns[5].HeaderText = "性别";
                dataGridView1.Columns[6].HeaderText = "采集时间";
                dataGridView1.Columns[7].HeaderText = "是否上传";
                dataGridView1.Columns[8].Visible = false; // deptid列


                dataGridView1.Columns[6].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";

                dataGridView1.Columns[0].Width = 40;
                dataGridView1.Columns[1].Width = 0;
                dataGridView1.Columns[2].Width = 100;
                dataGridView1.Columns[3].Width = 100;
                dataGridView1.Columns[4].Width = 140;
                dataGridView1.Columns[5].Width = 80;
                dataGridView1.Columns[6].Width = 140;
                dataGridView1.Columns[7].Width = 140;
                dataGridView1.Columns[8].Width = 0;

                dataGridView1.Columns[1].ReadOnly = true;
                dataGridView1.Columns[2].ReadOnly = true;
                dataGridView1.Columns[3].ReadOnly = true;
                dataGridView1.Columns[4].ReadOnly = true;
                dataGridView1.Columns[5].ReadOnly = true;
                dataGridView1.Columns[6].ReadOnly = true;
                dataGridView1.Columns[7].ReadOnly = true;
                dataGridView1.Columns[8].ReadOnly = true;
            }
            catch (Exception e)
            {
                MessageBox.Show("列表初始化出错");
            }
        }
        /// <summary>
        /// 查询条件
        /// </summary>
        /// <returns></returns>
        private string getSearchStr() {

            string upLoadCase = comboBoxUpLoad.Text;
            string searchStr = "";
            if (txt_dept.Text != "")
                searchStr += " and DEPTID = " + deptId;
            if (upLoadCase != "")
            {
                switch (upLoadCase)
                {
                    case "未上传":
                        searchStr += " and FLAG_UPLOAD = 0";
                        break;
                    case "已上传":
                        searchStr += " and FLAG_UPLOAD = 1";
                        break;
                    case "上传失败":
                        searchStr += " and FLAG_UPLOAD = 2 or FLAG_UPLOAD = 3 or FLAG_UPLOAD = 4";
                        break;
                    default:
                        break;
                }
            }
            return searchStr;
        }

        private void roundButtonSearch_Click(object sender, EventArgs e)
        {
            currentPage = 1;

            treeView_dept.Visible = false;
            string searctStr = getSearchStr();
            LoadData(searctStr);
        }

        private void roundButtonDelete_Click(object sender, EventArgs e)
        {
            int count = 0;            
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if ((bool)dataGridView1.Rows[i].Cells[0].EditedFormattedValue == true)
                {
                    DataGridViewRow row = dataGridView1.Rows[i];
                    Int64 personId = Convert.ToInt64(row.Cells["ID"].Value.ToString());
                    iPersonService.DeletePerson(personId);
                    count++;
                }
            }
            if (count == 0)
            {
                MessageBox.Show("请至少选中一项","提示");
                return;
            }
            else
            {
                if (MessageBox.Show("确认删除记录?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                    == DialogResult.Yes)
                {
                    string searctStr = getSearchStr();
                    LoadData(searctStr);
                    MessageBox.Show("采集记录删除成功","提示");
                }
            }            
        }


        /// <summary>
        /// 上传所选记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void roundButtonUpCheck_Click(object sender, EventArgs e)
        {
            try
            {
                List<CollListView> uploadDatas = new List<CollListView>();
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    if ((bool)dataGridView1.Rows[i].Cells[0].EditedFormattedValue == true)
                    {
                        DataGridViewRow row = dataGridView1.Rows[i];
                        long id = Convert.ToInt64(row.Cells["ID"].Value.ToString());
                        CollListView collListView = collListViewService.GetCollListViewById(id);

                        uploadDatas.Add(collListView);
                    }
                }
                if (uploadDatas.Count > 0)
                {
                    if (MessageBox.Show("确认上传所选数据?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        // 整理数据
                        string data = "[";
                        int index = 0;
                        foreach(CollListView collListView in uploadDatas)
                        {
                            if (collListView.flagUpload == "1") continue;

                            try
                            {
                                if (index == 0) data = data + JsonConvert.SerializeObject(collListView);
                                else data = data + "," + JsonConvert.SerializeObject(collListView);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                            
                            index++;
                        }
                        data = data + "]";
                        if (data == "[]")
                        {
                            MessageBox.Show("操作失败,所选数据都是已上传的数据!");
                            return;
                        }
                        string response = HttpHelper.Http("/person/uploadPerson", "POST", data, null);
                        if (response != "")
                        {
                            JObject json = (JObject)JsonConvert.DeserializeObject(response);//或者JObject jo = JObject.Parse(jsonText);
                            if (json["code"].ToString() == "500")
                            {
                                MessageBox.Show("上传失败,服务器异常!", "提示");
                            }
                            else if (json["code"].ToString() == "200" || json["code"].ToString() == "501")
                            {
                                string irisRepeatPerson = "";
                                string codeRepeatPerson = "";
                                string deptErrorPerson = "";
                                if (json["code"].ToString() == "200")
                                {
                                    MessageBox.Show("上传成功", "提示");
                                }
                                else if (json["code"].ToString() == "501")
                                {
                                    JObject reData = (JObject)JsonConvert.DeserializeObject(json["data"].ToString());
                                    irisRepeatPerson = reData["irisRepeatList"].ToString();
                                    codeRepeatPerson = reData["codeRepeatList"].ToString();
                                    deptErrorPerson = reData["deptErrorList"].ToString();
                                    MessageBox.Show("操作成功,但部分数据上传失败!", "提示");
                                }

                                //更新上传状态
                                foreach (CollListView collListView in uploadDatas)
                                {
                                    iPersonService.UpdateFlagUpload(collListView.id, "1");
                                    if (irisRepeatPerson.Contains(collListView.deptId.ToString()) && irisRepeatPerson.Contains(collListView.name))
                                    {
                                        iPersonService.UpdateFlagUpload(collListView.id, "2");
                                    }
                                    else if (deptErrorPerson.Contains(collListView.deptId.ToString()) && deptErrorPerson.Contains(collListView.name))
                                    {
                                        iPersonService.UpdateFlagUpload(collListView.id, "3");
                                    }
                                    else if (codeRepeatPerson.Contains(collListView.deptId.ToString()) && codeRepeatPerson.Contains(collListView.name))
                                    {
                                        iPersonService.UpdateFlagUpload(collListView.id, "4");
                                    }
                                }

                                string searctStr = getSearchStr();
                                LoadData(searctStr);
                            }
                            else
                            {
                                MessageBox.Show(json["message"].ToString());
                            }

                        }
                        else
                        {
                            MessageBox.Show("服务器访问异常!");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("请至少选中一项", "提示");
                    return;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("服务器异常!");
                LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, "上传选中数据失败:" + ex.Message);
            }
        }
       
        private void dataGridView1_CellDoubleClick_1(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
                return;
            DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
            try
            {
                Int64 id = Convert.ToInt64(row.Cells["ID"].Value.ToString());
                Person person = new Person();
                person.id = id;
                FormMain.person = new Person();
                FormMain.irisData = new IrisData();
                iPersonService.GetDetail(id, ref FormMain.person,  ref FormMain.irisData);

                IInsertService insertService = new InsertImpl();
                insertService.setToFirst(FormMain.formMain.panel_Insert);
                insertService.SetPanelControl(FormMain.formMain.panel_Insert, "detail");

                FormMain.formMain.panel_Insert.BringToFront();
                FormMain.formMain.panel4.Visible = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("详情查看失败:" + ex, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

      

        
        private void buttonNext_Click(object sender, EventArgs e)
        {
            if (currentPage < pageCount)
            {
                currentPage++;
                string searctStr = getSearchStr();
                LoadData(searctStr);
            }
        }


        private void buttonPre_Click(object sender, EventArgs e)
        {
            if (currentPage > 1)
            {
                currentPage--;
                string searctStr = getSearchStr();
                LoadData(searctStr);
            }
        }        

        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            dataGridView1.ClearSelection();
        }

        private void comboBoxUpLoad_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxUpLoad.SelectedIndex > -1) txt_UpLoad.Text = comboBoxUpLoad.SelectedItem.ToString();
        }

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

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.RowIndex != -1)
            {
                DataGridViewCheckBoxCell cb = (DataGridViewCheckBoxCell)dataGridView1.Rows[e.RowIndex].Cells[0];               
                if (Convert.ToBoolean(cb.Value) == false)
                {
                    ckBox.CheckState = CheckState.Unchecked;
                }                
            }
        }

        private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (dataGridView1.IsCurrentCellDirty)
            {
                dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }

        private void btn_dropDept_Click(object sender, EventArgs e)
        {
            try
            {
                treeView_dept.Visible = !treeView_dept.Visible;
                FormMain.formMain.ctrlDept.loadData();

                treeView_dept.Nodes.Clear();
                int index = 0;
                if (CtrlDept.parent.Rows.Count > 0)
                {
                    foreach (DataRow rowParent in CtrlDept.parent.Rows)
                    {
                        TreeNode parentNode = new TreeNode();
                        parentNode.Text = rowParent[2].ToString();
                        parentNode.Tag = rowParent[0].ToString();
                        treeView_dept.Nodes.Add(parentNode);
                        foreach (DataRow rowChild in CtrlDept.children.Tables[index].Rows)
                        {
                            TreeNode childNode = new TreeNode();
                            childNode.Text = rowChild[2].ToString();
                            childNode.Tag = rowChild[0].ToString();
                            treeView_dept.Nodes[index].Nodes.Add(childNode);
                        }
                        index++;
                    }
                }
                else
                {
                    foreach (DataRow rowChild in CtrlDept.children.Tables[index].Rows)
                    {
                        TreeNode childNode = new TreeNode();
                        childNode.Text = rowChild[2].ToString();
                        childNode.Tag = rowChild[0].ToString();
                        treeView_dept.Nodes.Add(childNode);
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void treeView_dept_AfterSelect(object sender, TreeViewEventArgs e)
        {
            deptId = treeView_dept.SelectedNode.Tag.ToString();
            txt_dept.Text = treeView_dept.SelectedNode.Text;
            treeView_dept.Visible = false;
        }

        private void CtrlCollectionFrom_Click(object sender, EventArgs e)
        {
            treeView_dept.Visible = false;
        }

        private void panel_background_Click(object sender, EventArgs e)
        {
            treeView_dept.Visible = false;
        }

        private void dataGridView1_Click(object sender, EventArgs e)
        {
            treeView_dept.Visible = false;
        }

        // 下发数据到设备
        private void btn_sendToDev_Click(object sender, EventArgs e)
        {
            try
            {

                List<CollListView> sendDataList = new List<CollListView>();

                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    if ((bool)dataGridView1.Rows[i].Cells[0].EditedFormattedValue == true)
                    {
                        DataGridViewRow row = dataGridView1.Rows[i];
                        long id = Convert.ToInt64(row.Cells["ID"].Value.ToString());
                        CollListView collListView = collListViewService.GetCollListViewById(id);

                        sendDataList.Add(collListView);
                    }
                }
                if (sendDataList.Count > 0)
                {
                    // 整理人员、虹膜数据
                    string data = "[";
                    int index = 0;
                    foreach (CollListView collListView in sendDataList)
                    {
                        if (index == 0) data = data + JsonConvert.SerializeObject(collListView);
                        else data = data + "," + JsonConvert.SerializeObject(collListView);
                        index++;
                    }
                    data = "data from offline" + data + "]";

                    //部门列表数据
                    //index = 0;
                    //DataTable deptTable = iDeptService.GetDepts();
                    //string rowStr = JsonConvert.SerializeObject(deptTable);
                    //data = data + rowStr;               

                    CtrlSendToDev ctrlSendToDev = new CtrlSendToDev(data);
                    if (ctrlSendToDev.ShowDialog() == DialogResult.OK)
                    {
                        dataGridView1.ClearSelection();
                    }
                }
                else
                {
                    MessageBox.Show("请至少选中一项!", "提示");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        // 已上传的数据不可选中
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //dataGridView1.EndEdit();
            //if (dataGridView1.Rows[e.RowIndex].Cells[7].Value.ToString() == "已上传")
            //{
            //    ((DataGridViewCheckBoxCell)dataGridView1.Rows[e.RowIndex].Cells[0]).Value = false;
            //}
        }          

       
    }
}