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

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

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<HeatTreatment>().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 HeatTreatment_AddNew(string DocNo,int Metaltype,DateTime Date,string DrawingNo,string Noofpcs,string wtg,string Typeofcycle,string Tempsetting,string Sokinghour, string SokingMinutes,string Startcycle,string Finishcycle,string TotalHours,string Quenchingtypeof,string Remarks,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 paramMetaltype = new SqlParameter("pMetaltype", Metaltype);
            var paramDate = new SqlParameter("pDate", Date);
            var paramDrawingNo = new SqlParameter("pDrawingNo", DrawingNo);
            var paramNoofpcs = new SqlParameter("pNoofpcs", Noofpcs);
            var paramwtg = new SqlParameter("pwtg", wtg);
            var paramTypeofcycle = new SqlParameter("pTypeofcycle", Typeofcycle);
            var paramTempsetting = new SqlParameter("pTempsetting", Tempsetting);
            var paramSokinghour = new SqlParameter("pSokinghour", Sokinghour);
            var paramSokingMinutes = new SqlParameter("pSokingMinutes", SokingMinutes);
            var paramStartcycle = new SqlParameter("pStartcycle", Startcycle);
            var paramFinishcycle = new SqlParameter("pFinishcycle", Finishcycle);
            var paramTotalHours = new SqlParameter("pTotalHours", TotalHours);
            var paramQuenchingtypeof = new SqlParameter("pQuenchingtypeof", Quenchingtypeof);
            var paramRemarks = new SqlParameter("pRemarks", Remarks);




            var dbContext = this.Database;

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

                try
                {
                    dbContext.ExecuteSqlCommand("Exec @ReturnValue = Sp_HeatTreatment_AddNew @pDocNo,@pMetaltype,@pDate,@pDrawingNo,@pNoofpcs,@pwtg,@pTypeofcycle,@pTempsetting,@pSokinghour,@pSokingMinutes,@pStartcycle,@pFinishcycle,@pTotalHours,@pQuenchingtypeof,@pRemarks",
                                                ReturnValueParam, paramDocNo, paramMetaltype, paramDate, paramDrawingNo, paramNoofpcs, paramwtg, paramTypeofcycle, paramTempsetting, paramSokinghour, paramSokingMinutes, paramStartcycle, paramFinishcycle, paramTotalHours, paramQuenchingtypeof, paramRemarks);

                    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 HeatTreatment_EditNew(int HeatId, string DocNo, int Metaltype, DateTime Date, string DrawingNo, string Noofpcs, string wtg, string Typeofcycle, string Tempsetting, string Sokinghour, string SokingMinutes, string Startcycle, string Finishcycle, string TotalHours, string Quenchingtypeof, string Remarks, ref string errorDesc)
        {
            errorDesc = "";

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

            var paramHeatId = new SqlParameter("pHeatId", HeatId);
            var paramDocNo = new SqlParameter("pDocNo", DocNo);
            var paramMetaltype = new SqlParameter("pMetaltype", Metaltype);
            var paramDate = new SqlParameter("pDate", Date);
            var paramDrawingNo = new SqlParameter("pDrawingNo", DrawingNo);
            var paramNoofpcs = new SqlParameter("pNoofpcs", Noofpcs);
            var paramwtg = new SqlParameter("pwtg", wtg);
            var paramTypeofcycle = new SqlParameter("pTypeofcycle", Typeofcycle);
            var paramTempsetting = new SqlParameter("pTempsetting", Tempsetting);
            var paramSokinghour = new SqlParameter("pSokinghour", Sokinghour);
            var paramSokingMinutes = new SqlParameter("pSokingMinutes", SokingMinutes);
            var paramStartcycle = new SqlParameter("pStartcycle", Startcycle);
            var paramFinishcycle = new SqlParameter("pFinishcycle", Finishcycle);
            var paramTotalHours = new SqlParameter("pTotalHours", TotalHours);
            var paramQuenchingtypeof = new SqlParameter("pQuenchingtypeof", Quenchingtypeof);
            var paramRemarks = new SqlParameter("pRemarks", Remarks);




            var dbContext = this.Database;

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

                try
                {
                    dbContext.ExecuteSqlCommand("Exec @ReturnValue = Sp_HeatTreatment_Edit @pHeatId,@pDocNo,@pMetaltype,@pDate,@pDrawingNo,@pNoofpcs,@pwtg,@pTypeofcycle,@pTempsetting,@pSokinghour,@pSokingMinutes,@pStartcycle,@pFinishcycle,@pTotalHours,@pQuenchingtypeof,@pRemarks",
                                                ReturnValueParam, paramHeatId, paramDocNo, paramMetaltype, paramDate, paramDrawingNo, paramNoofpcs, paramwtg, paramTypeofcycle, paramTempsetting, paramSokinghour, paramSokingMinutes, paramStartcycle, paramFinishcycle, paramTotalHours, paramQuenchingtypeof, paramRemarks);

                    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<HeatTreatment> HeatTreatment_GetAll(int HeatId)
        {
            var paramHeatId = new SqlParameter("pHeatId", HeatId);


            return this.Database.SqlQuery<HeatTreatment>(
                            "Sp_HeatTreatment_GetAll @pHeatId",
                            paramHeatId).ToList();
        }

    }
}