zhangf
2024-07-26 698995469a3fcdc3164fc486d18bdbe059b6c92e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.gkhy.fourierSpecialGasMonitor.service.impl;
 
import com.gkhy.fourierSpecialGasMonitor.commons.domain.Result;
import com.gkhy.fourierSpecialGasMonitor.commons.domain.SearchResult;
import com.gkhy.fourierSpecialGasMonitor.commons.enums.ResultCode;
import com.gkhy.fourierSpecialGasMonitor.commons.exception.BusinessException;
import com.gkhy.fourierSpecialGasMonitor.commons.model.PageQuery;
import com.gkhy.fourierSpecialGasMonitor.domain.account.entity.User;
import com.gkhy.fourierSpecialGasMonitor.domain.account.enums.UserStatusEnum;
import com.gkhy.fourierSpecialGasMonitor.domain.account.repository.jpa.UserRepository;
import com.gkhy.fourierSpecialGasMonitor.entity.GasCategory;
import com.gkhy.fourierSpecialGasMonitor.entity.GasThreshold;
import com.gkhy.fourierSpecialGasMonitor.entity.GasWarnUser;
import com.gkhy.fourierSpecialGasMonitor.entity.query.FindGasCategoryPageQuery;
import com.gkhy.fourierSpecialGasMonitor.entity.query.FindGasWarnUserPageQuery;
import com.gkhy.fourierSpecialGasMonitor.entity.req.CreateGasWarnUserReqDTO;
import com.gkhy.fourierSpecialGasMonitor.entity.req.DelGasWarnUserByIdReqDTO;
import com.gkhy.fourierSpecialGasMonitor.entity.req.UpdateGasWarnUserReqDTO;
import com.gkhy.fourierSpecialGasMonitor.entity.resp.FindGasCategoryPageRespDTO;
import com.gkhy.fourierSpecialGasMonitor.entity.resp.FindGasWarnUserPageRespDTO;
import com.gkhy.fourierSpecialGasMonitor.enums.DeleteStatusEnum;
import com.gkhy.fourierSpecialGasMonitor.repository.GasWarnUserRepository;
import com.gkhy.fourierSpecialGasMonitor.service.GasWarnUserService;
import com.gkhy.fourierSpecialGasMonitor.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.ArrayList;
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;
    }
}