using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Debug;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using wispro.sp.entity;
using wispro.sp.entity.workflowDefine;
using wispro.sp.entity.workflowInstance;

namespace wispro.sp.api
{
    public partial class spDbContext : DbContext
    {
        [Obsolete]
        public static readonly LoggerFactory LoggerFactory = new LoggerFactory(new[] { new DebugLoggerProvider() });

        public spDbContext()
        {
        }

        public spDbContext(DbContextOptions<spDbContext> options)
            : base(options)
        {
        }

        public virtual DbSet<CalMonth> CalMonths { get; set; }
        public virtual DbSet<Customer> Customers { get; set; }
        public virtual DbSet<ItemStaff> ItemStaffs { get; set; }
        public virtual DbSet<PerformanceItem> PerformanceItems { get; set; }
        public virtual DbSet<StaffGrade> StaffGrades { get; set; }
        public virtual DbSet<VerifyCoefficient> VerifyCoefficients { get; set; }
        public virtual DbSet<Staff> Staffs { get; set; }

        public virtual DbSet<Message> Messages { get; set; }

        public virtual DbSet<MessageReadRecord> MessageReadRecords { get;set;}

        public virtual DbSet<MessagePerformanceItem> MessagePerformanceItems { get; set; }

        public virtual DbSet<AttachFile> AttachFiles { get; set; }

        public virtual DbSet<BasePointRule> BasePointRules { get; set; }

        public virtual DbSet<StaffStatistics> StaffStatistics { get; set; }

        public virtual DbSet<InputField> InputFields { get; set; }

        public virtual DbSet<InputFieldValue> InputFieldValues { get; set; }

        public virtual DbSet<AppealType> AppealTypes { get; set; }

        public virtual DbSet<AppealRecord> AppealRecords { get; set; }

        public virtual DbSet<SelectValue> SelectValues { get; set; }

        public virtual DbSet<CaseCeoffcient> CaseCeoffcients { get; set; }

        public virtual DbSet<Department> Departments { get; set; }

        public virtual DbSet<Position> Positions { get; set; }

        public virtual DbSet<StaffStatisticsforLevel> StaffStatisticsforLevels { get; set; }

        #region 流程定义
        public virtual DbSet<Workflow> Workflows { get; set; }

        public virtual DbSet<Step> Steps { get; set; }

        public virtual DbSet<TrasferCondition> TrasferConditions { get; set; }

        public virtual DbSet<entity.workflowDefine.Action> Actions { get; set; }

        public virtual DbSet<InputValueSetting> InputValueSettings { get; set; }

        public virtual DbSet<WorkflowInstance> WorkflowInstances { get; set; }

        public virtual DbSet<InputValue> InputValues { get; set; }

        public virtual DbSet<StepInstance> StepInstances { get; set; }

        #endregion

        #region 专案数据库
        public virtual DbSet<ProjectInfo> ProjectInfos { get; set; }

        public virtual DbSet<ProjectContentRecord> ProjectContentRecords { get; set; }
        public virtual DbSet<ProjectWorkContent> ProjectWorkContents { get; set; }
        #endregion

        public virtual DbSet<DepartmentPosition> DepartmentPositions { get; set; }

        
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlServer(utility.ConfigHelper.GetSectionValue("ConnectionStrings:DefaultConnect"));// "Data Source=(local);Initial Catalog=spDB;User ID=sa;Password=Lqftiu807005");// Configuration.GetConnectionString("DefaultConnect"));
                //ConfigurationManager.AppSettings["ValidAudience"]
            }

            //_ = optionsBuilder.UseLoggerFactory(LoggerFactory);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<CaseCeoffcient>( entity=>
            {
                entity.ToTable("CaseCeofficient");

                entity.HasKey(e=>e.Ceoffcient);
            });
            modelBuilder.Entity<BasePointRule>(entity=> {
                entity.ToTable("BasePointRule");

                entity.Property(e => e.PointExpress).HasMaxLength(100);
                entity.Property(e => e.Rule).HasMaxLength(1000);
                entity.Property(e => e.Type).HasMaxLength(20);

            });
            modelBuilder.Entity<CalMonth>(entity =>
                {
                    entity.ToTable("CalMonth");

                    entity.Property(e => e.Year).HasColumnName("year");

                    entity.Property(e => e.Month).HasColumnName("month");

                    entity.Property(e => e.Status).HasColumnName("status");
                }
            ) ;
            modelBuilder.Entity<Customer>(entity =>
            {
                entity.ToTable("Customer");

                entity.Property(e => e.Address).HasMaxLength(500);

                entity.Property(e => e.ContactMan).HasMaxLength(50);

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasMaxLength(200);

                entity.Property(e => e.Phone).HasMaxLength(50);

                entity.HasOne(d => d.ResponseMan)
                    .WithMany(p => p.Customers)
                    .HasForeignKey(d => d.ResponseManId)
                    .HasConstraintName("FK_Customer_Staff");
            });

            modelBuilder.Entity<ItemStaff>(entity =>
            {
                entity.HasKey(e => new { e.ItemId, e.DoPersonId });

                entity.ToTable("ItemStaff");

                entity.Property(e => e.PerformancePoint);//.HasColumnType("numeric(18, 2)");

                entity.HasOne(d => d.DoPerson)
                    .WithMany(p => p.ItemStaffs)
                    .HasForeignKey(d => d.DoPersonId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_ItemStaff_Staff");

                entity.HasOne(d => d.Item)
                    .WithMany(p => p.ItemStaffs)
                    .HasForeignKey(d => d.ItemId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_ItemStaff_PerformanceItem");
            });

            modelBuilder.Entity<PerformanceItem>(entity =>
            {
                entity.ToTable("PerformanceItem");

                entity.Property(e => e.ApplicationName).HasMaxLength(500);

                entity.Property(e => e.ApplicationType).HasMaxLength(50);

                entity.Property(e => e.BasePoint);

                entity.Property(e => e.BusinessType).HasMaxLength(50);

                entity.Property(e => e.CaseCoefficient).HasMaxLength(10);

                entity.Property(e => e.CaseName).HasMaxLength(500);

                entity.Property(e => e.CaseNo).HasMaxLength(50);

                entity.Property(e => e.CaseStage).HasMaxLength(50);

                entity.Property(e => e.CaseState).HasMaxLength(50);

                entity.Property(e => e.CaseType).HasMaxLength(50);

                entity.Property(e => e.EntrustingDate).HasColumnType("date");

                entity.Property(e => e.CustomerLimitDate).HasColumnType("date");

                entity.Property(e => e.DoItem).HasMaxLength(50);

                entity.Property(e => e.DoItemCoefficient).HasMaxLength(50);

                entity.Property(e => e.DoItemState).HasMaxLength(50);

                entity.Property(e => e.FinalizationDate).HasColumnType("date");

                entity.Property(e => e.FinishedDate).HasColumnType("date");

                entity.Property(e => e.FirstDraftDate).HasColumnType("date");

                entity.Property(e => e.InternalDate).HasColumnType("date");

                entity.Property(e => e.OverDueMemo).HasMaxLength(2000);

                entity.Property(e => e.PreOastaffId).HasColumnName("PreOAStaffId");

                entity.Property(e => e.ReturnDate).HasColumnType("date");
                

                entity.HasOne(d => d.Customer)
                    .WithMany(p => p.PerformanceItems)
                    .HasForeignKey(d => d.CustomerId)
                    .HasConstraintName("FK_PerformanceItem_Customer");

                entity.HasOne(d => d.PreOastaff)
                    .WithMany()
                    .HasForeignKey(d => d.PreOastaffId)
                    .HasConstraintName("FK_PerformanceItem_Staff");

                entity.HasOne(d => d.Reviewer)
                    .WithMany(p => p.ReviewerItems)
                    .HasForeignKey(d => d.ReviewerId)
                    .HasConstraintName("FK_PerformanceItem_Reviewer");

                entity.HasOne(d => d.WorkflowUser)
                    .WithMany()
                    .HasForeignKey(d => d.WorkflowUserId)
                    .HasConstraintName("FK_PerformanceItem_WorkflowUser");

                entity.HasOne(d => d.CalMonth)
                    .WithMany(p => p.PerformanceItems)
                    .HasForeignKey(d => d.CalMonthId)
                    .HasConstraintName("FK_PerformanceItem_CalMonth");
            });

            #region 专案
            modelBuilder.Entity<ProjectInfo>(entity =>
            {
                entity.ToTable("ProjectInfo");

                entity.HasKey(entity=>entity.CaseNo);

                entity.HasOne(d => d.Customer)
                    .WithMany()
                    .HasForeignKey(d => d.CustomerId)
                    .HasConstraintName("FK_ProjectInfo_Customer");

                entity.HasOne(d => d.WorkflowUser)
                    .WithMany()
                    .HasForeignKey(d => d.WorkflowUserId)
                    .HasConstraintName("FK_ProjectInfo_Staff");

                entity.HasOne(d => d.Reviewer)
                    .WithMany()
                    .HasForeignKey(d => d.ReviewerId)
                    .HasConstraintName("FK_ProjectInfo_Reviewer");

                
            });

            modelBuilder.Entity<ProjectContentRecord>(entity =>
            {
                entity.ToTable("ProjectContentRecord");

                
                entity.HasOne(d => d.CalMonth)
                    .WithMany()
                    .HasForeignKey(d => d.CalMonthId)
                    .HasConstraintName("FK_ProjectContentRecord_CalMonth");

                entity.HasOne(d => d.Project)
                    .WithMany()
                    .HasForeignKey(d => d.ProjectNo)
                    .HasConstraintName("FK_ProjectContentRecord_ProjectInfo");

                entity.HasOne(d => d.Staff)
                    .WithMany()
                    .HasForeignKey(d => d.StaffId)
                    .HasConstraintName("FK_Staff_ProjectContentRecord");

                entity.HasOne(d => d.Reviewer)
                    .WithMany()
                    .HasForeignKey(d => d.ReviewerId)
                    .HasConstraintName("FK_Reviewer_ProjectContentRecord");


            });

            modelBuilder.Entity<ProjectWorkContent>(entity =>
            {
                entity.ToTable("ProjectWorkContent");

                entity.HasOne(d => d.ContentRecord)
                    .WithMany(d=>d.ProjectWorkContents)
                    .HasForeignKey(d=>d.ContentRecordId)
                    .HasConstraintName("FK_ProjectRecord_WorkflowContent");

                
            });
            #endregion

            modelBuilder.Entity<StaffGrade>(entity =>
            {
                entity.ToTable("StaffGrade");

                entity.Property(e => e.Coefficient);//.HasColumnType("numeric(18, 2)");

                entity.Property(e => e.Grade)
                    .IsRequired()
                    .HasMaxLength(5)
                    .IsFixedLength(true);
            });

            modelBuilder.Entity<VerifyCoefficient>(entity =>
            {
                entity.HasKey(e => new { e.CheckerId, e.DoPersonId });

                entity.ToTable("VerifyCoefficient");

                entity.Property(e => e.Coefficient);//.HasColumnType("numeric(18, 2)");

                entity.HasOne(d => d.Checker)
                    .WithMany(p => p.VerifyCoefficientCheckers)
                    .HasForeignKey(d => d.CheckerId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_VerifyCoefficient_StaffGrade");

                entity.HasOne(d => d.DoPerson)
                    .WithMany(p => p.VerifyCoefficientDoPeople)
                    .HasForeignKey(d => d.DoPersonId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_VerifyCoefficient_StaffGrade1");
            });

            modelBuilder.Entity<Staff>(entity =>
            {
                entity.ToTable("Staff");

                //entity.Property(e => e.Id).ValueGeneratedNever();

                entity.Property(e => e.Account)
                    .IsRequired()
                    .HasMaxLength(50);

                entity.Property(e => e.Department).HasMaxLength(50);

                entity.Property(e => e.EntyDate).HasColumnType("date");

                entity.Property(e => e.IsCalPerformsnce).HasColumnName("isCalPerformsnce");

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasMaxLength(50);

                entity.Property(e => e.Password).IsRequired();

                entity.Property(e => e.Status)
                    .IsRequired()
                    .HasMaxLength(25);

                entity.Property(e => e.WorkPlace).HasMaxLength(50);

                entity.HasOne(d => d.StaffGrade)
                    .WithMany(p => p.Staff)
                    .HasForeignKey(d => d.StaffGradeId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_Staff_StaffGrade");
            });


            modelBuilder.Entity<StaffStatistics>(entity =>
            {
                entity.ToTable("StaffStatistics");

                entity.HasOne(d => d.CalMonth)
                    .WithMany()
                    .HasForeignKey(d=>d.CalMonthId);

                entity.HasOne(d => d.Staff)
                    .WithMany()
                    .HasForeignKey(d => d.StaffId);

                entity.HasOne(d => d.Item)
                    .WithMany()
                    .HasForeignKey(d => d.ItemId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_StaffStatistics_PerformanceItem");
            });

            modelBuilder.Entity<StaffStatisticsforLevel>(entity =>
            {
                entity.ToTable("StaffStatisticsforLevel");

                entity.HasOne(d => d.CalMonth)
                    .WithMany()
                    .HasForeignKey(d => d.CalMonthId);

                entity.HasOne(d => d.Staff)
                    .WithMany()
                    .HasForeignKey(d => d.StaffId);

                entity.HasOne(d => d.Grade)
                    .WithMany()
                    .HasForeignKey(d => d.GradeId);
                
            });

            modelBuilder.Entity<Message>(entity =>
            {
                //entity.HasKey(e => e.Id);

                entity.ToTable("Message");

                entity.Property(e => e.Type);
                entity.Property(e => e.MessageInfo).HasMaxLength(500);

                entity.HasOne(d => d.From)
                    .WithMany()
                    .HasForeignKey(d => d.FromId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_From_Staff");

            });

            modelBuilder.Entity<MessageReadRecord>(entity =>
            {
                entity.HasKey(e => new { e.MessageId,e.StaffId});

                entity.ToTable("MessageReadRecord");

                entity.Property(e => e.isReaded);

                entity.HasOne(d => d.Staff)
                    .WithMany()
                    .HasForeignKey(d => d.StaffId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_MessageReadRecord_Staff");

                entity.HasOne(d => d.Message)
                    .WithMany()
                    .HasForeignKey(d => d.MessageId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_MessageReadRecord_Message");

            });

            modelBuilder.Entity<MessagePerformanceItem>( entity =>
            {
                entity.HasKey(e=> new { e.ItemId,e.MessageId});

                entity.HasOne(d => d.Message)
                    .WithMany(m => m.RelatedItem)
                    .HasForeignKey(d => d.MessageId)
                    .HasConstraintName("FK_MessagePerformanceItem_Message");

                entity.HasOne(d => d.Item )
                    .WithMany()
                    .HasForeignKey(d => d.ItemId)
                    .HasConstraintName("FK_MessagePerformanceItem_Item");
            });

            modelBuilder.Entity<AttachFile>(entity =>
            {

                entity.ToTable("AttachFile");

                entity.Property(e=>e.Name).HasMaxLength(200);
                entity.Property(e => e.SavePath).HasMaxLength(200);

                entity.HasOne(d => d.UploadUser)
                    .WithMany()
                    .HasForeignKey(d => d.UploadUserId)
                    .HasConstraintName("FK_AttachFile_UpdateUser")
                    .OnDelete(DeleteBehavior.NoAction);

                entity.HasOne(d => d.AppealRecord)
                    .WithMany(a=>a.AttachFiles)
                    .HasForeignKey(d => d.AppealRecordId)
                    .HasConstraintName("FK_AttachFile_AppealRecord")
                    .IsRequired(false); 
            });

            modelBuilder.Entity<AppealType>(entity =>
            {
                entity.ToTable("AppealType");

                entity.Property(e => e.Name).HasMaxLength(50);
                entity.Property(e => e.ReviewerExpress).HasMaxLength(500);
                entity.Property(e => e.CanDoExpress).HasMaxLength(500);
            });

            modelBuilder.Entity<AppealRecord>(entity =>
            {
                entity.ToTable("AppealRecord");

                entity.Property(e => e.Reason).HasMaxLength(500);

                entity.HasOne(d => d.Creater)
                    .WithMany()
                    .HasForeignKey(d => d.CreaterId);

                entity.HasOne(d => d.Reviewer)
                    .WithMany().OnDelete(DeleteBehavior.NoAction)
                    .HasForeignKey(d => d.ReviewerId);

                entity.HasOne(d => d.Type)
                    .WithMany()
                    .OnDelete(DeleteBehavior.NoAction)
                    .HasForeignKey(d => d.TypeId);

                entity.HasOne(d => d.Item)
                    .WithMany()
                    .HasForeignKey(d=>d.ItemId);
            }) ;

            modelBuilder.Entity<InputField>(entity=> {
                entity.ToTable("InputField");

                entity.Property(e=>e.FieldName).HasMaxLength(50);
                entity.Property(e => e.FieldType).HasMaxLength(50);                
                entity.Property(e => e.MapObjectField).HasMaxLength(50);

                entity.HasOne(d => d.AppealType)
                    .WithMany()
                    .HasForeignKey(d=>d.AppealTypeId);

                //entity.HasKey(d=>new { d.AppealTypeId,d.AppealState});

            });

            modelBuilder.Entity<SelectValue>(entity => {
                entity.ToTable("SelectValue");
                
                entity.HasOne(d => d.InputField)
                    .WithMany(s=>s.SelectValues)
                    .HasForeignKey(d => d.InputFieldId);

                //entity.HasKey(d=>new { d.AppealTypeId,d.AppealState});

            });

            modelBuilder.Entity<InputFieldValue>(entity => {
                entity.ToTable("InputFieldValue");

                entity.Property(e => e.Value).HasMaxLength(500);
                
                entity.HasOne(d => d.InputField)
                    .WithMany()
                    .HasForeignKey(d => d.InputFieldId);

                entity.HasOne(d => d.AppealRecord)
                    .WithMany()
                    .HasForeignKey(d => d.AppealRecordId);

               //entity.HasKey(d=>new { d.AppealRecordId,d.InputFieldId});

            });

            modelBuilder.Entity<Department>(entity => {
                entity.ToTable("Department");
                
            });

            modelBuilder.Entity<Position>(entity => {
                entity.ToTable("Position");
            });

            modelBuilder.Entity<DepartmentPosition>(entity=> {
                entity.ToTable("DepartmentPosition");

                entity.HasOne(d => d.department)
                    .WithMany()
                    .HasForeignKey(d => d.departmentId);

                entity.HasOne(d => d.Position)
                    .WithMany()
                    .HasForeignKey(d=>d.PositionId);

                entity.HasOne(d => d.Staff)
                    .WithMany(d=>d.Positions)
                    .HasForeignKey(d => d.StaffId);
            });

            modelBuilder.Entity<StaffGrade>().HasData(
                new StaffGrade[]
                {
                    new StaffGrade() {Id=1, Grade = "S级", Coefficient = 1.2},
                    new StaffGrade() {Id=2, Grade = "A3级", Coefficient = 1.1},
                    new StaffGrade() {Id =3, Grade = "A2级", Coefficient = 1.1},
                    new StaffGrade() {Id=4, Grade = "A1级", Coefficient = 1.1},
                    new StaffGrade() {Id=5, Grade = "B3级", Coefficient = 1.0},
                    new StaffGrade() {Id=6, Grade = "B2级", Coefficient = 1.0},
                    new StaffGrade() {Id=7, Grade = "B1级", Coefficient = 0.9},
                    new StaffGrade() {Id=8, Grade = "C3级", Coefficient = 0.9},
                    new StaffGrade() {Id=9, Grade = "C2级", Coefficient = 0.7},
                    new StaffGrade() {Id= 10, Grade = "C1级", Coefficient = 0.7},
                    new StaffGrade() {Id=11, Grade = "D3级", Coefficient = 0.6},
                    new StaffGrade() {Id=12, Grade = "D2级", Coefficient = 0.6},
                    new StaffGrade() {Id=13, Grade = "D1级", Coefficient = 0.5},
                    new StaffGrade() {Id=14, Grade = "A级", Coefficient = 1.1},
                    new StaffGrade() {Id=15, Grade = "B级", Coefficient = 1.0},
                    new StaffGrade() {Id=16, Grade = "C级", Coefficient = 0.9}
                });

            modelBuilder.Entity<VerifyCoefficient>().HasData(
                new VerifyCoefficient[]
                {
                    
                    new VerifyCoefficient() { CheckerId  = 1, DoPersonId =5, Coefficient =0.3},
                    new VerifyCoefficient() { CheckerId  = 1, DoPersonId =6, Coefficient =0.3},
                    new VerifyCoefficient() { CheckerId  = 1, DoPersonId =7, Coefficient =0.3},
                    new VerifyCoefficient() { CheckerId  = 1, DoPersonId =8, Coefficient =0.3},
                    new VerifyCoefficient() { CheckerId  = 1, DoPersonId =9, Coefficient =0.4},
                    new VerifyCoefficient() { CheckerId  = 1, DoPersonId =10, Coefficient =0.4},
                    new VerifyCoefficient() { CheckerId  = 1, DoPersonId =11, Coefficient =0.5},
                    new VerifyCoefficient() { CheckerId  = 1, DoPersonId =12, Coefficient =0.5},
                    new VerifyCoefficient() { CheckerId  = 1, DoPersonId =13, Coefficient =0.6},
                    
                    new VerifyCoefficient() { CheckerId  = 2, DoPersonId =5, Coefficient =0.2},
                    new VerifyCoefficient() { CheckerId  = 2, DoPersonId =6, Coefficient =0.2},
                    new VerifyCoefficient() { CheckerId  = 2, DoPersonId =7, Coefficient =0.2},
                    new VerifyCoefficient() { CheckerId  = 2, DoPersonId =8, Coefficient =0.2},
                    new VerifyCoefficient() { CheckerId  = 2, DoPersonId =9, Coefficient =0.3},
                    new VerifyCoefficient() { CheckerId  = 2, DoPersonId =10, Coefficient =0.3},
                    new VerifyCoefficient() { CheckerId  = 2, DoPersonId =11, Coefficient =0.4},
                    new VerifyCoefficient() { CheckerId  = 2, DoPersonId =12, Coefficient =0.4},
                    new VerifyCoefficient() { CheckerId  = 2, DoPersonId =13, Coefficient =0.5},
                    
                    new VerifyCoefficient() { CheckerId  = 3, DoPersonId =5, Coefficient =0.2},
                    new VerifyCoefficient() { CheckerId  = 3, DoPersonId =6, Coefficient =0.2},
                    new VerifyCoefficient() { CheckerId  = 3, DoPersonId =7, Coefficient =0.2},
                    new VerifyCoefficient() { CheckerId  = 3, DoPersonId =8, Coefficient =0.2},
                    new VerifyCoefficient() { CheckerId  = 3, DoPersonId =9, Coefficient =0.3},
                    new VerifyCoefficient() { CheckerId  = 3, DoPersonId =10, Coefficient =0.3},
                    new VerifyCoefficient() { CheckerId  = 3, DoPersonId =11, Coefficient =0.4},
                    new VerifyCoefficient() { CheckerId  = 3, DoPersonId =12, Coefficient =0.4},
                    new VerifyCoefficient() { CheckerId  = 3, DoPersonId =13, Coefficient =0.5},
                    
                    new VerifyCoefficient() { CheckerId  = 4, DoPersonId =5, Coefficient =0.2},
                    new VerifyCoefficient() { CheckerId  = 4, DoPersonId =6, Coefficient =0.2},
                    new VerifyCoefficient() { CheckerId  = 4, DoPersonId =7, Coefficient =0.2},
                    new VerifyCoefficient() { CheckerId  = 4, DoPersonId =8, Coefficient =0.2},
                    new VerifyCoefficient() { CheckerId  = 4, DoPersonId =9, Coefficient =0.3},
                    new VerifyCoefficient() { CheckerId  = 4, DoPersonId =10, Coefficient =0.3},
                    new VerifyCoefficient() { CheckerId  = 4, DoPersonId =11, Coefficient =0.4},
                    new VerifyCoefficient() { CheckerId  = 4, DoPersonId =12, Coefficient =0.4},
                    new VerifyCoefficient() { CheckerId  = 4, DoPersonId =13, Coefficient =0.5},
                    
                    new VerifyCoefficient() { CheckerId  = 5, DoPersonId =5, Coefficient =0.2},
                    new VerifyCoefficient() { CheckerId  = 5, DoPersonId =6, Coefficient =0.2},
                    new VerifyCoefficient() { CheckerId  = 5, DoPersonId =7, Coefficient =0.2},
                    new VerifyCoefficient() { CheckerId  = 5, DoPersonId =8, Coefficient =0.2},
                    new VerifyCoefficient() { CheckerId  = 5, DoPersonId =9, Coefficient =0.3},
                    new VerifyCoefficient() { CheckerId  = 5, DoPersonId =10, Coefficient =0.3},
                    new VerifyCoefficient() { CheckerId  = 5, DoPersonId =11, Coefficient =0.4},
                    new VerifyCoefficient() { CheckerId  = 5, DoPersonId =12, Coefficient =0.4},
                    new VerifyCoefficient() { CheckerId  = 5, DoPersonId =13, Coefficient =0.5},
                    
                    new VerifyCoefficient() { CheckerId  = 6, DoPersonId =5, Coefficient =0.2},
                    new VerifyCoefficient() { CheckerId  = 6, DoPersonId =6, Coefficient =0.2},
                    new VerifyCoefficient() { CheckerId  = 6, DoPersonId =7, Coefficient =0.2},
                    new VerifyCoefficient() { CheckerId  = 6, DoPersonId =8, Coefficient =0.2},
                    new VerifyCoefficient() { CheckerId  = 6, DoPersonId =9, Coefficient =0.3},
                    new VerifyCoefficient() { CheckerId  = 6, DoPersonId =10, Coefficient =0.3},
                    new VerifyCoefficient() { CheckerId  = 6, DoPersonId =11, Coefficient =0.4},
                    new VerifyCoefficient() { CheckerId  = 6, DoPersonId =12, Coefficient =0.4},
                    new VerifyCoefficient() { CheckerId  = 6, DoPersonId =13, Coefficient =0.5}

                }
                );

            #region 初始化绩效点数规则
            //List<BasePointRule> rules = new List<BasePointRule>();
            //DataTable dt = wispro.sp.utility.NPOIExcel.ExcelToDataTable("c:\\temp\\20211130-点数规则-lcy-v1.xlsx", true);

            //foreach (DataRow row in dt.Rows)
            //{
            //    BasePointRule rule = new BasePointRule()
            //    {
            //        Rule = row["规则"].ToString(),
            //        PointExpress = row["点数计算"].ToString(),
            //        Priority = int.Parse(row["优先级修订"].ToString()),
            //        Type = row["类型"].ToString()

            //    };

            //    rules.Add(rule);
            //}

            //for (int i = 0; i < rules.Count; i++)
            //{
            //    rules[i].Id = i + 1;
            //}


            //modelBuilder.Entity<BasePointRule>().HasData(rules);
            #endregion

            AppealType[] appealTypes = new AppealType[]
            {
                new AppealType(){Id=1, Name ="绩效点数分配比率",CanDoExpress = "p.ItemStaffs.Count()>1",NeedReview=true},
                new AppealType(){Id=2,Name ="案件系数复核",CanDoExpress = "p.DoItem==\"新申请\"",ReviewerExpress ="p.Reviewer",NeedReview =true },
                new AppealType(){Id=3,Name ="处理事项系数复核",CanDoExpress = "p.DoItem==\"新申请\"",ReviewerExpress ="p.Reviewer", NeedReview = true},
                new AppealType(){Id=4,Name ="案件缺漏申诉",CanDoExpress ="",ReviewerExpress ="p.Reviewer",Type =1, NeedReview = true},
                new AppealType(){Id=5,Name ="案件严重超期说明",CanDoExpress ="p.isDanger()", NeedReview = true},
                new AppealType(){Id=6,Name ="按照翻译字数算绩效备注",CanDoExpress ="p.DoItem==\"新申请\" || p.DoItem==\"翻译\"", NeedReview = true}
            };

            InputField[] inputFields = new InputField[] {
                new InputField(){Id=1,AppealTypeId =1,AppealState =0,FieldName ="分配比率",
                    MapObjectField ="ItemStaffs.PerformancePoint",
                    FieldType = typeof(double).ToString(),
                    MapObjectFieldLabel="ItemStaffs.DoPerson.Name"
                },
                new InputField(){Id=3,AppealTypeId =1,AppealState =0,FieldName ="原因",FieldType =typeof(string).ToString() },
                new InputField(){Id=4,AppealTypeId =1,AppealState =1,FieldName ="备注",FieldType =typeof(string).ToString() },
                new InputField(){Id=5,AppealTypeId =1,AppealState =1,FieldName ="审核意见",FieldType =typeof(string).ToString()},

                
                new InputField(){Id=6,AppealTypeId =2,AppealState =0,FieldName ="案件系数", MapObjectField ="CaseCoefficient",FieldType = typeof(string).ToString() },
                new InputField(){Id=9,AppealTypeId =2,AppealState =1,FieldName ="备注",FieldType =typeof(string).ToString() },
                new InputField(){Id=10,AppealTypeId =2,AppealState =1,FieldName ="审核意见",FieldType =typeof(string).ToString()},


                new InputField(){Id=11,AppealTypeId =3,AppealState =0,FieldName ="处理事项系数", MapObjectField ="DoItemCoefficient",FieldType = typeof(string).ToString() },
                new InputField(){Id=12,AppealTypeId =3,AppealState =1,FieldName ="备注",FieldType =typeof(string).ToString() },
                new InputField(){Id=13,AppealTypeId =3,AppealState =1,FieldName ="审核意见",FieldType =typeof(string).ToString()},

                new InputField(){Id=14,AppealTypeId =4,AppealState =0,FieldName ="我方文号", FieldType = typeof(string).ToString() },
                new InputField(){Id=15,AppealTypeId =4,AppealState =0,FieldName ="处理事项", FieldType = typeof(string).ToString() },
                new InputField(){Id=16,AppealTypeId =4,AppealState =1,FieldName ="备注",FieldType =typeof(string).ToString() },
                new InputField(){Id=17,AppealTypeId =4,AppealState =1,FieldName ="审核意见",FieldType =typeof(string).ToString()},
                 
                new InputField(){Id=18,AppealTypeId =5,AppealState =0,FieldName ="超期说明",MapObjectField ="OverDueMemo", FieldType = typeof(string).ToString() },
                new InputField(){Id=21,AppealTypeId =5,AppealState =1,FieldName ="审核意见", FieldType = typeof(string).ToString() },
                new InputField(){Id=22,AppealTypeId =5,AppealState =1,FieldName ="备注", FieldType = typeof(string).ToString() },

                new InputField(){Id=19,AppealTypeId =6,AppealState =0,FieldName ="翻译类型", MapObjectField ="AgentFeedbackMemo", FieldType = typeof(string).ToString() },
                new InputField(){Id=20,AppealTypeId =6,AppealState =0,FieldName ="翻译字数", MapObjectField ="WordCount", FieldType = typeof(int).ToString() },
                new InputField(){Id=23,AppealTypeId =6,AppealState =1,FieldName ="审核意见",  FieldType = typeof(string).ToString() },
                new InputField(){Id=24,AppealTypeId =6,AppealState =1,FieldName ="翻译字数", MapObjectField ="WordCount", FieldType = typeof(int).ToString() },

            };

            List<SelectValue> selectValues = new List<SelectValue>() {
                new SelectValue(){Id =1, InputFieldId =5, Value ="同意" },
                new SelectValue(){Id =2, InputFieldId =5, Value ="拒绝" },
                new SelectValue(){Id =3, InputFieldId =10, Value ="同意" },
                new SelectValue(){Id =4, InputFieldId =10, Value ="拒绝" },
                new SelectValue(){Id =5, InputFieldId =13, Value ="同意" },
                new SelectValue(){Id =6, InputFieldId =13, Value ="拒绝" },
                new SelectValue(){Id =7, InputFieldId =17, Value ="同意" },
                new SelectValue(){Id =8, InputFieldId =17, Value ="拒绝" },

                new SelectValue(){Id =9, InputFieldId =6, Value ="S" },
                new SelectValue(){Id =10, InputFieldId =6, Value ="A" },
                new SelectValue(){Id =11, InputFieldId =6, Value ="B" },
                new SelectValue(){Id =12, InputFieldId =6, Value ="C" },
                new SelectValue(){Id =13, InputFieldId =6, Value ="D" },

                new SelectValue(){Id =14, InputFieldId =11, Value ="实质" },
                new SelectValue(){Id =15, InputFieldId =11, Value ="形式" },

                new SelectValue(){Id =16, InputFieldId =19, Value ="中-德" },
                new SelectValue(){Id =17, InputFieldId =19, Value ="中-英" },
                new SelectValue(){Id =18, InputFieldId =19, Value ="英-中" },

                new SelectValue(){Id =19, InputFieldId =21, Value ="同意" },
                new SelectValue(){Id =20, InputFieldId =21, Value ="拒绝" },

                new SelectValue(){Id =21, InputFieldId =23, Value ="同意" },
                new SelectValue(){Id =22, InputFieldId =23, Value ="拒绝" },
            };

            List<CaseCeoffcient> caseCeoffcients = new List<CaseCeoffcient>()
            {
                new CaseCeoffcient(){ Ceoffcient="S",Value =2.5},
                new CaseCeoffcient(){ Ceoffcient="A",Value =1.5},
                new CaseCeoffcient(){ Ceoffcient="B",Value =1.0},
                new CaseCeoffcient(){ Ceoffcient="C",Value =0.7},
                new CaseCeoffcient(){ Ceoffcient="D",Value =0.4}
            };
            modelBuilder.Entity<AppealType>().HasData(appealTypes);
            modelBuilder.Entity<InputField>().HasData(inputFields);
            modelBuilder.Entity<SelectValue>().HasData(selectValues);
            modelBuilder.Entity<CaseCeoffcient>().HasData(caseCeoffcients);

            #region 流程定义

            modelBuilder.Entity<Workflow>(entity => {
                entity.ToTable("Workflow");

                entity.HasOne(d => d.InitAction)
                    .WithMany()
                    .HasForeignKey(d => d.InitActionId)
                    .OnDelete(DeleteBehavior.NoAction);

                entity.HasOne(d => d.CreateUser)
                    .WithMany()
                    .HasForeignKey(d=>d.CreateUserId);
            });

            modelBuilder.Entity<Step>(entity => {
                entity.ToTable("Step");

                entity.HasOne(d => d.workflow)
                    .WithMany()
                    .HasForeignKey(d => d.workflowId);
            });

            modelBuilder.Entity<TrasferCondition>(entity=> {
                entity.ToTable("TrasferCondition");

                entity.HasOne(d=>d.Step)
                    .WithMany()
                    .HasForeignKey(d=>d.StepId)
                    .OnDelete(DeleteBehavior.NoAction);

                entity.HasOne(d => d.nextStep)
                    .WithMany()
                    .HasForeignKey(d => d.nextStepId)
                    .OnDelete(DeleteBehavior.NoAction);

            });

            modelBuilder.Entity<entity.workflowDefine.Action>(entity => {
                entity.ToTable("Action");

                entity.HasOne(d => d.step)
                    .WithMany()
                    .HasForeignKey(d => d.StepId);
                
            });

            modelBuilder.Entity<InputValueSetting>(entity => {
                entity.ToTable("InputValueSetting");

                entity.HasOne(d => d.action)
                    .WithMany(d=>d.inputValuesSettings)
                    .HasForeignKey(d => d.actionId)
                    .OnDelete(DeleteBehavior.NoAction);

                entity.HasOne(d => d.ParentSetting)
                    .WithMany()
                    .HasForeignKey(d => d.ParentSettingId);
            });

            modelBuilder.Entity<WorkflowInstance>(entity=> {
                entity.ToTable("WorkflowInstance");

                entity.HasOne(d=>d.workflow)
                    .WithMany()
                    .HasForeignKey(d=>d.workflowId);
            });

            modelBuilder.Entity<StepInstance>(entity => {
                entity.ToTable("StepInstance");

                entity.HasOne(d => d.Step)
                    .WithMany()
                    .HasForeignKey(d => d.stepId);

                entity.HasOne(d => d.workflowInstance)
                    .WithMany()
                    .HasForeignKey(d => d.workflowInstanceId)
                    .OnDelete(DeleteBehavior.NoAction);
            });

            modelBuilder.Entity<InputValue>(entity => {
                entity.ToTable("InputValue");

                entity.HasOne(d => d.workflowInstance)
                    .WithMany()
                    .HasForeignKey(d => d.workflowInstanceId);

                entity.HasOne(d => d.valueSetting)
                    .WithMany()
                    .HasForeignKey(d => d.valueSettingId);
            });

            #endregion

            

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }
}
