Newer
Older
IRIS_COLLECT / IOM_cs / insertForm / sysSetting / CtrlDept.cs
yangqianqian on 29 Dec 2020 19 KB first
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IOM_cs.insertForm.data.dto;
using IOM_cs.tool;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using IOM_cs.irisDb.service;
using IOM_cs.irisDb;
using IOM_cs.irisDb.model;
using System.Reflection;

namespace IOM_cs.insertForm.sysSetting
{
    public partial class CtrlDept : UserControl
    {
        IDeptService iDeptService = (IDeptService)DbService.getEntity("IDeptService");
        IPersonService iPersonService = (IPersonService)DbService.getEntity("IPersonService");

        public static CtrlDept ctrlDept;
        private DataTable rootNode;
        public static DataTable parent = new DataTable();
        public static DataSet children = new DataSet();

        public static DataTable sourceTable = new DataTable();

        public static string simplenameSearch = "";
        public CtrlDept()
        {
            InitializeComponent();

            ctrlDept = this;

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

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


            Label col1 = new Label();
            col1.Text = "序号";
            col1.Font = new Font("微软雅黑", 10.5f, FontStyle.Bold);
            col1.ForeColor = ColorTranslator.FromHtml("#333333");
            col1.Location = new Point(27, 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(95, 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(362, 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(555, 11);
            dataGridView1.Controls.Add(col4);
            col4.BringToFront();

            Label col5 = new Label();
            col5.AutoSize = true;
            col5.Text = "排序";
            col5.Font = new Font("微软雅黑", 10.5f, FontStyle.Bold);
            col5.ForeColor = ColorTranslator.FromHtml("#333333");
            col5.Location = new Point(701, 11);
            dataGridView1.Controls.Add(col5);
            col5.BringToFront();

        }

        private void CtrlDept_Load(object sender, EventArgs e)
        {
            ctrlDept = this;
            loadData();
            dataGridView1.DataSource = sourceTable;
            initDataGridView();
            dataGridView1.ClearSelection();
            
        }

        public void loadData()
        {
            try
            {
                parent = new DataTable();
                children = new DataSet();

                sourceTable = iDeptService.GetDepts();
                if (sourceTable.Rows.Count > 0)
                {
                    DataTable tableNew = sourceTable.Clone();
                    rootNode = sourceTable.Clone();
                    parent = sourceTable.Clone();


                    string rootId = "";
                    // 顶级
                    foreach (DataRow row in sourceTable.Rows)
                    {
                        if (Convert.ToInt64(row["PID"]) < 0)
                        {
                            sourceTable.Rows.Remove(row);
                            break;
                            //rootNode.Rows.Add(row.ItemArray);
                            //rootNode.Rows[0]["SIMPLENAME"] = "∨  " + rootNode.Rows[0]["SIMPLENAME"];
                            //rootId = row["ID"].ToString();
                        }
                    }
                    // 一级组织
                    parent.Columns["SIMPLENAME"].DataType = typeof(string);
                    foreach (DataRow row in sourceTable.Rows)
                    {
                        if (row["PID"].ToString() == rootId || Convert.ToInt64(row["PID"]) == 0)
                        {
                            row["SIMPLENAME"] = "    " + row["SIMPLENAME"];
                            parent.Rows.Add(row.ItemArray);
                        }

                    }
                    // 部门
                    if (parent.Rows.Count > 0)
                    {
                        foreach (DataRow parentRow in parent.Rows)
                        {
                            DataTable dt = sourceTable.Clone();
                            dt.Columns["SIMPLENAME"].DataType = typeof(string);
                            foreach (DataRow row in sourceTable.Rows)
                            {
                                if (row["PID"].ToString() == parentRow["ID"].ToString())
                                {
                                    row["SIMPLENAME"] = "          " + row["SIMPLENAME"];
                                    dt.Rows.Add(row.ItemArray);
                                }
                            }
                            if (dt.Rows.Count > 0)
                            {
                                parentRow["SIMPLENAME"] = "   ∨  " + parentRow["SIMPLENAME"].ToString().Trim();
                            }
                            children.Tables.Add(dt);
                        }

                        //重构sourceTable
                        sourceTable.Rows.Clear();
                        //sourceTable.ImportRow(rootNode.Rows[0]);
                        int tableIndex = 0;
                        foreach (DataRow parentRow in parent.Rows)
                        {
                            sourceTable.Rows.Add(parentRow.ItemArray);

                            if (children.Tables[tableIndex].Rows.Count > 0)
                            {
                                foreach (DataRow dr in children.Tables[tableIndex].Rows)
                                {
                                    sourceTable.Rows.Add(dr.ItemArray);
                                }
                            }
                            tableIndex++;
                        }
                    }
                    else
                    {
                        children.Tables.Add(sourceTable);
                    }


                    DataColumn indexCol = new DataColumn();
                    indexCol.ColumnName = "index";
                    sourceTable.Columns.Add(indexCol);
                    sourceTable.Columns["index"].SetOrdinal(0);
                }
            }
            catch (Exception E)
            {
                MessageBox.Show("加载部门信息异常!");
                //LogHelper.WriteLog();
            }
        }


        private void initDataGridView()
        {
            try
            {
                dataGridView1.Columns[1].Visible = false;
                dataGridView1.Columns[6].Visible = false;
                dataGridView1.Columns[7].Visible = false;

                dataGridView1.Columns[1].HeaderText = "id";
                dataGridView1.Columns[2].HeaderText = "简称";
                dataGridView1.Columns[3].HeaderText = "全称";
                dataGridView1.Columns[4].HeaderText = "备注";
                dataGridView1.Columns[5].HeaderText = "";



                //820
                dataGridView1.Columns[0].Width = 80;
                dataGridView1.Columns[1].Width = 10;
                dataGridView1.Columns[2].Width = 220;
                dataGridView1.Columns[3].Width = 160;
                dataGridView1.Columns[4].Width = 120;
                dataGridView1.Columns[5].Width = 100;

                dataGridView1.Columns[0].ReadOnly = true;
                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[0].SortMode = DataGridViewColumnSortMode.NotSortable;
                dataGridView1.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable;
                dataGridView1.Columns[2].SortMode = DataGridViewColumnSortMode.NotSortable;
                dataGridView1.Columns[3].SortMode = DataGridViewColumnSortMode.NotSortable;
                dataGridView1.Columns[4].SortMode = DataGridViewColumnSortMode.NotSortable;
                dataGridView1.Columns[5].SortMode = DataGridViewColumnSortMode.NotSortable;
                dataGridView1.Columns[6].SortMode = DataGridViewColumnSortMode.NotSortable;
                dataGridView1.Columns[7].SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            catch (Exception e)
            {
                MessageBox.Show("页面初始化错误" );
            }
        }
               

        public void getDeptList()
        {
            DialogResult re = MessageBox.Show("此操作会删除现有的全部人员数据、虹膜数据,且不可恢复,确定仍要获取新的组织机构?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (re == DialogResult.Yes)
            {
                try
                {
                    string api = "/dept/listAll";
                    string receive = HttpHelper.Http(api, "GET","", null);
                    JObject json = (JObject)JsonConvert.DeserializeObject(receive);//或者JObject jo = JObject.Parse(jsonText);
                    if (json["code"].ToString() == "200")
                    {
                        if (iDeptService.clearDept() == 0)
                        {
                            JArray data = (JArray)json["data"];
                            foreach (var jObject in data)
                            {
                                Dept dept = new Dept();

                                dept.Id = jObject["id"].ToString();//获取字符串中id值
                                dept.Num = Convert.ToInt32(jObject["num"]);
                                dept.Pid = jObject["pid"].ToString();
                                dept.Pids = jObject["pids"].ToString();
                                dept.Simplename = jObject["simplename"].ToString();
                                dept.Fullname = jObject["fullname"].ToString();
                                dept.Tips = jObject["tips"].ToString();
                                dept.Version = jObject["version"].ToString();

                                iDeptService.AddDept(dept);
                            }
                            iPersonService.deleteAllData();

                            MessageBox.Show("获取成功!");
                            loadData();
                        }
                    }
                    else
                    {
                        MessageBox.Show("服务器异常!");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("获取失败!" + ex.Message);
                }
            }
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush b = new SolidBrush(this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), this.dataGridView1.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 40, e.RowBounds.Location.Y + 12);
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                dataGridView1.ClearSelection();
                dataGridView1.CurrentCell = null;
                //if (e.RowIndex % 2 != 0)
                //{
                //    dataGridView1.RowTemplate.DefaultCellStyle.SelectionBackColor = ColorTranslator.FromHtml("#ECEEF2");
                //}
                //else
                //{
                //    dataGridView1.RowTemplate.DefaultCellStyle.SelectionBackColor = ColorTranslator.FromHtml("#FFFFFF");
                //}
                // 点击“顶级”
                if (e.RowIndex == 0 && dataGridView1.Rows[0].Cells["SIMPLENAME"].Value.ToString()=="顶级")
                {
                    string s = dataGridView1.Rows[0].Cells[2].Value.ToString();
                    if (s.Contains("∨"))
                    {
                        CurrencyManager cm = (CurrencyManager)BindingContext[dataGridView1.DataSource];

                        cm.SuspendBinding();

                        for (int i = 1; i < dataGridView1.Rows.Count; i++)
                        {
                            dataGridView1.Rows[i].Visible = false;
                        }

                        cm.ResumeBinding();


                        dataGridView1.Rows[0].Cells[2].Value = s.Replace("∨", ">");
                    }
                    if (s.Contains(">"))
                    {
                        CurrencyManager cm = (CurrencyManager)BindingContext[dataGridView1.DataSource];

                        cm.SuspendBinding();

                        for (int i = 1; i < dataGridView1.Rows.Count; i++)
                        {
                            dataGridView1.Rows[i].Visible = true;
                        }

                        cm.ResumeBinding();

                        dataGridView1.Rows[0].Cells[2].Value = s.Replace(">", "∨");
                    }
                }

                int tableIndex = isParentNode(dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString());
                if (tableIndex != -1)
                {
                    string simplename = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();

                    if (simplename.Contains("∨"))
                    {
                        CurrencyManager cm = (CurrencyManager)BindingContext[dataGridView1.DataSource];
                        cm.SuspendBinding();
                        for (int j = e.RowIndex; j < dataGridView1.Rows.Count; j++)
                        {
                            if (isChild(tableIndex, dataGridView1.Rows[j].Cells[1].Value.ToString()))
                            {
                                dataGridView1.Rows[j].Visible = false;
                            }
                        }
                        cm.ResumeBinding();

                        dataGridView1.Rows[e.RowIndex].Cells[2].Value = simplename.Replace("∨", ">");
                    }
                    if (simplename.Contains(">"))
                    {
                        CurrencyManager cm = (CurrencyManager)BindingContext[dataGridView1.DataSource];
                        cm.SuspendBinding();

                        for (int j = e.RowIndex; j < dataGridView1.Rows.Count; j++)
                        {                            
                            if (isChild(tableIndex, dataGridView1.Rows[j].Cells[1].Value.ToString()))
                            {
                                dataGridView1.Rows[j].Visible = true;
                            }                            
                        }

                        cm.ResumeBinding();
                        dataGridView1.Rows[e.RowIndex].Cells[2].Value = simplename.Replace(">", "∨");
                    }
                }

                dataGridView1.ClearSelection();
                dataGridView1.CurrentCell = null;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private int isParentNode(string id)
        {
            int num = 0;
            try
            {
                foreach (DataRow row in parent.Rows)
                {
                    if (row[0].ToString() == id)
                    {
                        return num;
                    }
                    num++;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            return -1;
        }

        private bool isChild(int tableIndex,string id)
        {
            try
            {
                DataTable dt = children.Tables[tableIndex];
                foreach (DataRow row in dt.Rows)
                {
                    if (row[0].ToString() == id)
                    {
                        return true;
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            return false;
        }

        public void search()
        {
            try
            {
                //dataGridView1.Rows.Clear();
                //dataGridView1.Columns.Clear();
                if (simplenameSearch != "")
                {
                    DataRow[] rows = sourceTable.Select("FULLNAME like '%" + simplenameSearch.Trim() + "%'");
                    dataGridView1.DataSource = rows;
                    if (rows == null || rows.Length == 0)
                    {
                        dataGridView1.DataSource = null;
                        return;
                    }

                    DataTable sourceTableNew = sourceTable.Clone();
                    int index = 0;
                    foreach (DataRow row in rows)
                    {
                        sourceTableNew.Rows.Add(row.ItemArray);
                        sourceTableNew.Rows[index][2] = sourceTableNew.Rows[index][2].ToString().Replace(">", "").Replace("∨", "").Trim();
                        index++;
                    }
                    dataGridView1.DataSource = sourceTableNew;
                }
                else
                {
                    dataGridView1.DataSource = sourceTable;
                }

                initDataGridView();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }


        public string getDeptNameById(Int64 id)
        {
            try
            {
                DataRow[] rows = sourceTable.Select("ID= '" + id + "'");
                if (rows.Count() == 1)
                {
                    return rows[0]["SIMPLENAME"].ToString();
                }
                else
                {
                    return "";
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            return "";
        }

        private void btn_getDeptList_Click(object sender, EventArgs e)
        {

        }
    }
}