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 DailyMaltingPlanningDbContext:DbContext
    {

        public DailyMaltingPlanningDbContext() : base()
        {
            Database.SetInitializer<DailyMaltingPlanningDbContext>(null);
            base.Database.Connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["RNGConnectionStrings"].ConnectionString;
        }

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

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Meltingplanng>().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  DailyMaltingMaster_AddNew(string DocNo, DateTime Date, int Unitid, int Metaltypeid, DateTime Planningdate,int Meltingopid,int Grindingopid,int Punchingopid,string Remarks,string Furnse,string Allowmetergrade, string Allowmetergradeqty, string Ingot, string Ingotqty, string Riser,string Riserqty,string Turning,string Rejection, string Anyothermetal, string Anyothermetalqty, ref int FId, ref string errorDesc)
        {
            errorDesc = "";

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

          
             var paramDocNo = new SqlParameter("pDocNo", DocNo);

            var paramDate = new SqlParameter("pDate", Date);

            var paramUnitid = new SqlParameter("pUnitid", Unitid);

            var paramMetaltypeid = new SqlParameter("pMetaltypeid", Metaltypeid);

            var paramPlanningdate = new SqlParameter("pPlanningdate", Planningdate);

            var paramMeltingopid = new SqlParameter("pMeltingopid", Meltingopid);

            var paramGrindingopid = new SqlParameter("pGrindingopid", Grindingopid);

            var paramPunchingopid = new SqlParameter("pPunchingopid", Punchingopid);

          
          var paramRemarks = new SqlParameter("pRemarks", Remarks);

            var paramFurnse = new SqlParameter("pFurnse", Furnse);

            var paramAllowmetergrade = new SqlParameter("pAllowmetergrade", Allowmetergrade);

            var paramAllowmetergradeqty = new SqlParameter("pAllowmetergradeqty", Allowmetergradeqty);

            var paramIngot = new SqlParameter("pIngot", Ingot);

            var paramIngotqty = new SqlParameter("pIngotqty", Ingotqty);


           var paramRiser = new SqlParameter("pRiser", Riser);

            var paramRiserqty = new SqlParameter("pRiserqty", Riserqty);

            var paramTurning  = new SqlParameter("pTurning", Turning);

            var paramRejection = new SqlParameter("pRejection", Rejection);

            var paramAnyothermetal = new SqlParameter("pAnyothermetal", Anyothermetal);

            var paramAnyothermetalqty = new SqlParameter("pAnyothermetalqty", Anyothermetalqty);

            var parampFId = new SqlParameter("pFId", System.Data.SqlDbType.Int);
            parampFId.Direction = System.Data.ParameterDirection.Output;


            var dbContext = this.Database;

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

                try
                {
                    dbContext.ExecuteSqlCommand("Exec @ReturnValue = Sp_DailyMaltingPlanning_AddNew  @pDocNo,@pDate,@pUnitid,@pMetaltypeid,@pPlanningdate,@pMeltingopid,@pGrindingopid,@pPunchingopid,@pRemarks,@pFurnse,@pAllowmetergrade,@pAllowmetergradeqty,@pIngot,@pIngotqty,@pRiser,@pRiserqty,@pTurning,@pRejection,@pAnyothermetal,@pAnyothermetalqty,@pFId OUT",
                                                ReturnValueParam, paramDocNo, paramDate, paramUnitid, paramMetaltypeid, paramPlanningdate, paramMeltingopid, paramGrindingopid, paramPunchingopid, paramRemarks, paramFurnse, paramAllowmetergrade, paramAllowmetergradeqty, paramIngot, paramIngotqty, paramRiser, paramRiserqty, paramTurning, paramRejection,paramAnyothermetal,paramAnyothermetalqty, parampFId);

                    errorNo = Convert.ToInt32(ReturnValueParam.Value);


                    FId = Convert.ToInt32(parampFId.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;
            }
        }
        public virtual List<Meltingplanng> DailyMaltingMaster_GetAll(int Id)
        {
            var paramId = new SqlParameter("pId", Id);



            return this.Database.SqlQuery<Meltingplanng>(
                            "Sp_DailyMaltingPlanning_GetAll @pId",
                            paramId).ToList();
        }
        public virtual int DailyMaltingMaster_Edit(int Id,string DocNo, DateTime Date, int Unitid, int Metaltypeid, DateTime Planningdate, int Meltingopid, int Grindingopid, int Punchingopid, string Remarks, string Furnse, string Allowmetergrade, string Allowmetergradeqty, string Ingot, string Ingotqty, string Riser, string Riserqty, string Turning, string Rejection,string Anyothermetal,string Anyothermetalqty, ref string errorDesc)
        {
            errorDesc = "";

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

            var paramId = new SqlParameter("pId", Id);
            var paramDocNo = new SqlParameter("pDocNo", DocNo);

            var paramDate = new SqlParameter("pDate", Date);

            var paramUnitid = new SqlParameter("pUnitid", Unitid);

            var paramMetaltypeid = new SqlParameter("pMetaltypeid", Metaltypeid);

            var paramPlanningdate = new SqlParameter("pPlanningdate", Planningdate);

            var paramMeltingopid = new SqlParameter("pMeltingopid", Meltingopid);

            var paramGrindingopid = new SqlParameter("pGrindingopid", Grindingopid);

            var paramPunchingopid = new SqlParameter("pPunchingopid", Punchingopid);


            var paramRemarks = new SqlParameter("pRemarks", Remarks);

            var paramFurnse = new SqlParameter("pFurnse", Furnse);

            var paramAllowmetergrade = new SqlParameter("pAllowmetergrade", Allowmetergrade);

            var paramAllowmetergradeqty = new SqlParameter("pAllowmetergradeqty", Allowmetergradeqty);

            var paramIngot = new SqlParameter("pIngot", Ingot);

            var paramIngotqty = new SqlParameter("pIngotqty", Ingotqty);


            var paramRiser = new SqlParameter("pRiser", Riser);

            var paramRiserqty = new SqlParameter("pRiserqty", Riserqty);

            var paramTurning = new SqlParameter("pTurning", Turning);

            var paramRejection = new SqlParameter("pRejection", Rejection);


            var paramAnyothermetal = new SqlParameter("pAnyothermetal", Anyothermetal);

            var paramAnyothermetalqty = new SqlParameter("pAnyothermetalqty", Anyothermetalqty);



            var dbContext = this.Database;

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

                try
                {
                    dbContext.ExecuteSqlCommand("Exec @ReturnValue = Sp_DailyMaltingPlanning_Edit  @pId,@pDocNo,@pDate,@pUnitid,@pMetaltypeid,@pPlanningdate,@pMeltingopid,@pGrindingopid,@pPunchingopid,@pRemarks,@pFurnse,@pAllowmetergrade,@pAllowmetergradeqty,@pIngot,@pIngotqty,@pRiser,@pRiserqty,@pTurning,@pRejection,@pAnyothermetal,@pAnyothermetalqty",
                                                ReturnValueParam, paramId, paramDocNo, paramDate, paramUnitid, paramMetaltypeid, paramPlanningdate, paramMeltingopid, paramGrindingopid, paramPunchingopid, paramRemarks, paramFurnse, paramAllowmetergrade, paramAllowmetergradeqty, paramIngot, paramIngotqty, paramRiser, paramRiserqty, paramTurning, paramRejection, paramAnyothermetal, paramAnyothermetalqty);

                    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;
            }
        }
        public virtual int DailyMaltingoneMaster_AddNew(string DrawingNo, int DieoperatorName, int Planqty, int Prodqty,int MaltingplanId, ref string errorDesc)
        {
            errorDesc = "";

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


            var paramDrawingNo = new SqlParameter("pDrawingNo", DrawingNo);

            var paramDieoperatorName = new SqlParameter("pDieoperatorName", DieoperatorName);

            var paramPlanqty = new SqlParameter("pPlanqty", Planqty);

            var paramProdqty = new SqlParameter("pProdqty", Prodqty);

            var paramMaltingplanId = new SqlParameter("pMaltingplanId", MaltingplanId);

            var dbContext = this.Database;

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

                try
                {
                    dbContext.ExecuteSqlCommand("Exec @ReturnValue = Sp_DailyMaltingPlanningone_AddNew  @pDrawingNo,@pDieoperatorName,@pPlanqty,@pProdqty,@pMaltingplanId",
                                                ReturnValueParam, paramDrawingNo, paramDieoperatorName, paramPlanqty, paramProdqty, paramMaltingplanId);

                    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;
            }
        }


        public virtual List<MeltingPlanningone> DailyMaltingoneMaster_GetAll(int MaltingplanId, int Maltingplanning1)
        {
            var paramMaltingplanId1 = new SqlParameter("pMaltingplanning1", Maltingplanning1);
            var paramMaltingplanId = new SqlParameter("pMaltingplanId", MaltingplanId);

            return this.Database.SqlQuery<MeltingPlanningone>(
                            "Sp_DailyMaltingPlanningone_GetAll @pMaltingplanning1,@pMaltingplanId",
                            paramMaltingplanId1,paramMaltingplanId).ToList();
        }



        public virtual int DailyMaltingoneMaster_Edit(int Maltingplanning1, string DrawingNo, int DieoperatorName, int Planqty, int Prodqty, ref string errorDesc)
        {
            errorDesc = "";

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

            var paramMaltingplanning1 = new SqlParameter("Maltingplanning1", Maltingplanning1);
            var paramDrawingNo = new SqlParameter("DrawingNo", DrawingNo);

            var paramDieoperatorName = new SqlParameter("DieoperatorName", DieoperatorName);

            var paramPlanqty = new SqlParameter("Planqty", Planqty);

            var paramProdqty = new SqlParameter("Prodqty", Prodqty);


            var dbContext = this.Database;

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

                try
                {
                    dbContext.ExecuteSqlCommand("Exec @ReturnValue = Sp_DailyMaltingPlanningone_Edit  @Maltingplanning1,@DrawingNo,@DieoperatorName,@Planqty,@Prodqty",
                                                ReturnValueParam, paramMaltingplanning1, paramDrawingNo, paramDieoperatorName, paramPlanqty, paramProdqty);

                    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;
            }
        }



    }
}