public IEnumerable<UserRole> FdpUserGetRoles(User forUser)
        {
            IList<UserRole> retVal = new List<UserRole>{UserRole.None};
            using (var conn = DbHelper.GetDBConnection())
            {
                try
                {
                    var para = new DynamicParameters();
                    para.Add("@CDSId", forUser.CDSId, DbType.String);
                   
                    var results = conn.Query<FdpUserRoleDataItem>("dbo.Fdp_UserRole_GetMany", para, commandType: CommandType.StoredProcedure);
                    var fdpUserRoleDataItems = results as IList<FdpUserRoleDataItem> ?? results.ToList();
                    if (fdpUserRoleDataItems.Any())
                    {
                        retVal = fdpUserRoleDataItems.Select(role => (UserRole) role.FdpUserRoleId).ToList();
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw;
                }
            }

            return retVal;
        }
        public static DynamicParameters FromCDSId(string cdsId, string parameterName = "CDSId")
        {
            var parameters = new DynamicParameters();
            parameters.Add("@" + parameterName, cdsId, dbType: DbType.String);

            return parameters;
        }
        public TakeRateDataItem TakeRateModelSummaryItemGet(TakeRateFilter filter)
        {
            TakeRateDataItem retVal;

            using (var conn = DbHelper.GetDBConnection())
            {
                try
                {
                    var para = new DynamicParameters();
                    
                    para.Add("@FdpVolumeHeaderId", filter.TakeRateId, DbType.Int32);
                    para.Add("@MarketId", filter.MarketId, DbType.Int32);
                    para.Add("@ModelId", filter.ModelId, DbType.Int32);
                    para.Add("@FdpModelId", filter.FdpModelId, DbType.Int32);

                    var results = conn.QueryMultiple(fdpTakeRateModelSummaryItemGetStoredProcedureName, para, commandType: CommandType.StoredProcedure);
                    retVal = results.Read<TakeRateDataItem>().First();

                    retVal.Notes = results.Read<TakeRateDataItemNote>();
                    retVal.History = results.Read<TakeRateDataItemAudit>();
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw;
                }
            }
            return retVal;
        }
        public IEnumerable<EngineCodeMapping> EngineCodeMappingGetMany()
        {
            IList<EngineCodeMapping> retVal;
            using (var conn = DbHelper.GetDBConnection())
            {
                try
                {
                    var para = new DynamicParameters();
                    para.Add("@TotalRecords", null, DbType.Int32, ParameterDirection.Output);

                    var results = conn.Query<EngineCodeMapping>("dbo.Fdp_EngineCode_GetMany", para, commandType: CommandType.StoredProcedure);
                    var totalRecords = para.Get<int?>("@TotalRecords");

                    retVal = new List<EngineCodeMapping>();

                    foreach (var result in results)
                    {
                        result.TotalRecords = totalRecords.Value;
                        retVal.Add(result);
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw;
                }
            }

            return retVal;
        }
        public FdpDerivativeMapping FdpDerivativeMappingDelete(FdpDerivativeMapping derivativeMapping)
        {
            FdpDerivativeMapping retVal = new EmptyFdpDerivativeMapping();
            using (IDbConnection conn = DbHelper.GetDBConnection())
            {
                try
                {
                    var para = new DynamicParameters();
                    para.Add("@FdpDerivativeMappingId", derivativeMapping.FdpDerivativeMappingId, DbType.Int32);
                    para.Add("@CDSId", CurrentCDSID, DbType.String);

                    var results = conn.Query<FdpDerivativeMapping>("Fdp_DerivativeMapping_Delete", para, commandType: CommandType.StoredProcedure);
                    if (results.Any())
                    {
                        retVal = results.First();
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw;
                }
            }
            return retVal;
        }
        public EngineCodeMapping EngineCodeMappingSave(EngineCodeMapping mapping)
        {
            var retVal = mapping;

            using (var conn = DbHelper.GetDBConnection())
            {
                try
                {
                    var para = new DynamicParameters();

                    para.Add("@ProgrammeId", mapping.ProgrammeId, DbType.Int32);
                    para.Add("@EngineId", mapping.EngineId, DbType.Int32);
                    para.Add("@ExternalEngineCode", 
                        string.IsNullOrEmpty(mapping.ExternalEngineCode) ? null : mapping.ExternalEngineCode, DbType.String);
                    para.Add("@MappingId", null, DbType.Int32, ParameterDirection.Output);

                    conn.Execute("dbo.Fdp_EngineCode_Save", para, commandType: CommandType.StoredProcedure);

                    if (!mapping.MappingId.HasValue)
                    {
                        mapping.MappingId = para.Get<int>("@MappingId");
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw;
                }
            }

            return mapping;
        }
        /// <summary>
        /// Gets basic search parameters to pass to the data store based on the filter
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <returns></returns>
        public static DynamicParameters GetParametersFromFilter(FilterBase filter)
        {
            var para = new DynamicParameters();

            if (filter.PageIndex.HasValue)
            {
                para.Add("@PageIndex", filter.PageIndex.Value, dbType: DbType.Int32);
            }

            if (filter.PageSize.HasValue)
            {
                para.Add("@PageSize", filter.PageSize.Value, dbType: DbType.Int32);
            }

            if (filter.SortIndex.HasValue)
            {
                para.Add("@SortIndex", filter.SortIndex.Value, dbType: DbType.Int32);
            }

            if (filter.SortDirection != SortDirection.NotSet)
            {
                para.Add("@SortDirection", (int)filter.SortDirection, dbType: DbType.Int32);
            }

            return para;
        }
        public bool ConfigurationSave(ConfigurationItem obj)
        {
            var retVal = true;
            const string procName = "dbo.Fdp_Configuration_Edit";

			using (var conn = DbHelper.GetDBConnection())
            {
				try
				{
					var para = new DynamicParameters();

					 para.Add("@p_ConfigurationKey", obj.ConfigurationKey, DbType.String, size: 50);
					 para.Add("@p_Value", obj.Value, DbType.String, size: -1);
					 para.Add("@p_Description", obj.Description, DbType.String, size: -1);
                     para.Add("@p_DataType", obj.DataType, DbType.String, size: 50);

					conn.Execute(procName, para, commandType: CommandType.StoredProcedure);
				}
				catch (Exception ex)
				{
                    Log.Error(ex);
                    throw;
				}
			}

            return retVal;
            
        }
        public IEnumerable<Vehicle> VehicleGetMany(string make, bool deepGet = false)             
        {
            IEnumerable<Vehicle> retVal = null;
            using (IDbConnection conn = DbHelper.GetDBConnection())
            {
                try
                {
                    List<Vehicle> vehicles;

                    var para = new DynamicParameters();
                    para.Add("@p_make", make, dbType: DbType.String, size: 500);
                    using (var multi = conn.QueryMultiple("dbo.OXO_Vehicle_GetMany", para, commandType: CommandType.StoredProcedure))
                    {
                        vehicles = multi.Read<Vehicle>().ToList();
                        if (deepGet)
                        {
                            var programmes = multi.Read<Programme>().ToList();
                            foreach (var vehicle in vehicles)
                            {
                                vehicle.Programmes = programmes.Where(c => c.ParentId == vehicle.Id).ToList();
                            }
                        }
                    }

                    retVal = vehicles;
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw;
                }
            }

            return retVal;
        }
        public Vehicle VehicleGet(int id, bool deepGet = false)
        {
            Vehicle retVal = null;

            using (IDbConnection conn = DbHelper.GetDBConnection())
            {
                try
                {
                    var para = new DynamicParameters();
                    para.Add("@p_Id", id, dbType: DbType.Int32);
                   // para.Add("@p_deep_get", deepGet, dbType: DbType.Boolean);
                    using (var multi = conn.QueryMultiple("dbo.OXO_Vehicle_Get", para, commandType: CommandType.StoredProcedure))
                    {
                        retVal = multi.Read<Vehicle>().FirstOrDefault(); ;
                        var programmes = multi.Read<Programme>().ToList();
                        if (retVal != null)
                            retVal.Programmes = programmes.ToList();
                    }


                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw;
                }
            }

            return retVal;
        }
        public IEnumerable<OXORuleResult> RuleResultGetMany(int docid, int progid, string level, int objid, bool? showWhat)
        {
            IEnumerable<OXORuleResult> retVal = null;   
			using (IDbConnection conn = DbHelper.GetDBConnection())
            {
				try
				{
					var para = new DynamicParameters();

                    para.Add("@p_oxo_doc_id", docid, dbType: DbType.Int32);
                    para.Add("@p_prog_id", progid, dbType: DbType.Int32);
                    para.Add("@p_Level", level, dbType: DbType.String, size: 3);
                    para.Add("@p_ObjectId", objid, dbType: DbType.Int32);
                    para.Add("@p_show_what", showWhat, dbType: DbType.Boolean);
					    
					retVal = conn.Query<OXORuleResult>("dbo.OXO_Programme_Rule_Result_GetMany", para, commandType: CommandType.StoredProcedure);
				}
				catch (Exception ex)
				{
                    Log.Error(ex);
                    throw;
				}
			}

            return retVal;   
        }
        public User FdpUserSave(User userToAdd)
        {
            User retVal = new EmptyUser();

            using (var conn = DbHelper.GetDBConnection())
            {
                try
                {
                    var para = new DynamicParameters();
                    para.Add("@CDSId", userToAdd.CDSId, DbType.String);
                    para.Add("@FullName", userToAdd.FullName, DbType.String);
                    para.Add("@Mail", userToAdd.Mail, DbType.String);
                    para.Add("@IsAdmin", userToAdd.IsAdmin, DbType.Boolean);
                    para.Add("@CreatorCDSId", CurrentCDSID, DbType.String);
                    
                    var results = conn.Query<UserDataItem>("dbo.Fdp_User_Save", para, commandType: CommandType.StoredProcedure);
                    var enumerable = results as IList<UserDataItem> ?? results.ToList();
                    if (enumerable.Any()) {
                        retVal = enumerable.First().ToUser();
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw;
                }
            }

            return retVal;
        }
        public bool OXORuleFeatureSave(RuleFeature obj, IDbConnection conn)
        {
            bool retVal = true;
            string procName = "dbo.OXO_Rule_Feature_New";

            try
            {

                obj.Save(this.CurrentCDSID);

                var para = new DynamicParameters();

                para.Add("@p_RuleId", obj.RuleId, dbType: DbType.Int32);
                para.Add("@p_ProgrammeId", obj.ProgrammeId, dbType: DbType.Int32);
                para.Add("@p_FeatureId", obj.FeatureId, dbType: DbType.Int32);
                para.Add("@p_CreatedBy", obj.CreatedBy, dbType: DbType.String, size: 8);
                para.Add("@p_CreatedOn", obj.CreatedOn, dbType: DbType.DateTime);
                para.Add("@p_UpdatedBy", obj.UpdatedBy, dbType: DbType.String, size: 8);
                para.Add("@p_LastUpdated", obj.LastUpdated, dbType: DbType.DateTime);

                conn.Execute(procName, para, commandType: CommandType.StoredProcedure);

            }
            catch (Exception ex)
            {
                Log.Error(ex);
                throw;
            }

            return retVal;

        }
        public bool ChangeDiarySave(ChangeDiary obj)
        {
            bool retVal = true;
            string procName = (obj.Id == 0 ? "dbo.OXO_ChangeDiary_New" : "dbo.OXO_ChangeDiary_Edit");

			using (IDbConnection conn = DbHelper.GetDBConnection())
            {
				try
				{
					var para = new DynamicParameters();

					 para.Add("@p_OXO_Doc_Id", obj.OXODocId, dbType: DbType.Int32);
					 para.Add("@p_Programme_Id", obj.ProgrammeId, dbType: DbType.Int32);
                     para.Add("@p_Version_Info", obj.VersionInfo, dbType: DbType.String, size: 500);					
					 para.Add("@p_Entry_Header", obj.EntryHeader, dbType: DbType.String, size: -1);
					 para.Add("@p_Entry_Date", obj.EntryDate, dbType: DbType.DateTime);
					 para.Add("@p_Markets", obj.Markets, dbType: DbType.String, size: -1);
					 para.Add("@p_Models", obj.Models, dbType: DbType.String, size: -1);
					 para.Add("@p_Features", obj.Features, dbType: DbType.String, size: -1);
					 para.Add("@p_Current_Fitment", obj.CurrentFitment, dbType: DbType.String, size: 50);
					 para.Add("@p_Proposed_Fitment", obj.ProposedFitment, dbType: DbType.String, size: 50);
					 para.Add("@p_Comment", obj.Comment, dbType: DbType.String, size: -1);
                     para.Add("@p_Pricing_Status", obj.PricingStatus, dbType: DbType.String, size: 500);
                     para.Add("@p_Digital_Status", obj.DigitalStatus, dbType: DbType.String, size: 500);
                     para.Add("@p_Requester", obj.Requester, dbType: DbType.String, size: 100);
					 para.Add("@p_PACN", obj.PACN, dbType: DbType.String, size: 100);
					 para.Add("@p_ETracker", obj.ETracker, dbType: DbType.String, size: 100);
					 para.Add("@p_Order_Call", obj.OrderCall, dbType: DbType.String, size: 100);
					 para.Add("@p_Build_Effective_Date", obj.BuildEffectiveDate, dbType: DbType.DateTime);
					    

					if (obj.Id == 0)
					{
						para.Add("@p_Id", dbType: DbType.Int32, direction: ParameterDirection.Output);
					}
					else
					{
						para.Add("@p_Id", obj.Id, dbType: DbType.Int32);
					}

					conn.Execute(procName, para, commandType: CommandType.StoredProcedure);

					if (obj.Id == 0)
					{
						obj.Id = para.Get<int>("@p_Id");
					}

				}
				catch (Exception ex)
				{
                    Log.Error(ex);
                    throw;
				}
			}

            return retVal;
            
        }
        public bool OXOProgrammeFileSave(OXOProgrammeFile obj)
        {
            bool retVal = true;
            string procName = (obj.Id == 0 ? "dbo.OXO_Programme_File_New" : "dbo.OXO_Programme_File_Edit");

			using (IDbConnection conn = DbHelper.GetDBConnection())
            {
				try
				{

                    obj.Save(this.CurrentCDSID);
                    
                    var para = new DynamicParameters();

                     para.Add("@p_Programme_Id", obj.ProgrammeId, dbType: DbType.Int32);
                     para.Add("@p_File_Category", obj.FileCategory, dbType: DbType.String, size: 100);
                     para.Add("@p_File_Comment", obj.FileComment, dbType: DbType.String, size: 2000);                    
                     para.Add("@p_File_Name", obj.FileName, dbType: DbType.String, size: 100);
                     para.Add("@p_File_Ext", obj.FileExt, dbType: DbType.String, size: 4);
					 para.Add("@p_File_Type", obj.FileType, dbType: DbType.String, size: 20);
					 para.Add("@p_File_Size", obj.FileSize, dbType: DbType.Int32);
                     para.Add("@p_gateway", obj.Gateway, dbType: DbType.String, size: 100);
                     para.Add("@p_PACN", obj.PACN, dbType: DbType.String, size: 10);					
					 para.Add("@p_File_Content", obj.FileContent, dbType: DbType.Binary);
					 para.Add("@p_Created_By", obj.CreatedBy, dbType: DbType.String, size: 8);
					 para.Add("@p_Created_On", obj.CreatedOn, dbType: DbType.DateTime);
					 para.Add("@p_Updated_By", obj.UpdatedBy, dbType: DbType.String, size: 8);
					 para.Add("@p_Last_Updated", obj.LastUpdated, dbType: DbType.DateTime);
					    

					if (obj.Id == 0)
					{
						para.Add("@p_Id", dbType: DbType.Int32, direction: ParameterDirection.Output);
					}
					else
					{
						para.Add("@p_Id", obj.Id, dbType: DbType.Int32);
					}

					conn.Execute(procName, para, commandType: CommandType.StoredProcedure);

					if (obj.Id == 0)
					{
						obj.Id = para.Get<int>("@p_Id");
					}

				}
				catch (Exception ex)
				{
                    Log.Error(ex);
                    throw;
				}
			}

            return retVal;
            
        }
        public IEnumerable<Feature> FeatureGetMany(string mode = "generic", int paramId = 0, int docid = 0, string lookup = null, string group = null, int excludeObjId = 0, int excludeDocId = 0, bool useOACode = false)
        {
            IEnumerable<Feature> retVal = new List<Feature>();
            using (IDbConnection conn = DbHelper.GetDBConnection())
            {
                try
                {
                    var para = new DynamicParameters();
                    string sp_name = "";
                    switch (mode)
                    {
                        case "generic":
                            sp_name = "dbo.OXO_GenericFeature_GetMany";
                            break;
                        case "vehicle":
                            sp_name = "dbo.OXO_VehicleFeature_GetMany";
                            para.Add("@p_vehicle_id", paramId, DbType.Int32);
                            para.Add("@p_group", group, DbType.String, size: 500);
                            para.Add("@p_exclude_progid", excludeObjId, DbType.Int32);
                            para.Add("@p_exclude_docid", excludeDocId, DbType.Int32);
                            para.Add("@p_use_OA_code", useOACode, DbType.Boolean);
                            break;
                        case "programme":
                            sp_name = "dbo.OXO_ProgrammeFeature_GetMany";
                            para.Add("@p_prog_id", paramId, DbType.Int32);
                            para.Add("@p_doc_id", docid, DbType.Int32);
                            para.Add("@p_group", group, DbType.String, size: 500);
                            para.Add("@p_exclude_packid", excludeObjId, DbType.Int32);
                            break;
                        case "gsf":
                            sp_name = "dbo.OXO_Vehicle_GSF_GetMany";
                            para.Add("@p_vehicle_id", paramId, DbType.Int32);
                            para.Add("@p_group", group, DbType.String, size: 500);
                            para.Add("@p_exclude_progid", excludeObjId, DbType.Int32);
                            para.Add("@p_exclude_docid", excludeDocId, DbType.Int32);
                            para.Add("@p_use_OA_code", useOACode, DbType.Boolean);
                            break;
                        case "fdp":
                            sp_name = "dbo.OXO_Feature_GetMany";
                            para.Add("@p_vehicle_id", paramId, DbType.Int32);
                            lookup = "@@@";
                            break;
                    }

                    para.Add("@p_lookup", lookup, DbType.String, size: 50);
                    retVal = conn.Query<Feature>(sp_name, para, commandType: CommandType.StoredProcedure);
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw;
                }
            }

            return retVal;
        }
        public bool ForecastComparisonViewSave(ForecastComparisonView obj)
        {
            bool retVal = true;
            string procName = (obj.Id == 0 ? "dbo.ForecastComparisonView_New" : "dbo.ForecastComparisonView_Edit");

			using (IDbConnection conn = DbHelper.GetDBConnection())
            {
				try
				{
					var para = new DynamicParameters();

					 para.Add("@p_ForecastId", obj.ForecastId, dbType: DbType.Int32);
					 para.Add("@p_CreatedOn", obj.CreatedOn, dbType: DbType.DateTime);
					 para.Add("@p_CreatedBy", obj.CreatedBy, dbType: DbType.String, size: 16);
					 para.Add("@p_VehicleId", obj.VehicleId, dbType: DbType.Int32);
					 para.Add("@p_ProgrammeId", obj.ProgrammeId, dbType: DbType.Int32);
					 para.Add("@p_GatewayId", obj.GatewayId, dbType: DbType.Int32);
					 para.Add("@p_Make", obj.Make, dbType: DbType.String, size: 500);
					 para.Add("@p_Code", obj.Code, dbType: DbType.String, size: 500);
					 para.Add("@p_Description", obj.Description, dbType: DbType.String, size: 1003);
					 para.Add("@p_ModelYear", obj.ModelYear, dbType: DbType.String, size: 50);
					 para.Add("@p_Gateway", obj.Gateway, dbType: DbType.String, size: 50);
					 para.Add("@p_SortOrder", obj.SortOrder, dbType: DbType.Int32);
					    

					if (obj.Id == 0)
					{
						para.Add("@p_Id", dbType: DbType.Int32, direction: ParameterDirection.Output);
					}
					else
					{
						para.Add("@p_Id", obj.Id, dbType: DbType.Int32);
					}

					conn.Execute(procName, para, commandType: CommandType.StoredProcedure);

					if (obj.Id == 0)
					{
						obj.Id = para.Get<int>("@p_Id");
					}

				}
				catch (Exception ex)
				{
                    Log.Error(ex);
                    throw;
				}
			}

            return retVal;
            
        }
        public bool RuleResultSave(OXORuleResult obj)
        {
            bool retVal = true;
            string procName = (obj.Id == 0 ? "dbo.OXO_Programme_Rule_Result_New" : "dbo.OXO_Programme_Rule_Result_Edit");

			using (IDbConnection conn = DbHelper.GetDBConnection())
            {
				try
				{
                    obj.Save(this.CurrentCDSID);

					var para = new DynamicParameters();

					 para.Add("@p_OXODocId", obj.OXODocId, dbType: DbType.Int32);
					 para.Add("@p_ProgrammeId", obj.ProgrammeId, dbType: DbType.Int32);
                     para.Add("@p_Level", obj.ObjectLevel, dbType: DbType.String, size: 3);
                     para.Add("@p_ObjectId", obj.ObjectId, dbType: DbType.Int32);
					 para.Add("@p_RuleId", obj.RuleId, dbType: DbType.Int32);
					 para.Add("@p_ModelId", obj.ModelId, dbType: DbType.Int32);
					 para.Add("@p_RuleResult", obj.RuleResult, dbType: DbType.Boolean);
					 para.Add("@p_CreatedBy", obj.CreatedBy, dbType: DbType.String, size: 8);
					 para.Add("@p_CreatedOn", obj.CreatedOn, dbType: DbType.DateTime);
					    

					if (obj.Id == 0)
					{
						para.Add("@p_Id", dbType: DbType.Int32, direction: ParameterDirection.Output);
					}
					else
					{
						para.Add("@p_Id", obj.Id, dbType: DbType.Int32);
					}

					conn.Execute(procName, para, commandType: CommandType.StoredProcedure);

					if (obj.Id == 0)
					{
						obj.Id = para.Get<int>("@p_Id");
					}

				}
				catch (Exception ex)
				{
                    Log.Error(ex);
                    throw;
				}
			}

            return retVal;
            
        }
        public bool FeatureRequestSave(FeatureRequest obj)
        {
            bool retVal = true;
            string procName = (obj.Id == 0 ? "dbo.OXO_FeatureRequest_New" : "dbo.OXO_FeatureRequest_Edit");

			using (IDbConnection conn = DbHelper.GetDBConnection())
            {
				try
				{
					var para = new DynamicParameters();

					 para.Add("@p_Programme_Id", obj.Programme_Id, dbType: DbType.Int32);
					 para.Add("@p_Description", obj.Description, dbType: DbType.String, size: 500);
					 para.Add("@p_Marketing_Description", obj.Marketing_Description, dbType: DbType.String, size: 500);
					 para.Add("@p_Notes", obj.Notes, dbType: DbType.String, size: 2000);
					 para.Add("@p_Feature_Group", obj.Feature_Group, dbType: DbType.String, size: 500);
					 para.Add("@p_Decision", obj.Decision, dbType: DbType.String, size: 500);
					 para.Add("@p_Created_By", obj.Created_By, dbType: DbType.String, size: 8);
					 para.Add("@p_Created_On", obj.Created_On, dbType: DbType.DateTime);
					 para.Add("@p_Updated_By", obj.Updated_By, dbType: DbType.String, size: 8);
					 para.Add("@p_Last_Updated", obj.Last_Updated, dbType: DbType.DateTime);
					    

					if (obj.Id == 0)
					{
						para.Add("@p_Id", dbType: DbType.Int32, direction: ParameterDirection.Output);
					}
					else
					{
						para.Add("@p_Id", obj.Id, dbType: DbType.Int32);
					}

					conn.Execute(procName, para, commandType: CommandType.StoredProcedure);

					if (obj.Id == 0)
					{
						obj.Id = para.Get<int>("@p_Id");
					}

				}
				catch (Exception ex)
				{
                    Log.Error(ex);
                    throw;
				}
			}

            return retVal;
            
        }
        public bool PermissionSave(Permission obj)
        {
            bool retVal = true;
            string procName = (obj.Id == 0 ? "dbo.OXO_Permission_New" : "dbo.OXO_Permission_Edit");

			using (IDbConnection conn = DbHelper.GetDBConnection())
            {
				try
				{
					var para = new DynamicParameters();

					 para.Add("@p_CDSID", obj.CDSID, dbType: DbType.String, size: 50);
					 para.Add("@p_Object_Type", obj.ObjectType, dbType: DbType.String, size: 500);
					 para.Add("@p_Object_Id", obj.ObjectId, dbType: DbType.Int32);
					 para.Add("@p_Object_Val", obj.ObjectVal, dbType: DbType.String, size: 500);
					 para.Add("@p_Operation", obj.Operation, dbType: DbType.String, size: 500);
					 para.Add("@p_Created_By", obj.CreatedBy, dbType: DbType.String, size: 50);
					 para.Add("@p_Created_On", obj.CreatedOn, dbType: DbType.DateTime);
					 para.Add("@p_Updated_By", obj.UpdatedBy, dbType: DbType.String, size: 50);
					 para.Add("@p_Last_Updated", obj.LastUpdated, dbType: DbType.DateTime);
					    

					if (obj.Id == 0)
					{
						para.Add("@p_Id", dbType: DbType.Int32, direction: ParameterDirection.Output);
					}
					else
					{
						para.Add("@p_Id", obj.Id, dbType: DbType.Int32);
					}

					conn.Execute(procName, para, commandType: CommandType.StoredProcedure);

					if (obj.Id == 0)
					{
						obj.Id = para.Get<int>("@p_Id");
					}

				}
				catch (Exception ex)
				{
                    Log.Error(ex);
                    throw;
				}
			}

            return retVal;
            
        }
        public bool ForecastComparisonSave(ForecastComparison obj)
        {
            bool retVal = true;
            string procName = (obj.Id == 0 ? "dbo.ForecastComparison_New" : "dbo.ForecastComparison_Edit");

			using (IDbConnection conn = DbHelper.GetDBConnection())
            {
				try
				{
					var para = new DynamicParameters();

					 para.Add("@p_ForecastComparisonId", obj.ForecastComparisonId, dbType: DbType.Int32);
					 para.Add("@p_ForecastId", obj.ForecastId, dbType: DbType.Int32);
					 para.Add("@p_ForecastVehicleId", obj.ForecastVehicleId, dbType: DbType.Int32);
					 para.Add("@p_CreatedOn", obj.CreatedOn, dbType: DbType.DateTime);
					 para.Add("@p_CreatedBy", obj.CreatedBy, dbType: DbType.String, size: 16);
					 para.Add("@p_SortOrder", obj.SortOrder, dbType: DbType.Int32);
					 para.Add("@p_IsActive", obj.IsActive, dbType: DbType.Boolean);
					    

					if (obj.Id == 0)
					{
						para.Add("@p_Id", dbType: DbType.Int32, direction: ParameterDirection.Output);
					}
					else
					{
						para.Add("@p_Id", obj.Id, dbType: DbType.Int32);
					}

					conn.Execute(procName, para, commandType: CommandType.StoredProcedure);

					if (obj.Id == 0)
					{
						obj.Id = para.Get<int>("@p_Id");
					}

				}
				catch (Exception ex)
				{
                    Log.Error(ex);
                    throw;
				}
			}

            return retVal;
            
        }
        public bool PermissionOperationSave(PermissionOperation obj)
        {
            bool retVal = true;
            string procName = (obj.Id == 0 ? "dbo.PermissionOperation_New" : "dbo.PermissionOperation_Edit");

			using (IDbConnection conn = DbHelper.GetDBConnection())
            {
				try
				{
					var para = new DynamicParameters();

					 para.Add("@p_FdpPermissionOperationId", obj.FdpPermissionOperationId, dbType: DbType.Int32);
					 para.Add("@p_FdpPermissionObjectType", obj.FdpPermissionObjectType, dbType: DbType.String, size: 50);
					 para.Add("@p_Operation", obj.Operation, dbType: DbType.String, size: 25);
					 para.Add("@p_Description", obj.Description, dbType: DbType.String, size: -1);
					    

					if (obj.Id == 0)
					{
						para.Add("@p_Id", dbType: DbType.Int32, direction: ParameterDirection.Output);
					}
					else
					{
						para.Add("@p_Id", obj.Id, dbType: DbType.Int32);
					}

					conn.Execute(procName, para, commandType: CommandType.StoredProcedure);

					if (obj.Id == 0)
					{
						obj.Id = para.Get<int>("@p_Id");
					}

				}
				catch (Exception ex)
				{
                    Log.Error(ex);
                    throw;
				}
			}

            return retVal;
            
        }
        public IEnumerable<News> NewsGetMany()
        {
            var results = Enumerable.Empty<News>();

            using (var connection = DbHelper.GetDBConnection())
            {
                try
                {
                    var para = new DynamicParameters();
                    results = connection.Query<News>("dbo.Fdp_News_GetMany", para, commandType: CommandType.StoredProcedure);
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw;
                }
            }
            return results;
        }
        public IEnumerable<Market> MarketGetMany()
        {
            IEnumerable<Market> retVal = null;
            using (var conn = DbHelper.GetDBConnection())
            {
                try
                {
                    var para = new DynamicParameters();
                    retVal = conn.Query<Market>("dbo.OXO_Market_GetMany", para, commandType: CommandType.StoredProcedure);
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw;
                }

            }
            return retVal;
        }
        public IEnumerable<NameValuePair> PreferenceGetMany(string cdsid)
        {
            IEnumerable<NameValuePair> retVal = null;
			using (IDbConnection conn = DbHelper.GetDBConnection())
            {
				try
				{
					var para = new DynamicParameters();
                    para.Add("@p_CDSID", cdsid, dbType: DbType.String, size: 50);                 	    
					retVal = conn.Query<NameValuePair>("dbo.OXO_Preference_GetMany", para, commandType: CommandType.StoredProcedure);
				}
				catch (Exception ex)
				{
                    Log.Error(ex);
				}
			}

            return retVal;   
        }
        public IEnumerable<ModelEngine> ModelEngineGetMany(int progId)
        {
            IEnumerable<ModelEngine> retVal = null;
			using (IDbConnection conn = DbHelper.GetDBConnection())
            {
				try
				{
					var para = new DynamicParameters();
                    para.Add("@p_prog_id", progId, DbType.Int32);    
					retVal = conn.Query<ModelEngine>("dbo.OXO_ModelEngine_GetMany", para, commandType: CommandType.StoredProcedure);
				}
				catch (Exception ex)
				{
                    Log.Error(ex);
                    throw;
				}
			}

            return retVal;   
        }
        public EmailTemplate EmailTemplateGet(EmailEvent emailEvent)
        {
            using (var conn = DbHelper.GetDBConnection())
            {
                EmailTemplate retVal;
                try
                {
                    var para = new DynamicParameters();
                    para.Add("@FdpEmailTemplateId", (int)emailEvent, DbType.Int32);
                    retVal = conn.Query<EmailTemplate>("dbo.Fdp_EmailTemplate_Get", para, commandType: CommandType.StoredProcedure).FirstOrDefault();
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw;
                }

                return retVal;
            }
        }
        public IEnumerable<ChangeSet> ChangeSetGetMany (int OXODocId)
        {
            IEnumerable<ChangeSet> retVal = null;
			using (IDbConnection conn = DbHelper.GetDBConnection())
            {
				try
				{
					var para = new DynamicParameters();
					para.Add("@p_OXODocId", OXODocId, dbType: DbType.Int32);
        			retVal = conn.Query<ChangeSet>("dbo.OXO_ChangeSet_GetMany", para, commandType: CommandType.StoredProcedure);
				}
				catch (Exception ex)
				{
                    Log.Error(ex);
                    throw;
				}
			}

            return retVal;   
        }
        public IEnumerable<ForecastComparison> ForecastComparisonGetMany()
        {
            IEnumerable<ForecastComparison> retVal = null;
			using (IDbConnection conn = DbHelper.GetDBConnection())
            {
				try
				{
					var para = new DynamicParameters();
					    
					retVal = conn.Query<ForecastComparison>("dbo.ForecastComparison_GetMany", para, commandType: CommandType.StoredProcedure);
				}
				catch (Exception ex)
				{
                    Log.Error(ex);
                    throw;
				}
			}

            return retVal;   
        }
        public NameValuePair PreferenceGet(int id)
        {
            NameValuePair retVal = null;

			using (IDbConnection conn = DbHelper.GetDBConnection())
			{
				try
				{
					var para = new DynamicParameters();
					para.Add("@p_Id", id, dbType: DbType.Int32);
                    retVal = conn.Query<NameValuePair>("dbo.OXO_Preference_Get", para, commandType: CommandType.StoredProcedure).FirstOrDefault();
				}
				catch (Exception ex)
				{
                    Log.Error(ex);
				}
			}

            return retVal;
        }