package com.gkhy.safePlatform.emergency.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.emergency.entity.*; import com.gkhy.safePlatform.emergency.enums.EmergencyResultCodes; import com.gkhy.safePlatform.emergency.excepiton.EmergencyException; import com.gkhy.safePlatform.emergency.model.dto.req.EmergencySuppliesInspectReqDTO; import com.gkhy.safePlatform.emergency.model.dto.req.EmergencySuppliesInspectUserReqDTO; import com.gkhy.safePlatform.emergency.model.dto.resp.EmergencySuppliesInspectDetailRespDTO; import com.gkhy.safePlatform.emergency.model.dto.resp.EmergencySuppliesInspectPageRespDTO; import com.gkhy.safePlatform.emergency.model.dto.resp.EmergencySuppliesInspectUserRespDTO; import com.gkhy.safePlatform.emergency.query.EmergencySuppliesInspectQuery; import com.gkhy.safePlatform.emergency.query.db.EmergencySuppliesInspectDBQuery; import com.gkhy.safePlatform.emergency.service.EmergencySuppliesInspectService; import com.gkhy.safePlatform.emergency.service.baseService.EmergencySuppliesInfoService; import com.gkhy.safePlatform.emergency.service.baseService.EmergencySuppliesInspectInfoService; import com.gkhy.safePlatform.emergency.service.baseService.EmergencySuppliesInspectUserInfoService; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.stream.Collectors; @Service("emergencySuppliesInspectService") public class EmergencySuppliesInspectServiceImpl implements EmergencySuppliesInspectService { @Autowired private EmergencySuppliesInspectInfoService emergencySuppliesInspectInfoService; @Autowired private EmergencySuppliesInspectUserInfoService emergencySuppliesInspectUserInfoService; @Autowired private EmergencySuppliesInfoService emergencySuppliesInfoService; @Override public SearchResultVO> selectEmergencySuppliesInspectList(PageQuery query) { Long pageIndex = query.getPageIndex(); Long pageSize = query.getPageSize(); Page page = new Page<>(pageIndex, pageSize); EmergencySuppliesInspectDBQuery emergencySuppliesInspectDBQuery = new EmergencySuppliesInspectDBQuery(); if (query.getSearchParams() != null) { BeanUtils.copyProperties(query.getSearchParams(), emergencySuppliesInspectDBQuery); } List emergencySuppliesInspectListDoInfoList = emergencySuppliesInspectInfoService.selectEmergencySuppliesInspectList(page,emergencySuppliesInspectDBQuery); List respList = BeanCopyUtils.copyBeanList(emergencySuppliesInspectListDoInfoList, EmergencySuppliesInspectPageRespDTO.class); return new SearchResultVO<>( true, pageIndex, pageSize, page.getTotal(), respList, ResultCodes.OK ); } @Override public ResultVO addEmergencySuppliesInspect(Long uid, EmergencySuppliesInspectReqDTO emergencySuppliesInspectReqDTO) { Long suppliesId = emergencySuppliesInspectReqDTO.getSuppliesId(); if (suppliesId==null){ throw new EmergencyException(EmergencyResultCodes.SUPPLIES_NULL); }else{ EmergencySuppliesInfoDetailDO emergencySuppliesInfoDetailDO = emergencySuppliesInfoService.selectEmergencySuppliesById(suppliesId); if (emergencySuppliesInfoDetailDO==null){ throw new EmergencyException(EmergencyResultCodes.SUPPLIES_NOT_EXIST); }else{ Date nowDate = new Date(); // 新增应急物资检查 EmergencySuppliesInspectInfo emergencySuppliesInspectInfo = new EmergencySuppliesInspectInfo(); BeanUtils.copyProperties(emergencySuppliesInspectReqDTO,emergencySuppliesInspectInfo); emergencySuppliesInspectInfo.setDelFlag(false); emergencySuppliesInspectInfo.setCreateUid(uid); emergencySuppliesInspectInfo.setGmtCreate(nowDate); emergencySuppliesInspectInfoService.addEmergencySuppliesInspect(emergencySuppliesInspectInfo); // 新增急演练计划应急人员表 if (!CollectionUtils.isEmpty(emergencySuppliesInspectReqDTO.getUserList())) { addEmergencySuppliesInspectUser(uid, emergencySuppliesInspectInfo.getId(), nowDate, emergencySuppliesInspectReqDTO.getUserList()); } return new ResultVO<>(ResultCodes.OK); } } } private void addEmergencySuppliesInspectUser(Long uid, Long inspectId, Date nowDate, List userList) { List emergencySuppliesInspectUserInfoList = BeanCopyUtils.copyBeanList(userList, EmergencySuppliesInspectUserInfo.class); emergencySuppliesInspectUserInfoList.forEach(EmergencySuppliesInspectUserInfo -> { EmergencySuppliesInspectUserInfo.setDelFlag(false); EmergencySuppliesInspectUserInfo.setCreateUid(uid); EmergencySuppliesInspectUserInfo.setGmtCreate(nowDate); EmergencySuppliesInspectUserInfo.setInspectId(inspectId); }); for (EmergencySuppliesInspectUserInfo emergencySuppliesInspectUserInfo : emergencySuppliesInspectUserInfoList) { emergencySuppliesInspectUserInfoService.addEmergencySuppliesInspectUser(emergencySuppliesInspectUserInfo); } } @Override public ResultVO getEmergencySuppliesInspectById(Long id) { EmergencySuppliesInspectDetailRespDTO emergencySuppliesInspectDetailRespDTO = new EmergencySuppliesInspectDetailRespDTO(); // 查询是否存在 EmergencySuppliesInspectInfoDetailDO emergencySuppliesInspectInfoDetailDO = emergencySuppliesInspectInfoService.selectEmergencySuppliesInspectById(id); if (emergencySuppliesInspectInfoDetailDO==null){ throw new EmergencyException(EmergencyResultCodes.SUPPLIES_INSPECT_NOT_EXIST); }else{ BeanUtils.copyProperties(emergencySuppliesInspectInfoDetailDO,emergencySuppliesInspectDetailRespDTO); // 查找对应的人员表 List emergencySuppliesInspectUserInfoDOList = emergencySuppliesInspectUserInfoService.selectEmergencySuppliesInspectUserBySuppliesInspectId(id); if (!CollectionUtils.isEmpty(emergencySuppliesInspectUserInfoDOList)) { List emergencySuppliesInspectUserRespDTOList = BeanCopyUtils.copyBeanList(emergencySuppliesInspectUserInfoDOList, EmergencySuppliesInspectUserRespDTO.class); emergencySuppliesInspectDetailRespDTO.setUserList(emergencySuppliesInspectUserRespDTOList); } return new ResultVO<>(ResultCodes.OK,emergencySuppliesInspectDetailRespDTO); } } @Override public ResultVO updateEmergencySuppliesInspect(Long uid, EmergencySuppliesInspectReqDTO emergencySuppliesInspectReqDTO) { Date nowDate = new Date(); // 查询是否存在 EmergencySuppliesInspectInfoDetailDO emergencySuppliesInspectInfoDetailDO = emergencySuppliesInspectInfoService.selectEmergencySuppliesInspectById(emergencySuppliesInspectReqDTO.getId()); if (emergencySuppliesInspectInfoDetailDO==null){ throw new EmergencyException(EmergencyResultCodes.SUPPLIES_INSPECT_NOT_EXIST); }else{ EmergencySuppliesInspectInfo emergencySuppliesInspectInfo = new EmergencySuppliesInspectInfo(); BeanUtils.copyProperties(emergencySuppliesInspectReqDTO,emergencySuppliesInspectInfo); emergencySuppliesInspectInfo.setUpdateUid(uid); emergencySuppliesInspectInfo.setGmtModitify(nowDate); emergencySuppliesInspectInfoService.updateEmergencySuppliesInspect(emergencySuppliesInspectInfo); // 更新急演练计划应急人员表 if (!CollectionUtils.isEmpty(emergencySuppliesInspectReqDTO.getUserList())) { updateEmergencySuppliesInspectUser(uid, emergencySuppliesInspectInfo.getId(), nowDate, emergencySuppliesInspectReqDTO.getUserList()); } return new ResultVO<>(ResultCodes.OK); } } private void updateEmergencySuppliesInspectUser(Long uid, Long suppliesInspectId, Date nowDate, List UserReqDTOList) { List emergencySuppliesInspectUserInfoDOList = emergencySuppliesInspectUserInfoService.selectEmergencySuppliesInspectUserBySuppliesInspectId(suppliesInspectId); List oldIdsList = emergencySuppliesInspectUserInfoDOList.stream().map(EmergencySuppliesInspectUserInfoDO::getId).collect(Collectors.toList()); List newIdsList = new ArrayList<>(); //新增的区域集合 List addList = new ArrayList<>(); //删除的区域集合(id) List deleteList = new ArrayList<>(); for (EmergencySuppliesInspectUserReqDTO emergencySuppliesInspectUserReqDTO : UserReqDTOList) { //如果不存在id则表示页面新增的附件 if (emergencySuppliesInspectUserReqDTO.getId() == null) { EmergencySuppliesInspectUserInfo emergencySuppliesInspectUserInfo = new EmergencySuppliesInspectUserInfo(); BeanUtils.copyProperties(emergencySuppliesInspectUserReqDTO, emergencySuppliesInspectUserInfo); emergencySuppliesInspectUserInfo.setDelFlag(false); emergencySuppliesInspectUserInfo.setGmtCreate(nowDate); emergencySuppliesInspectUserInfo.setCreateUid(uid); emergencySuppliesInspectUserInfo.setInspectId(suppliesInspectId); addList.add(emergencySuppliesInspectUserInfo); } //如果存在id则判断页面是否删除 else { newIdsList.add(emergencySuppliesInspectUserReqDTO.getId()); } } for (Long oldId : oldIdsList) { if (!newIdsList.contains(oldId)) { deleteList.add(oldId); } } if (!CollectionUtils.isEmpty(addList)) { for (EmergencySuppliesInspectUserInfo emergencySuppliesInspectUserInfo : addList) { emergencySuppliesInspectUserInfoService.addEmergencySuppliesInspectUser(emergencySuppliesInspectUserInfo); } } if (!CollectionUtils.isEmpty(deleteList)) { emergencySuppliesInspectUserInfoService.deleteEmergencySuppliesInspectUserByIds(deleteList); } } @Override public ResultVO batchDeleteEmergencySuppliesInspect(String ids) { if (StringUtils.isBlank(ids)) { throw new EmergencyException(EmergencyResultCodes.SUPPLIES_INSPECT_NULL); } else { String[] idArr = ids.split(","); for (String id : idArr) { deleteEmergencySuppliesInspect(Long.valueOf(id)); } return new ResultVO(ResultCodes.OK); } } private void deleteEmergencySuppliesInspect(Long id) { //查询是否存在 EmergencySuppliesInspectInfoDetailDO emergencySuppliesInspectInfoDetailDO = emergencySuppliesInspectInfoService.selectEmergencySuppliesInspectById(id); if (emergencySuppliesInspectInfoDetailDO==null){ throw new EmergencyException(EmergencyResultCodes.SUPPLIES_INSPECT_NOT_EXIST); }else{ Long suppliesInspectId = emergencySuppliesInspectInfoDetailDO.getId(); emergencySuppliesInspectInfoService.deleteEmergencySuppliesInspect(suppliesInspectId); //删除人员 emergencySuppliesInspectUserInfoService.deleteEmergencySuppliesInspectUserBySuppliesInspectId(suppliesInspectId); } } }