using PushPlatform.Common.Util;
using PushInfo.Core.DAL.MySql;
using PushInfo.Core.IDAL;
using System;
using System.Configuration;
using System.Reflection;

namespace PushInfo.Core
{
    /// <summary>
    /// 数据访问层工厂
    /// </summary>
    internal class DALFactory
    {
        /// <summary>
        /// 使用的数据库，配置名称必须和DAL文件夹下的对应数据库文件夹名称相同
        /// </summary>
        private static readonly string _DB = ConfigurationManager.AppSettings["DB"];

        /// <summary>
        /// DAL 程序集名称
        /// </summary>
        private static readonly string _AssemblyName = "PushInfo.Core";

        /// <summary>
        /// 创建 TableInfo 数据访问层
        /// </summary>
        /// <returns></returns>
        internal static ITableInfoDAL CreateTableInfoDAL()
        {
            string className = string.Concat(_AssemblyName, ".DAL.", _DB, ".", "TableInfoDAL");
            return (ITableInfoDAL)Assembly.Load(_AssemblyName).CreateInstance(className);
        }

        /// <summary>
        /// 创建 ProjectInfo 数据访问层
        /// </summary>
        /// <returns></returns>
        internal static IProjectInfo CreateProjectInfoDAL()
        {
            try
            {
                string className = string.Concat(_AssemblyName, ".DAL.", _DB, ".", "ProjectInfoDAL");
                return (IProjectInfo)Assembly.Load(_AssemblyName).CreateInstance(className);
            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 创建 Map_Project_Push 数据访问层
        /// </summary>
        /// <returns></returns>
        internal static IMapProjectPushDAL CreateMapProjectPushDAL()
        {
            try
            {
                string className = string.Concat(_AssemblyName, ".DAL.", _DB, ".", "MapProjectPushDAL");
                return (IMapProjectPushDAL)Assembly.Load(_AssemblyName).CreateInstance(className);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                throw new Exception(ex.Message);
            }
        }


        /// <summary>
        /// 创建 MappushUserRoleDAL 数据访问层
        /// </summary>
        /// <returns></returns>
        internal static IMappushUserRoleDAL CreateMappushUserRoleDAL()
        {
            try
            {
                string className = string.Concat(_AssemblyName, ".DAL.", _DB, ".", "MappushUserRoleDAL");
                return (IMappushUserRoleDAL)Assembly.Load(_AssemblyName).CreateInstance(className);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 创建 PushMessageDAL 数据访问层
        /// </summary>
        /// <returns></returns>
        internal static IPushMessageDAL CreatePushMessageDAL()
        {
            string className = string.Concat(_AssemblyName, ".DAL.", _DB, ".", "PushMessageDAL");
            return (IPushMessageDAL)Assembly.Load(_AssemblyName).CreateInstance(className);
        }

        /// <summary>
        /// 创建 PushMessageDAL 数据访问层
        /// </summary>
        /// <returns></returns>
        internal static ISysPushDAL CreateSysPushDAL()
        {
            string className = string.Concat(_AssemblyName, ".DAL.", _DB, ".", "SysPushDAL");
            return (ISysPushDAL)Assembly.Load(_AssemblyName).CreateInstance(className);
        }

        /// <summary>
        /// 创建 SelectItemDetailDAL 数据访问层
        /// </summary>
        /// <returns></returns>
        internal static ISelectItemDetailDAL CreateSelectItemDetailDAL()
        {
            string className = string.Concat(_AssemblyName, ".DAL.", _DB, ".", "SelectItemDetailDAL");
            return (ISelectItemDetailDAL)Assembly.Load(_AssemblyName).CreateInstance(className);
        }
        /// <summary>
        /// 创建微信模板 数据访问层
        /// </summary>
        /// <returns></returns>
        internal static IWechatMsgmodelDAL CreateWechatMsgmodelDAL()
        {
            string className = string.Concat(_AssemblyName, ".DAL.", _DB, ".", "WechatMsgmodelDAL");
            return (IWechatMsgmodelDAL)Assembly.Load(_AssemblyName).CreateInstance(className);
        }

        /// <summary>
        /// 创建存储过程 数据访问层
        /// </summary>
        /// <returns></returns>
        internal static QueryProcOrSqlDAL CreateQueryProcDAL()
        {
            string className = string.Concat(_AssemblyName, ".DAL.", _DB, ".", "QueryProcOrSqlDAL");
            return (QueryProcOrSqlDAL)Assembly.Load(_AssemblyName).CreateInstance(className);
        }

        /// <summary>
        /// 创建存储过程 数据访问层
        /// </summary>
        /// <returns></returns>
        internal static SysSendCfgDAL CreateSysSendCfgDAL()
        {
            string className = string.Concat(_AssemblyName, ".DAL.", _DB, ".", "SysSendCfgDAL");
            return (SysSendCfgDAL)Assembly.Load(_AssemblyName).CreateInstance(className);
        }
    }
}
