using CallCenterApi.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 CallCenterApi.DAL
{
    public class telloc_users
    {
        public Model.telloc_users GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from telloc_users ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)
            };
            parameters[0].Value = id;

            Model.telloc_users model = new Model.telloc_users();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        public Model.telloc_users GetModel(int userid, int telid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from telloc_users ");
            strSql.Append(" where tellocid=@tellocid and userid=@userid and isdelete=0");
            SqlParameter[] parameters = {
                    new SqlParameter("@tellocid", SqlDbType.Int,4),
                    new SqlParameter("@userid", SqlDbType.Int,4)
            };
            parameters[0].Value = telid;
            parameters[1].Value = userid;

            Model.telloc_users model = new Model.telloc_users();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM telloc_users ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        public int Add(Model.telloc_users model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into telloc_users(");
            strSql.Append("tellocid,userid,isdelete)");
            strSql.Append(" values (");
            strSql.Append("@tellocid,@userid,@isdelete)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@tellocid", SqlDbType.Int,4),
                    new SqlParameter("@userid", SqlDbType.Int,4),
                    new SqlParameter("@isdelete", SqlDbType.Int,4)};
            parameters[0].Value = model.tellocid;
            parameters[1].Value = model.userid;
            parameters[2].Value = model.isdelete;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public bool Update(Model.telloc_users model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update telloc_users set ");
            strSql.Append("tellocid=@tellocid,");
            strSql.Append("userid=@userid,");
            strSql.Append("isdelete=@isdelete");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@tellocid", SqlDbType.Int,4),
                    new SqlParameter("@userid", SqlDbType.Int,4),
                    new SqlParameter("@isdelete", SqlDbType.Int,4),
                    new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.tellocid;
            parameters[1].Value = model.userid;
            parameters[2].Value = model.isdelete;
            parameters[3].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.telloc_users DataRowToModel(DataRow row)
        {
            Model.telloc_users model = new Model.telloc_users();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["tellocid"] != null && row["tellocid"].ToString() != "")
                {
                    model.tellocid = int.Parse(row["tellocid"].ToString());
                }
                if (row["userid"] != null && row["userid"].ToString() != "")
                {
                    model.userid = int.Parse(row["userid"].ToString());
                }
                if (row["isdelete"] != null && row["isdelete"].ToString() != "")
                {
                    model.isdelete = int.Parse(row["isdelete"].ToString());
                }
            }
            return model;
        }
    }
}
