Newer
Older
IRIS_COLLECT / IOM_cs / insertForm / CtrlDetail.cs
yangqianqian on 29 Dec 2020 19 KB first
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using IOM_cs.irisDb.model;
using IOM_cs.irisDb.service;
using IOM_cs.irisDb;
using IOM_cs.insertForm.sysSetting;
using System.Data;

namespace IOM_cs.insertForm
{
    public partial class CtrlDetail : UserControl
    {

        IPersonService iPersonService = (IPersonService)DbService.getEntity("IPersonService");

        string deptId = "";
        string idCardNoBefore = "";
        string staffNoBefort = "";
       
        public CtrlDetail()
        {          

            InitializeComponent();

            ////pic_det_photo.Location = new Point(690, 68);
            //pic_det_photo.Parent = this;
            //pic_det_photo.BringToFront();
            ////panel_eyeImage.Location = new Point(204, 830);
            //panel_eyeImage.Parent = this;
            //panel_eyeImage.BringToFront();
            deptId = "";
            resetDetail();
            setDetailValue(FormMain.person,FormMain.irisData);            

            // 民族
            var items = new System.Collections.ArrayList(Enum.GetValues(typeof(nation))).ToArray();
            comboBox_edit_nation.Items.Clear();
            comboBox_edit_nation.Items.AddRange(items);                     
           
        }


        #region 详情、编辑、删除

        private void resetDetail()
        {
            // 基础信息
            label_det_name.Text = "";
            label_det_staffNo.Text = "";
            label_det_idCardNo.Text = "";
            label_det_nation.Text = "";
            label_det_sex.Text = "";
            label_det_cerAuth.Text = "";
            label_det_addr1.Text = "";
            label_det_birthday.Text = "";
            label_det_remarks.Text = "";
            pic_det_photo.Image = null;          
                       

            // 生物特征
            // 左眼
            pic_det_imageL1.Image = null;
            label_det_qL.Text = "";
            // 右眼
            pic_det_imageR1.Image = null;
            label_det_qR.Text = "";
        }

        // 给详情页赋值
        private void setDetailValue(Person person, IrisData irisData)
        {
            try
            {
                idCardNoBefore = person.idCardNo;
                staffNoBefort = person.staffNo;

                // 基础信息
                label_det_name.Text = person.name;
                label_det_staffNo.Text = person.staffNo;
                label_det_addr1.Text = person.addr;
                label_det_birthday.Text = person.birthday.ToString("yyyy-MM-dd").Replace("0001-01-01","");
                label_det_cerAuth.Text = person.cerAuth;
                label_det_dept.Text = FormMain.formMain.ctrlDept.getDeptNameById(person.deptId).Replace("∨", "").Replace(">", "").Trim();
                label_det_remarks.Text = person.remarks;
                label_det_idCardNo.Text = person.idCardNo;
                label_det_nation.Text = person.nation==""?"":Enum.GetName(typeof(nation), Convert.ToInt16(person.nation));
                if(person.sex != "")label_det_sex.Text = Enum.GetName(typeof(sex), Convert.ToInt16(person.sex));
               
                //证件照
                if (person.photoData != null && person.photoData != "")
                {
                    string base64 = person.photoData.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "").Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", "");//将base64头部信息替换
                    byte[] bytes = Convert.FromBase64String(base64);
                    MemoryStream ms = new MemoryStream(bytes);
                    Image mImage = Image.FromStream(ms);
                    pic_det_photo.Image = Image.FromStream(ms);
                    pic_photo_edit.Image = Image.FromStream(ms);
                }
                              

                
                // 生物特征
                //左眼
                string base64L = irisData.imageL1Data;
                if (base64L != null && base64L != "") 
                { 
                    pic_det_imageL1.Image = Base64StringToImage(base64L);
                    pic_imageL_edit.Image = Base64StringToImage(base64L);
                }
                if (irisData.qL1 != "0" && irisData.qL1 != "")
                {
                    label_det_qL.Text = "质量:" + irisData.qL1;
                    label_qL_edit.Text = "质量:" + irisData.qL1;
                }
                //右眼
                string base64R = irisData.imageR1Data;
                if (base64R != null && base64R != "")
                {
                    pic_det_imageR1.Image = Base64StringToImage(base64R);
                    pic_imageR_edit.Image = Base64StringToImage(base64R);
                }
                if (irisData.qR1 != "0" && irisData.qR1 != "")
                {
                    label_det_qR.Text = "质量:" + irisData.qR1;
                    label_qR_edit.Text = "质量:" + irisData.qR1;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("获取详情失败!"+ex.Message);
            }
        }


        // 编辑 
        private void btn_edit_Click(object sender, EventArgs e)
        {
            try
            {
                panel_edit.Visible = true;
                panel_detail_labels.Visible = false;
                treeView_dept.Visible = false;

                //基础信息
                txt_edit_name.Text = label_det_name.Text;
                if (label_det_sex.Text == "男") radio_edit_male.Checked = true;
                else if (label_det_sex.Text == "女") radio_edit_female.Checked = true;
                txt_edit_idCardNo.Text = label_det_idCardNo.Text;


                if (label_det_nation.Text != "") comboBox_edit_nation.SelectedItem = Enum.Parse(typeof(nation), label_det_nation.Text);
                txt_edit_cerAuth.Text = label_det_cerAuth.Text;
                txt_edit_birthday.Text = label_det_birthday.Text;
                txt_edit_addr.Text = label_det_addr1.Text;
                txt_edit_dept.Text = label_det_dept.Text;

                txt_edit_staffNo.Text = label_det_staffNo.Text;
                txt_edit_nation.Text = label_det_nation.Text;
                txt_edit_remarks.Text = label_det_remarks.Text;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
           
        }

        // 取消编辑
        private void btn_cancelEdit_Click(object sender, EventArgs e)
        {
            panel_edit.Visible = false;
            panel_detail_labels.Visible = true; 
        } 
        
        // 删除
        private void btn_delete_Click(object sender, EventArgs e)
        {
            DialogResult r = MessageBox.Show("确认删除此人员信息?", "提示", MessageBoxButtons.YesNo);
            if (r == DialogResult.Yes)
            {
                int re = iPersonService.DeletePerson(FormMain.person.id);

                if (re == 0)
                { 
                    if (pic_det_imageL1.Image != null) pic_det_imageL1.Image.Dispose();
                    if (pic_det_imageR1.Image != null) pic_det_imageR1.Image.Dispose();

                    MessageBox.Show("删除成功!", "提示信息");

                    //跳转到采集列表
                    FormMain.insertService.setToFirst(FormMain.formMain.panel_Insert);
                    FormMain.insertService.SetPanelControl(FormMain.formMain.panel_Insert, "collect");

                }
                else
                {
                    MessageBox.Show("删除失败!", "错误");
                }
            }
        }
       
       

        // 保存编辑
        private void btn_saveEdit_Click_1(object sender, EventArgs e)
        {
            try
            {
                if (txt_edit_dept.Text.Trim() == "")
                {
                    MessageBox.Show("部门信息不能为空", "提示");
                }
                if (txt_edit_idCardNo.Text != "" && txt_edit_idCardNo.Text.Trim()!=idCardNoBefore)
                {
                    if (!Validation.CheckIDCard(txt_edit_idCardNo.Text.Trim()))
                    {
                        MessageBox.Show(this, "请输入正确的身份证号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }

                    //判断身份证号是否重复
                    if (iPersonService.IsIdCardNoPresent(txt_edit_idCardNo.Text.Trim()) > 0)
                    {
                        MessageBox.Show("已存在该证件号码!");
                        return;
                    }

                }
                // 与身份证号相互校验
                if (txt_edit_idCardNo.Text != "" )
                {
                    if (txt_edit_idCardNo.Text.Length == 18)
                    {
                        if ((Convert.ToInt16(txt_edit_idCardNo.Text.Substring(16, 1)) % 2) == 1)//奇数
                        {
                            radio_edit_male.Checked = true;
                        }
                        else radio_edit_female.Checked = true;
                    }
                    if (txt_edit_birthday.Text != "")
                    {
                        string birth = "";
                        if (txt_edit_idCardNo.Text.Length == 15) birth = ("19" + txt_edit_idCardNo.Text.Substring(6, 6)).Insert(2, "-").Insert(5, "-");
                        else if (txt_edit_idCardNo.Text.Length == 18) birth = txt_edit_idCardNo.Text.Substring(6, 8).Insert(4, "-").Insert(7, "-");
                        if (birth != txt_edit_birthday.Text)
                        {
                            MessageBox.Show(this, "所选生日与身份证号中的生日不一致!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return;
                        }
                    }
                }

                //判断员工编号是否重复
                if (txt_edit_staffNo.Text.Trim() != "" && txt_edit_staffNo.Text.Trim()!=staffNoBefort)
                {
                    if (iPersonService.IsStaffNoPresent(txt_edit_staffNo.Text.Trim()) > 0)
                    {
                        MessageBox.Show("已存在该员工编号!");
                        return;
                    }
                }


                if (txt_edit_name.Text.Trim() == "")
                {
                    MessageBox.Show("姓名不能为空", "提示");
                }
                else
                {
                    if (!Validation.CheckName(txt_edit_name.Text.Trim()))
                    {
                        MessageBox.Show(this, "请输入真实姓名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                }

                // 收集数据
                // 基础信息
                FormMain.person.name = txt_edit_name.Text.Trim();
                if (radio_edit_male.Checked) FormMain.person.sex = ((int)sex.男).ToString(); else if (radio_edit_female.Checked) FormMain.person.sex = ((int)sex.女).ToString(); // 性别               
                FormMain.person.nation = txt_edit_nation.Text.Trim() != "" ? comboBox_edit_nation.SelectedItem.GetHashCode().ToString() : null; // 民族
                if(txt_edit_birthday.Text.Trim() != "") FormMain.person.birthday = Convert.ToDateTime(txt_edit_birthday.Text);//生日
                FormMain.person.addr = txt_edit_addr.Text;
                FormMain.person.cerAuth = txt_edit_cerAuth.Text;
                if (deptId != "") FormMain.person.deptId = Convert.ToInt64(deptId);
                FormMain.person.idCardNo = txt_edit_idCardNo.Text.Trim() != "" ? txt_edit_idCardNo.Text:null;
                FormMain.person.remarks = txt_edit_remarks.Text;
                FormMain.person.staffNo = txt_edit_staffNo.Text.Trim() != "" ? txt_edit_staffNo.Text : null;



                // 更新数据库
                int re = iPersonService.Update(FormMain.person);
                if (re == 0)
                {
                    MessageBox.Show("保存成功");

                    // 跳转到详情页
                    panel_detail_labels.Visible = true;
                    panel_edit.Visible = false;
                    setDetailValue(FormMain.person, FormMain.irisData);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }     
       
               
        
        // 返回采集列表
        private void btn_back_Click_1(object sender, EventArgs e)
        {
            FormMain.insertService.setToFirst(FormMain.formMain.panel_Insert);
            FormMain.insertService.SetPanelControl(FormMain.formMain.panel_Insert, "collect");
            IOM_cs.insertForm.collection.CtrlCollectionFrom.ctrlCollectionFrom.dataGridView1.ClearSelection();
        }          
       
        
        //计算年龄
        private string countAge(DateTime birthday)
        {
            string ageS = "";
            if (null != birthday)
            {
                DateTime now = DateTime.Now;
                int age = now.Year - birthday.Year;
                //再考虑月、天的因素
                if (now.Month < birthday.Month || (now.Month == birthday.Month && now.Day < birthday.Day))
                {
                    age--;
                }
                ageS = age.ToString();
            }

            return ageS;
        }


        #endregion

        

     

        // 解决控件加载花屏问题
        //protected override CreateParams CreateParams
        //{
        //    get
        //    {
        //        var cp = base.CreateParams;
        //        //WS_EX_COMPOSITED
        //        cp.ExStyle |= 0x02000000;
        //        return cp;
        //    }
        //}


        /// <summary>
        /// base64编码的文本转为图片
        /// </summary>
        public Bitmap Base64StringToImage(string basestr)
        {
            Bitmap bitmap = null;
            try
            {
                String inputStr = basestr.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "").Replace("data:image/jpg;base64,", "").Replace("data:image/bmp;base64,", "");//将base64头部信息替换
                        
                byte[] arr = Convert.FromBase64String(inputStr);
                MemoryStream ms = new MemoryStream(arr);
                Bitmap bmp = new Bitmap(ms);
                ms.Close();
                ms.Dispose();
                bitmap = bmp;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Base64StringToImage 转换失败\nException:" + ex.Message);
            }

            return bitmap;
        }

        private void CtrlDetail_Load(object sender, EventArgs e)
        {
            panel1.Click += (obj, arg) => { panel1.Select(); };
            InitializePanelScroll(panel1, panel1);
            //btn_back.Focus();
        }


        /// <summary>
        /// 递归初始化Panel内部各容器和控件
        /// </summary>
        /// <param name="container"></param>
        /// <param name="panelRoot"></param>
        private static void InitializePanelScroll(Control container, Control panelRoot)
        {
            foreach (Control control in container.Controls)
            {
                if (control is Panel)
                {
                    control.Click += (obj, arg) => { panelRoot.Select(); };
                    InitializePanelScroll(control, panelRoot);
                }
                //else if (control is Label)
                //{
                //    control.Click += (obj, arg) => { panelRoot.Select(); };
                //}
            }
        }


        // 部门下拉框
        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_edit_dept.Text = treeView_dept.SelectedNode.Text;
            treeView_dept.Visible = false;
        }

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

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

        private void comboBox_edit_nation_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox_edit_nation.SelectedIndex > -1) txt_edit_nation.Text = comboBox_edit_nation.SelectedItem.ToString();
        }

        private void btn_DropBirth_Click(object sender, EventArgs e)
        {
            picker_edit_birthday.Focus();
            SendKeys.Send("{F4}"); //模拟按下F4,弹出日期面板
        }

        private void picker_edit_birthday_ValueChanged(object sender, EventArgs e)
        {
            txt_edit_birthday.Text = picker_edit_birthday.Value.ToString("yyyy-MM-dd");
        }

        



       
        

    
        

       
       
        
     
    }
}