using System;
using System.Data;
using Ease.Core.DataAccess;
using HRM.BO;

namespace HRM.DA
{
    internal class EducationLevelDA
    {
        #region constructor

        public EducationLevelDA()
        {
        }

        #endregion

        #region Insert function

        internal static void Insert(TransactionContext tc, EducationLevel item)
        {
            tc.ExecuteNonQuery(
                "INSERT INTO EDUCATIONLEVEL(EDUCATIONLEVELID, CODE, DESCRIPTION, EDUCATIONTYPEID, SEQUENCENO, STATUS, CREATEDBY, CREATIONDATE)" +
                " VALUES(%n, %s, %s, %n, %n, %n, %n, %d)", item.ID, item.Code, item.Description,
                DataReader.GetNullValue(item.EducationTypeID, 0), item.Sequence, item.Status, item.CreatedBy,
                item.CreatedDate);
        }

        #endregion

        #region Update function

        internal static void Update(TransactionContext tc, EducationLevel item)
        {
            tc.ExecuteNonQuery(
                "UPDATE EDUCATIONLEVEL SET code=%s, description=%s, educationtypeid=%n, sequencenO=%n, status=%n, modifiedby=%n, modifieddate=%d" +
                " WHERE EDUCATIONLEVELID=%n", item.Code, item.Description, item.EducationTypeID, item.Sequence,
                item.Status, item.ModifiedBy, item.ModifiedDate, item.ID);
        }

        #endregion

        #region Get function

        internal static IDataReader Get(TransactionContext tc)
        {
            return tc.ExecuteReader("SELECT * FROM EDUCATIONLEVEL");
        }

        internal static IDataReader Get(TransactionContext tc, EnumStatus status)
        {
            if (status == EnumStatus.Regardless)
            {
                return tc.ExecuteReader("SELECT * FROM EDUCATIONLEVEL Order By SequenceNo");
            }
            else
            {
                return tc.ExecuteReader("SELECT * FROM EDUCATIONLEVEL Where Status=%n Order By SequenceNo", status);
            }
        }

        internal static IDataReader Get(TransactionContext tc, int nID)
        {
            return tc.ExecuteReader("SELECT * FROM EDUCATIONLEVEL WHERE EDUCATIONLEVELID=%n", nID);
        }

        internal static IDataReader Get(TransactionContext tc, string sCode)
        {
            return tc.ExecuteReader("SELECT * FROM EDUCATIONLEVEL WHERE Code=%s", sCode);
        }

        internal static IDataReader GetByType(TransactionContext tc, int typeID)
        {
            return tc.ExecuteReader("SELECT * FROM EducationLevel where EducationTypeID=%n ORDER BY SEQUENCENO",
                typeID);
        }

        internal static IDataReader GetByLevelID(TransactionContext tc, int nLevelID)
        {
            return tc.ExecuteReader("SELECT * FROM EducationLevel where EducationLevelID=%n", nLevelID);
        }

        internal static IDataReader GetByLevelIDs(TransactionContext tc, string nLevelIDs)
        {
            return tc.ExecuteReader("SELECT * FROM EducationLevel where EducationLevelID in (%q)", nLevelIDs);
        }

        #endregion

        #region Delete function

        internal static void Delete(TransactionContext tc, int nID)
        {
            tc.ExecuteNonQuery("DELETE FROM EDUCATIONLEVEL WHERE EDUCATIONLEVELID=%n", nID);
        }

        #endregion

        internal static DataSet GetManpower(TransactionContext tc, string sparam)
        {
            DataSet rootDataset = new DataSet();
            DataSet tempdataset = new DataSet();
            try
            {
                string query = SQLParser.MakeSQL(
                    @"SELECT d.DESCRIPTION Department, edu.DESCRIPTION qualification,COUNT(e.EMPLOYEEID) HeadCount
                                                FROM EMPLOYEE e,EMPACADEMIC a,EDUCATIONLEVEL edu,department d
                                                WHERE e.EMPLOYEEID=a.EMPLOYEEID
                                                AND a.EDUCATIONLEVELID=edu.EDUCATIONLEVELID
                                                AND e.DEPARTMENTID=d.DEPARTMENTID
                                                %q
                                                GROUP BY d.DESCRIPTION,edu.DESCRIPTION
                                                ORDER BY d.DESCRIPTION,edu.DESCRIPTION ",
                    sparam);

                tempdataset = tc.ExecuteDataSet(query);

                tempdataset.Tables[0].TableName = "Manpower";
                rootDataset.Tables.Add(tempdataset.Tables[0].Copy());
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return rootDataset;
        }

        internal static IDataReader GetByID(TransactionContext tc, int id)
        {
            string sql = SQLParser.MakeSQL(@"SELECT * FROM EducationLevel WHERE EDUCATIONLEVELID =%n", id);
            return tc.ExecuteReader(sql);
        }
    }
}