using irisDataBase.Model;
using irisHelper;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace irisDataBase.Service.mysqlImpl
{
    class IrisPersonViewServiceImpl: IrisPersonViewService
    {
        
        /// <summary>
        /// 根据id，从本地数据库获取PersonView 
        /// </summary>       
        /// <returns> staff </returns>
        public PersonView getPersonViewByPersonIdLocal(long id)
        {
            int langue = Convert.ToInt32(ConfigHelper.GetAppConfig("language"));
            PersonView personView = new PersonView();

            if (DatabaseLocal.mySqlConnect.State == ConnectionState.Closed)
            {
                if (langue == 1)
                    MessageBox.Show("与本地数据库连接断开，软件即将关闭，请检查后再启动！", "提示");
                else
                    MessageBox.Show("Disconnect from local database", "message");
                Application.Exit();
                return null;
            }

            try
            {
                //MySqlDataReader aReader = null;
                string sQry = "SELECT ID , NAME, SEX, PERSON_ID,DEPTID,SIMPLENAME,ID_CARD_NO,PHOTO,PHOTO_DATA FROM person_view where PERSON_ID=@PERSON_ID";
                MySqlCommand aCommand = new MySqlCommand(sQry, DatabaseLocal.mySqlConnect);
                aCommand.Parameters.Add
                    (
                    "@PERSON_ID", MySqlDbType.Int64,
                    20
                    ).Value = (Int64)id;


                using (MySqlDataReader aReader = aCommand.ExecuteReader())
                {
                    if (aReader.Read())
                    {
                        //姓名
                        if (!aReader.IsDBNull(1)) personView.setName(aReader.GetString(1));
                        //性别
                        if (!aReader.IsDBNull(2))
                        {
                            string sexDict = aReader.GetString(2);
                            if (sexDict == "1")
                            {
                                if (langue == 1) personView.setSex("男");
                                else personView.setSex("Male");
                            }
                            else if (sexDict == "2")
                            {
                                if (langue == 1) personView.setSex("女");
                                else personView.setSex("Female");
                            }
                        }
                        //部门
                        if (!aReader.IsDBNull(5)) personView.setDeptName(aReader.GetString(5));

                        if (!aReader.IsDBNull(6)) personView.setIdCardNo(aReader.GetString(6));
                        //照片路径
                        if (!aReader.IsDBNull(7)) personView.setPhoto(aReader.GetString(7));

                        //照片数据
                        if (!aReader.IsDBNull(8))
                        {
                            string base64 = aReader.GetString(8);
                            base64 = base64.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);
                            ms.Dispose();
                            personView.setPhotoData(mImage);

                        }
                        else
                        {
                            personView.setPhotoData(null);
                        }

                    }

                    //aReader.Close();
                    //aReader.Dispose();
                    aCommand.Dispose();
                    LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, "getPersonViewByPersonIdLocal success ");

                }
            }
            catch (Exception e)
            {
                LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, "  getPersonViewByPersonIdLocal : " + e.Message);

            }
            return personView;
        }

    }
}
