This commit is contained in:
2026-07-07 15:16:11 +08:00
15 changed files with 919 additions and 51 deletions
@@ -307,6 +307,9 @@ public class SiteCugApplyController {
if (item == null || StrUtil.hasBlank(item.getReserveStartTime(), item.getReserveEndTime())) {
continue;
}
if (isOccupiedByCurrentUser(item)) {
continue;
}
DateTime reserveStart = DateUtil.parseDateTime(item.getReserveStartTime());
DateTime reserveEnd = DateUtil.parseDateTime(item.getReserveEndTime());
if (!reserveEnd.isAfter(reserveStart)) {
@@ -389,6 +392,13 @@ public class SiteCugApplyController {
}
}
// 当前登录人占用了该预约时,该预约占用的时间块对当前登录人放开,对其他人仍保持锁定
private boolean isOccupiedByCurrentUser(SiteCugApply apply) {
return apply != null
&& StrUtil.isNotBlank(apply.getOccupyUserId())
&& StrUtil.equals(apply.getOccupyUserId(), SecurityUtil.getUserId());
}
private boolean intersectsAny(int start, int end, List<int[]> ranges) {
for (int[] range : ranges) {
if (range != null && start < range[1] && end > range[0]) {
@@ -821,5 +831,22 @@ public class SiteCugApplyController {
for (ProcessTask task : doingTaskList) {
flowEngine.executeProcessTask(task.getId(), SecurityUtil.getUserId(), args);
}
if (applyService.shouldDirectFinishOccupiedApply(apply)) {
directFinishOccupiedApply(instance, args);
}
}
// 占用人提交与占用时间交叉的预约时,复用已有流程引擎自动结束后续审核任务
private void directFinishOccupiedApply(ProcessInstance instance, Dict args) {
List<ProcessTask> doingTaskList = flowEngine.processTaskService().getDoingTaskList(instance.getId(), null);
if (doingTaskList.isEmpty()) {
return;
}
Dict directArgs = args.clone();
directArgs.set(FlowConst.SUBMIT_TYPE, ProcessSubmitTypeEnum.AGREE.getCode());
directArgs.set(FlowConst.APPROVAL_COMMENT, "占用预约自动通过");
for (ProcessTask task : doingTaskList) {
flowEngine.executeAndJumpToEnd(task.getId(), FlowConst.AUTO_ID, directArgs);
}
}
}
@@ -105,6 +105,13 @@ public class SiteCugManageController {
if ((fullDayOpenHour == null || fullDayOpenHour.isEmpty()) && info.getReserveTimeType() == 2 && info.getOpenHours() != null && !info.getOpenHours().isEmpty()) {
fullDayOpenHour = info.getOpenHours().get(0);
}
// 场地预约次数限制作为后续提交校验的依据,保存场地时必须配置为正整数
if (info.getUnionYearReserveLimit() == null || info.getUnionYearReserveLimit() <= 0) {
return Result.error("分工会每年预约次数必须大于0");
}
if (info.getClubWeekReserveLimit() == null || info.getClubWeekReserveLimit() <= 0) {
return Result.error("社团每周预约次数必须大于0");
}
if (info.getReserveTimeType() == 1) {
if (segmentedOpenHours == null || segmentedOpenHours.isEmpty()) {
return Result.error("Please add at least one booking slot");
@@ -1,6 +1,7 @@
package com.budwk.app.zhgh.dayofficework.siteCug.controller;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.StrUtil;
import com.budwk.app.base.page.Pagination;
import com.budwk.app.base.result.Result;
import com.budwk.app.flow.engine.FlowEngine;
@@ -24,6 +25,7 @@ import org.nutz.mvc.annotation.Param;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
@IocBean
@Ok("json:full")
@@ -87,6 +89,28 @@ public class SiteCugRecordController {
return Result.success();
}
@At
@ApiOperation("占用预约记录")
@Aop(TransAop.READ_COMMITTED)
@SaCheckPermission("siteCug.record")
public Result occupy(@Param("id") String id, @Param("message") String message) {
Map<Boolean, String> result = applyService.occupyRecord(id, message, false);
return result.containsKey(false) ? Result.error(result.get(false)) : Result.success(result.get(true));
}
@At
@ApiOperation("批量占用预约记录")
@Aop(TransAop.READ_COMMITTED)
@SaCheckPermission("siteCug.record")
public Result occupyBatch(@Param("ids") String ids, @Param("message") String message) {
if (StrUtil.isBlank(ids)) {
return Result.error("请选择需要占用的预约");
}
List<String> idList = StrUtil.split(ids, ",").stream().filter(StrUtil::isNotBlank).toList();
Map<Boolean, String> result = applyService.occupyRecords(idList, message, false);
return result.containsKey(false) ? Result.error(result.get(false)) : Result.success(result.get(true));
}
@At
@Ok("void")
@ApiOperation("导出预约记录")
@@ -110,5 +110,15 @@ public class SiteCugApply extends BaseModel {
@ColDefine(type = ColType.VARCHAR, width = 200)
private String backOption;
@Column
@Comment("占用人ID")
@ColDefine(type = ColType.VARCHAR, width = 32)
private String occupyUserId;
@Column
@Comment("占用通知消息")
@ColDefine(type = ColType.VARCHAR, width = 1000)
private String occupyMessage;
private String yearlyReserveEndDate;
}
@@ -83,6 +83,18 @@ public class SiteCugInfo extends BaseModel {
@ColDefine(type = ColType.VARCHAR, width = 20)
private String termEndDate;
@Column
@Comment("分工会每年预约次数")
@ColDefine(type = ColType.INT)
@Default("2")
private Integer unionYearReserveLimit;
@Column
@Comment("社团每周预约次数")
@ColDefine(type = ColType.INT)
@Default("2")
private Integer clubWeekReserveLimit;
@Column
@Comment("场地的禁用时间")
@ColDefine(type = ColType.MYSQL_JSON)
@@ -7,6 +7,7 @@ import org.nutz.dao.Cnd;
import org.nutz.dao.sql.Sql;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
public interface SiteCugApplyService extends BaseService<SiteCugApply> {
@@ -21,4 +22,13 @@ public interface SiteCugApplyService extends BaseService<SiteCugApply> {
// 场馆预约查询页的导出逻辑统一放在 service,controller 只负责接收请求
void exportRecord(SiteCugRecordPageForm pageForm, HttpServletResponse response);
// 占用单条预约记录,保存占用人和通知内容;真实发送消息代码当前保留为注释,便于测试流程
Map<Boolean, String> occupyRecord(String id, String message, boolean sendMsg);
// 批量占用预约记录,先统一校验再保存,避免出现部分记录已占用、部分记录失败的状态
Map<Boolean, String> occupyRecords(List<String> ids, String message, boolean sendMsg);
// 判断当前预约是否命中当前登录人的占用时间段,命中后提交预约可自动通过审核
boolean shouldDirectFinishOccupiedApply(SiteCugApply apply);
}
@@ -10,10 +10,10 @@ import cn.hutool.core.util.StrUtil;
import com.budwk.app.base.service.impl.BaseServiceImpl;
import com.budwk.app.base.utils.CommonDownloadUtil;
import com.budwk.app.base.utils.PageUtil;
import com.budwk.app.flow.entity.ProcessInstance;
import com.budwk.app.flow.enums.ProcessInstanceStateEnum;
import com.budwk.app.sys.models.SysHoliday;
import com.budwk.app.sys.models.Sys_union;
import com.budwk.app.sys.services.SysMsgService;
import com.budwk.app.web.commons.auth.utils.SecurityUtil;
import com.budwk.app.zhgh.club.model.SysClub;
import com.budwk.app.zhgh.club.service.SysClubService;
@@ -46,6 +46,8 @@ public class SiteCugApplyServiceImpl extends BaseServiceImpl<SiteCugApply> imple
@Inject
private SysClubService sysClubService;
@Inject
private SysMsgService sysMsgService;
public SiteCugApplyServiceImpl(Dao dao) {
super(dao);
@@ -78,6 +80,10 @@ public class SiteCugApplyServiceImpl extends BaseServiceImpl<SiteCugApply> imple
if (!Boolean.TRUE.equals(siteInfo.getState())) {
return Map.of(false, "该场地未开启预约");
}
Map<Boolean, String> termDateRangeValidate = validateTermDateRange(apply, siteInfo);
if (termDateRangeValidate.containsKey(false)) {
return termDateRangeValidate;
}
if (apply.getJoinCount() == null || apply.getJoinCount() <= 0) {
return Map.of(false, "预约人数必须大于0");
}
@@ -96,6 +102,10 @@ public class SiteCugApplyServiceImpl extends BaseServiceImpl<SiteCugApply> imple
if (intersectsDisabledTime(siteInfo, apply.getReserveStartTime(), apply.getReserveEndTime())) {
return Map.of(false, "预约时间落在禁用时间内");
}
Map<Boolean, String> reserveLimitValidate = validateReserveLimit(apply, siteInfo);
if (reserveLimitValidate.containsKey(false)) {
return reserveLimitValidate;
}
Sql sql = Sqls.create("""
select
@@ -108,8 +118,7 @@ public class SiteCugApplyServiceImpl extends BaseServiceImpl<SiteCugApply> imple
Cnd cnd = Cnd.NEW();
cnd.and(SiteCugApply::getSiteId, "=", apply.getSiteId());
cnd.andEX("sa.id", "!=", apply.getId());
List<Integer> stateList = List.of(ProcessInstanceStateEnum.DOING.getCode(), ProcessInstanceStateEnum.FINISHED.getCode(), ProcessInstanceStateEnum.PENDING.getCode());
cnd.and(ProcessInstance::getState, "in", stateList);
cnd.and("ins.state", "in", buildEffectiveProcessStateList());
sql.setCondition(cnd);
List<SiteCugApply> applyList = listEntity(sql);
@@ -118,6 +127,9 @@ public class SiteCugApplyServiceImpl extends BaseServiceImpl<SiteCugApply> imple
boolean overlap = DateUtil.parseDateTime(item.getReserveStartTime()).isBefore(DateUtil.parseDateTime(apply.getReserveEndTime()))
&& DateUtil.parseDateTime(item.getReserveEndTime()).isAfter(DateUtil.parseDateTime(apply.getReserveStartTime()));
if (overlap) {
if (isOccupiedByCurrentUser(item)) {
continue;
}
if (StrUtil.isBlank(apply.getId()) && StrUtil.equals(item.getApplyUserId(), SecurityUtil.getUserId())) {
return Map.of(false, "该时间段您已存在预约");
}
@@ -127,6 +139,236 @@ public class SiteCugApplyServiceImpl extends BaseServiceImpl<SiteCugApply> imple
return Map.of(true, "");
}
// 分工会预约必须落在后台配置的本学期时间内,批量预约逐条复用该校验防止接口越权提交学期外日期
private Map<Boolean, String> validateTermDateRange(SiteCugApply apply, SiteCugInfo siteInfo) {
if (!StrUtil.equals(apply.getReserveType(), "union")
|| StrUtil.hasBlank(siteInfo.getTermStartDate(), siteInfo.getTermEndDate())) {
return Map.of(true, "");
}
try {
DateTime reserveStart = DateUtil.parseDateTime(apply.getReserveStartTime());
DateTime reserveEnd = DateUtil.parseDateTime(apply.getReserveEndTime());
Date termStart = DateUtil.beginOfDay(DateUtil.parseDate(siteInfo.getTermStartDate()));
Date termEnd = DateUtil.endOfDay(DateUtil.parseDate(siteInfo.getTermEndDate()));
if (reserveStart.isBefore(termStart) || reserveEnd.isAfter(termEnd)) {
return Map.of(false, "预约时间必须在本学期时间范围内");
}
return Map.of(true, "");
} catch (Exception e) {
return Map.of(false, "本学期时间配置有误,请联系管理员处理");
}
}
@Override
public Map<Boolean, String> occupyRecord(String id, String message, boolean sendMsg) {
if (StrUtil.isBlank(message)) {
return Map.of(false, "请填写通知消息");
}
SiteCugApply apply = fetch(id);
if (apply == null) {
return Map.of(false, "记录不存在");
}
Map<Boolean, String> validate = validateOccupyApply(apply);
if (validate.containsKey(false)) {
return validate;
}
saveOccupyApply(apply, message, sendMsg);
return Map.of(true, "占用成功");
}
// 已通过校验后统一保存占用信息;测试阶段真实发送消息代码保留注释
private void saveOccupyApply(SiteCugApply apply, String message, boolean sendMsg) {
// 保存占用人和通知内容,后续预约校验据此只放行当前占用人
apply.setOccupyUserId(SecurityUtil.getUserId());
apply.setOccupyMessage(message);
updateIgnoreNull(apply);
// 测试占用流程时先不真实发送消息,保留日志用于确认保存和跳转链路是否正常
log.info("场地预约占用测试消息,applyId={}receiverLoginName={}message={}", apply.getId(), apply.getApplyLoginName(), message);
if (sendMsg) {
// sysMsgService.sendMsgInSys(List.of(apply.getApplyLoginName()), "场地预约占用通知", message, SecurityUtil.getUserId());
}
}
@Override
public Map<Boolean, String> occupyRecords(List<String> ids, String message, boolean sendMsg) {
if (ids == null || ids.isEmpty()) {
return Map.of(false, "请选择需要占用的预约");
}
if (StrUtil.isBlank(message)) {
return Map.of(false, "请填写通知消息");
}
List<SiteCugApply> applyList = new ArrayList<>();
for (String id : ids) {
SiteCugApply apply = fetch(id);
if (apply == null) {
return Map.of(false, "记录不存在");
}
Map<Boolean, String> validate = validateOccupyApply(apply);
if (validate.containsKey(false)) {
return validate;
}
applyList.add(apply);
}
for (SiteCugApply apply : applyList) {
saveOccupyApply(apply, message, sendMsg);
}
return Map.of(true, "占用成功");
}
// 占用前统一校验预约状态,避免单条和批量接口出现不同判断
private Map<Boolean, String> validateOccupyApply(SiteCugApply apply) {
if (StrUtil.isBlank(apply.getApplyLoginName())) {
return Map.of(false, "预约人工号为空,无法发送消息");
}
if (StrUtil.isBlank(apply.getReserveStartTime()) || !DateUtil.parseDateTime(apply.getReserveStartTime()).isAfter(DateUtil.date())) {
return Map.of(false, "预约已开始,不能占用");
}
Integer state = queryInstanceState(apply.getId());
if (state == null || state != ProcessInstanceStateEnum.FINISHED.getCode()) {
return Map.of(false, "仅可占用已通过且未开始的预约记录");
}
if (StrUtil.isNotBlank(apply.getOccupyUserId()) && !StrUtil.equals(apply.getOccupyUserId(), SecurityUtil.getUserId())) {
return Map.of(false, "该预约已被其他人占用");
}
return Map.of(true, "");
}
private Integer queryInstanceState(String id) {
Sql sql = Sqls.create("""
SELECT
ins.state AS instanceState
FROM
site_cug_apply info
LEFT JOIN wf_process_instance ins ON ins.businessNo = info.id
WHERE info.id = @id
""");
sql.params().set("id", id);
sql.setCallback(Sqls.callback.maps());
dao().execute(sql);
List<NutMap> list = sql.getList(NutMap.class);
return list != null && !list.isEmpty() ? list.get(0).getInt("instanceState") : null;
}
// 当前登录人占用了该记录时,允许其再次提交与该记录有交叉的预约
private boolean isOccupiedByCurrentUser(SiteCugApply apply) {
return apply != null
&& StrUtil.isNotBlank(apply.getOccupyUserId())
&& StrUtil.equals(apply.getOccupyUserId(), SecurityUtil.getUserId());
}
@Override
public boolean shouldDirectFinishOccupiedApply(SiteCugApply apply) {
if (apply == null || StrUtil.hasBlank(apply.getSiteId(), apply.getReserveStartTime(), apply.getReserveEndTime())) {
return false;
}
Sql sql = Sqls.create("""
select
sa.*
from
site_cug_apply sa
left join wf_process_instance ins on ins.businessNo = sa.id
$condition
""");
Cnd cnd = Cnd.NEW();
cnd.and("sa.siteId", "=", apply.getSiteId());
cnd.andEX("sa.id", "!=", apply.getId());
cnd.and("sa.occupyUserId", "=", SecurityUtil.getUserId());
cnd.and("ins.state", "=", ProcessInstanceStateEnum.FINISHED.getCode());
sql.setCondition(cnd);
List<SiteCugApply> applyList = listEntity(sql);
// 只要新预约时间和当前登录人占用的已通过预约有交叉,就允许本次预约免审通过
return applyList.stream().anyMatch(item ->
DateUtil.parseDateTime(item.getReserveStartTime()).isBefore(DateUtil.parseDateTime(apply.getReserveEndTime()))
&& DateUtil.parseDateTime(item.getReserveEndTime()).isAfter(DateUtil.parseDateTime(apply.getReserveStartTime()))
);
}
// 按预约主体统计有效预约次数,批量预约同一个批次号只计一次,避免一次批量提交占用多次额度
private Map<Boolean, String> validateReserveLimit(SiteCugApply apply, SiteCugInfo siteInfo) {
if (StrUtil.equals(apply.getReserveType(), "union")) {
if (siteInfo.getUnionYearReserveLimit() == null || siteInfo.getUnionYearReserveLimit() <= 0) {
return Map.of(false, "请先配置分工会每年预约次数");
}
String[] range = buildYearRange(apply.getReserveStartTime());
long count = countEffectiveReserveTimes(apply, "sa.applyUnionId", apply.getApplyUnionId(), range[0], range[1]);
if (count >= siteInfo.getUnionYearReserveLimit()) {
return Map.of(false, "该分工会本年度预约次数已达" + siteInfo.getUnionYearReserveLimit() + "次,不能继续预约");
}
}
if (StrUtil.equals(apply.getReserveType(), "club")) {
if (siteInfo.getClubWeekReserveLimit() == null || siteInfo.getClubWeekReserveLimit() <= 0) {
return Map.of(false, "请先配置社团每周预约次数");
}
String[] range = buildWeekRange(apply.getReserveStartTime());
long count = countEffectiveReserveTimes(apply, "sa.clubId", apply.getClubId(), range[0], range[1]);
if (count >= siteInfo.getClubWeekReserveLimit()) {
return Map.of(false, "该协会本周预约次数已达" + siteInfo.getClubWeekReserveLimit() + "次,不能继续预约");
}
}
return Map.of(true, "");
}
// 分工会每年次数按预约开始时间所在自然年统计
private String[] buildYearRange(String reserveStartTime) {
DateTime reserveStart = DateUtil.parseDateTime(reserveStartTime);
String year = String.valueOf(reserveStart.year());
return new String[]{year + "-01-01 00:00:00", year + "-12-31 23:59:59"};
}
// 协会每周次数按自然周统计,周一为开始、周日为结束
private String[] buildWeekRange(String reserveStartTime) {
DateTime reserveStart = DateUtil.parseDateTime(reserveStartTime);
int week = reserveStart.dayOfWeek() - 1;
int offsetToMonday = week == 0 ? -6 : 1 - week;
DateTime weekStart = DateUtil.offsetDay(reserveStart, offsetToMonday);
DateTime weekEnd = DateUtil.offsetDay(weekStart, 6);
return new String[]{DateUtil.formatDate(weekStart) + " 00:00:00", DateUtil.formatDate(weekEnd) + " 23:59:59"};
}
// 统计预约主体在指定时间范围内的有效预约次数,所有场地合计,不按场地过滤
private long countEffectiveReserveTimes(SiteCugApply apply, String targetField, String targetId, String rangeStart, String rangeEnd) {
if (StrUtil.isBlank(targetId)) {
return 0;
}
Sql sql = Sqls.create("""
select
sa.*
from
site_cug_apply sa
left join wf_process_instance ins on ins.businessNo = sa.id
$condition
""");
Cnd cnd = Cnd.NEW();
cnd.andEX("sa.reserveType", "=", apply.getReserveType());
cnd.andEX(targetField, "=", targetId);
cnd.andEX("sa.reserveStartTime", ">=", rangeStart);
cnd.andEX("sa.reserveStartTime", "<=", rangeEnd);
if (StrUtil.isNotBlank(apply.getId())) {
cnd.andEX("sa.id", "!=", apply.getId());
}
cnd.and("ins.state", "in", buildEffectiveProcessStateList());
sql.setCondition(cnd);
List<SiteCugApply> applyList = listEntity(sql);
return applyList.stream()
.map(this::buildReserveCountKey)
.distinct()
.count();
}
// 只有待审核、办理中、已通过的流程占用预约次数,驳回记录不占额度
private List<Integer> buildEffectiveProcessStateList() {
return List.of(ProcessInstanceStateEnum.DOING.getCode(), ProcessInstanceStateEnum.FINISHED.getCode(), ProcessInstanceStateEnum.PENDING.getCode());
}
// 批量预约同一批次号只算一次,普通预约按单条申请计算
private String buildReserveCountKey(SiteCugApply apply) {
if (apply != null && StrUtil.isNotBlank(apply.getBackOption()) && apply.getBackOption().startsWith("YEARLY_BATCH:")) {
return apply.getBackOption();
}
return apply == null ? "" : apply.getId();
}
@Override
public Sql buildRecordSql() {
// 场馆预约查询页和导出页共用同一套返回字段,统一在 service 中维护,避免 controller 重复拼 SQL
@@ -135,6 +377,7 @@ public class SiteCugApplyServiceImpl extends BaseServiceImpl<SiteCugApply> imple
info.*,
si.name AS siteName,
ins.id AS instanceId,
ins.state AS instanceState,
ins.processDefineId instanceProcessDefineId,
CASE WHEN info.backOption LIKE 'YEARLY_BATCH:%' THEN 1 ELSE 0 END AS yearlyBatch,
CASE WHEN info.reserveType = 'club' THEN '协会预约' ELSE '分工会预约' END AS reserveTypeName,
@@ -315,7 +558,3 @@ public class SiteCugApplyServiceImpl extends BaseServiceImpl<SiteCugApply> imple
return Map.of(false, "预约类型不合法");
}
}