package com.ycl.service.caseHandler.impl;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycl.bo.AdminUserDetails;
import com.ycl.common.constant.BaseCaseStatus;
import com.ycl.common.constant.StepName;
import com.ycl.common.util.DateUtil;
import com.ycl.dto.casePool.IllegalBuildingParam;
import com.ycl.dto.casePool.ViolationParam;
import com.ycl.entity.caseHandler.*;
import com.ycl.entity.common.ImageResources;
import com.ycl.entity.dict.DataDictionary;
import com.ycl.entity.video.VideoAlarmReport;
import com.ycl.exception.ApiException;
import com.ycl.mapper.caseHandler.*;
import com.ycl.mapper.common.ImageResourcesMapper;
import com.ycl.mapper.dict.DataDictionaryMapper;
import com.ycl.remote.dto.*;
import com.ycl.remote.service.CityPlatformService;
import com.ycl.service.caseHandler.IBaseCaseService;
import com.ycl.service.caseHandler.IViolationsService;
import com.ycl.service.video.IVideoAlarmReportService;
import com.ycl.vo.casePool.CasePoolViolationVO;
import com.ycl.vo.casePool.CurrentSitVo;
import com.ycl.vo.casePool.FilesPictureVo;
import com.ycl.vo.casePool.HandlePassVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* 案件基本信息 服务实现类
*
*
* @author wl
* @since 2022-09-24
*/
@Service
public class BaseCaseServiceImpl extends ServiceImpl implements IBaseCaseService {
private CityPlatformService cityPlatformService;
private IViolationsService violationsService;
private IVideoAlarmReportService videoAlarmReportService;
@Value("${fdfs.fileUrl}")
private String fileUrl;
@Autowired
public void setCityPlatformService(CityPlatformService cityPlatformService) {
this.cityPlatformService = cityPlatformService;
}
@Autowired
public void setViolationsService(IViolationsService violationsService) {
this.violationsService = violationsService;
}
@Autowired
public void setVideoAlarmReportService(IVideoAlarmReportService videoAlarmReportService) {
this.videoAlarmReportService = videoAlarmReportService;
}
@Resource
BaseCaseMapper baseCaseMapper;
@Resource
DataDictionaryMapper dataDictionaryMapper;
@Resource
ViolationsMapper violationsMapper;
@Resource
IllegalBuildingMapper illegalBuildingMapper;
@Resource
ArrivalSituationMapper arrivalSituationMapper;
@Resource
InvestigationMapper investigationMapper;
@Resource
WritMapper writMapper;
@Resource
DisposeRecordMapper disposeRecordMapper;
@Resource
WorkflowConfigStepMapper workflowConfigStepMapper;
@Resource
WorkflowConfigMapper workflowConfigMapper;
@Resource
ImageResourcesMapper imageResourcesMapper;
@Resource
DispatchInfoMapper dispatchInfoMapper;
@Resource
PartyInfoMapper partyInfoMapper;
@Override
public String uploadEvent(Integer caseId) {
BaseCase baseCase = this.getById(caseId);
Violations violations = violationsService.getById(caseId);
String medias = "";
String eventDesc = "";
if (violations != null) {
eventDesc = violations.getDescription();
VideoAlarmReport videoAlarmReport = videoAlarmReportService.getById(violations.getVideoAlarmReportId());
if (videoAlarmReport != null) {
StringBuilder stringBuilder = new StringBuilder().append("[{'mediaURL':'").append(fileUrl).append(videoAlarmReport.getPicData()).append("'}]");
medias = stringBuilder.toString();
}
}
EventAddParamDto dto = EventAddParamDto.builder().y84(baseCase.getLatitude() != null ? baseCase.getLatitude().toString() : "").x84(baseCase.getLongitude() != null ? baseCase.getLongitude().toString() : "").source(11).address(baseCase.getSite()).eventDesc(eventDesc).eventSign(baseCase.getCode()).medias(medias).build();
String msg = cityPlatformService.addEvent(dto);
ResultResponseDto result = JSONObject.parseObject(msg, ResultResponseDto.class);
if (result.getCode() == 0) {
EventAddResponseDto responseDto = JSONObject.parseObject(result.getResult(), EventAddResponseDto.class);
baseCase.setTaskCode(responseDto.getTaskcode());
this.updateById(baseCase);
return null;
} else {
return result.getMsg();
}
}
@Override
public String processEvent(Integer caseId) {
BaseCase baseCase = this.getById(caseId);
EventProcessParamDto paramDto = EventProcessParamDto.builder().eventSign(baseCase.getCode()).taskcode(baseCase.getTaskCode()).build();
String msg = cityPlatformService.getEventProcess(paramDto);
ResultResponseDto responseDto = JSONObject.parseObject(msg, ResultResponseDto.class);
if (responseDto.getCode() == 0) {
EventProcessResponseDto eventProcessResponseDto = JSONObject.parseObject(responseDto.getResult(), EventProcessResponseDto.class);
/*********** 未处理市平台返回数据 ***************/
return null;
} else {
return responseDto.getMsg();
}
}
@Override
public Page listViolationsPage(Page page, Integer state, Integer resource) {
Integer type = 01;
Integer hours = 60;
Integer day = 24;
Page violationsPage = baseCaseMapper.listViolationsPage(page, state, type, resource);
violationsPage.getRecords().stream().forEach(item -> {
if (item.getCloseTime() == null) {
Duration duration = Duration.between(item.getAlarmTime(), LocalDateTime.now());
long continueHours = duration.toHours() - (duration.toDays() * day);
long minutes = duration.toMinutes() - (duration.toHours() * hours);
String continueTime = duration.toDays() + "天" + continueHours + "时" + minutes + "分钟";
item.setContinueTime(continueTime);
} else {
Duration duration = Duration.between(item.getAlarmTime(), item.getCloseTime());
long continueHours = duration.toHours() - (duration.toDays() * day);
long minutes = duration.toMinutes() - (duration.toHours() * hours);
String continueTime = duration.toDays() + "天" + continueHours + "时" + minutes + "分钟";
item.setContinueTime(continueTime);
}
});
return violationsPage;
}
@Override
public Page listIllegalBuilding(Page page, Integer state, Integer resource) {
Integer type = 02;
return baseCaseMapper.listIllegalBuildingsPage(page, state, type, resource);
}
@Override
public Boolean saveViolationCase(ViolationParam violationParam, Long id) {
Violations violations = new Violations();
BeanUtils.copyProperties(violationParam, violations);
Integer value = 1;
violations.setId(id);
setDisposeRecord(id, violationParam.getLimitTime());
return violationsMapper.insert(violations) == value ? true : false;
}
private void setDisposeRecord(Long id, String limitTime) {
String stepName = StepName.DISPATCH.getName();
QueryWrapper stepQuery = new QueryWrapper<>();
stepQuery.eq("name", stepName);
WorkflowConfigStep workflowConfigStep = workflowConfigStepMapper.selectOne(stepQuery);
Integer state = 0;
DisposeRecord disposeRecord = new DisposeRecord();
disposeRecord.setWorkflowConfigStepId(workflowConfigStep.getId());
disposeRecord.setHandlerRoleId(workflowConfigStep.getRoleId());
disposeRecord.setBaseCaseId(id);
disposeRecord.setState(state);
disposeRecord.setCreateTime(LocalDateTime.now());
AdminUserDetails userDetails = (AdminUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
disposeRecord.setCreateUser(userDetails.getUserId());
disposeRecord.setStartTime(LocalDateTime.now());
disposeRecord.setStepName(stepName);
if (!StringUtils.isEmpty(limitTime)) {
disposeRecord.setLimitTime(LocalDateTime.parse(limitTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
disposeRecordMapper.insert(disposeRecord);
}
@Override
public Boolean saveIllegalBuildingCase(IllegalBuildingParam illegalBuildingParam, Long id) {
IllegalBuilding illegalBuilding = new IllegalBuilding();
BeanUtils.copyProperties(illegalBuildingParam, illegalBuilding);
setDisposeRecord(id, illegalBuildingParam.getLimitTime());
Integer value = 1;
illegalBuilding.setBaseCaseId(id);
return illegalBuildingMapper.insert(illegalBuilding) == value ? true : false;
}
@Override
public BaseCaseDetail baseCaseDetail(String code) {
BaseCaseDetail bcd = new BaseCaseDetail();
//1.查询案件基本信息
Map map = new HashMap();
map.put("code", code);
BaseCase baseCase = baseCaseMapper.selectCondMap(map);
if (baseCase == null) {
return null;
}
//查询案件违规信息
if (baseCase.getCategory() == 1) {
Map mapV = new HashMap();
mapV.put("id", baseCase.getId());
Violations violations = violationsMapper.selectCondMap(mapV);
baseCase.setViolations(violations);
} else {
IllegalBuilding illegalBuilding = illegalBuildingMapper.selectById(baseCase.getId());
LambdaQueryWrapper dict = new LambdaQueryWrapper<>();
dict.eq(DataDictionary::getId, illegalBuilding.getCategoryId());
DataDictionary dictionary = dataDictionaryMapper.selectOne(dict);
if (dictionary != null) {
illegalBuilding.setCategoryText(dictionary.getName());
}
baseCase.setIllegalBuilding(illegalBuilding);
}
//获取调度信息
Map mapD = new HashMap();
mapD.put("baseCaseId", baseCase.getId());
DispatchInfo dispatchInfo = dispatchInfoMapper.selectCondMap(mapD);
baseCase.setDispatchInfo(dispatchInfo);
bcd.setBaseCase(baseCase);
//案件相关信息
//2.查询办理经过信息
//2.1查询处置流程配置
HandlePassVo handlePassVo = new HandlePassVo();
QueryWrapper wrapperWc = new QueryWrapper<>();
wrapperWc.lambda().eq(WorkflowConfig::getCode, "ddlc");
List wcs = workflowConfigMapper.selectList(wrapperWc);
//2.2查询处置流程环节配置
Map mapWcs = new HashMap();
mapWcs.put("workflowConfigId", wcs.get(0).getId());
mapWcs.put("baseCaseId", baseCase.getId());
List listWcs = workflowConfigStepMapper.selectRecordByWorkflowConfigStepId(mapWcs);
//处理用环节时间
for (WorkflowConfigStep step : listWcs) {
List records = step.getDisposeRecords();
if (records == null) {
continue;
}
for (DisposeRecord record : records) {
if (record != null && record.getEndTime() != null && record.getStartTime() != null) {
record.setLinkTime(DateUtil.getDistanceDateTime(DateUtil.fromLocalDateTime(record.getStartTime()), DateUtil.fromLocalDateTime(record.getEndTime())));
}
}
step.setDisposeRecords(records);
}
//2.3设置调度流程
handlePassVo.setWorkflowConfigSteps(listWcs);
bcd.setHandlePassVo(handlePassVo);
//3.查询案卷图片信息
FilesPictureVo filesPictureVo = new FilesPictureVo();
QueryWrapper wrapperIr = new QueryWrapper<>();
wrapperIr.lambda().eq(ImageResources::getBelongToId,baseCase.getId());
List irs = imageResourcesMapper.selectList(wrapperIr);
filesPictureVo.setImageResources(irs);
bcd.setFilesPictureVo(filesPictureVo);
//4.查询问题处理信息
/*ProblemProVo problemProVo = new ProblemProVo();
//4.2查询处置流程环节配置
List listWcsPp = new ArrayList<>();
//4.3设置调度流程
problemProVo.setWorkflowConfigSteps(listWcsPp);
bcd.setProblemProVo(problemProVo);*/
//5.查询现场情况信息
CurrentSitVo currentSitVo = new CurrentSitVo();
QueryWrapper wrapperAs = new QueryWrapper<>();
wrapperAs.lambda().eq(ArrivalSituation::getBaseCaseId, baseCase.getId());
//设置到达现场情况
ArrivalSituation as = arrivalSituationMapper.selectOne(wrapperAs);
currentSitVo.setArrivalSituation(as);
//设置调查取证
QueryWrapper wrapperI = new QueryWrapper<>();
wrapperI.lambda().eq(Investigation::getBaseCaseId, baseCase.getId());
Investigation investigation = investigationMapper.selectOne(wrapperI);
if (investigation != null && investigation.getPartyId() != null) {
//获取当事人信息
Map mapP = new HashMap();
mapP.put("id", investigation.getPartyId());
PartyInfo partyInfo = partyInfoMapper.selectCondMap(mapP);
//填充当事人
investigation.setPartyInfo(partyInfo);
}
currentSitVo.setInvestigation(investigation);
//设置告知违法
QueryWrapper wrapperW = new QueryWrapper<>();
wrapperW.lambda().eq(Writ::getBaseCaseId, baseCase.getId());
Writ writ = writMapper.selectOne(wrapperW);
currentSitVo.setWrit(writ);
bcd.setCurrentSitVo(currentSitVo);
return bcd;
}
@Override
public ArrayList listCaseImages(Integer id, Integer type) {
Integer illegalBuildingType = 02;
ArrayList caseImages = new ArrayList<>();
caseImages.add(investigationMapper.selectOne(new LambdaQueryWrapper().eq(Investigation::getBaseCaseId, id)).getPic());
caseImages.add(arrivalSituationMapper.selectOne(new LambdaQueryWrapper().eq(ArrivalSituation::getBaseCaseId, id)).getSituationPic());
if (type == illegalBuildingType) {
Writ writ = writMapper.selectOne(new LambdaQueryWrapper().eq(Writ::getBaseCaseId, id));
caseImages.add(writ.getWritPic());
caseImages.add(writ.getOriginalPic());
caseImages.add(writ.getOtherPic());
caseImages.add(writ.getRectifiedPic());
}
return caseImages;
}
@Override
public void endCase(Long caseId, String result, String opinion) {
//获取当前登陆用户信息
AdminUserDetails user = (AdminUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String endCaseName = StepName.CLOSING_REGISTER.getName();
BaseCase baseCase = new BaseCase();
baseCase.setState(BaseCaseStatus.CLOSING_REGISTER);
baseCase.setId(caseId);
baseCase.setFinalOpinion(opinion);
baseCaseMapper.updateById(baseCase);
QueryWrapper stepQurey = new QueryWrapper<>();
stepQurey.eq("name", endCaseName);
WorkflowConfigStep workflowConfigStep = workflowConfigStepMapper.selectOne(stepQurey);
if (workflowConfigStep == null) {
throw new ApiException("未查询到该流程环节");
}
UpdateWrapper updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("base_case_id", baseCase.getId()).eq("workflow_config_step_id", workflowConfigStep.getId());
//修改结案记录
DisposeRecord disposeRecord = new DisposeRecord();
disposeRecord.setHandlerId(user.getUserId());
//结案已结束
disposeRecord.setState(1);
disposeRecord.setResult(result);
disposeRecord.setEndTime(LocalDateTime.now());
disposeRecord.setHandlerId(user.getUserId());
disposeRecordMapper.update(disposeRecord, updateWrapper);
}
}