type ExamResult struct {
gorm.Model
ExamManagementID uint
ExamManagement ExamManagement `json:"examManagement" ` // 一场考试,其中有试卷,有试题,有试题答案
//MarkExamPaperRecord MarkExamPaperRecord //每一场考试对应的结果
EachStudentExamResults []EachStudentExamResult `json:"each_student_exam_results"` //每一场考试中有很多批阅试卷和未批阅试卷 ,包含一些基本信息
UnreadPaperNumber uint `json:"unread_paper_number" gorm:"comment:待批阅试卷数量"`
ExamUserNumber uint `json:"exam_user_number" gorm:"comment:考试人数"`
}
type ExamManagement struct {
global.GVA_MODEL
ExamName string `json:"exam_name" gorm:"not null;comment:考试名称"`
ExamType string `json:"exam_type" gorm:"not null;comment:考试类型"`
ExamPrice string `json:"exam_price" gorm:"not null;default:免费;comment:考试价格"` // /
PassingGrade string `json:"passing_grade" gorm:"not null;comment:及格分"`
ExamDuration string `json:"exam_duration" gorm:"not null;comment:考试时长"`
NumberOfPoints string `json:"number_of_points" gorm:"comment:积分数量"`
MinimumSubmissionTime string `json:"minimum_submission_time" gorm:"comment:最低交卷时长"`
ExamDepartmentId string `json:"exam_department_id" gorm:"comment:考试所属部门Id"`
UserId string `json:"user_id" gorm:"comment:指定人员Id"`
ThankYouAfterTheExam string `json:"thank_you_after_the_exam" gorm:"comment:考后感谢文字"`
TimeAllowedToBeLate string `json:"time_allowed_to_be_late" gorm:"comment:允许迟到时长"`
NotesOnExam string `json:"notes_on_exam" gorm:"comment:考前注意事项"`
PasswordPassword string `json:"password_password" gorm:"comment:口令密码"`
TotalScore string `json:"total_score" gorm:"comment:考试总分"`
State string `json:"state" gorm:"default:正常;comment:状态"`
ExamState string `json:"exam_state" gorm:"comment:考试状态"`
ExamAuthority string `json:"exam_authority" gorm:"comment:考试权限"` // (1代表完全公开,2代表部门开放,3代表指定人员,4代表口令密码)
ExamTimeSet time.Time `json:"exam_time_set" gorm:"comment:考试时间设置;default:1970-01-01 08:00:00"`
ExamTimeStart time.Time `json:"exam_time_start" gorm:"comment:考试开始时间;default:1970-01-01 08:00:00"`
ExamTimeEnd time.Time `json:"exam_time_end" gorm:"comment:考试结束时间;default:1970-01-01 08:00:00"`
LimitExamTime bool `json:"limit_exam_time" gorm:"comment:限制考试时间"`
NumberOfExams uint `json:"number_of_exams" gorm:"not null;comment:限考次数"`
ExamResultDisplay uint `json:"exam_result_display" gorm:"comment:考试结果显示;default:1"` // (1代表仅感谢文字,2代表感谢文字+成绩,3代表显示试卷明细)
ExamCreatedBy uint `json:"exam_created_by" gorm:"comment:考试创建人"`
ExamPaperID uint `json:"exam_paper_id" gorm:"comment:试卷Id"`
ExamPaper ExamPaper `json:"exam_paper" `
DeptID uint `json:"dept_id" gorm:"comment:考试所属部门"`
}
上面是两个结构体,一个是考试 ,一个是考试结果(阅卷管理)
下面是一场场的考试

每一场考试都有对应的考试结果(阅卷管理)

我们可以看到阅卷管理上面,有两个筛选条件,一个是考试类型,一个是考试名称,我们的筛选条件不是在考试结果里面,而是在考试结果中的 ExamManagement 字段中,此字段对应的是一个考试对象,所以这个筛选和平常的不一样。
我们先用preload进行一下筛选
func (s *MarkExamPaperService) ReviewPaging(p *request.ParamReviewPaging) (ExamResultList []examination.ExamResult, total int64, err error) {
limit := p.PageSize
offset := p.PageSize * (p.Page - 1)
db := global.GVA_DB.Model(&examination.ExamResult{})
if p.ExamName != "" {
db = db.Preload("ExamManagement", "exam_name like ?", "%"+p.ExamName+"%")
}
if p.ExamType != "" {
db = db.Preload("ExamManagement", "exam_type = ?", p.ExamType)
}
err = db.Limit(limit).Offset(offset).Find(&ExamResultList).Count(&total).Error //preload获取考试的基本信息
return
}
我们使用if判断,当筛选条件不为空的时候,我们使用preload预加载,同时附带上预加载的条件exam_name like ? 和 exam_type = ? ,这个时候我们把返回的结果查看一下
当时我把exam_type = "模拟考试"的时候(此时考试结果里面的考试全部都是正式考试),模拟考试的基本信息还是被查出来了,但是我们想要的结果其实是正式考试,所以此处使用preload不符合要求。

下面我们使用一下Joins,之前一直听说Joins,但是迟迟没有使用,现在这个场景,应该是应用Joins的时候了。
我们最终要筛选的是考试结果,所以我在模型的后面跟了一个Where查询,里面放上一个func 函数,然后在函数里面写上Joins
err = global.GVA_DB.Model(&examination.ExamResult{}).Where(func(db *gorm.DB) {
db.Joins("ExamManagement").Where("exam_name like ?", "%"+p.ExamName+"%")
}).Find(&ExamResultList,).Count(&total).Error
最终的结果是报错了,Where里面是不能放func的
于是乎,gorm应该是不支持根据模型下面关系模型的属性进行筛选原模型,在我求教他人后,还是我发通过gorm解决这个需求。
解决方案一:
考试结果和考试的模型调换,我们现在是考试结果中有考试,我们可以换成考试中有考试结果,这个时候我们再筛选的时候,我们就是根据考试本身的属性进行筛选了,这样肯定是可以筛选出来的。
解决方案二:
我们使用原生的sql语句,进行连表查询
global.GVA_DB.Raw("SELECT m.exam_name as exam_name,m.exam_type ,r.unread_paper as unread_paper FROM exam_results r INNER JOIN exam_managements m ON r.exam_management_id = m.id WHERE m.exam_name like \"%02%\"").Scan(&ExamResultList)
//定义一个用来接收的结构体
type Temp struct {
UnreadPaper uint `json:"unread_paper" gorm:"comment:待批阅试卷数量"`
ExamName string `json:"exam_name" gorm:"not null;comment:考试名称"`
ExamType string `json:"exam_type" gorm:"not null;comment:考试类型"`
}