using Kingdee.CDP.WebApi.SDK;
using Microsoft.EntityFrameworkCore;
using Oracle.ManagedDataAccess.Client;
using RB_MES_API.Models;
using RB_MES_API.Models.Cloud;
using RB_MES_API.Models.Pangu;
using RB_MES_APICore.Models;
using RB_MES_APICore.Models.Cloud;

namespace RB_MES_API.Context
{
    /// <summary>
    /// 沃比数据库实体
    /// </summary>
    public class RBContext : DbContext
    {
        public void ConfigureServices(IServiceCollection services)
        {
            //var connectionString = ConnectionString.GetConfig("ConnectionStrings", "RBConn");
            var connectionString = ApiSettingsHelper.GetConfig("RBConn");
            services.AddDbContextPool<RBContext>(options =>
            {

                options.UseSqlServer(connectionString, sqlServerOptionsAction: sqlOptions =>
                {
                    sqlOptions.CommandTimeout(600);
                    sqlOptions.MaxBatchSize(32767);
                });

            });
        }
        /// <summary>
        /// 构造RBContext
        /// </summary>
        /// <param name="options"></param>
        public RBContext(DbContextOptions<RBContext> options) : base(options)
        {
        }
        //在Startup中使用AddDbContext注册数据库连接时使用！！！
        //protected override void OnConfiguring(DbContextOptionsBuilder contextOptionsBuilder)
        //{
        //    string connnstr = ConnectionString.GetConfig("ConnectionStrings", "RBConn");
        //    contextOptionsBuilder.UseSqlServer(connnstr);
        //}
        /// <summary>
        /// Model 
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<ERPPutOperationAnchor>().ToTable("r_ERPPutOperationAnchor");
            modelBuilder.Entity<FormIDType>().ToTable("r_FormIDType");
            modelBuilder.Entity<FunctionList>().ToTable("r_FunctionList");
            modelBuilder.Entity<SelectJoinID>().ToTable("r_SelectJoinID");
            //modelBuilder.Entity<MesRequestStatu>().ToTable("r_MesRequestJson");
            modelBuilder.Entity<RBLog>().ToTable("RBLog");
            modelBuilder.Entity<BillConvert>().ToTable("r_BillConvertes");
            modelBuilder.Entity<ReceiveStockBillType>().ToTable("r_ReceiveStockBillTypes");
            modelBuilder.Entity<SystemProfile>().ToTable("r_SystemProfiles");
            modelBuilder.Entity<CloudBillQuery>().ToTable("r_CloudBillQuery");
            modelBuilder.Entity<SelectClumnConvert>().ToTable("r_ClumnConvert");
            modelBuilder.Entity<DataSaveBatch>().ToTable("r_DataSaveBatch");
            modelBuilder.Entity<ReciveDataBack>().ToTable("r_ReciveDataBack");
            modelBuilder.Entity<LockPRD_MO>().ToTable("r_LockPRD_MO");      //英搏尔项目特有的
            modelBuilder.Entity<CustomService>().ToTable("r_CustomServices");
            modelBuilder.Entity<BillTypeRules>().ToTable("r_BillTypeRules");
            modelBuilder.Entity<AuthorizationInfo>().ToTable("t_AuthorizationInfo");
            modelBuilder.Entity<ApiHostService>().ToTable("r_ApiHostServices");
            modelBuilder.Entity<SystemProfile>().HasKey(c => new { c.FCategoryID, c.FKey });
            modelBuilder.Entity<FormIDType>().HasIndex(c => new { c.FFunctionID, c.FDocType });
            modelBuilder.Entity<SelectJoinID>().HasIndex(c => c.FDocType);
            modelBuilder.Entity<RBLog>().HasIndex(c => new { c.FDateTime, c.FDocType });
            modelBuilder.Entity<BillConvert>().HasIndex(c => new { c.FReceiveID, c.FPageID, c.FK3FiledName }).IsUnique();
            modelBuilder.Entity<ReceiveStockBillType>().HasIndex(c => new { c.FFunctionID, c.ErpBillTypeID }).IsUnique();
            modelBuilder.Entity<CloudBillQuery>().HasIndex(c => c.FFormIDTypeID).IsUnique();
            modelBuilder.Entity<SelectClumnConvert>().HasIndex(c => c.FFunctionID);
            modelBuilder.Entity<ReciveDataBack>().HasIndex(c => new { c.FDocType, c.FErpID, c.FSeq }).IsUnique();
            modelBuilder.Entity<LockPRD_MO>().HasIndex(c => new { c.ERP_MO, c.MO_SEQ });
            modelBuilder.Entity<BillTypeRules>().HasIndex(c => new { c.FPostTypeID, c.FSBillTypeNumber });
            //相关表名称的和类的对应
            base.OnModelCreating(modelBuilder);
        }

        /// <summary>
        /// 用于检测单据是否已经进行过推送
        /// </summary>
        public DbSet<ERPPutOperationAnchor> r_ERPPutOperationAnchor { get; set; }

        /// <summary>
        /// 查询接口字段转换，可用于各种接口的字段转换
        /// </summary>
        public DbSet<FormIDType> r_FormIDType { get; set; }
        /// <summary>
        /// 接口类型定义表
        /// </summary>
        public DbSet<FunctionList> r_FunctionList { get; set; }
        /// <summary>
        /// 查询类接口定义
        /// </summary>
        public DbSet<SelectJoinID> r_SelectJoinID { get; set; }
        /// <summary>
        /// 沃比系统接收接口日志，防止重复传单
        /// </summary>
        //public DbSet<MesRequestStatu> r_MesRequestJson { get; set; }
        /// <summary>
        /// 沃比系统错误日志，包含程序错误
        /// </summary>
        public DbSet<RBLog> RBLog { get; set; }
        /// <summary>
        /// 接收单据类型接口传参定义
        /// </summary>
        public DbSet<BillConvert> r_BillConvertes { get; set; }
        /// <summary>
        /// 接收单据类型定义
        /// </summary>
        public DbSet<ReceiveStockBillType> r_ReceiveStockBillTypes { get; set; }
        /// <summary>
        /// 系统文件，包含各种参数定义
        /// </summary>
        public DbSet<SystemProfile> r_SystemProfiles { get; set; }
        /// <summary>
        /// ExecuteBillQuery接口JSON格式数据字段
        /// </summary>
        public DbSet<CloudBillQuery> r_CloudBillQuery { get; set; }
        /// <summary>
        /// 查询接口字段转换，可用于各种接口的字段转换
        /// </summary>
        public DbSet<SelectClumnConvert> r_ClumnConvert { get; set; }
        /// <summary>
        /// 批量推送单据到CLOUD配置
        /// </summary>
        public DbSet<DataSaveBatch> r_DataSaveBatch { get; set; }
        /// <summary>
        /// 向ERP推单备份
        /// </summary>
        public DbSet<ReciveDataBack> r_ReciveDataBack { get; set; }
        /// <summary>
        /// 向ERP推单备份
        /// </summary>
        public DbSet<LockPRD_MO> r_LockPRD_MO { get; set; }
        /// <summary>
        /// Cloud自定义接口配置
        /// </summary>
        public DbSet<CustomService> r_CustomServices { get; set; }
        /// <summary>
        /// 接收单据类型定义
        /// </summary>
        public DbSet<BillTypeRules> r_BillTypeRules { get; set; }
        /// <summary>
        /// 调用外部接口鉴权相关信息
        /// </summary>
        public DbSet<AuthorizationInfo> t_AuthorizationInfo { get; set; }
        /// <summary>
        /// 使用定时任务的列表
        /// </summary>
        public DbSet<ApiHostService> r_ApiHostServices { get; set; }
    }
}
