“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
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
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.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.GasCategoryConfiguration;
import com.gkhy.testFourierSpecialGasMonitor.entity.req.GasCategoryConfigurationReqDTO;
import com.gkhy.testFourierSpecialGasMonitor.repository.GasCategoryConfigurationRepository;
import com.gkhy.testFourierSpecialGasMonitor.service.GasCategoryConfigurationService;
import com.gkhy.testFourierSpecialGasMonitor.utils.ThreadLocalUtil;
import io.micrometer.core.instrument.util.StringUtils;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.time.LocalDateTime;
import java.util.Optional;
 
@Service
public class GasCategoryConfigurationServiceImpl implements GasCategoryConfigurationService {
    @Autowired
    private UserRepository userRepository;
 
    @Autowired
    private RedissonClient redissonClient;
 
    @Autowired
    private GasCategoryConfigurationRepository gasCategoryConfigurationRepository;
 
 
    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 updateById(GasCategoryConfigurationReqDTO reqDTO) {
        if (reqDTO==null)
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"参数不能为空");
 
        if (reqDTO.getOrientation()==null)
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"方位不能为空");
 
        if (reqDTO.getConcentration()==null)
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"本底浓度不能为空");
 
        if (reqDTO.getMultiplication()==null)
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"倍增系数不能为空");
 
        GasCategoryConfiguration byOrientation = gasCategoryConfigurationRepository.findByGasCategoryIdAndOrientation(reqDTO.getGasCategoryId(), reqDTO.getOrientation());
        if (byOrientation!=null && byOrientation.getId() != reqDTO.getId())
            throw new BusinessException(this.getClass(),ResultCode.BUSINESS_ERROR.getCode(),"同一方位不能重复添加");
 
 
        User user = getCurrentUser();
        Optional<GasCategoryConfiguration> byId = gasCategoryConfigurationRepository.findById(reqDTO.getId());
        if (byId.isPresent()){
            GasCategoryConfiguration gasCategoryConfiguration = byId.get();
            BeanUtils.copyProperties(reqDTO,gasCategoryConfiguration);
            gasCategoryConfiguration.setUpdateBy(user.getRealName());
            gasCategoryConfiguration.setUpdateTime(LocalDateTime.now());
            GasCategoryConfiguration save = gasCategoryConfigurationRepository.save(gasCategoryConfiguration);
            if (save==null)
                throw new BusinessException(this.getClass(),ResultCode.SYSTEM_ERROR_DATABASE_FAIL.getCode(),"更新气体参数失败");
        }
 
        return Result.success();
    }
 
    @Override
    public Result deletedById(Integer id) {
        Optional<GasCategoryConfiguration> byId = gasCategoryConfigurationRepository.findById(id);
        if (byId.isPresent()){
            GasCategoryConfiguration gasCategoryConfiguration = byId.get();
            gasCategoryConfiguration.setDelFlag(1);
            GasCategoryConfiguration save = gasCategoryConfigurationRepository.save(gasCategoryConfiguration);
            if (save==null)
                throw new BusinessException(this.getClass(),ResultCode.SYSTEM_ERROR_DATABASE_FAIL.getCode(),"删除气体参数失败");
        }
 
        return Result.success();
    }
 
    @Override
    public Result createGasCategoryConfiguration(GasCategoryConfigurationReqDTO reqDTO) {
        if (reqDTO==null)
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"参数不能为空");
        if (reqDTO.getOrientation()==null)
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"方位不能为空");
 
        if (reqDTO.getConcentration()==null)
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"本底浓度不能为空");
 
        if (reqDTO.getMultiplication()==null)
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"倍增系数不能为空");
 
        GasCategoryConfiguration byOrientation = gasCategoryConfigurationRepository.findByGasCategoryIdAndOrientation(reqDTO.getGasCategoryId(), reqDTO.getOrientation());
        if (byOrientation!=null)
            throw new BusinessException(this.getClass(),ResultCode.BUSINESS_ERROR.getCode(),"同一方位不能重复添加");
 
        User currentUser = getCurrentUser();
        GasCategoryConfiguration gasCategoryConfiguration = new GasCategoryConfiguration();
        BeanUtils.copyProperties(reqDTO,gasCategoryConfiguration);
        gasCategoryConfiguration.setCreateBy(currentUser.getRealName());
        gasCategoryConfiguration.setCreateTime(LocalDateTime.now());
        gasCategoryConfiguration.setUpdateTime(LocalDateTime.now());
        gasCategoryConfiguration.setUpdateBy(currentUser.getRealName());
        gasCategoryConfiguration.setDelFlag(0);
        GasCategoryConfiguration save = gasCategoryConfigurationRepository.save(gasCategoryConfiguration);
        if (save==null)
            throw new BusinessException(this.getClass(),ResultCode.SYSTEM_ERROR_DATABASE_FAIL.getCode(),"新增气体参数失败");
        return Result.success();
    }
}