using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Aram_CRM_Project.BLL.Entity;

namespace Aram_CRM_Project.BLL
{
    class CrTypeCRUD : Aram_CRM_Project.DAL.DA
    {

        public int InsertCrType(CrType CrTypeVar)
        {
            try
            {
                base.SQL = "SP_InsertCrType";
                base.InitializeCommand();
                base.AddParameter("@Name", CrTypeVar.Name);
                base.AddParameter("@Comment", CrTypeVar.Comment);
                return base.ExecuteStoredProcedure();
            }
            catch (Exception exp)
            {
                if (exp.Message.Contains("UNIQUE KEY") && exp.Message.Contains("duplicate"))
                    return -1;
                else
                    return 0;
            }
        }

        public int UpdateCrType(CrType CrTypeVar)
        {
            try
            {
                base.SQL = "SP_UpdateCrType";
                base.InitializeCommand();
                base.AddParameter("@Id", CrTypeVar.Id);
                base.AddParameter("@Name", CrTypeVar.Name);
                base.AddParameter("@Comment", CrTypeVar.Comment);
                return base.ExecuteStoredProcedure();
            }
            catch (Exception exp)
            {
                if (exp.Message.Contains("UNIQUE KEY") && exp.Message.Contains("duplicate"))
                    return -1;
                else
                    return 0;
            }
        }

        public int DeleteCrType(string Id)
        {
            try
            {
                base.SQL = "SP_DeleteCrType";
                base.InitializeCommand();
                base.AddParameter("@Id", Id);
                return base.ExecuteStoredProcedure();
            }
            catch (Exception exp)
            {
                if (exp.Message.Contains("COLUMN REFERENCE"))
                    return -1;
                else
                    return 0;
            }
        }

        public CrType ReadCrType(string Id)
        {
            try
            {
                CrType CrTypeVar = new CrType();
                DataTable dtTemp = new DataTable();
                base.SQL = "SP_ReadCrType";
                base.InitializeCommand();
                base.AddParameter("@Id", Id);
                base.FillDataTable(dtTemp);
                CrTypeVar.Id = dtTemp.Rows[0]["Id"].ToString();
                CrTypeVar.Name = dtTemp.Rows[0]["Name"].ToString();
                CrTypeVar.Comment = dtTemp.Rows[0]["Comment"].ToString();
                dtTemp.Dispose();
                return CrTypeVar;
            }
            catch (Exception exp)
            {
                return null;
            }
        }

        public DataTable SearchCrType(string Name)
        {
            try
            {
                DataTable dtTemp = new DataTable();
                base.SQL = "SP_SearchCrType";
                base.InitializeCommand();
                base.AddParameter("@Name", Name);
                base.FillDataTable(dtTemp);
                return dtTemp;
            }
            catch (Exception exp)
            {
                return null;
            }
        }

    }
}
