org.openurp.edu.web.helper.ClazzSearchHelper Maven / Gradle / Ivy
/*
* OpenURP, Agile University Resource Planning Solution.
*
* Copyright © 2014, The OpenURP Software.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful.
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package org.openurp.edu.web.helper;
import org.apache.commons.lang3.StringUtils;
import org.beangle.commons.collection.CollectUtils;
import org.beangle.commons.collection.Order;
import org.beangle.commons.dao.query.builder.OqlBuilder;
import org.beangle.commons.lang.BitStrings;
import org.beangle.commons.lang.Strings;
import org.beangle.commons.lang.tuple.Pair;
import org.beangle.orm.hibernate.udt.HourMinute;
import org.beangle.orm.hibernate.udt.WeekDay;
import org.beangle.orm.hibernate.udt.WeekState;
import org.beangle.struts2.helper.Params;
import org.beangle.struts2.helper.QueryHelper;
import org.openurp.base.edu.model.Project;
import org.openurp.base.edu.model.Semester;
import org.openurp.base.edu.model.WeekTimeBuilder;
import org.openurp.base.model.AuditStatus;
import org.openurp.base.model.Department;
import org.openurp.base.std.model.Squad;
import org.openurp.code.edu.model.ExamType;
import org.openurp.code.edu.model.ClazzTag;
import org.openurp.edu.clazz.model.ScheduleSuggest;
import org.openurp.edu.clazz.model.Clazz;
import org.openurp.edu.clazz.model.ClazzRestrictionMeta;
import org.openurp.edu.clazz.model.Schedule;
import org.openurp.edu.exam.model.ExamTaker;
import org.openurp.web.helper.SearchHelper;
import java.util.Iterator;
import java.util.List;
public class ClazzSearchHelper extends SearchHelper {
/**
* 构造教学任务查询Query,控制数据级权限
* 本方法是控制数据级权限的
*
* @param request
* @param clazz
* @return
* @see buildQuery(boolean)
*/
public OqlBuilder buildQuery() {
return buildQuery(true);
}
/**
* 查找教学任务task
* 1)查找行政班级中以:adminClass.name为参数
* 2)查询排课情况以:courseActivity开头
* 3)查询排考情况以:examActivity开头
* 4)考试安排完成:clazz.schedule.isExamArrangeComplete
* 5)排考查询分组情况:arrangeInfo.examGrouped
* 6)日历以:semester开头
* 7)查询教师以:teacher
* 8)特殊的选课属性:electInfo.electCountCompare标识选课人数上限和实际人数的比较
* 9)特殊的排课属性:arrangeInfo.endWeek
*
* @param request
* @param clazz
* @return
*/
public OqlBuilder buildQuery(boolean applyRestriction) {
OqlBuilder query = OqlBuilder.from(Clazz.class, "clazz");
if (applyRestriction) {
boolean shouldApply = true;
if (projectContext.isRoot() || projectContext.getDeparts().containsAll(projectContext.getProject().getDepartments())) {
shouldApply = false;
}
if (shouldApply) {
query.where("clazz.teachDepart in(:departs)", projectContext.getDeparts());
}
// projectContext.applyRestriction(query);
}
Integer semesterId = Params.getInt("clazz.semester.id");
if (semesterId == null) semesterId = Params.getInt("semesterId");
Semester semester = entityDao.get(Semester.class, semesterId);
String clazzIdSeq = Params.get("splitIds");
if (Strings.isNotEmpty(clazzIdSeq)) {
query.where("clazz.id in (:clazzIds)", Strings.splitToLong(clazzIdSeq));
query.limit(QueryHelper.getPageLimit());
return query;
}
Project project = projectContext.getProject();
query.where("clazz.project=:project", project);
QueryHelper.populateConditions(query,
"clazz.project.id,clazz.schedule.status,clazz.id,clazz.courseType.name");
// 查询课程类别
String courseTypeName = Params.get("clazz.courseType.name");
boolean courseTypeNameNotLike = Params.getBool("fake.courseType.name.notLike");
if (Strings.isNotBlank(Strings.trim(courseTypeName))) {
if (courseTypeNameNotLike) {
query.where("clazz.courseType.name not like :courseTypeName", "%" + courseTypeName + "%");
} else {
query.where("clazz.courseType.name like :courseTypeName", "%" + courseTypeName + "%");
}
}
// 教师
Boolean teacherIsNull = Params.getBoolean("fake.teacher.null");
Integer teacherDepart = Params.getInt("fake.teacher.department.id");
String teacherName = Params.get("teacher.name");
if (Strings.isBlank(teacherName)) {
teacherName = Params.get("clazz.teacher.name");
}
if ((teacherDepart != null || Strings.isNotBlank(teacherName)) && null == teacherIsNull) teacherIsNull = false;
if (teacherIsNull != null) {
if (Boolean.TRUE.equals(teacherIsNull)) {
query.where("size(clazz.teachers) = 0");
} else {
if (teacherDepart == null && Strings.isBlank(teacherName)) {
query.where("size(clazz.teachers) > 0");
} else if (null != teacherDepart && Strings.isBlank(teacherName)) {
query.where("exists (from clazz.teachers tt where tt.department = :teacherDepartment)",
entityDao.get(Department.class, teacherDepart));
} else if (null == teacherDepart && Strings.isNotBlank(teacherName)) {
query.where("exists (from clazz.teachers tt where tt.name like :tname)",
'%' + teacherName + '%');
} else {
query
.where(
"exists (from clazz.teachers tt where tt.name like :tname adn tt.department =:teacherDepartment)",
'%' + teacherName + '%', entityDao.get(Department.class, teacherDepart));
}
}
}
// 审核状态
String status = Params.get("fake.state");
if (Strings.isNotBlank(status)) {
query.where("clazz.status = :status", AuditStatus.valueOf(status.toUpperCase()));
}
// 查询课程安排情况
Long buildingId = Params.getLong("fack.building.id");
Integer weekday = Params.getInt("fake.time.weekday");
String courseUnit = Params.get("courseActivity.time.beginAt");
if (null == courseUnit) courseUnit = Params.get("fake.time.unit");
Integer activityWeekStart = Params.getInt("fake.time.weekstart");
Integer activityWeekEnd = Params.getInt("fake.time.weekend");
Long activityWeekState = null;
if (null != activityWeekStart || null != activityWeekEnd) {
if (null == activityWeekStart) activityWeekStart = activityWeekEnd;
if (null == activityWeekEnd) activityWeekEnd = activityWeekStart;
if (activityWeekEnd >= activityWeekStart && activityWeekEnd < 52 && activityWeekStart > 0) {
StringBuilder sb = new StringBuilder(Strings.repeat("0", 53));
for (int i = activityWeekStart; i <= activityWeekEnd; i++)
sb.setCharAt(i, '1');
activityWeekState = BitStrings.binValueOf(sb.toString());
}
}
if (null != buildingId || Strings.isNotBlank(courseUnit) || null != weekday || null != activityWeekState) {
StringBuilder activityQuery = new StringBuilder(
"exists( from clazz.schedule.activities as courseActivity where 1=1 ");
if (Strings.isNotBlank(courseUnit)) {
activityQuery.append("and courseActivity.time.beginAt <= :beginAt"
+ " and courseActivity.time.endAt >= :beginAt ");
query.param("beginAt", new HourMinute(courseUnit));
}
if (null != weekday) {
activityQuery.append(" and courseActivity.time.startOn in (:startOn)");
query.param("startOn", WeekTimeBuilder.getYearStartOns(semester, WeekDay.get(weekday)));
}
if (null != activityWeekState) activityQuery.append(" and bitand(courseActivity.time.weekstate,"
+ activityWeekState + ")>0");
if (null != buildingId) activityQuery
.append(" and exists(from courseActivity.rooms as cr where cr.room.building.id=" + buildingId + ")");
activityQuery.append(")");
query.where(activityQuery.toString());
}
// 排课状态
String courseScheduleStatus = Params.get("clazz.schedule.status");
if (Strings.isNotEmpty(courseScheduleStatus)) {
Schedule.Status s = Schedule.Status.valueOf(courseScheduleStatus);
if (s.equals(Schedule.Status.DONT_ARRANGE)) {
query.where("clazz.schedule.creditHours =0");
} else if (s.equals(Schedule.Status.NEED_ARRANGE)) {
query.where("clazz.schedule.creditHours >0 and size(clazz.schedule.activities)=0");
} else {
query.where("clazz.schedule.creditHours >0 and size(clazz.schedule.activities)>0");
}
}
// 周数
Integer weeks = Params.getInt("fake.weeks");
if (null != weeks) {
query.where("week_state_weeks(clazz.schedule.weekstate)=:weeks", weeks);
}
// 周课时
Float weekHours = Params.getFloat("fake.weekHours");
if (null != weekHours) {
query
.where(
"week_state_weeks(clazz.schedule.weekstate) >0 and (clazz.schedule.creditHours / week_state_weeks(clazz.schedule.weekstate) ) = :weekHours",
weekHours.intValue());
}
// 起止周
Integer startWeek = Params.getInt("fake.week.start");
Integer endWeek = Params.getInt("fake.week.end");
if (null != startWeek || null != endWeek) {
List weekIndecies = CollectUtils.newArrayList();
if (null == startWeek) startWeek = 1;
if (null == endWeek) endWeek = semester.getWeeks();
while (startWeek <= endWeek) {
weekIndecies.add(startWeek);
startWeek += 1;
}
query.where("bitand(clazz.schedule.weekstate,:weekstate)>0", WeekState.of(weekIndecies).value);
}
// 人数上限
Integer limitCountStart = Params.getInt("fake.limitCount.start");
if (null != limitCountStart) {
query.where("clazz.enrollment.capacity >= :limitCountStart", limitCountStart);
}
Integer limitCountEnd = Params.getInt("fake.limitCount.end");
if (null != limitCountEnd) {
query.where("clazz.enrollment.capacity <= :limitCountEnd", limitCountEnd);
}
// 实际人数
Integer stdCountStart = Params.getInt("fake.stdCount.start");
if (null != stdCountStart) {
query.where("clazz.enrollment.stdCount >= :stdCountStart", stdCountStart);
}
Integer stdCountEnd = Params.getInt("fake.stdCount.end");
if (null != stdCountEnd) {
query.where("clazz.enrollment.stdCount <= :stdCountEnd", stdCountEnd);
}
// 查询跨院任务
Boolean crossdepart = Params.getBoolean("fake.crossdepart");
if (Boolean.TRUE.equals(crossdepart)) {
query.where("(clazz.enrollment.depart != clazz.teachDepart or clazz.enrollment.depart is null)");
} else if (Boolean.FALSE.equals(crossdepart)) {
query.where("clazz.enrollment.depart = clazz.teachDepart");
}
Boolean guapai = Params.getBoolean("fake.guapai");
if (Boolean.TRUE.equals(guapai)) {
query.where("exists (select tag.id from clazz.tags tag where tag.id=:guaPai)",
ClazzTag.PredefinedTags.GUAPAI.getId());
} else if (Boolean.FALSE.equals(guapai)) {
query.where("not exists (select tag.id from clazz.tags tag where tag.id=:guaPai)",
ClazzTag.PredefinedTags.GUAPAI.getId());
}
String squadName = Params.get("fake.squad.name");
if (Strings.isNotBlank(squadName)) {
OqlBuilder squadQuery = OqlBuilder.from(Squad.class, "squad");
squadQuery.where("squad.name like :name", "%" + squadName + "%").where(
"squad.project = :project", project);
List squades = entityDao.search(squadQuery);
if (CollectUtils.isNotEmpty(squades)) {
StringBuilder squadCondition = new StringBuilder();
squadCondition
.append("exists (")
.append("select litem.id from clazz.enrollment.restrictions lgroup join lgroup.items litem where")
.append(" litem.meta=").append(ClazzRestrictionMeta.Squad.getId()).append(" and ")
.append(" litem.included=true ").append(" and (");
for (Iterator iter = squades.iterator(); iter.hasNext(); ) {
Squad squad = iter.next();
squadCondition.append("locate(',").append(squad.getId().toString())
.append(",',litem.contents) > 0").append(" or litem.contents ='").append(squad.getId().toString())
.append("' ");
if (iter.hasNext()) {
squadCondition.append(" or ");
}
}
squadCondition.append(")").append(")");
query.where(squadCondition.toString());
}
}
// FIXME 查询选课人数
Integer compare = Params.getInt("electInfo.electCountCompare");
if (null != compare) {
String op = "";
if (compare.intValue() == 0) {
op = "=";
} else if (compare.intValue() < 0) {
op = "<";
} else {
op = ">";
}
query.where("clazz.enrollment.stdCount " + op + " clazz.enrollment.capacity");
}
/** 排考相关查询条件 */
Integer examTypeId = Params.getInt("examType.id");
// 需要补考的教学任务
if (examTypeId != null && examTypeId.equals(ExamType.DELAY)) query.where("1=0");
if (examTypeId != null && examTypeId.equals(ExamType.MAKEUP)) {
query
.where(
"exists (from "
+ ExamTaker.class.getName()
+ " examTaker where examTaker.examType.id in (:examTypeIds)"
+ " and examTaker.clazz.project.id = :projectId and examTaker.semester.id = :semesterId and examTaker.clazz = clazz)",
new Integer[]{ExamType.MAKEUP, ExamType.DELAY}, Params.getInt("clazz.project.id"),
semesterId);
}
// Integer isExamArrangeComplete = Params.getInt("isExamArrangeComplete"); // 0:未安排,1:已安排,2:全部
// if (isExamArrangeComplete == null) {
// isExamArrangeComplete = 3;
// }
// // 查找已安排了考试的教学任务
// if (isExamArrangeComplete == 1) {
// String activitySubQuery = "exists(from " + ExamActivity.class.getName()
// + "examActivity left join examActivity.examRooms examRoom where examActivity.clazz=clazz and "
// + "examActivity.examType.id=:examTypeId";
// List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy