using RNGClassLibrary;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace RNGWcfService1.ContextClass
{
    public class DrawingItemMasterDbContext: DbContext
    {
        public DrawingItemMasterDbContext() : base()
        {
            Database.SetInitializer<DrawingItemMasterDbContext>(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 DrawingItemMaster_AddNew(int ItemId, int MachinMasterId, int StageProcessId, string MachineTime, string ClampintTime, ref string errorDesc)
        {
            errorDesc = "";

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

            var paramItemId = new SqlParameter("pItemId", ItemId);
            var paramMachinMasterId = new SqlParameter("pMachinMasterId", MachinMasterId);
            var paramStageProcessId = new SqlParameter("pStageProcessId", StageProcessId);
            var paramMachineTime = new SqlParameter("pMachineTime", MachineTime);
            var paramClampintTime = new SqlParameter("pClampintTime", ClampintTime);
           
            var dbContext = this.Database;

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

                try
                {
                    dbContext.ExecuteSqlCommand("Exec @ReturnValue = Sp_DrawingItemMaster_AddNew  @pItemId,@pMachinMasterId,@pStageProcessId,@pMachineTime,@pClampintTime",
                                                ReturnValueParam, paramItemId, paramMachinMasterId, paramStageProcessId, paramMachineTime, paramClampintTime);

                    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 DrawingItemMaster_Edit(int Id,int ItemId, int MachinMasterId, int StageProcessId, string MachineTime, string ClampintTime, 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 paramItemId = new SqlParameter("pItemId", ItemId);
            var paramMachinMasterId = new SqlParameter("pMachinMasterId", MachinMasterId);
            var paramStageProcessId = new SqlParameter("pStageProcessId", StageProcessId);
            var paramMachineTime = new SqlParameter("pMachineTime", MachineTime);
            var paramClampintTime = new SqlParameter("pClampintTime", ClampintTime);

            var dbContext = this.Database;

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

                try
                {
                    dbContext.ExecuteSqlCommand("Exec @ReturnValue = Sp_DrawingItemMaster_Edit @pId,@pItemId,@pMachinMasterId,@pStageProcessId,@pMachineTime,@pClampintTime",
                                                ReturnValueParam, paramId, paramItemId, paramMachinMasterId, paramStageProcessId, paramMachineTime, paramClampintTime);

                    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<DrawingItemMaster> DrawingItemMaster_GetAll(int Id)
        {
            var paramId = new SqlParameter("pId", Id);

            return this.Database.SqlQuery<DrawingItemMaster>(
                            "Sp_DrawingItemMaster_GetAll @pId",
                            paramId).ToList();
        }
    }
}