using RMYY_CallCenter_Api.DB;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RMYY_CallCenter_Api.Dal
{
   public  class T_Sys_Login_Logs
    {
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(RMYY_CallCenter_Api.Model.T_Sys_Login_Logs model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Sys_Login_Logs(");
            strSql.Append("T_username,T_usercode,T_result,T_login_ip,T_channel,T_time)");
            strSql.Append(" values (");
            strSql.Append("@T_username,@T_usercode,@T_result,@T_login_ip,@T_channel,@T_time)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@T_username", SqlDbType.NVarChar,100),
                    new SqlParameter("@T_usercode", SqlDbType.NVarChar,100),
                    new SqlParameter("@T_result",SqlDbType.NVarChar,100),
                    new SqlParameter("@T_login_ip", SqlDbType.NVarChar,100),
                    new SqlParameter("@T_channel", SqlDbType.Int ,4),
                    new SqlParameter("@T_time", SqlDbType.DateTime)
                   };
            parameters[0].Value = model.T_username;
            parameters[1].Value = model.T_usercode;
            parameters[2].Value = model.T_result;
            parameters[3].Value = model.T_login_ip;
            parameters[4].Value = model.T_channel;
            parameters[5].Value = model.T_time;
            

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(RMYY_CallCenter_Api.Model.T_Sys_Login_Logs model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Sys_Login_Logs set ");
            strSql.Append("T_username=@T_username,");
            strSql.Append("T_usercode=@T_usercode,");
            strSql.Append("T_result=@T_result,");
            strSql.Append("T_login_ip=@T_login_ip,");
            strSql.Append("T_channel=@T_channel,");
            strSql.Append("T_time=@T_time,");
            strSql.Append("T_LoginOutDate=@T_LoginOutDate");
            strSql.Append(" where T_id=@T_id");
            SqlParameter[] parameters = {
                    new SqlParameter("@T_username", SqlDbType.NVarChar,100),
                    new SqlParameter("@T_usercode", SqlDbType.NVarChar,100),
                    new SqlParameter("@T_result", SqlDbType.NVarChar,100),
                    new SqlParameter("@T_login_ip", SqlDbType.NVarChar,100),
                    new SqlParameter("@T_channel", SqlDbType.Int,4),
                    new SqlParameter("@T_time", SqlDbType.DateTime),
                    new SqlParameter("@T_LoginOutDate", SqlDbType.DateTime),
                    new SqlParameter("@T_id", SqlDbType.Int,4)};
            parameters[0].Value = model.T_username;
            parameters[1].Value = model.T_usercode;
            parameters[2].Value = model.T_result;
            parameters[3].Value = model.T_login_ip;
            parameters[4].Value = model.T_channel;
            parameters[5].Value = model.T_time;
            parameters[6].Value = model.T_LoginOutDate;
            parameters[7].Value = model.T_id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 根据工单编号得到一个最新对象实体
        /// </summary>
        public RMYY_CallCenter_Api.Model.T_Sys_Login_Logs GetModel(string T_usercode)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from T_Sys_Login_Logs WITH(NOLOCK) ");
            strSql.Append(" where T_usercode=@T_usercode ");
            strSql.Append(" order by T_time desc");
            SqlParameter[] parameters = {
                    new SqlParameter("@T_usercode", SqlDbType.NVarChar,50)
            };
            parameters[0].Value = T_usercode;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]); ;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RMYY_CallCenter_Api.Model.T_Sys_Login_Logs DataRowToModel(DataRow row)
        {
            RMYY_CallCenter_Api.Model.T_Sys_Login_Logs model = new RMYY_CallCenter_Api.Model.T_Sys_Login_Logs();
            if (row != null)
            {
                if (row["T_id"] != null && row["T_id"].ToString() != "")
                {
                    model.T_id = row["T_id"].ToString();
                }
                if (row["T_username"] != null)
                {
                    model.T_username = row["T_username"].ToString();
                }
                if (row["T_usercode"] != null)
                {
                    model.T_usercode = row["T_usercode"].ToString();
                }
                if (row["T_result"] != null)
                {
                    model.T_result = row["T_result"].ToString();
                }
                if (row["T_login_ip"] != null)
                {
                    model.T_login_ip = row["T_login_ip"].ToString();
                }
                if (row["T_channel"] != null && row["T_channel"].ToString() != "")
                {
                    model.T_channel = int.Parse(row["T_channel"].ToString());
                }
                if (row["T_time"] != null && row["T_time"].ToString() != "")
                {
                    model.T_time = DateTime.Parse(row["T_time"].ToString());
                }
                if (row["T_LoginOutDate"] != null && row["T_LoginOutDate"].ToString() != "")
                {
                    model.T_LoginOutDate = DateTime.Parse(row["T_LoginOutDate"].ToString());
                }
            }
            return model;
        }

    }
}
