using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System.Configuration;
using wispro.sp.entity;


namespace wispro.sp.api
{
    public partial class spDbContext : DbContext
    {
        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; }

        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"]
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            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(200);

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

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

                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.DoItemMemo).HasMaxLength(500);

                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(100);

                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.CalMonth)
                    .WithMany(p => p.PerformanceItems)
                    .HasForeignKey(d => d.CalMonthId)
                    .HasConstraintName("FK_PerformanceItem_CalMonth");
            });

            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<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");
            });

            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}
                });

            modelBuilder.Entity<VerifyCoefficient>().HasData(
                new VerifyCoefficient[]
                {
                    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},
                }
                );


            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }
}
