“djh”
2024-12-05 eee41a5fb58e6547a43929430f4b72908119db6e
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
package com.gkhy.testFourierSpecialGasMonitor.service.impl;
 
import com.gkhy.testFourierSpecialGasMonitor.commons.domain.Result;
import com.gkhy.testFourierSpecialGasMonitor.commons.enums.ResultCode;
import com.gkhy.testFourierSpecialGasMonitor.commons.exception.BusinessException;
import com.gkhy.testFourierSpecialGasMonitor.decorator.WarningThresholdUpdateEvent;
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.GasThreshold;
import com.gkhy.testFourierSpecialGasMonitor.entity.req.UpdateGasThresholdReqDTO;
import com.gkhy.testFourierSpecialGasMonitor.entity.resp.GasThresholdListRespDTO;
import com.gkhy.testFourierSpecialGasMonitor.repository.GasThresholdRepository;
import com.gkhy.testFourierSpecialGasMonitor.service.GasThresholdService;
import com.gkhy.testFourierSpecialGasMonitor.utils.ThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
 
/**
 * @author Mr.huang
 * @decription
 * @date 2023/8/9 14:33
 */
@Service
public class GasThresholdServiceImpl implements GasThresholdService {
 
 
    @Autowired
    private GasThresholdRepository gasThresholdRepository;
 
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;
 
    @Autowired
    private UserRepository userRepository;
 
    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 gasThresholdList() {
        Result success = Result.success();
        List<GasThreshold> gasThresholds = gasThresholdRepository.findAll();
        if (!CollectionUtils.isEmpty(gasThresholds)){
            List<GasThresholdListRespDTO> collect = gasThresholds.stream().map(gasThreshold -> {
                GasThresholdListRespDTO dto = new GasThresholdListRespDTO();
                BeanUtils.copyProperties(gasThreshold, dto);
                return dto;
            }).collect(Collectors.toList());
            success.setData(collect);
        }
        return success;
    }
 
    @Override
    public synchronized Result updateGasThreshold(UpdateGasThresholdReqDTO reqDto) {
        User currentUser = getCurrentUser();
        if (reqDto == null || reqDto.getId() == null)
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"参数不能为空");
        if (reqDto.getThreshold() <= 0)
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"连续超过阈值点数不能小于0");
        Optional<GasThreshold> thresholdRepositoryById = gasThresholdRepository.findById(reqDto.getId());
        if (!thresholdRepositoryById.isPresent())
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"预警该配置不存在");
        GasThreshold threshold = thresholdRepositoryById.get();
        threshold.setThreshold(reqDto.getThreshold());
        threshold.setGmtModified(LocalDateTime.now());
        threshold.setLastmodifiedby(currentUser.getRealName());
        GasThreshold save = gasThresholdRepository.save(threshold);
        if (save == null)
            throw new BusinessException(this.getClass(), ResultCode.SYSTEM_ERROR_DATABASE_FAIL.getCode(),"预警设置修改失败");
        applicationEventPublisher.publishEvent(new WarningThresholdUpdateEvent("事件源--预警阈值发生变更"));
        return Result.success();
    }
 
    @Override
    public List<GasThreshold> findAll() {
        return gasThresholdRepository.findAll();
    }
}