|
|
|
@ -1,24 +1,29 @@ |
|
|
|
package com.zdxt.enforcementcode.service.impl; |
|
|
|
|
|
|
|
import org.dromara.common.core.domain.R; |
|
|
|
import org.dromara.common.core.utils.MapstructUtils; |
|
|
|
import org.dromara.common.core.utils.StringUtils; |
|
|
|
import org.dromara.common.mybatis.core.page.TableDataInfo; |
|
|
|
import org.dromara.common.mybatis.core.page.PageQuery; |
|
|
|
import org.dromara.common.satoken.utils.LoginHelper; |
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
import com.zdxt.enforcementcode.domain.bo.EfcodeCpReserveBo; |
|
|
|
import com.zdxt.enforcementcode.domain.vo.EfcodeCpReserveVo; |
|
|
|
import com.zdxt.enforcementcode.domain.EfcodeCpReserve; |
|
|
|
import com.zdxt.enforcementcode.domain.EfcodeCpPlan; |
|
|
|
import com.zdxt.enforcementcode.mapper.EfcodeCpReserveMapper; |
|
|
|
import com.zdxt.enforcementcode.mapper.EfcodeCpPlanMapper; |
|
|
|
import com.zdxt.enforcementcode.service.IEfcodeCpReserveService; |
|
|
|
import com.zdxt.enforcementcode.service.IEfcodeCpSettingService; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Collection; |
|
|
|
import java.util.*; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
/** |
|
|
|
* 行政执法检查计划-预约Service业务层处理 |
|
|
|
@ -32,6 +37,8 @@ import java.util.Collection; |
|
|
|
public class EfcodeCpReserveServiceImpl implements IEfcodeCpReserveService { |
|
|
|
|
|
|
|
private final EfcodeCpReserveMapper baseMapper; |
|
|
|
private final EfcodeCpPlanMapper cpPlanMapper; |
|
|
|
private final IEfcodeCpSettingService cpSettingService; |
|
|
|
|
|
|
|
/** |
|
|
|
* 查询行政执法检查计划-预约 |
|
|
|
@ -82,7 +89,7 @@ public class EfcodeCpReserveServiceImpl implements IEfcodeCpReserveService { |
|
|
|
lqw.eq(StringUtils.isNotBlank(bo.getOpMessage()), EfcodeCpReserve::getOpMessage, bo.getOpMessage()); |
|
|
|
lqw.eq(bo.getOpSetDate() != null, EfcodeCpReserve::getOpSetDate, bo.getOpSetDate()); |
|
|
|
lqw.eq(bo.getOpSetTime() != null, EfcodeCpReserve::getOpSetTime, bo.getOpSetTime()); |
|
|
|
lqw.eq(bo.getFrom() != null, EfcodeCpReserve::getFrom, bo.getFrom()); |
|
|
|
lqw.eq(bo.getResFrom() != null, EfcodeCpReserve::getResFrom, bo.getResFrom()); |
|
|
|
return lqw; |
|
|
|
} |
|
|
|
|
|
|
|
@ -137,4 +144,151 @@ public class EfcodeCpReserveServiceImpl implements IEfcodeCpReserveService { |
|
|
|
} |
|
|
|
return baseMapper.deleteByIds(ids) > 0; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 预约前置检查 |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public R<Void> checkCanReserve(String targetPlanId) { |
|
|
|
Long currentUserId = LoginHelper.getUserId(); |
|
|
|
// 1. 查询目标计划
|
|
|
|
EfcodeCpPlan targetPlan = cpPlanMapper.selectById(targetPlanId); |
|
|
|
if (targetPlan == null) { |
|
|
|
return R.fail("目标计划不存在"); |
|
|
|
} |
|
|
|
// 2. 判断当前用户是否有同一时间+同一时段+同一企业的计划
|
|
|
|
EfcodeCpPlan myPlan = findMyMatchingPlan(targetPlan, currentUserId); |
|
|
|
if (myPlan == null) { |
|
|
|
return R.fail("请先添加我的计划再进行预约"); |
|
|
|
} |
|
|
|
// 3. 判断 plan_group_id 人数是否已达上限
|
|
|
|
String planGroupId = findExistingPlanGroupId(targetPlanId); |
|
|
|
if (planGroupId != null) { |
|
|
|
int maxUnionUnit = cpSettingService.getMaxUnionUnit(); |
|
|
|
long activeCount = countActiveReserves(planGroupId); |
|
|
|
if (activeCount >= maxUnionUnit) { |
|
|
|
return R.fail("该计划的预约已经到达最大数量(" + maxUnionUnit + "),无法参与预约"); |
|
|
|
} |
|
|
|
// 4. 判断当前用户是否已经预约过
|
|
|
|
boolean alreadyReserved = checkAlreadyReserved(myPlan.getId(), planGroupId); |
|
|
|
if (alreadyReserved) { |
|
|
|
return R.fail("您已经预约过该计划"); |
|
|
|
} |
|
|
|
} |
|
|
|
return R.ok(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 执行预约 |
|
|
|
*/ |
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
@Override |
|
|
|
public R<Void> doReserve(String targetPlanId, String opMessage) { |
|
|
|
Long currentUserId = LoginHelper.getUserId(); |
|
|
|
// 1. 查询目标计划
|
|
|
|
EfcodeCpPlan targetPlan = cpPlanMapper.selectById(targetPlanId); |
|
|
|
if (targetPlan == null) { |
|
|
|
return R.fail("目标计划不存在"); |
|
|
|
} |
|
|
|
// 2. 查找当前用户匹配的计划
|
|
|
|
EfcodeCpPlan myPlan = findMyMatchingPlan(targetPlan, currentUserId); |
|
|
|
if (myPlan == null) { |
|
|
|
return R.fail("请先添加我的计划再进行预约"); |
|
|
|
} |
|
|
|
// 3. 判断目标计划在reserve表中是否已存在
|
|
|
|
String planGroupId = findExistingPlanGroupId(targetPlanId); |
|
|
|
int maxUnionUnit = cpSettingService.getMaxUnionUnit(); |
|
|
|
|
|
|
|
if (planGroupId == null) { |
|
|
|
// 不存在:插入2条记录(目标计划 + 我的计划),plan_group_id = 目标计划ID
|
|
|
|
planGroupId = targetPlanId; |
|
|
|
if (2 > maxUnionUnit) { |
|
|
|
return R.fail("该计划的预约已经到达最大数量(" + maxUnionUnit + "),无法参与预约"); |
|
|
|
} |
|
|
|
// 目标计划记录
|
|
|
|
insertReserveRecord(targetPlanId, planGroupId, "", targetPlan.getPlanDate(), targetPlan.getPlanTime(), 0L); |
|
|
|
// 我的计划记录
|
|
|
|
insertReserveRecord(myPlan.getId(), planGroupId, opMessage, targetPlan.getPlanDate(), targetPlan.getPlanTime(), 0L); |
|
|
|
} else { |
|
|
|
// 已存在:检查人数上限
|
|
|
|
long activeCount = countActiveReserves(planGroupId); |
|
|
|
if (activeCount >= maxUnionUnit) { |
|
|
|
return R.fail("该计划的预约已经到达最大数量(" + maxUnionUnit + "),无法参与预约"); |
|
|
|
} |
|
|
|
// 检查是否已经预约过
|
|
|
|
boolean alreadyReserved = checkAlreadyReserved(myPlan.getId(), planGroupId); |
|
|
|
if (alreadyReserved) { |
|
|
|
return R.fail("您已经预约过该计划"); |
|
|
|
} |
|
|
|
// 插入1条记录
|
|
|
|
insertReserveRecord(myPlan.getId(), planGroupId, opMessage, targetPlan.getPlanDate(), targetPlan.getPlanTime(), 0L); |
|
|
|
} |
|
|
|
return R.ok(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 查找当前用户同一时间+同一时段+同一企业的计划 |
|
|
|
*/ |
|
|
|
private EfcodeCpPlan findMyMatchingPlan(EfcodeCpPlan targetPlan, Long currentUserId) { |
|
|
|
LambdaQueryWrapper<EfcodeCpPlan> lqw = Wrappers.lambdaQuery(); |
|
|
|
lqw.eq(EfcodeCpPlan::getPlanDate, targetPlan.getPlanDate()); |
|
|
|
lqw.eq(EfcodeCpPlan::getPlanTime, targetPlan.getPlanTime()); |
|
|
|
lqw.eq(EfcodeCpPlan::getEnterpriseId, targetPlan.getEnterpriseId()); |
|
|
|
lqw.eq(EfcodeCpPlan::getCreateBy, currentUserId); |
|
|
|
lqw.eq(EfcodeCpPlan::getIsDelete, 0L); |
|
|
|
lqw.last("LIMIT 1"); |
|
|
|
return cpPlanMapper.selectOne(lqw); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 查找目标计划在reserve表中已存在的plan_group_id |
|
|
|
*/ |
|
|
|
private String findExistingPlanGroupId(String targetPlanId) { |
|
|
|
LambdaQueryWrapper<EfcodeCpReserve> lqw = Wrappers.lambdaQuery(); |
|
|
|
lqw.eq(EfcodeCpReserve::getPlanId, targetPlanId); |
|
|
|
lqw.last("LIMIT 1"); |
|
|
|
EfcodeCpReserve existing = baseMapper.selectOne(lqw); |
|
|
|
return existing != null ? existing.getPlanGroupId() : null; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 统计plan_group_id下状态为同意(1)或待确认(3)的记录数 |
|
|
|
*/ |
|
|
|
private long countActiveReserves(String planGroupId) { |
|
|
|
return baseMapper.selectCount( |
|
|
|
Wrappers.<EfcodeCpReserve>lambdaQuery() |
|
|
|
.eq(EfcodeCpReserve::getPlanGroupId, planGroupId) |
|
|
|
.in(EfcodeCpReserve::getOpStatus, Arrays.asList(1L, 3L)) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 检查当前用户的计划是否已在该分组中预约过 |
|
|
|
*/ |
|
|
|
private boolean checkAlreadyReserved(String myPlanId, String planGroupId) { |
|
|
|
long count = baseMapper.selectCount( |
|
|
|
Wrappers.<EfcodeCpReserve>lambdaQuery() |
|
|
|
.eq(EfcodeCpReserve::getPlanId, myPlanId) |
|
|
|
.eq(EfcodeCpReserve::getPlanGroupId, planGroupId) |
|
|
|
.in(EfcodeCpReserve::getOpStatus, Arrays.asList(1L, 3L)) |
|
|
|
); |
|
|
|
return count > 0; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 插入一条预约记录 |
|
|
|
*/ |
|
|
|
private void insertReserveRecord(String planId, String planGroupId, String opMessage, Date opSetDate, Long opSetTime, Long resFrom) { |
|
|
|
EfcodeCpReserve record = new EfcodeCpReserve(); |
|
|
|
record.setPlanId(planId); |
|
|
|
record.setPlanGroupId(planGroupId); |
|
|
|
record.setReserveTime(new Date()); |
|
|
|
record.setOpStatus(3L); // 待确认
|
|
|
|
record.setOpOrder(0L); |
|
|
|
record.setOpMessage(opMessage); |
|
|
|
record.setOpSetDate(opSetDate); |
|
|
|
record.setOpSetTime(opSetTime); |
|
|
|
record.setResFrom(resFrom); |
|
|
|
baseMapper.insert(record); |
|
|
|
} |
|
|
|
} |
|
|
|
|