package com.gkhy.assess.system.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gkhy.assess.common.enums.DeleteFlagEnum;
import com.gkhy.assess.common.enums.RequestSourceEnum;
import com.gkhy.assess.common.exception.ApiException;
import com.gkhy.assess.system.domain.AssAccessoryFile;
import com.gkhy.assess.system.domain.AssInvestigation;
import com.gkhy.assess.system.domain.AssPlanPerson;
import com.gkhy.assess.system.enums.AccessoryFileTypeEnum;
import com.gkhy.assess.system.enums.ReportProgressEnum;
import com.gkhy.assess.system.mapper.AssInvestigationMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.assess.system.service.AssAccessoryFileService;
import com.gkhy.assess.system.service.AssInvestigationService;
import com.gkhy.assess.system.service.AssProjectService;
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.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
*
* 现场勘验记录表 服务实现类
*
*
* @author kzy
* @since 2023-12-12 10:46:54
*/
@Service
public class AssInvestigationServiceImpl extends ServiceImpl implements AssInvestigationService {
@Autowired
private AssProjectService projectService;
@Autowired
private AssAccessoryFileService accessoryFileService;
@Override
@Transactional(rollbackFor = RuntimeException.class)
public Long addInvestigation(AssInvestigation investigation) {
Long projectId=investigation.getProjectId();
projectService.checkUserAllowed(projectId);
String location=investigation.getLocation();
String[] locationArray = location.split(",");
if(locationArray.length!=2){
throw new ApiException("现场勘验位置格式不正确");
}
// checkInvestigationCount(projectId);
investigation.setCreateBy(ShiroUtils.getSysUser().getUsername());
if(investigation.getIsSafetyCheck()!=null&&investigation.getIsSafetyCheck()==1){
Integer fileCount=accessoryFileService.getAccessoryFileCountByProjectId(projectId,null, AccessoryFileTypeEnum.INVESTINGATION_ATTACHMENT.getCode());
if(fileCount==0){
throw new ApiException("未上传现场安全检查表");
}
}
List accessFiles=new ArrayList<>();
accessFiles.addAll(investigation.getCompanyImages());
accessFiles.addAll(investigation.getDeviceImages());
accessFiles.addAll(investigation.getInvestingationImages());
if(accessFiles.isEmpty()){
throw new ApiException("现场勘验照片不能为空");
}
if(investigation.getInvestingationVideos()!=null&& !investigation.getInvestingationVideos().isEmpty()){
accessFiles.addAll(investigation.getInvestingationVideos());
}
if(investigation.getAssAccessoryFiles()!=null&&!investigation.getAssAccessoryFiles().isEmpty()){
accessFiles.addAll(investigation.getAssAccessoryFiles());
}
List fileIds=new ArrayList<>();
for(AssAccessoryFile accessoryFile:accessFiles){
if(accessoryFile.getId()==null){
throw new ApiException("附件或图片id不能为空");
}
fileIds.add(accessoryFile.getId());
}
baseMapper.insert(investigation);
//更新图片过程id
accessoryFileService.batchUpdateAccessoryFileProcessId(fileIds,investigation.getId());
return investigation.getId();
}
public void checkInvestigationCount(Long projectId){
//校验项目下勘验记录数量
int count= baseMapper.getCountByProjectId(projectId);
if(count>0){
throw new ApiException("项目下已存在现场勘验记录");
}
}
@Override
@Transactional(rollbackFor = RuntimeException.class)
public int editInvestigation(AssInvestigation investigation) {
if(investigation.getId()==null){
throw new ApiException("现场勘验记录id不能为空!");
}
projectService.checkUserAllowed(investigation.getProjectId());
String location=investigation.getLocation();
String[] locationArray = location.split(",");
if(locationArray.length!=2){
throw new ApiException("现场勘验位置格式不正确");
}
investigation.setUpdateBy(ShiroUtils.getSysUser().getUsername());
List accessFiles=new ArrayList<>();
accessFiles.addAll(investigation.getCompanyImages());
accessFiles.addAll(investigation.getDeviceImages());
accessFiles.addAll(investigation.getInvestingationImages());
if(investigation.getInvestingationVideos()!=null&& !investigation.getInvestingationVideos().isEmpty()){
accessFiles.addAll(investigation.getInvestingationVideos());
}
if(investigation.getAssAccessoryFiles()!=null&&!investigation.getAssAccessoryFiles().isEmpty()){
accessFiles.addAll(investigation.getAssAccessoryFiles());
}
List fileIds=new ArrayList<>();
for(AssAccessoryFile accessoryFile:accessFiles){
if(accessoryFile.getId()==null){
throw new ApiException("附件或图片id不能为空");
}
fileIds.add(accessoryFile.getId());
}
accessoryFileService.batchUpdateAccessoryFileProcessId(fileIds,investigation.getId());
int row=baseMapper.updateById(investigation);
return row;
}
@Override
public List getInvestigationByProjectId(Long projectId) {
projectService.checkUserAllowed(projectId);
List investigations= baseMapper.getInvestigationByProjectId(projectId);
return investigations;
}
@Override
public AssInvestigation getInvestigationById(Long investigationId) {
AssInvestigation investigation= baseMapper.getInvestigationById(investigationId);
if(investigation!=null) {
investigation.setAssAccessoryFiles(accessoryFileService.getAccessoryFileByProjectId(investigation.getProjectId(),investigation.getId(), AccessoryFileTypeEnum.INVESTINGATION_ATTACHMENT.getCode()));
investigation.setCompanyImages(accessoryFileService.getAccessoryFileByProjectId(investigation.getProjectId(),investigation.getId(), AccessoryFileTypeEnum.COMPANY_IMAGE.getCode()));
investigation.setDeviceImages(accessoryFileService.getAccessoryFileByProjectId(investigation.getProjectId(),investigation.getId(), AccessoryFileTypeEnum.DEVICE_IMAGE.getCode()));
investigation.setInvestingationImages(accessoryFileService.getAccessoryFileByProjectId(investigation.getProjectId(),investigation.getId(), AccessoryFileTypeEnum.INVESTINGATION_IMAGE.getCode()));
investigation.setInvestingationVideos(accessoryFileService.getAccessoryFileByProjectId(investigation.getProjectId(),investigation.getId(), AccessoryFileTypeEnum.INVESTINGATION_VIDEO.getCode()));
}
return investigation;
}
@Override
public void doInvestigationProcess(Map map) {
Long projectId= Long.parseLong(map.get("projectId").toString());
projectService.checkUserAllowed(projectId);
LambdaQueryWrapper< AssInvestigation > lambdaQueryWrapper = Wrappers.lambdaQuery()
.eq(AssInvestigation::getProjectId, projectId)
.eq(AssInvestigation::getDelFlag, DeleteFlagEnum.UN_DELETE);
Long count= count(lambdaQueryWrapper);
if(count<1){
throw new ApiException("现场勘验记录不能为空");
}
//校验项目状态
projectService.checkReportProgress(projectId, ReportProgressEnum.WORK_NOTIFICATION);
//更新项目状态
projectService.changeReportProgress(projectId,ReportProgressEnum.INVESTINGATION);
}
}