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 AnodiszingDbContext: DbContext
    {
        public AnodiszingDbContext() : base()
        {
            Database.SetInitializer<HardnessRecordDbContext>(null);
            base.Database.Connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["RNGConnectionStrings"].ConnectionString;
        }

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

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Anodiszing>().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 Anodiszing_AddNew(int Metaltype,DateTime Date, string Aluwire, string Titaniumbolts, string Dmwater, string SulphuricAcid, string Nitricacid, string CausticSoda, string Starttimeofchillingplant, string StartEnergyreading, string CloseEnergyreading, ref int Anodiszingid, ref string errorDesc)
        {
            errorDesc = "";

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



            var paramMetaltype = new SqlParameter("pMetaltype", Metaltype);
            var paramDate = new SqlParameter("pDate", Date);
            var paramAluwire = new SqlParameter("pAluwire", Aluwire);
            var paramTitaniumbolts = new SqlParameter("pTitaniumbolts", Titaniumbolts);
            var paramDmwater = new SqlParameter("pDmwater", Dmwater);
            var paramSulphuricAcid = new SqlParameter("pSulphuricAcid", SulphuricAcid);
            var paramNitricacid = new SqlParameter("pNitricacid", Nitricacid);
            var paramCausticSoda = new SqlParameter("pCausticSoda", CausticSoda);
            var paramStarttimeofchillingplant = new SqlParameter("pStarttimeofchillingplant", Starttimeofchillingplant);
            var paramStartEnergyreading = new SqlParameter("pStartEnergyreading", StartEnergyreading);
            var paramCloseEnergyreading = new SqlParameter("pCloseEnergyreading", CloseEnergyreading);


           

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

            var dbContext = this.Database;

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

                try
                {
                    dbContext.ExecuteSqlCommand("Exec @ReturnValue = Sp_Anodiszing_AddNew  @pMetaltype,@pDate,@pAluwire,@pTitaniumbolts,@pDmwater,@pSulphuricAcid,@pNitricacid,@pCausticSoda,@pStarttimeofchillingplant,@pStartEnergyreading,@pCloseEnergyreading,@pAnodiszingid OUT",
                                                ReturnValueParam, paramMetaltype, paramDate, paramAluwire, paramTitaniumbolts, paramDmwater, paramSulphuricAcid, paramNitricacid, paramCausticSoda, paramStarttimeofchillingplant, paramStartEnergyreading, paramCloseEnergyreading, paramAnodiszingid);

                    errorNo = Convert.ToInt32(ReturnValueParam.Value);

                    Anodiszingid = Convert.ToInt32(paramAnodiszingid.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 Anodiszingone_AddNew(int Anodiszingid, string StartTime, string FinishTime, string Temp, string Qty, string WeightPerPices, string ThicknessMicron, string OkPices, string Rework,string Remark, ref string errorDesc)
        {
            errorDesc = "";

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



            var paramAnodiszingid = new SqlParameter("pAnodiszingid", Anodiszingid);
            var paramStartTime = new SqlParameter("pStartTime", StartTime);
            var paramFinishTime = new SqlParameter("pFinishTime", FinishTime);
            var paramTemp = new SqlParameter("pTemp", Temp);
            var paramQty = new SqlParameter("pQty", Qty);
            var paramWeightPerPices = new SqlParameter("pWeightPerPices", WeightPerPices);
            var paramThicknessMicron = new SqlParameter("pThicknessMicron", ThicknessMicron);
            var paramOkPices = new SqlParameter("pOkPices", OkPices);
            var paramRework = new SqlParameter("pRework", Rework);
            var paramRemark = new SqlParameter("pRemark", Remark);
            
            var dbContext = this.Database;
            

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

                try
                {
                    dbContext.ExecuteSqlCommand("Exec @ReturnValue = Sp_Anodiszingone_AddNew   @pAnodiszingid,@pStartTime,@pFinishTime,@pTemp,@pQty,@pWeightPerPices,@pThicknessMicron,@pOkPices,@pRework,@pRemark",
                                                ReturnValueParam, paramAnodiszingid, paramStartTime, paramFinishTime, paramTemp, paramQty, paramWeightPerPices, paramThicknessMicron, paramOkPices, paramRework, paramRemark);

                    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 Anodiszingone_Edit(int Anodiszingidone, string StartTime, string FinishTime, string Temp, string Qty, string WeightPerPices, string ThicknessMicron, string OkPices, string Rework, string Remark, ref string errorDesc)
        {
            errorDesc = "";

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



            var paramAnodiszingidone = new SqlParameter("pAnodiszingidone", Anodiszingidone);
            var paramStartTime = new SqlParameter("pStartTime", StartTime);
            var paramFinishTime = new SqlParameter("pFinishTime", FinishTime);
            var paramTemp = new SqlParameter("pTemp", Temp);
            var paramQty = new SqlParameter("pQty", Qty);
            var paramWeightPerPices = new SqlParameter("pWeightPerPices", WeightPerPices);
            var paramThicknessMicron = new SqlParameter("pThicknessMicron", ThicknessMicron);
            var paramOkPices = new SqlParameter("pOkPices", OkPices);
            var paramRework = new SqlParameter("pRework", Rework);
            var paramRemark = new SqlParameter("pRemark", Remark);

            var dbContext = this.Database;


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

                try
                {
                    dbContext.ExecuteSqlCommand("Exec @ReturnValue = Sp_Anodiszingone_Edit   @pAnodiszingidone,@pStartTime,@pFinishTime,@pTemp,@pQty,@pWeightPerPices,@pThicknessMicron,@pOkPices,@pRework,@pRemark",
                                                ReturnValueParam, paramAnodiszingidone, paramStartTime, paramFinishTime, paramTemp, paramQty, paramWeightPerPices, paramThicknessMicron, paramOkPices, paramRework, paramRemark);

                    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 Anodiszing_Edit(int Anodiszingid, int Metaltype, DateTime Date, string Aluwire, string Titaniumbolts, string Dmwater, string SulphuricAcid, string Nitricacid, string CausticSoda, string Starttimeofchillingplant, string StartEnergyreading, string CloseEnergyreading, ref string errorDesc)
        {
            errorDesc = "";

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


            var paramAnodiszingid = new SqlParameter("pAnodiszingid", Anodiszingid);
            var paramMetaltype = new SqlParameter("pMetaltype", Metaltype);
            var paramDate = new SqlParameter("pDate", Date);
            var paramAluwire = new SqlParameter("pAluwire", Aluwire);
            var paramTitaniumbolts = new SqlParameter("pTitaniumbolts", Titaniumbolts);
            var paramDmwater = new SqlParameter("pDmwater", Dmwater);
            var paramSulphuricAcid = new SqlParameter("pSulphuricAcid", SulphuricAcid);
            var paramNitricacid = new SqlParameter("pNitricacid", Nitricacid);
            var paramCausticSoda = new SqlParameter("pCausticSoda", CausticSoda);
            var paramStarttimeofchillingplant = new SqlParameter("pStarttimeofchillingplant", Starttimeofchillingplant);
            var paramStartEnergyreading = new SqlParameter("pStartEnergyreading", StartEnergyreading);
            var paramCloseEnergyreading = new SqlParameter("pCloseEnergyreading", CloseEnergyreading);

            
            var dbContext = this.Database;

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

                try
                {
                    dbContext.ExecuteSqlCommand("Exec @ReturnValue = Sp_Anodiszing_Edit  @pAnodiszingid,@pMetaltype,@pDate,@pAluwire,@pTitaniumbolts,@pDmwater,@pSulphuricAcid,@pNitricacid,@pCausticSoda,@pStarttimeofchillingplant,@pStartEnergyreading,@pCloseEnergyreading",
                                                ReturnValueParam, paramAnodiszingid,paramMetaltype, paramDate, paramAluwire, paramTitaniumbolts, paramDmwater, paramSulphuricAcid, paramNitricacid, paramCausticSoda, paramStarttimeofchillingplant, paramStartEnergyreading, paramCloseEnergyreading);

                    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<Anodiszing> Anodiszing_GetAlll(int Anodiszingid)
        {
            var paramAnodiszingid = new SqlParameter("pAnodiszingid", Anodiszingid);
          

            return this.Database.SqlQuery<Anodiszing>(
                            "Sp_Anodiszing_Getall @pAnodiszingid",
                           paramAnodiszingid).ToList();
        }



        public virtual List<Anodiszingone> Anodiszingone_GetAll(int Anodiszingidone,int Anodiszingid)
        {
            var paramAnodiszingidone = new SqlParameter("pAnodiszingidone", Anodiszingidone);
            var paramAnodiszingid = new SqlParameter("pAnodiszingid", Anodiszingid);


            return this.Database.SqlQuery<Anodiszingone>(
                            "Sp_Anodiszingone_Getall @pAnodiszingidone,@pAnodiszingid",
                           paramAnodiszingidone, paramAnodiszingid).ToList();
        }

    }
}