package com.gkhy.huataiFourierSpecialGasMonitor.service.impl;
|
|
import com.gkhy.huataiFourierSpecialGasMonitor.commons.domain.Result;
|
import com.gkhy.huataiFourierSpecialGasMonitor.commons.domain.SearchResult;
|
import com.gkhy.huataiFourierSpecialGasMonitor.commons.enums.ResultCode;
|
import com.gkhy.huataiFourierSpecialGasMonitor.commons.exception.BusinessException;
|
import com.gkhy.huataiFourierSpecialGasMonitor.commons.model.PageQuery;
|
import com.gkhy.huataiFourierSpecialGasMonitor.domain.account.entity.User;
|
import com.gkhy.huataiFourierSpecialGasMonitor.domain.account.enums.UserStatusEnum;
|
import com.gkhy.huataiFourierSpecialGasMonitor.domain.account.repository.jpa.UserRepository;
|
import com.gkhy.huataiFourierSpecialGasMonitor.entity.GasWarnUser;
|
import com.gkhy.huataiFourierSpecialGasMonitor.entity.query.FindGasWarnUserPageQuery;
|
import com.gkhy.huataiFourierSpecialGasMonitor.entity.req.CreateGasWarnUserReqDTO;
|
import com.gkhy.huataiFourierSpecialGasMonitor.entity.req.DelGasWarnUserByIdReqDTO;
|
import com.gkhy.huataiFourierSpecialGasMonitor.entity.req.UpdateGasWarnUserReqDTO;
|
import com.gkhy.huataiFourierSpecialGasMonitor.entity.resp.FindGasWarnUserPageRespDTO;
|
import com.gkhy.huataiFourierSpecialGasMonitor.enums.DeleteStatusEnum;
|
import com.gkhy.huataiFourierSpecialGasMonitor.repository.GasWarnUserRepository;
|
import com.gkhy.huataiFourierSpecialGasMonitor.service.GasWarnUserService;
|
import com.gkhy.huataiFourierSpecialGasMonitor.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<FindGasWarnUserPageQuery> 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<GasWarnUser> specification = new Specification<GasWarnUser>() {
|
@Override
|
public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) {
|
Set<Predicate> 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<List<FindGasWarnUserPageRespDTO>> searchResult = new SearchResult<>();
|
searchResult.setPageIndex(pageQuery.getPageIndex());
|
searchResult.setPageSize(pageQuery.getPageSize());
|
searchResult.setSuccess();
|
Page<GasWarnUser> pageResult = gasWarnUserRepository.findAll(specification,pageable);
|
searchResult.setTotal(pageResult.getTotalElements());
|
searchResult.setPages(pageResult.getTotalPages());
|
if (!CollectionUtils.isEmpty(pageResult.getContent())){
|
List<FindGasWarnUserPageRespDTO> 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<GasWarnUser> findAll() {
|
List<GasWarnUser> gasWarnUsers = gasWarnUserRepository.findAllByStatus(DeleteStatusEnum.DELECT_NO.getStatus());
|
return gasWarnUsers;
|
}
|
}
|