package com.estate.expert.admin.service.impl; import com.estate.expert.admin.domain.common.request.PageReq; import com.estate.expert.admin.domain.common.response.PageResp; import com.estate.expert.admin.domain.exam.task.check.CheckTaskRepository; import com.estate.expert.admin.domain.exam.task.check.CheckTaskState; import com.estate.expert.admin.domain.plan.*; import com.estate.expert.admin.domain.user.User; import com.estate.expert.admin.enums.BizResultCode; import com.estate.expert.admin.service.PlanService; import com.estate.expert.admin.service.common.CrudServiceBaseImpl; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import javax.annotation.Resource; import java.time.LocalDateTime; import java.util.List; /** * 计划服务实现 */ @Slf4j @Service public class PlanServiceImpl extends CrudServiceBaseImpl implements PlanService { @Resource private PlanRepository planRepository; @Resource private CheckTaskRepository checkTaskRepository; @Override public void updateState(Integer id) { var plan = get(id); if (plan == null) { return; } var checkTasks = checkTaskRepository.findAllByPlanIdAndDeleted(id, false); // 新建计划,无任务,无需更新状态 if (CollectionUtils.isEmpty(checkTasks) && plan.getState() == PlanState.PUBLISHED) { return; } else { // 有任务未完成 var executing = checkTasks.stream().anyMatch(task -> task.getState() != CheckTaskState.Finished && task.getState() != CheckTaskState.Reviewed); if (executing) { plan.setState(PlanState.EXECUTING); } else { plan.setState(PlanState.COMPLETE); plan.setCompletedTime(LocalDateTime.now()); } planRepository.save(plan); } } public PageResp list(String keyword, List planTypes, List planStates, List clientIds, Integer creatorId, PageReq pageReq) { var page = planRepository.findPlans(keyword, CollectionUtils.isEmpty(planTypes) ? null : planTypes, CollectionUtils.isEmpty(planStates) ? null : planStates, CollectionUtils.isEmpty(clientIds) ? null : clientIds, creatorId, null, pageReq.toPageRequest()); return PageResp.of(page, toDto(page.getContent())); } public List list(Integer clientId) { return planRepository.findAllByClientId(clientId); } @Override public List list(String keyword, List planType, List planStates, List clientIds, Integer creatorId) { return planRepository.findPlans(keyword, CollectionUtils.isEmpty(planType) ? null : planType, CollectionUtils.isEmpty(planStates) ? null : planStates, CollectionUtils.isEmpty(clientIds) ? null : clientIds, creatorId, null); } @Override public PageResp myPlans(Integer userId, String keyword, List planTypes, List planStates, List clientIds, PageReq pageReq) { var myPlanIds = checkTaskRepository.listMyTaskPlans(userId).stream().distinct().toList(); if(CollectionUtils.isEmpty(myPlanIds)) { return PageResp.empty(); } var page = planRepository.myPlans(keyword, CollectionUtils.isEmpty(planTypes) ? null : planTypes, CollectionUtils.isEmpty(planStates) ? null : planStates, CollectionUtils.isEmpty(clientIds) ? null : clientIds, userId, myPlanIds, pageReq.toPageRequest()); return PageResp.of(page, toDto(page.getContent())); } @Override public Plan add(User creator, Plan entity) { entity.setState(PlanState.PUBLISHED); BizResultCode.PLAN_SYSTEM_MODEL_NOT_EXIST.check(entity.getSystemModel() == null); BizResultCode.PLAN_DEVICE_MODEL_NOT_EXIST.check(entity.getDeviceModel() == null); BizResultCode.PlAN_MODEL_EXPERT_SHOULD_PAIRED.check(!entity.getSystemModel().equals(entity.getSystemFuzzyExpert().getModel())); BizResultCode.PlAN_MODEL_EXPERT_SHOULD_PAIRED.check(!entity.getSystemModel().equals(entity.getSystemScoreExpert().getModel())); BizResultCode.PlAN_MODEL_EXPERT_SHOULD_PAIRED.check(!entity.getDeviceModel().equals(entity.getDeviceFuzzyExpert().getModel())); BizResultCode.PlAN_MODEL_EXPERT_SHOULD_PAIRED.check(!entity.getDeviceModel().equals(entity.getDeviceScoreExpert().getModel())); return super.add(creator, entity); } }