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

        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 FettlingProduction_AddNew(DateTime Date, string UnitId, ref int FettlingProductionId, ref string errorDesc)
        {
            errorDesc = "";

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

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

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


            var dbContext = this.Database;

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

                try
                {
                    dbContext.ExecuteSqlCommand("Exec @ReturnValue = Sp_FettlingProduction_AddNew  @pDate,@pUnitId,@pFettlingProductionId OUT",
                                                ReturnValueParam, paramDate, paramUnitId, parampFettlingProductionId);

                    errorNo = Convert.ToInt32(ReturnValueParam.Value);


                    FettlingProductionId = Convert.ToInt32(parampFettlingProductionId.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 FettlingProduction_Edit(int Id, DateTime Date, string UnitId, 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 paramDate = new SqlParameter("pDate", Date);
            var paramUnitId = new SqlParameter("pUnitId", UnitId);


            var dbContext = this.Database;

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

                try
                {
                    dbContext.ExecuteSqlCommand("Exec @ReturnValue = Sp_FettlingProduction_Edit  @pId,@pDate,@pUnitId",
                                                ReturnValueParam, paramId, paramDate, paramUnitId);

                    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 FettlingProductionone_AddNew(int FettlingProductionId, string VendorId, string itemId, int Quantity, decimal ActualWeight, decimal StdWeight,string PoNumber, ref string errorDesc)
        {
            errorDesc = "";

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


            var paramFettlingProductionId = new SqlParameter("pFettlingProductionId", FettlingProductionId);

            var paramVendorId = new SqlParameter("pVendorId", VendorId);
            var paramitemId = new SqlParameter("pitemId", itemId);
            var paramQuantity = new SqlParameter("pQuantity", Quantity);
            var paramActualWeight = new SqlParameter("pActualWeight", ActualWeight);
            var paramStdWeight = new SqlParameter("pStdWeight", StdWeight);
            var paramPoNumber = new SqlParameter("pPoNumber", PoNumber);

            var dbContext = this.Database;

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

                try
                {
                    dbContext.ExecuteSqlCommand("Exec @ReturnValue = Sp_FettlingProductionone_AddNew  @pFettlingProductionId,@pVendorId,@pitemId,@pQuantity,@pActualWeight,@pStdWeight,@pPoNumber",
                                                ReturnValueParam, paramFettlingProductionId, paramVendorId, paramitemId, paramQuantity, paramActualWeight, paramStdWeight, paramPoNumber);

                    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 FettlingProductionone_Edit(int Id, string VendorId, string itemId, int Quantity, decimal ActualWeight, decimal StdWeight,string PoNumber, 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 paramVendorId = new SqlParameter("pVendorId", VendorId);
            var paramitemId = new SqlParameter("pitemId", itemId);
            var paramQuantity = new SqlParameter("pQuantity", Quantity);
            var paramActualWeight = new SqlParameter("pActualWeight", ActualWeight);
            var paramStdWeight = new SqlParameter("pStdWeight", StdWeight);
            var paramPoNumber = new SqlParameter("pPoNumber", PoNumber);
            
            var dbContext = this.Database;

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

                try
                {
                    dbContext.ExecuteSqlCommand("Exec @ReturnValue = Sp_FettlingProductionone_Edit  @pId,@pVendorId,@pitemId,@pQuantity,@pActualWeight,@pStdWeight,@pPoNumber",
                                                ReturnValueParam, paramId, paramVendorId, paramitemId, paramQuantity, paramActualWeight, paramStdWeight, paramPoNumber);

                    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<FettlingProduction> FettlingProduction_GetAll(int Id)
        {
            var paramCncId = new SqlParameter("pId", Id);

            return this.Database.SqlQuery<FettlingProduction>(
                            "Sp_FettlingProduction_GetAll @pId",
                            paramCncId).ToList();
        }

        public virtual List<FettlingProductionOne> FettlingProductionOne_GetAll(int Id, int FettlingProductionId)
        {
            var paramId = new SqlParameter("pId", Id);
            var paramFettlingProductionId = new SqlParameter("pFettlingProductionId", FettlingProductionId);

            return this.Database.SqlQuery<FettlingProductionOne>(
                            "Sp_FettlingProductionone_GetAll @pId,@pFettlingProductionId",
                            paramId, paramFettlingProductionId).ToList();
        }

    }
}