using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RNGClassLibrary;
using System.Data.Entity;
using System.Data.SqlClient;

namespace RNGWcfService1.ContextClass
{
    public class RejectionEntryDbContext:DbContext
    {
        public RejectionEntryDbContext() : base()
        {
            Database.SetInitializer<RejectionEntryDbContext>(null);
            base.Database.Connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["RNGConnectionStrings"].ConnectionString;
        }

        public System.Data.Entity.DbSet<RejectionEntry> User { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<RejectionEntry>().MapToStoredProcedures();
        }


        public void GetErrorDescription(int ErrorNo, ref string ErrorDesc)
        {
            var pErrCodeParam = new SqlParameter("pErrorNo", ErrorNo);

            var pErrDescParam = new SqlParameter("pErrorDesc", System.Data.SqlDbType.VarChar, 200);
            pErrDescParam.Direction = System.Data.ParameterDirection.Output;
            pErrDescParam.Value = "";

            this.Database.ExecuteSqlCommand("spErrors_GetErrorDesc @pErrorNo, @pErrorDesc out", pErrCodeParam, pErrDescParam);

            ErrorDesc = Convert.ToString(pErrDescParam.Value);
        }



        public virtual int RejectionEntry_AddNew(DateTime Date, string UnitName, string ProcessName,string Remarks,string Status,string ItemName, string IsActive, string RejectQty,string Reason, string UserID,string FinalYear,int MainId, ref string errorDesc)
        {
            errorDesc = "";
            var ReturnValueParam = new SqlParameter("@ReturnValue", System.Data.SqlDbType.Int);
            var dbContext = this.Database;

            ReturnValueParam.Direction = System.Data.ParameterDirection.Output;
          
            using (var dbContextTransaction = dbContext.BeginTransaction())
            {
                int errorNo = 0;
                try
                {

                    if (errorNo >= 0) //**** Success
                    {
                       
                                var ReturnValueParam1 = new SqlParameter("@ReturnValue", System.Data.SqlDbType.Int);
                                ReturnValueParam1.Direction = System.Data.ParameterDirection.Output;



                                var paramDate = new SqlParameter("pDate", Date);
                                var paramUnitName = new SqlParameter("pUnitName", UnitName);
                                var paramProcessName = new SqlParameter("pProcessName", ProcessName);
                                var paramItemName = new SqlParameter("pItemName", ItemName);
                                var paramRejectQty = new SqlParameter("pRejectQty", RejectQty);
                                var paramReason = new SqlParameter("pReason", Reason);
                                var paramRemarks = new SqlParameter("pRemarks", Remarks);
                                var paramIsActive = new SqlParameter("pIsActive", IsActive);
                                var paramUserID = new SqlParameter("pUserID", UserID);
                                var paramFinalYear = new SqlParameter("pFinalYear", FinalYear);
                                var paramStatus = new SqlParameter("pStatus", Status);
                                var paramMainId = new SqlParameter("pMainId", MainId);

                                dbContext.ExecuteSqlCommand("Exec @ReturnValue = Sp_RejectionEntry_AddNew @pDate,@pUnitName,@pProcessName,@pItemName,@pRejectQty,@pReason,@pRemarks,@pIsActive,@pUserID,@pFinalYear,@pStatus,@pMainId",
                                                            ReturnValueParam1, paramDate, paramUnitName, paramProcessName, paramItemName, paramRejectQty, paramReason, paramRemarks, paramIsActive, paramUserID, paramFinalYear, paramStatus, paramMainId);

                                errorNo = Convert.ToInt32(ReturnValueParam1.Value);

                              


                          
                        dbContextTransaction.Commit();
                        this.SaveChanges();
                    }
                    else //**** Error ****
                    {
                        dbContextTransaction.Rollback();
                        GetErrorDescription(errorNo, ref errorDesc);
                    }

                }
                

                catch (Exception ex)
                {
                    dbContextTransaction.Rollback();

                    errorNo = -1;
                    errorDesc = Convert.ToString(ex.Message);
                }
                return errorNo;
            }

           
        }

        public virtual int RejectionEntry_Edit(int Rejection_Id, DateTime Date, string UnitName, string ProcessName, string Remarks,string ItemName, string IsActive, string RejectQty, string Reason, string UserID, string FinalYear, string Status, int MainId, ref string errorDesc)
        {
            errorDesc = "";
            var ReturnValueParam = new SqlParameter("@ReturnValue", System.Data.SqlDbType.Int);
            var dbContext = this.Database;

            ReturnValueParam.Direction = System.Data.ParameterDirection.Output;

            using (var dbContextTransaction = dbContext.BeginTransaction())
            {
                int errorNo = 0;
                try
                {

                    if (errorNo >= 0) //**** Success
                    {
                      
                                var ReturnValueParam1 = new SqlParameter("@ReturnValue", System.Data.SqlDbType.Int);
                                ReturnValueParam1.Direction = System.Data.ParameterDirection.Output;



                                var paramRejection_Id = new SqlParameter("pRejection_Id", Rejection_Id);
                                var paramDate = new SqlParameter("pDate", Date);
                                var paramUnitName = new SqlParameter("pUnitName", UnitName);
                                var paramProcessName = new SqlParameter("pProcessName", ProcessName);
                                var paramItemName = new SqlParameter("pItemName", ItemName);
                                var paramRejectQty = new SqlParameter("pRejectQty", RejectQty);
                                var paramReason = new SqlParameter("pReason", Reason);
                                var paramRemarks = new SqlParameter("pRemarks", Remarks);
                                var paramIsActive = new SqlParameter("pIsActive", IsActive);
                                var paramUserID = new SqlParameter("pUserID", UserID);
                                var paramFinalYear = new SqlParameter("pFinalYear", FinalYear);
                                var paramStatus = new SqlParameter("pStatus", Status);
                                var paramMainId = new SqlParameter("pMainId", MainId);

                                dbContext.ExecuteSqlCommand("Exec @ReturnValue = Sp_RejectionEntry_Edit @pRejection_Id,@pDate,@pUnitName,@pProcessName,@pItemName,@pRejectQty,@pReason,@pRemarks,@pIsActive,@pUserID,@pFinalYear,@pStatus,@pMainId",
                                                 ReturnValueParam, paramRejection_Id, paramDate, paramUnitName, paramProcessName, paramItemName, paramRejectQty, paramReason, paramRemarks, paramIsActive, paramUserID, paramFinalYear, paramStatus, paramMainId);



                                errorNo = Convert.ToInt32(ReturnValueParam1.Value);

                            
                        dbContextTransaction.Commit();
                        this.SaveChanges();
                    }
                    else //**** Error ****
                    {
                        dbContextTransaction.Rollback();
                        GetErrorDescription(errorNo, ref errorDesc);
                    }

                }


                catch (Exception ex)
                {
                    dbContextTransaction.Rollback();

                    errorNo = -1;
                    errorDesc = Convert.ToString(ex.Message);
                }
                return errorNo;
            }
            }



        public virtual List<RejectionEntry> RejectionEntry_GetAll(int Rejection_Id)
        {
            var paramRejection_Id = new SqlParameter("pRejection_Id", Rejection_Id);
          


            return this.Database.SqlQuery<RejectionEntry>(
                            "Sp_RejectionEntry_GetAll @pRejection_Id",
                            paramRejection_Id).ToList();
        }


        public virtual List<RejectionEntry> RejectionEntryget_GetAll(int Rejection_Id)
        {
            var paramRejection_Id = new SqlParameter("pRejection_Id", Rejection_Id);



            return this.Database.SqlQuery<RejectionEntry>(
                            "Sp_RejectionEntryget_GetAll @pRejection_Id",
                            paramRejection_Id).ToList();
        }

        public virtual List<RejactionentryOne> RejectionEntryMainid_GetAll(int Mainid)
        {
            var paramMainId = new SqlParameter("@pMainId", Mainid);



            return this.Database.SqlQuery<RejactionentryOne>(
                            "Sp_RejectionEntryMainid_GetAll @pMainId",
                            paramMainId).ToList();
        }


        public virtual List<RejectionEntry> Sp_RejectionEntrysingle_GetAll(int Rejection_Id)
        {
            var paramRejection_Id = new SqlParameter("@pRejection_Id", Rejection_Id);



            return this.Database.SqlQuery<RejectionEntry>(
                            "Sp_RejectionEntrysingle_GetAll @pRejection_Id",
                            paramRejection_Id).ToList();
        }
    }
}