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.GasWarnUser; import com.gkhy.testFourierSpecialGasMonitor.entity.query.FindGasWarnUserPageQuery; import com.gkhy.testFourierSpecialGasMonitor.entity.req.CreateGasWarnUserReqDTO; import com.gkhy.testFourierSpecialGasMonitor.entity.req.DelGasWarnUserByIdReqDTO; import com.gkhy.testFourierSpecialGasMonitor.entity.req.UpdateGasWarnUserReqDTO; import com.gkhy.testFourierSpecialGasMonitor.entity.resp.FindGasWarnUserPageRespDTO; import com.gkhy.testFourierSpecialGasMonitor.enums.DeleteStatusEnum; import com.gkhy.testFourierSpecialGasMonitor.repository.GasWarnUserRepository; import com.gkhy.testFourierSpecialGasMonitor.service.GasWarnUserService; 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.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; 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 15:00 */ @Service public class GasWarnUserServiceImpl implements GasWarnUserService { @Autowired private UserRepository userRepository; @Autowired private GasWarnUserRepository gasWarnUserRepository; private static ReentrantLock gasWarnUserIdlock = 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 createGasWarnUser(CreateGasWarnUserReqDTO reqDto) { 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.getRealName())) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"预警通知人员真实姓名不能为空"); if (StringUtils.isBlank(reqDto.getPhone())) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"预警通知人员手机号不能为空"); gasWarnUserIdlock.lock(); try { GasWarnUser byUserIdAndStatus = gasWarnUserRepository.findByUserIdAndStatus(reqDto.getUserId(), DeleteStatusEnum.DELECT_NO.getStatus()); if (byUserIdAndStatus != null) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "预警通知人员已存在"); GasWarnUser gasWarnUser = new GasWarnUser(); BeanUtils.copyProperties(reqDto, gasWarnUser); gasWarnUser.setStatus(DeleteStatusEnum.DELECT_NO.getStatus()); gasWarnUser.setGmtModified(LocalDateTime.now()); gasWarnUser.setGmtCreate(LocalDateTime.now()); gasWarnUser.setLastmodifiedby(getCurrentUser().getRealName()); gasWarnUser.setCreatedby(getCurrentUser().getRealName()); GasWarnUser save = gasWarnUserRepository.save(gasWarnUser); if (save == null) throw new BusinessException(this.getClass(), ResultCode.SYSTEM_ERROR_DATABASE_FAIL.getCode(), "预警通知人员保存失败"); }finally { gasWarnUserIdlock.unlock(); } return Result.success(); } @Override public Result delGasWarnUserById(DelGasWarnUserByIdReqDTO reqDto) { if (reqDto == null || reqDto.getId() == null) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"参数不能为空"); GasWarnUser gasWarnUser = gasWarnUserRepository.findByIdAndStatus(reqDto.getId(), DeleteStatusEnum.DELECT_NO.getStatus()); if (gasWarnUser != null){ gasWarnUser.setStatus(DeleteStatusEnum.DELECT_YES.getStatus()); gasWarnUser.setLastmodifiedby(getCurrentUser().getRealName()); gasWarnUser.setGmtModified(LocalDateTime.now()); GasWarnUser save = gasWarnUserRepository.save(gasWarnUser); if (save == null) throw new BusinessException(this.getClass(), ResultCode.SYSTEM_ERROR_DATABASE_FAIL.getCode(),"预警通知人员删除失败"); } return Result.success(); } @Override @Transactional public Result updateGasWarnUser(UpdateGasWarnUserReqDTO reqDto) { if (reqDto == null || reqDto.getId() == 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.getRealName())) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"预警通知人员真实姓名不能为空"); if (StringUtils.isBlank(reqDto.getPhone())) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"预警通知人员手机号不能为空"); gasWarnUserIdlock.lock(); try { GasWarnUser gasWarnUser = gasWarnUserRepository.findByUserIdAndStatus(reqDto.getUserId(), DeleteStatusEnum.DELECT_NO.getStatus()); if (gasWarnUser != null && !reqDto.getUserId().equals(gasWarnUser.getUserId())) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "预警通知人员已存在"); GasWarnUser gasWarnUserById = gasWarnUserRepository.findByIdAndStatus(reqDto.getId(), DeleteStatusEnum.DELECT_NO.getStatus()); BeanUtils.copyProperties(reqDto, gasWarnUserById); gasWarnUserById.setGmtModified(LocalDateTime.now()); gasWarnUserById.setLastmodifiedby(getCurrentUser().getRealName()); GasWarnUser save = gasWarnUserRepository.save(gasWarnUserById); if (save == null) throw new BusinessException(this.getClass(), ResultCode.SYSTEM_ERROR_DATABASE_FAIL.getCode(), "预警通知人员更新失败"); }finally { gasWarnUserIdlock.unlock(); } return Result.success(); } @Override public Result findGasWarnUserPage(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<>(); FindGasWarnUserPageQuery searchParams = pageQuery.getSearchParams(); if (searchParams != null && !StringUtils.isBlank(searchParams.getRealName())){ predicateList.add(criteriaBuilder.like(root.get("realName").as(String.class),"%"+searchParams.getRealName()+"%")); } 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 = gasWarnUserRepository.findAll(specification,pageable); searchResult.setTotal(pageResult.getTotalElements()); searchResult.setPages(pageResult.getTotalPages()); if (!CollectionUtils.isEmpty(pageResult.getContent())){ List collect = pageResult.getContent().stream().map(gasWarnUser -> { FindGasWarnUserPageRespDTO dto = new FindGasWarnUserPageRespDTO(); BeanUtils.copyProperties(gasWarnUser, dto); return dto; }).collect(Collectors.toList()); searchResult.setData(collect); } return searchResult; } @Override public List findAll() { List gasWarnUsers = gasWarnUserRepository.findAllByStatus(DeleteStatusEnum.DELECT_NO.getStatus()); return gasWarnUsers; } }