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 ShortMachineProductionDbContext: DbContext
    {
        public ShortMachineProductionDbContext() : 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 ShortMachineProduction_AddNew(DateTime Date, string UnitId, ref int FShortMachineProductionId, 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 parampFShortMachineProductionId = new SqlParameter("pFShortMachineProductionId", System.Data.SqlDbType.Int);
            parampFShortMachineProductionId.Direction = System.Data.ParameterDirection.Output;


            var dbContext = this.Database;

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

                try
                {
                    dbContext.ExecuteSqlCommand("Exec @ReturnValue = Sp_ShortMachineProduction_AddNew  @pDate,@pUnitId,@pFShortMachineProductionId OUT",
                                                ReturnValueParam, paramDate, paramUnitId, parampFShortMachineProductionId);

                    errorNo = Convert.ToInt32(ReturnValueParam.Value);


                    FShortMachineProductionId = Convert.ToInt32(parampFShortMachineProductionId.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 ShortMachineProduction_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_ShortMachineProduction_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 ShortMachineProductionone_AddNew(int ShortMachineProductionId, string VendorId, string MachineNo, string itemId, decimal 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 paramShortMachineProductionId = new SqlParameter("pShortMachineProductionId", ShortMachineProductionId);

            var paramVendorId = new SqlParameter("pVendorId", VendorId);
            var paramMachineNo = new SqlParameter("pMachineNo", MachineNo);
            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_ShortMachineProductionone_AddNew  @pShortMachineProductionId,@pVendorId,@pMachineNo,@pitemId,@pQuantity,@pActualWeight,@pStdWeight,@pPoNumber",
                                                ReturnValueParam, paramShortMachineProductionId, paramVendorId, paramMachineNo, 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 ShortMachineProductionone_Edit(int Id, string VendorId, string MachineNo, string itemId, decimal 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 paramMachineNo = new SqlParameter("pMachineNo", MachineNo);
            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_ShortMachineProductionone_Edit  @pId,@pVendorId,@pMachineNo,@pitemId,@pQuantity,@pActualWeight,@pStdWeight,@pPoNumber",
                                                ReturnValueParam, paramId, paramVendorId, paramMachineNo, 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<ShortMachineProduction> ShortMachineProduction_GetAll(int Id)
        {
            var paramCncId = new SqlParameter("pId", Id);

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

        public virtual List<ShortMachineProductionOne> ShortMachineProductionOne_GetAll(int Id,int ShortMachineProductionId)
        {
            var paramId = new SqlParameter("pId", Id);
            var paramShortMachineProductionId = new SqlParameter("pShortMachineProductionId", ShortMachineProductionId);

            return this.Database.SqlQuery<ShortMachineProductionOne>(
                            "Sp_ShortMachineProductionone_GetAll @pId,@pShortMachineProductionId",
                            paramId, paramShortMachineProductionId).ToList();
        }

    }
}