package com.gkhy.safePlatform.incidentManage.service.impl; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.gkhy.safePlatform.commons.enums.ResultCodes; import com.gkhy.safePlatform.commons.query.PageQuery; import com.gkhy.safePlatform.commons.utils.BeanCopyUtils; import com.gkhy.safePlatform.commons.utils.StringUtils; import com.gkhy.safePlatform.commons.vo.ResultVO; import com.gkhy.safePlatform.commons.vo.SearchResultVO; import com.gkhy.safePlatform.incidentManage.entity.*; import com.gkhy.safePlatform.incidentManage.enums.AccidentResultCodes; import com.gkhy.safePlatform.incidentManage.exception.AccidentException; import com.gkhy.safePlatform.incidentManage.model.dto.req.AccidentCaseReqDTO; import com.gkhy.safePlatform.incidentManage.model.dto.resp.AccidentCaseDetailRespDTO; import com.gkhy.safePlatform.incidentManage.model.dto.resp.AccidentCasePageRespDTO; import com.gkhy.safePlatform.incidentManage.query.AccidentCaseQuery; import com.gkhy.safePlatform.incidentManage.query.db.AccidentCaseDBQuery; import com.gkhy.safePlatform.incidentManage.service.AccidentCaseService; import com.gkhy.safePlatform.incidentManage.service.baseService.AccidentCaseInfoService; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.*; @Service("accidentCaseService") public class AccidentCaseServiceImpl implements AccidentCaseService { @Autowired private AccidentCaseInfoService accidentCaseInfoService; @Override public SearchResultVO> selectAccidentCaseList(PageQuery query) { Long pageIndex = query.getPageIndex(); Long pageSize = query.getPageSize(); Page page = new Page<>(pageIndex, pageSize); AccidentCaseDBQuery accidentCaseDBQuery = new AccidentCaseDBQuery(); if (query.getSearchParams() != null) { BeanUtils.copyProperties(query.getSearchParams(), accidentCaseDBQuery); } List accidentCaseInfoPageDOList = accidentCaseInfoService.selectAccidentCaseList(page, accidentCaseDBQuery); List respList = BeanCopyUtils.copyBeanList(accidentCaseInfoPageDOList, AccidentCasePageRespDTO.class); return new SearchResultVO<>( true, pageIndex, pageSize, page.getPages(), page.getTotal(), respList, ResultCodes.OK ); } @Override public ResultVO addAccidentCase(Long uid, AccidentCaseReqDTO accidentCaseReqDTO) { //必填项验证 checkRequired(accidentCaseReqDTO); Date nowDate = new Date(); //1.新增事故案例 AccidentCaseInfo accidentCaseInfo = new AccidentCaseInfo(); BeanUtils.copyProperties(accidentCaseReqDTO, accidentCaseInfo); accidentCaseInfo.setDelFlag(false); accidentCaseInfo.setCreateUid(uid); accidentCaseInfo.setGmtCreate(nowDate); accidentCaseInfoService.addAccidentCase(accidentCaseInfo); return new ResultVO(ResultCodes.OK); } @Override public ResultVO getAccidentCaseById(Long id) { AccidentCaseDetailRespDTO AccidentCaseDetailRespDTO = new AccidentCaseDetailRespDTO(); //查询是否存在 AccidentCaseInfoDetailDO AccidentCaseInfoDetailDO = accidentCaseInfoService.selectAccidentCaseById(id); if (AccidentCaseInfoDetailDO == null) { throw new AccidentException(AccidentResultCodes.CASE_NOT_EXIST); } else { BeanUtils.copyProperties(AccidentCaseInfoDetailDO, AccidentCaseDetailRespDTO); return new ResultVO<>(ResultCodes.OK, AccidentCaseDetailRespDTO); } } @Override public ResultVO updateAccidentCase(Long uid, AccidentCaseReqDTO accidentCaseReqDTO) { Date nowDate = new Date(); //查询是否存在 AccidentCaseInfoDetailDO AccidentCaseInfoDetailDO = accidentCaseInfoService.selectAccidentCaseById(accidentCaseReqDTO.getId()); if (AccidentCaseInfoDetailDO == null) { throw new AccidentException(AccidentResultCodes.CASE_NOT_EXIST); } else { AccidentCaseInfo accidentCaseInfo = new AccidentCaseInfo(); BeanUtils.copyProperties(accidentCaseReqDTO, accidentCaseInfo); accidentCaseInfo.setUpdateUid(uid); accidentCaseInfo.setGmtModitify(nowDate); accidentCaseInfoService.updateAccidentCase(accidentCaseInfo); return new ResultVO(ResultCodes.OK); } } @Override public ResultVO batchDeleteAccidentCase(Long[] ids) { if (ids == null || ids.length==0){ throw new AccidentException(AccidentResultCodes.CASE_NULL); } else { for (Long id : ids){ deleteAccidentCase(id); } return new ResultVO(ResultCodes.OK); } } private void deleteAccidentCase(Long id) { //查询是否存在 AccidentCaseInfoDetailDO AccidentCaseInfoDetailDO = accidentCaseInfoService.selectAccidentCaseById(id); if (AccidentCaseInfoDetailDO == null) { throw new AccidentException(AccidentResultCodes.CASE_NOT_EXIST); } else { accidentCaseInfoService.deleteAccidentCaseById(id); } } /** * 验证必填项 * * @return */ private void checkRequired(AccidentCaseReqDTO AccidentCaseReqDTO) { // 事故案例标题 if (AccidentCaseReqDTO.getCaseTitle() == null) { throw new AccidentException(AccidentResultCodes.CASE_TITLE_NULL); } // 事故案例内容 if (StringUtils.isBlank(AccidentCaseReqDTO.getCaseContent())) { throw new AccidentException(AccidentResultCodes.CASE_CONTENT_NULL); } } }