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

namespace irisDataBase.Service.mysqlImpl
{
    public class IrisRecordsServiceImpl: IrisRecordsService
    {
        /// <summary>
        /// 往本地数据库写识别记录,id自增
        /// </summary>
        /// <returns>0 on success</returns>
        public int insertRecordsLocal(long personId, DateTime now, string temp,
            string devCode,string doorCode, string inoutType)
        {
            try
            {
                if (DatabaseLocal.mySqlConnect.State == ConnectionState.Closed)
                {
                    MessageBox.Show("与本地数据库断开，软件即将关闭，请检查后再启动！");
                    Application.Exit();
                    return -1;
                }

                string sQry = "INSERT INTO recognition_records(PERSON_ID,DATETIME,DEV_CODE, DOOR_CODE, INOUT_TYPE,TEMPERATURE )" +
                    "values(@PERSON_ID,@DATETIME,@DEV_CODE,@DOOR_CODE,@INOUT_TYPE,@TEMPERATURE)";

                MySqlCommand cmd = new MySqlCommand(sQry, DatabaseLocal.mySqlConnect);
                cmd.Parameters.Add("@PERSON_ID", MySqlDbType.Int64, 20).Value = (Int64)personId;
                cmd.Parameters.Add("@DATETIME", MySqlDbType.DateTime, 0).Value = now;
                cmd.Parameters.Add("@DEV_CODE", MySqlDbType.String, 255).Value = devCode;
                cmd.Parameters.Add("@DOOR_CODE", MySqlDbType.String, 255).Value = doorCode;
                cmd.Parameters.Add("@INOUT_TYPE", MySqlDbType.String, 1).Value = inoutType;
                cmd.Parameters.Add("@TEMPERATURE", MySqlDbType.String, 20).Value = temp;

                cmd.ExecuteNonQuery();

                cmd.Dispose();
            }
            catch (MySqlException e)
            {
                LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, " insertRecordsLocal : " + e.Message);
                return -1;
            }
            return 0;
        }


    }
}
