You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/StudyCenter/DbConfig.cs

343 lines
20 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Application.Domain.Entities;
using Infrastructure.Application.Entites.Settings;
using Infrastructure.Data;
using Infrastructure.Extensions;
using Infrastructure.Security;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
namespace StudyCenter
{
public class DbConfig : IDbConfig
{
private readonly IEncryptionService _encryptionService;
public DbConfig(IEncryptionService encryptionService)
{
this._encryptionService = encryptionService;
}
public void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
}
public void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Setting>();
modelBuilder.Entity<User>().Property(o => o.UserName).IsRequired();
modelBuilder.Entity<User>().HasIndex(o => o.UserName).IsUnique();
modelBuilder.Entity<Role>().Property(o => o.Name).IsRequired();
modelBuilder.Entity<Role>().HasIndex(o => o.Name).IsUnique();
modelBuilder.Entity<PermissionCategory>().HasOne(o => o.Parent).WithMany(o => o.Children).HasForeignKey(o => o.ParentId).OnDelete(DeleteBehavior.SetNull);
modelBuilder.Entity<PermissionCategory>().HasIndex(o => o.Number).IsUnique();
modelBuilder.Entity<Permission>().HasIndex(o => o.Number).IsUnique();
modelBuilder.Entity<Permission>().HasOne(o => o.Category).WithMany(o => o.Permissions).HasForeignKey(o => o.CategoryId).OnDelete(DeleteBehavior.SetNull); modelBuilder.Entity<UserRole>().HasOne(o => o.User).WithMany(o => o.UserRoles).HasForeignKey(o => o.UserId);
modelBuilder.Entity<UserRole>().HasOne(o => o.Role).WithMany(o => o.UserRoles).HasForeignKey(o => o.RoleId);
modelBuilder.Entity<UserRole>().HasIndex(o => new { o.UserId, o.RoleId }).IsUnique();
modelBuilder.Entity<RolePermission>().HasOne(o => o.Role).WithMany(o => o.RolePermissions).HasForeignKey(o => o.RoleId);
modelBuilder.Entity<RolePermission>().HasOne(o => o.Permission).WithMany(o => o.RolePermissions).HasForeignKey(o => o.PermissionId);
modelBuilder.Entity<RolePermission>().HasIndex(o => new { o.RoleId, o.PermissionId }).IsUnique();
//学校课程
//专业分类用来组织学校、学院、系等
modelBuilder.Entity<MajorCategory>().HasOne(o => o.Parent).WithMany(o => o.Children).HasForeignKey(o => o.ParentId);
modelBuilder.Entity<MajorCategory>().HasIndex(o => new { o.ParentId, o.Number }).IsUnique();
//专业用来组织不同学制如3年专科、4年本科
modelBuilder.Entity<Major>().HasOne(o => o.Category).WithMany(o => o.Majors).HasForeignKey(o => o.CategoryId);
//每批次入学的学生,按照学制、就业方向等制定不同的教学计划
modelBuilder.Entity<TeachingPlan>().HasOne(o => o.Major).WithMany(o => o.Plans).HasForeignKey(o => o.MajorId);
//每个教学计划包含不同个学期和就业方向
modelBuilder.Entity<Semester>().HasOne(o => o.Plan).WithMany(o => o.Semesters).HasForeignKey(o => o.PlanId);
//每个学期包含多个学期课程
modelBuilder.Entity<SemesterCourse>().HasOne(o => o.Semester).WithMany(o => o.Courses).HasForeignKey(o => o.SemesterId);
//一个课程期对应多个教学计划学期中的课程,这样将学校的学期课程和课程的期统一起来
modelBuilder.Entity<SemesterCourse>().HasOne(o => o.Term).WithMany(o => o.SemesterCourses).HasForeignKey(o => o.TermId);
//课程分类
modelBuilder.Entity<CourseCategory>().HasOne(o => o.Parent).WithMany(o => o.Children).HasForeignKey(o => o.ParentId);
modelBuilder.Entity<CourseCategory>().HasIndex(o => new { o.ParentId, o.Number }).IsUnique();
//课程分类
modelBuilder.Entity<BookCategory>().HasOne(o => o.Parent).WithMany(o => o.Children).HasForeignKey(o => o.ParentId);
modelBuilder.Entity<BookCategory>().HasIndex(o => new { o.ParentId, o.Number }).IsUnique();
modelBuilder.Entity<Book>().HasOne(o => o.BookCategory).WithMany(o => o.Books).HasForeignKey(o => o.BookCategoryId);
modelBuilder.Entity<Section>().HasOne(o => o.Parent).WithMany(o => o.Children).HasForeignKey(o => o.ParentId);
modelBuilder.Entity<Section>().HasIndex(o => new { o.ParentId, o.Number }).IsUnique();
//每本书有多个章节
modelBuilder.Entity<Section>().HasOne(o => o.Book).WithMany(o => o.Sections).HasForeignKey(o => o.BookId);
//课程
modelBuilder.Entity<Course>().HasOne(o => o.Category).WithMany(o => o.Courses).HasForeignKey(o => o.CategoryId);
//每个课程有多个期
modelBuilder.Entity<Term>().HasOne(o => o.Course).WithMany(o => o.Terms).HasForeignKey(o => o.CourseId);
modelBuilder.Entity<Term>().HasOne(o => o.Book).WithMany(o => o.Terms).HasForeignKey(o => o.BookId);
//每期有多个课时
modelBuilder.Entity<Lesson>().HasOne(o => o.Term).WithMany(o => o.Lessons).HasForeignKey(o => o.TermId);
//课时和资源多对多多对多
modelBuilder.Entity<LessonResource>().HasOne(o => o.Lesson).WithMany(o => o.LessonResources).HasForeignKey(o => o.LessionId);
modelBuilder.Entity<LessonResource>().HasOne(o => o.Resource).WithMany(o => o.LessonResources).HasForeignKey(o => o.ResourceId);
modelBuilder.Entity<LessonResource>().HasIndex(o => new { o.LessionId, o.ResourceId }).IsUnique();
//学生和课程的期多对多
modelBuilder.Entity<TermUser>().HasOne(o => o.Term).WithMany(o => o.TermUsers).HasForeignKey(o => o.TermId);
modelBuilder.Entity<TermUser>().HasOne(o => o.User).WithMany(o => o.TermUsers).HasForeignKey(o => o.UserId);
modelBuilder.Entity<TermUser>().HasIndex(o => new { o.TermId, o.UserId }).IsUnique();
modelBuilder.Entity<ResourceCategory>().HasOne(o => o.Parent).WithMany(o => o.Children).HasForeignKey(o => o.ParentId);
modelBuilder.Entity<Resource>().HasOne(o => o.User).WithMany(o => o.Resources).HasForeignKey(o => o.UserId);
modelBuilder.Entity<Resource>().HasOne(o => o.Category).WithMany(o => o.Resources).HasForeignKey(o => o.CategoryId);
modelBuilder.Entity<Resource>().HasOne(o => o.Course).WithMany(o => o.Resources).HasForeignKey(o => o.CourseId);
modelBuilder.Entity<Resource>().HasOne(o => o.Section).WithMany(o => o.Resources).HasForeignKey(o => o.SectionId);
modelBuilder.Entity<Resource>().Property(o => o.Name).IsRequired().HasMaxLength(255);
//习题
modelBuilder.Entity<Question>().HasOne(o => o.User).WithMany(o => o.Questions).HasForeignKey(o => o.UserId);
modelBuilder.Entity<Question>().HasOne(o => o.Course).WithMany(o => o.Questions).HasForeignKey(o => o.CourseId);
modelBuilder.Entity<Question>().HasOne(o => o.Section).WithMany(o => o.Questions).HasForeignKey(o => o.SectionId);
//习题答题记录
modelBuilder.Entity<UserQuestion>().HasOne(o => o.User).WithMany(o => o.UserQuestions).HasForeignKey(o => o.UserId);
modelBuilder.Entity<UserQuestion>().HasOne(o => o.Question).WithMany(o => o.UserQuestions).HasForeignKey(o => o.QuestionId);
modelBuilder.Entity<UserQuestion>().HasIndex(o => new { o.UserId, o.QuestionId }).IsUnique();
//试题答题记录
modelBuilder.Entity<UserPaperQuestion>().HasOne(o => o.User).WithMany(o => o.UserPaperQuestions).HasForeignKey(o => o.UserId);
modelBuilder.Entity<UserPaperQuestion>().HasOne(o => o.PaperQuestion).WithMany(o => o.UserPaperQuestions).HasForeignKey(o => o.PaperQuestionId);
modelBuilder.Entity<UserPaperQuestion>().HasIndex(o => new { o.UserId, o.PaperQuestionId }).IsUnique();
//试卷答题记录
modelBuilder.Entity<UserPaper>().HasOne(o => o.User).WithMany(o => o.UserPapers).HasForeignKey(o => o.UserId);
modelBuilder.Entity<UserPaper>().HasOne(o => o.Paper).WithMany(o => o.UserPapers).HasForeignKey(o => o.PaperId);
modelBuilder.Entity<UserPaper>().HasIndex(o => new { o.UserId, o.PaperId }).IsUnique();
//考题是具有习题的属性和分值等其他和试卷相关的属性
modelBuilder.Entity<PaperQuestion>().HasOne(o => o.Paper).WithMany(o => o.Questions).HasForeignKey(o => o.PaperId);
}
public void Seed(DbContext dbContext)
{
var set = dbContext.Set<Setting>();
set.Add(new Setting { Name = "name", Value = "学习中心", Type = SettingType.Text });
set.Add(new Setting { Name = "logo", Value = "/images/logo.png", Type = SettingType.ImageUrl });
set.Add(new Setting { Name = "copyright", Value = "Copyright © {0} Company. All rights reserved", Type = SettingType.Html });
dbContext.SaveChanges();
foreach (var item in dbContext.Model.GetEntityTypes())
{
var type = item.ClrType;
var name = type.GetDisplayName();
var number = type.Name;
var category = new PermissionCategory
{
Name = name,
Number = type.Name
};
category.Permissions.Add(new Permission { Name = $"查看{name}", Number = $"Read-{number}" });
category.Permissions.Add(new Permission { Name = $"添加{name}", Number = $"Add-{number}" });
category.Permissions.Add(new Permission { Name = $"修改{name}", Number = $"Edit-{number}" });
category.Permissions.Add(new Permission { Name = $"删除{name}", Number = $"Delete-{number}" });
dbContext.Set<PermissionCategory>().Add(category);
}
dbContext.SaveChanges();
var saRole = new Role { Name = "超级管理员", IsReadOnly = true };
var adminRole = new Role { Name = "管理员", IsReadOnly = true };
foreach (var item in dbContext.Set<Permission>())
{
saRole.RolePermissions.Add(new RolePermission { Permission = item, IsReadOnly = true });
if (!item.Name.Contains("删除"))
{
adminRole.RolePermissions.Add(new RolePermission { Permission = item, IsReadOnly = true });
}
}
dbContext.Set<User>().Add(new User
{
UserName = "super",
UserRoles = new List<UserRole> { new UserRole { Role = saRole } }
});
var user1Id = dbContext.Set<User>().Add(new User
{
UserName = "admin",
UserRoles = new List<UserRole> { new UserRole { Role = adminRole } }
}).Entity.Id;
dbContext.SaveChanges();
//课程分类
dbContext.Set<CourseCategory>().Add(new CourseCategory { Name = "高职高专", Number = "2" });
dbContext.Set<CourseCategory>().Add(new CourseCategory { Name = "大学大专", Number = "3" });
dbContext.Set<CourseCategory>().Add(new CourseCategory { Name = "大学", Number = "4" });
dbContext.Set<CourseCategory>().Add(new CourseCategory { Name = "研究生", Number = "5" });
dbContext.Set<CourseCategory>().Add(new CourseCategory { Name = "成人教育", Number = "6" });
dbContext.Set<CourseCategory>().Add(new CourseCategory { Name = "职业培训", Number = "7" });
dbContext.SaveChanges();
//教学计划
var major1 =
dbContext.Set<MajorCategory>().Add(new MajorCategory
{
Name = "计算机学院",
Number = "0",
Majors = new List<Major>
{
new Major{
Name="计算机科学与技术",
Plans = new List<TeachingPlan>
{
new TeachingPlan
{
Start=DateTime.Now.Date,
Years=4,
Degree="学士",
Career="软件工程方向",
Semesters = new List<Semester>
{
new Semester
{
Name="第1学期",
Courses = new List<SemesterCourse>
{
new SemesterCourse
{
Name="计算机应用",
Number ="106090100",
Category="学科基础课",
Type="必修课",
Score=4
}
}
}
}
}
}
}
}
});
dbContext.SaveChanges();
//资源类型初始化
var resourceTypeId1 = dbContext.Set<ResourceCategory>().Add(new ResourceCategory { Name = "文档", Number = "01" }).Entity.Id;
dbContext.Set<ResourceCategory>().Add(new ResourceCategory { Name = "音频", Number = "01" });
dbContext.Set<ResourceCategory>().Add(new ResourceCategory { Name = "视频", Number = "01" });
dbContext.Set<ResourceCategory>().Add(new ResourceCategory { Name = "动画", Number = "01" });
dbContext.Set<ResourceCategory>().Add(new ResourceCategory { Name = "虚拟仿真", Number = "01" });
//资源分类初始化
dbContext.SaveChanges();
dbContext.Set<CourseCategory>().Add(new CourseCategory
{
Name = "中职中专",
Number = "1",
Children = new List<CourseCategory> {
new CourseCategory
{
Name="公开课",
Number="1",
DisplayOrder=1
},
new CourseCategory
{
Name="专业课",
Number="2",
DisplayOrder=2,
Children=new List<CourseCategory>
{
new CourseCategory
{
Name = "信息技术类",
Number = "10609",
DisplayOrder = 106,
Children = new List<CourseCategory>
{
new CourseCategory
{
Name="计算机应用",
Number="106090100",
DisplayOrder=106010100,
Courses = new List<Course>
{
new Course
{
Name="Java基础",
Number="10001",
Image="/upload/1.jpg",
Description="本课程是以Java语言来讲授程序设计的入门知识而非具体教授Java语言关于Java语言的全面知识还需要学习本课程的后续课程——“Java语言程序设计进阶”。",
Terms = new List<Term>
{
new Term
{
Name="2018-2019学年下学期",
Start= new DateTime(2018,9,1),
End=new DateTime(2019,1,1),
Lessons=new List<Lesson>
{
new Lesson
{
Name="导论",
DisplayOrder=1
},
new Lesson
{
Name="基础",
DisplayOrder=2
}
}
}
}
}
}
}
}
}
}
}
}
});
dbContext.SaveChanges();
dbContext.Set<BookCategory>().Add(new BookCategory
{
Name = "理科教材",
Number = "0000",
Books = new List<Book>
{
new Book
{
Name="计算机应用基础",
Number="ISBN 0000",
Sections = new List<Section>
{
new Section
{
Name="导论上",
Number="1.1"
},
new Section
{
Name="导论下",
Number="1.2"
}
}
}
}
});
dbContext.SaveChanges();
dbContext.Set<Resource>().Add(new Resource
{
CategoryId = dbContext.Set<ResourceCategory>().FirstOrDefault(o => o.Name == "视频").Id,
Name = "导论",
File = "/upload/1.mp4",
UserId = user1Id,
CourseId = dbContext.Set<Course>().FirstOrDefault(o => o.Name == "Java基础").Id,
SectionId = dbContext.Set<Section>().FirstOrDefault(o => o.Name == "导论上").Id,
});
dbContext.SaveChanges();
dbContext.Set<LessonResource>().Add(new LessonResource
{
Name = "视频",
LessionId = dbContext.Set<Lesson>().FirstOrDefault(o => o.Name == "导论").Id,
ResourceId = dbContext.Set<Resource>().FirstOrDefault(o => o.Name == "导论").Id,
});
dbContext.SaveChanges();
foreach (var item in dbContext.Set<CourseCategory>())
{
item.UpdatePath();
}
dbContext.SaveChanges();
}
}
}