using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RNGClassLibrary;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Data.Entity.Infrastructure;
namespace RNGWcfService1.ContextClass
{
    public class UserReightDbContext: DbContext
    {
        public UserReightDbContext() : base()
        {
            Database.SetInitializer<UserReightDbContext>(null);
            base.Database.Connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["RNGConnectionStrings"].ConnectionString;
        }
        public System.Data.Entity.DbSet<UserRight> User { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<UserRight>().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 Userreight_AddNew(int EmployId, string ItemTypeMaster, string MetalMaster, string MetalGradeMaster, string ShiftMaster, string ProductionUnitMaster, string StageProcessMaster, string RejectionReasonMaster, string ItemMaster, string CustomerMaster, string EmployeeMaster, string VendorMaster, string ChangePassword, string HeatTreatment, string DailyFoundryProduction, string MeltingPlanning, string MaterialPurchaseInward, string OrderIRNForProductionPlaning, string OtherMetalIssue, string FuranceLogSheet,string CastingHardness, string RejectionEntry, string DisposalEntry, string DespatchEntry, string IRNforProductionPlaning, string IRNeightCastingHardness, string IRNoneFurnaceLogSheet, string IRNtwoFurnaceLogSheet,string IRNsixPageone, string IRNsixPagetwo, string IRNnineRejectionEntry, string DailyProductionSummary, string materialIssue, string IrnsevenDailyItemSummary, string CastingMonth, string DespatchReport,string MonthStocktotalUnit, ref string errorDesc)
        {
            errorDesc = "";

            var ReturnValueParam = new SqlParameter("@ReturnValue", System.Data.SqlDbType.Int);
            ReturnValueParam.Direction = System.Data.ParameterDirection.Output;


           

              var paramEmployId = new SqlParameter("pEmployId", EmployId);
            var paramItemTypeMaster = new SqlParameter("pItemTypeMaster", ItemTypeMaster);
            var paramMetalMaster = new SqlParameter("pMetalMaster", MetalMaster);
            var paramMetalGradeMaster = new SqlParameter("pMetalGradeMaster", MetalGradeMaster);
            var paramShiftMaster = new SqlParameter("pShiftMaster", ShiftMaster);
            var paramProductionUnitMaster = new SqlParameter("pProductionUnitMaster", ProductionUnitMaster);
            var paramStageProcessMaster = new SqlParameter("pStageProcessMaster", StageProcessMaster);
            var paramRejectionReasonMaster = new SqlParameter("pRejectionReasonMaster", RejectionReasonMaster);
            var paramItemMaster = new SqlParameter("pItemMaster", ItemMaster);
            var paramCustomerMaster = new SqlParameter("pCustomerMaster", CustomerMaster);
            var paramEmployeeMaster = new SqlParameter("pEmployeeMaster", EmployeeMaster);
            var paramVendorMaster = new SqlParameter("pVendorMaster", VendorMaster);
            var paramChangePassword = new SqlParameter("pChangePassword", ChangePassword);
            var paramHeatTreatment = new SqlParameter("pHeatTreatment", HeatTreatment);
            var paramDailyFoundryProduction = new SqlParameter("pDailyFoundryProduction", DailyFoundryProduction);
            var paramMeltingPlanning = new SqlParameter("pMeltingPlanning", MeltingPlanning);
            var paramMaterialPurchaseInward = new SqlParameter("pMaterialPurchaseInward", MaterialPurchaseInward);
            var paramOrderIRNForProductionPlaning = new SqlParameter("pOrderIRNForProductionPlaning", OrderIRNForProductionPlaning);
            var paramOtherMetalIssue = new SqlParameter("pOtherMetalIssue", OtherMetalIssue);
            var paramFuranceLogSheet = new SqlParameter("pFuranceLogSheet", FuranceLogSheet);
            var paramCastingHardness = new SqlParameter("pCastingHardness", CastingHardness);
            var paramRejectionEntry = new SqlParameter("pRejectionEntry", RejectionEntry);
            var paramDisposalEntry = new SqlParameter("pDisposalEntry", DisposalEntry);
            var paramDespatchEntry = new SqlParameter("pDespatchEntry", DespatchEntry);
            var paramIRNforProductionPlaning = new SqlParameter("pIRNforProductionPlaning", IRNforProductionPlaning);
            var paramIRNeightCastingHardness = new SqlParameter("pIRNeightCastingHardness", IRNeightCastingHardness);
            var paramIRNoneFurnaceLogSheet = new SqlParameter("pIRNoneFurnaceLogSheet", IRNoneFurnaceLogSheet);
            var paramIRNtwoFurnaceLogSheet = new SqlParameter("pIRNtwoFurnaceLogSheet", IRNtwoFurnaceLogSheet);
            var paramIRNsixPageone = new SqlParameter("pIRNsixPageone", IRNsixPageone);
            var paramIRNsixPagetwo = new SqlParameter("pIRNsixPagetwo", IRNsixPagetwo);
            var paramIRNnineRejectionEntry = new SqlParameter("pIRNnineRejectionEntry", IRNnineRejectionEntry);
            var paramDailyProductionSummary = new SqlParameter("pDailyProductionSummary", DailyProductionSummary);
            var parammaterialIssue = new SqlParameter("pmaterialIssue", materialIssue);
            var paramIrnsevenDailyItemSummary = new SqlParameter("pIrnsevenDailyItemSummary", IrnsevenDailyItemSummary);
            var paramCastingMonth = new SqlParameter("pCastingMonth", CastingMonth);
            var paramDespatchReport = new SqlParameter("pDespatchReport", DespatchReport);
            var paramMonthStocktotalUnit = new SqlParameter("pMonthStocktotalUnit", MonthStocktotalUnit);



            


            var dbContext = this.Database;

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

                try
                {
                    dbContext.ExecuteSqlCommand("Exec @ReturnValue = Sp_userright_AddNew  @pEmployId,@pItemTypeMaster,@pMetalMaster,@pMetalGradeMaster,@pShiftMaster,@pProductionUnitMaster,@pStageProcessMaster,@pRejectionReasonMaster,@pItemMaster, @pCustomerMaster,@pEmployeeMaster,@pVendorMaster,@pChangePassword,@pHeatTreatment,@pDailyFoundryProduction,@pMeltingPlanning,@pMaterialPurchaseInward,@pOrderIRNForProductionPlaning, @pOtherMetalIssue,@pFuranceLogSheet,@pCastingHardness,@pRejectionEntry,@pDisposalEntry,@pDespatchEntry,@pIRNforProductionPlaning, @pIRNeightCastingHardness,@pIRNoneFurnaceLogSheet,@pIRNtwoFurnaceLogSheet,@pIRNsixPageone,@pIRNsixPagetwo,@pIRNnineRejectionEntry,@pDailyProductionSummary,@pmaterialIssue,@pIrnsevenDailyItemSummary,@pCastingMonth,@pDespatchReport,@pMonthStocktotalUnit",
                                                ReturnValueParam, paramEmployId, paramItemTypeMaster, paramMetalMaster, paramMetalGradeMaster, paramShiftMaster, paramProductionUnitMaster, paramStageProcessMaster, paramRejectionReasonMaster, paramItemMaster, paramCustomerMaster, paramEmployeeMaster, paramVendorMaster, paramChangePassword, paramHeatTreatment, paramDailyFoundryProduction, paramMeltingPlanning, paramMaterialPurchaseInward, paramOrderIRNForProductionPlaning, paramOtherMetalIssue, paramFuranceLogSheet, paramCastingHardness, paramRejectionEntry, paramDisposalEntry, paramDespatchEntry, paramIRNforProductionPlaning, paramIRNeightCastingHardness, paramIRNoneFurnaceLogSheet, paramIRNtwoFurnaceLogSheet, paramIRNsixPageone, paramIRNsixPagetwo, paramIRNnineRejectionEntry, paramDailyProductionSummary, parammaterialIssue, paramIrnsevenDailyItemSummary, paramCastingMonth, paramDespatchReport, paramMonthStocktotalUnit);

                    errorNo = Convert.ToInt32(ReturnValueParam.Value);

                    if (errorNo >= 0) //**** Success
                    {
                        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;
            }
        }


    }
}