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 CustomerGroupCRUD : Aram_CRM_Project.DAL.DA
    {

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

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

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

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

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

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