package com.gkhy.testFourierSpecialGasMonitor.service.impl; import com.gkhy.testFourierSpecialGasMonitor.commons.domain.Result; import com.gkhy.testFourierSpecialGasMonitor.commons.domain.SearchResult; import com.gkhy.testFourierSpecialGasMonitor.commons.enums.ResultCode; import com.gkhy.testFourierSpecialGasMonitor.commons.exception.BusinessException; import com.gkhy.testFourierSpecialGasMonitor.commons.model.PageQuery; import com.gkhy.testFourierSpecialGasMonitor.domain.account.entity.User; import com.gkhy.testFourierSpecialGasMonitor.domain.account.enums.UserStatusEnum; import com.gkhy.testFourierSpecialGasMonitor.domain.account.repository.jpa.UserRepository; import com.gkhy.testFourierSpecialGasMonitor.entity.Region; import com.gkhy.testFourierSpecialGasMonitor.entity.RegionLngLat; import com.gkhy.testFourierSpecialGasMonitor.entity.query.FindRegionPageQuery; import com.gkhy.testFourierSpecialGasMonitor.entity.req.*; import com.gkhy.testFourierSpecialGasMonitor.entity.resp.FindRegionByIdLngLatRespDTO; import com.gkhy.testFourierSpecialGasMonitor.entity.resp.FindRegionByIdRespDTO; import com.gkhy.testFourierSpecialGasMonitor.entity.resp.FindRegionLngLatPageRespDTO; import com.gkhy.testFourierSpecialGasMonitor.entity.resp.FindRegionPageRespDTO; import com.gkhy.testFourierSpecialGasMonitor.enums.DeleteStatusEnum; import com.gkhy.testFourierSpecialGasMonitor.repository.RegionLngLatRepository; import com.gkhy.testFourierSpecialGasMonitor.repository.RegionRepository; import com.gkhy.testFourierSpecialGasMonitor.service.RegionService; import com.gkhy.testFourierSpecialGasMonitor.utils.ThreadLocalUtil; import io.micrometer.core.instrument.util.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import javax.persistence.criteria.*; import java.time.LocalDateTime; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.concurrent.locks.ReentrantLock; import java.util.stream.Collectors; /** * @author Mr.huang * @decription * @date 2023/8/9 10:46 */ @Service public class RegionServiceImpl implements RegionService { @Autowired private UserRepository userRepository; @Autowired private RegionRepository regionRepository; @Autowired private RegionLngLatRepository regionLngLatRepository; private static final ReentrantLock regionNamelock = new ReentrantLock(); private User getCurrentUser(){ Long userId = ThreadLocalUtil.get().getId(); User user = userRepository.findUserByIdAndStatus(userId, UserStatusEnum.STATUS_ACTIVE.getStatus()); if (user == null) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"未成功获取用户信息"); return user; } @Override public Result createRegion(CreateRegionReqDTO reqDto) { User currentUser = getCurrentUser(); if (reqDto == null) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"参数不能为空"); if (StringUtils.isBlank(reqDto.getName())) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"区域名称为空"); if (StringUtils.isBlank(reqDto.getColor())) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"区域颜色不能为空"); if (CollectionUtils.isEmpty(reqDto.getRegionLngLats()) || reqDto.getRegionLngLats().size() < 3) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"经纬度数据不能为空或个数少于3"); regionNamelock.lock(); try { Region regionold = findByNameAndStatus(reqDto.getName()); if (regionold != null) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"区域名称已存在"); Region region = new Region(); BeanUtils.copyProperties(reqDto,region); region.setCreatedby(currentUser.getRealName()); region.setLastmodifiedby(currentUser.getRealName()); region.setStatus(DeleteStatusEnum.DELECT_NO.getStatus()); region.setGmtCreate(LocalDateTime.now()); region.setGmtModified(LocalDateTime.now()); Region save = regionRepository.save(region); if (save == null) throw new BusinessException(this.getClass(), ResultCode.SYSTEM_ERROR_DATABASE_FAIL.getCode(),"区域保存失败"); List regionLngLats = reqDto.getRegionLngLats(); List collect = regionLngLats.stream().map(regionLngLat -> { RegionLngLat lngLat = new RegionLngLat(); BeanUtils.copyProperties(regionLngLat, lngLat); lngLat.setRegionId(save.getId()); return lngLat; }).collect(Collectors.toList()); List lats = regionLngLatRepository.saveAll(collect); if (CollectionUtils.isEmpty(lats)) throw new BusinessException(this.getClass(), ResultCode.SYSTEM_ERROR_DATABASE_FAIL.getCode(),"区域相关经纬度保存失败"); }finally { regionNamelock.unlock(); } return Result.success(); } @Override public Result delRegionById(DelRegionByIdReqDTO reqDto) { User currentUser = getCurrentUser(); if (reqDto == null || reqDto.getId() == null) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"参数不能为空"); Region region = regionRepository.findByIdAndStatus(reqDto.getId(), DeleteStatusEnum.DELECT_NO.getStatus()); if (region == null){ return Result.success(); } region.setStatus(DeleteStatusEnum.DELECT_YES.getStatus()); region.setLastmodifiedby(currentUser.getRealName()); region.setGmtModified(LocalDateTime.now()); Region save = regionRepository.save(region); if (save == null) throw new BusinessException(this.getClass(), ResultCode.SYSTEM_ERROR_DATABASE_FAIL.getCode(),"区域删除失败"); return Result.success(); } @Override public Result findRegionById(Integer id) { Result success = Result.success(); if (id == null) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"参数不能为空"); Region region = regionRepository.findByIdAndStatus(id, DeleteStatusEnum.DELECT_NO.getStatus()); if (region != null){ FindRegionByIdRespDTO findRegionByIdRespDTO = new FindRegionByIdRespDTO(); BeanUtils.copyProperties(region,findRegionByIdRespDTO); List regionLngLats = region.getRegionLngLats(); if (!CollectionUtils.isEmpty(regionLngLats)){ List collect = regionLngLats.stream().map(regionLngLat -> { FindRegionByIdLngLatRespDTO findRegionByIdLngLatRespDTO = new FindRegionByIdLngLatRespDTO(); BeanUtils.copyProperties(regionLngLat, findRegionByIdLngLatRespDTO); return findRegionByIdLngLatRespDTO; }).collect(Collectors.toList()); findRegionByIdRespDTO.setLngLatRespDTOS(collect); } success.setData(findRegionByIdRespDTO); } return success; } @Override public Result findRegionPage(PageQuery pageQuery) { if (pageQuery == null || pageQuery.getPageIndex() == null || pageQuery.getPageSize() == null){ throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL,"分页参数不能为空"); } Pageable pageable = PageRequest.of(pageQuery.getPageIndex()-1, pageQuery.getPageSize()); Specification specification = new Specification() { @Override public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) { Set predicateList = new HashSet<>(); FindRegionPageQuery searchParams = pageQuery.getSearchParams(); if (searchParams != null && !StringUtils.isBlank(searchParams.getName())){ predicateList.add(criteriaBuilder.like(root.get("name").as(String.class),"%"+searchParams.getName()+"%")); } predicateList.add(criteriaBuilder.equal(root.get("status").as(Byte.class), DeleteStatusEnum.DELECT_NO.getStatus())); return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()])); } }; SearchResult> searchResult = new SearchResult<>(); searchResult.setPageIndex(pageQuery.getPageIndex()); searchResult.setPageSize(pageQuery.getPageSize()); searchResult.setSuccess(); Page pageResult = regionRepository.findAll(specification,pageable); searchResult.setTotal(pageResult.getTotalElements()); searchResult.setPages(pageResult.getTotalPages()); if (!CollectionUtils.isEmpty(pageResult.getContent())){ List dtos = pageResult.getContent().stream().map(region -> { FindRegionPageRespDTO dto = new FindRegionPageRespDTO(); BeanUtils.copyProperties(region, dto); if (!CollectionUtils.isEmpty(region.getRegionLngLats())) { List collect = region.getRegionLngLats().stream().map(regionLngLat -> { FindRegionLngLatPageRespDTO findRegionLngLatPageRespDTO = new FindRegionLngLatPageRespDTO(); BeanUtils.copyProperties(regionLngLat, findRegionLngLatPageRespDTO); return findRegionLngLatPageRespDTO; }).collect(Collectors.toList()); dto.setRegionLngLats(collect); } return dto; }).collect(Collectors.toList()); searchResult.setData(dtos); } return searchResult; } @Override @Transactional public Result updateRegion(UpdateRegionReqDTO reqDto) { User currentUser = getCurrentUser(); if (reqDto == null || reqDto.getId() == null) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"参数不能为空"); Region region = regionRepository.findByIdAndStatus(reqDto.getId(), DeleteStatusEnum.DELECT_NO.getStatus()); if (region == null) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"区域不存在"); if (StringUtils.isBlank(reqDto.getName())) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"区域名称为空"); if (StringUtils.isBlank(reqDto.getColor())) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"区域颜色不能为空"); if (CollectionUtils.isEmpty(reqDto.getRegionLngLats()) || reqDto.getRegionLngLats().size() < 3) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"经纬度数据不能为空或个数少于3"); regionNamelock.lock(); try { Region regionold = findByNameAndStatus(reqDto.getName()); if (regionold != null && !regionold.getId().equals(reqDto.getId())) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "区域名称已存在"); region.setName(reqDto.getName()); region.setColor(reqDto.getColor()); region.setLastmodifiedby(currentUser.getRealName()); region.setGmtModified(LocalDateTime.now()); if (regionRepository.save(region) == null) throw new BusinessException(this.getClass(), ResultCode.SYSTEM_ERROR_DATABASE_FAIL.getCode(),"区域保存失败"); regionLngLatRepository.deleteAllByRegionId(reqDto.getId()); List regionLngLats = reqDto.getRegionLngLats(); List collect = regionLngLats.stream().map(regionLngLat -> { RegionLngLat lngLat = new RegionLngLat(); BeanUtils.copyProperties(regionLngLat, lngLat); lngLat.setRegionId(reqDto.getId()); return lngLat; }).collect(Collectors.toList()); List lats = regionLngLatRepository.saveAll(collect); if (CollectionUtils.isEmpty(lats)) throw new BusinessException(this.getClass(), ResultCode.SYSTEM_ERROR_DATABASE_FAIL.getCode(), "区域相关经纬度保存失败"); }finally { regionNamelock.unlock(); } return Result.success(); } @Override public List findAll() { List regions = regionRepository.findAllByStatus(DeleteStatusEnum.DELECT_NO.getStatus()); return regions; } private Region findByNameAndStatus(String name){ return regionRepository.findByNameAndStatus(name, DeleteStatusEnum.DELECT_NO.getStatus()); } }