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

namespace CallCenterApi.DAL
{
    public class T_Cus_CustomerField
    {
        public Model.T_Cus_CustomerField GetModel(int id)
        {
            string sql = "select top 1 F_Id,F_FieldType,F_FieldId,F_Name,F_DBTypeName,F_DBFieldName,F_Sort,F_CharLength,F_IsNecessary,F_IsExportOrInput,F_IsShowOnList,F_IsShowOnScrean,F_Search from T_Cus_CustomerField where F_Id=@F_Id ";
            var dt = DbHelperSQL.Query(sql, new SqlParameter("@F_Id", id)).Tables[0];
            Model.T_Cus_CustomerField model = null;
            if (dt.Rows.Count > 0)
            {
                var row = dt.Rows[0];
                model = new Model.T_Cus_CustomerField
                {
                    F_Id = Convert.ToInt32(row["F_Id"]),
                    F_FieldType = Convert.ToInt32(row["F_FieldType"]),
                    F_FieldId = Convert.ToInt32(row["F_FieldId"]),
                    F_Name = row["F_Name"].ToString(),
                    F_DBTypeName = row["F_DBTypeName"].ToString(),
                    F_DBFieldName = row["F_DBFieldName"].ToString(),
                    F_Sort = row["F_Sort"] == DBNull.Value ? 0 : Convert.ToInt32(row["F_Sort"]),
                    F_CharLength = row["F_CharLength"] == DBNull.Value ? 0 : Convert.ToInt32(row["F_CharLength"]),
                    F_IsNecessary = row["F_IsNecessary"] == DBNull.Value ? 0 : Convert.ToInt32(row["F_IsNecessary"]),
                    F_IsExportOrInput = row["F_IsExportOrInput"] == DBNull.Value ? 0 : Convert.ToInt32(row["F_IsExportOrInput"]),
                    F_IsShowOnList = row["F_IsShowOnList"] == DBNull.Value ? 0 : Convert.ToInt32(row["F_IsShowOnList"]),
                    F_IsShowOnScrean = row["F_IsShowOnScrean"] == DBNull.Value ? 0 : Convert.ToInt32(row["F_IsShowOnScrean"]),
                    F_Search = row["F_Search"] == DBNull.Value ? 0 : Convert.ToInt32(row["F_Search"]),
                };
            }
            return model;
        }

        public bool Update(Model.T_Cus_CustomerField model)
        {
            string sql = "update T_Cus_CustomerField set  F_Id=@F_Id,F_FieldType=@F_FieldType,F_FieldId=@F_FieldId,F_Name=@F_Name,F_DBTypeName=@F_DBTypeName,F_DBFieldName=@F_DBFieldName,F_Sort=@F_Sort,F_CharLength=@F_CharLength,F_IsNecessary=@F_IsNecessary,F_IsExportOrInput=@F_IsExportOrInput,F_IsShowOnList=@F_IsShowOnList,F_IsShowOnScrean=@F_IsShowOnScrean,F_Search=@F_Search where F_Id=@F_Id";
            return DbHelperSQL.ExecuteSql(sql, new SqlParameter[] {
                new SqlParameter("@F_Id",model.F_Id),
                new SqlParameter("@F_FieldType",model.F_FieldType),
                new SqlParameter("@F_FieldId",model.F_FieldId),
                new SqlParameter("@F_Name",model.F_Name),
                new SqlParameter("@F_DBTypeName",model.F_DBTypeName),
                new SqlParameter("@F_DBFieldName",model.F_DBFieldName),
                new SqlParameter("@F_Sort",model.F_Sort),
                new SqlParameter("@F_CharLength",model.F_CharLength),
                new SqlParameter("@F_IsNecessary",model.F_IsNecessary),
                new SqlParameter("@F_IsExportOrInput",model.F_IsExportOrInput),
                new SqlParameter("@F_IsShowOnList",model.F_IsShowOnList),
                new SqlParameter("@F_IsShowOnScrean",model.F_IsShowOnScrean),
                new SqlParameter("@F_Search",model.F_Search)
            }) > 0;
        }

        public DataTable GetList()
        {
            string sql = "select F_Id,F_FieldType,F_FieldId,F_Name,F_DBTypeName,F_DBFieldName,F_Sort,F_CharLength,F_IsNecessary,F_IsExportOrInput,F_IsShowOnList,F_IsShowOnScrean,F_Search from T_Cus_CustomerField";
            return DbHelperSQL.Query(sql).Tables[0];
        }

        public bool Delete(int id)
        {
            string sql = "delete T_Cus_CustomerField where F_Id=@F_Id";
            return DbHelperSQL.ExecuteSql(sql, new SqlParameter("@F_Id", id)) > 0;
        }

        public int Add(Model.T_Cus_CustomerField model)
        {
            string sql = "insert into T_Cus_CustomerField (F_FieldType,F_FieldId,F_Name,F_DBTypeName,F_DBFieldName) value (@F_FieldType,@F_FieldId,@F_Name,@F_DBTypeName,@F_DBFieldName)";
            return DbHelperSQL.ExecuteSql(sql, new SqlParameter[] {
                new SqlParameter("@F_FieldType",model.F_FieldType),
                new SqlParameter("@F_FieldId",model.F_FieldId),
                new SqlParameter("@F_Name",model.F_Name),
                new SqlParameter("@F_DBTypeName",model.F_DBTypeName),
                new SqlParameter("@F_DBFieldName",model.F_DBFieldName),
           });

        }
    }
}
