package com.gkhy.assess.system.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.assess.common.exception.ApiException;
import com.gkhy.assess.system.domain.AssEstimatePlan;
import com.gkhy.assess.system.domain.AssEstimateSchedule;
import com.gkhy.assess.system.domain.AssPlanPerson;
import com.gkhy.assess.system.enums.PlayRoleEnum;
import com.gkhy.assess.system.enums.ReportProgressEnum;
import com.gkhy.assess.system.mapper.AssEstimatePlanMapper;
import com.gkhy.assess.system.service.*;
import com.gkhy.assess.system.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
*
* 评价项目计划表 服务实现类
*
*
* @author kzy
* @since 2023-12-12 10:46:54
*/
@Service
public class AssEstimatePlanServiceImpl extends ServiceImpl implements AssEstimatePlanService {
@Autowired
private AssProjectService projectService;
@Autowired
private AssEstimateScheduleService estimateScheduleService;
@Autowired
private AssPlanPersonService planPersonService;
@Autowired
private AssDeviceService deviceService;
@Override
@Transactional(rollbackFor = RuntimeException.class)
public int addEstimatePlan(AssEstimatePlan estimatePlan) {
Long projectId=estimatePlan.getProjectId();
projectService.checkUserAllowed(projectId);
checkEstimatePlanCount(projectId);
//校验项目状态
projectService.checkReportProgress(projectId, ReportProgressEnum.ESTIMATE_TASK);
//校验是否设置了组长
checkMajorPlanPersonCount(projectId);
estimatePlan.setCreateBy(ShiroUtils.getSysUser().getUsername());
int row=baseMapper.insert(estimatePlan);
if(row>0) {
//更新项目状态
projectService.changeReportProgress(projectId,ReportProgressEnum.ESTIMATE_PLAN);
//todo 机构负责人
planPersonService.addPlanPerson(new AssPlanPerson().setPersonId(estimatePlan.getAuserId())
.setPlayRole(PlayRoleEnum.ANGENCY_LEADER.getCode())
.setProjectId(projectId));
}
//todo 新增评价日程安排
estimateScheduleService.saveBatch(estimatePlan.getEstimateSchedules());
return row;
}
public void checkEstimatePlanCount(Long projectId){
//校验项目下评价计划数量
int contractCount= baseMapper.getCountByProjectId(projectId);
if(contractCount>0){
throw new ApiException("项目下已存在评价计划信息");
}
}
/**
* 校验项目组成员是否设置了组长
* @param projectId
*/
public void checkMajorPlanPersonCount(Long projectId){
int count=planPersonService.getMajorPlanPersonCountByProject(projectId);
if(count<1){
throw new ApiException("项目组成员中必须有一个人设置为组长");
}
}
@Override
@Transactional(rollbackFor = RuntimeException.class)
public int editEstimatePlan(AssEstimatePlan estimatePlan) {
projectService.checkUserAllowed(estimatePlan.getProjectId());
checkMajorPlanPersonCount(estimatePlan.getProjectId());
AssEstimatePlan oldPlan=getById(estimatePlan.getId());
estimatePlan.setUpdateBy(ShiroUtils.getSysUser().getUsername());
int row=baseMapper.updateById(estimatePlan);
List estimateScheduleList=estimatePlan.getEstimateSchedules();
if(estimateScheduleList!=null&&estimateScheduleList.size()>0){
estimateScheduleService.updateBatchById(estimateScheduleList);
}
if(row>0) {
if (estimatePlan.getAuserId() != null && !estimatePlan.getAuserId().equals(oldPlan.getAuserId())) {
// planPersonService.checkPersonUnique(new AssPlanPerson().setPersonId(estimatePlan.getAuserId()).setProjectId(oldPlan.getProjectId()));
//todo 删除旧的项目成员
planPersonService.deletePlanPersonByPersonId(oldPlan.getAuserId(),estimatePlan.getProjectId(),PlayRoleEnum.ANGENCY_LEADER.getCode());
//todo 新增新的项目成员
planPersonService.addPlanPerson(new AssPlanPerson().setPersonId(estimatePlan.getAuserId())
.setPlayRole(PlayRoleEnum.ANGENCY_LEADER.getCode())
.setProjectId(oldPlan.getProjectId()));
}
}
return row;
}
@Override
public AssEstimatePlan getEstimatePlanByProjectId(Long projectId) {
projectService.checkUserAllowed(projectId);
AssEstimatePlan assEstimatePlan= baseMapper.getEstimatePlanByProjectId(projectId);
assEstimatePlan.setPlanPersons(planPersonService.getByProjectId(projectId));
assEstimatePlan.setEstimateSchedules(estimateScheduleService.getByProjectId(projectId));
assEstimatePlan.setDevices(deviceService.getByProjectId(projectId));
return assEstimatePlan;
}
@Override
public AssEstimatePlan getEstimatePlanById(Long estimatePlanId) {
AssEstimatePlan estimatePlan= baseMapper.getEstimatePlanById(estimatePlanId);
projectService.checkUserAllowed(estimatePlan.getProjectId());
return estimatePlan;
}
}