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

namespace RMYY_CallCenter_Api.Dal
{
   public  class T_Wowo_wocodetypedal
    {
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Addrepairitems(RMYY_CallCenter_Api.Model.T_Wowo_wocodetype model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Wowo_wocodetype(");
            strSql.Append("T_Woname )");
            strSql.Append(" values (");
            strSql.Append("@T_Woname)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@T_Woname", SqlDbType.NVarChar,100)};
            parameters[0].Value = model.T_Woname;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        #region 修改
        /// <summary>
        /// 修改一条院区信息
        /// </summary>
        public bool Updaterepair(RMYY_CallCenter_Api.Model.T_Wowo_wocodetype model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Wowo_wocodetype set ");
            strSql.Append("T_Woname=@T_Woname,");
            SqlParameter[] parameters = {
                    new SqlParameter("@T_Wowocodecenter", SqlDbType.NVarChar,100)};
            parameters[0].Value = model.T_Woname;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion
        #region 删除
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Deleterepair(int T_Woid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Wowo_wocodetype ");
            strSql.Append(" where T_Woid=@T_Woid");
            SqlParameter[] parameters = {
                    new SqlParameter("@T_Woid", SqlDbType.Int,4)
            };
            parameters[0].Value = T_Woid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion
        #region 查询
        /// <summary>
        /// 获取院区信息
        /// </summary>
        public List<RMYY_CallCenter_Api.Model.T_Wowo_wocodetype> GetListall()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM  T_Wowo_wocodetype ");
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 分页获取
        /// </summary>
        /// <param name="strWhere"></param>
        /// <param name="orderby"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <returns></returns>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.T_Wowocodeid desc");
            }
            strSql.Append(")AS Row, T.*  from T_Wowo_wocodetype T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RMYY_CallCenter_Api.Model.T_Wowo_wocodetype GetModel(int T_Woid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 T_Woname from T_Wowo_wocodetype ");
            strSql.Append(" where T_Woid=@T_Woid");
            SqlParameter[] parameters = {
                    new SqlParameter("@T_Woid", SqlDbType.Int,4)
            };
            parameters[0].Value = T_Woid;

            RMYY_CallCenter_Api.Model.T_Wowo_wocodetype model = new RMYY_CallCenter_Api.Model.T_Wowo_wocodetype();
            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 List<RMYY_CallCenter_Api.Model.T_Wowo_wocodetype> DataTableToList(DataTable dt)
        {
            List<RMYY_CallCenter_Api.Model.T_Wowo_wocodetype> modelList = new List<RMYY_CallCenter_Api.Model.T_Wowo_wocodetype>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                RMYY_CallCenter_Api.Model.T_Wowo_wocodetype model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RMYY_CallCenter_Api.Model.T_Wowo_wocodetype DataRowToModel(DataRow row)
        {
            RMYY_CallCenter_Api.Model.T_Wowo_wocodetype model = new RMYY_CallCenter_Api.Model.T_Wowo_wocodetype();
            if (row != null)
            {
                #region 主表信息======================
                //利用反射获得属性的所有公共属性
                Type modelType = model.GetType();
                for (int i = 0; i < row.Table.Columns.Count; i++)
                {
                    PropertyInfo proInfo = modelType.GetProperty(row.Table.Columns[i].ColumnName);
                    if (proInfo != null && row[i] != DBNull.Value)
                    {
                        //用索引值设置属性值
                        proInfo.SetValue(model, row[i], null);
                    }
                }
                #endregion
            }
            return model;
        }
        #endregion

    }
}
