危化品全生命周期管理后端
heheng
2025-02-26 996c091a4913ac768324b7ea69a8494ba9d6ece0
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
package com.gkhy.hazmat.system.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.hazmat.common.api.CommonPage;
import com.gkhy.hazmat.common.constant.Constant;
import com.gkhy.hazmat.common.domain.entity.SysUser;
import com.gkhy.hazmat.common.enums.UserTypeEnum;
import com.gkhy.hazmat.common.exception.ApiException;
import com.gkhy.hazmat.common.service.RedisService;
import com.gkhy.hazmat.common.utils.PageUtils;
import com.gkhy.hazmat.common.utils.SecurityUtils;
import com.gkhy.hazmat.system.domain.HzProduct;
import com.gkhy.hazmat.system.domain.SysConfig;
import com.gkhy.hazmat.system.domain.TaBooComparison;
import com.gkhy.hazmat.system.mapper.TaBooComparisonMapper;
import com.gkhy.hazmat.system.service.TaBooComparisonService;
import org.springframework.stereotype.Service;
 
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 系统配置表 服务实现类
 * </p>
 *
 * @author kzy
 * @since 2023-11-13 08:39:55
 */
@Service
public class TaBooComparisonServiceImpl extends ServiceImpl<TaBooComparisonMapper, TaBooComparison> implements TaBooComparisonService {
 
    @Resource
    private RedisService redisService;
 
    @PostConstruct
    public void init(){
        loadingTaBooCache();
    }
 
 
    @Override
    public CommonPage selectTaBooComparisonList(TaBooComparison taBooComparison) {
        PageUtils.startPage();
        List<TaBooComparison> taBooComparisonList=baseMapper.selectTaBooComparisonList(taBooComparison);
        return CommonPage.restPage(taBooComparisonList);
    }
 
    @Override
    public int insertTaBooComparison(TaBooComparison taBooComparison) {
        checkUserAllowed();
        checkData(taBooComparison);
        taBooComparison.setCreateBy(SecurityUtils.getUsername());
        taBooComparison.setUniqueCode(taBooComparison.getLeftCode()+"_"+taBooComparison.getTopCode());
        int row=baseMapper.insert(taBooComparison);
        if(row<1){
            throw new ApiException("新增失败");
        }
        // 更新缓存
        loadingTaBooCache();
        return row;
    }
 
    @Override
    public int updateTaBooComparison(TaBooComparison taBooComparison) {
        checkUserAllowed();
        checkData(taBooComparison);
        taBooComparison.setUpdateBy(SecurityUtils.getUsername());
        taBooComparison.setUniqueCode(taBooComparison.getLeftCode()+"_"+taBooComparison.getTopCode());
        int row=baseMapper.updateById(taBooComparison);
        if(row<1){
            throw new ApiException("更新失败");
        }
        // 更新缓存
        loadingTaBooCache();
        return row;
    }
 
    public void checkUserAllowed() {
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            throw new ApiException("管理员不能操作");
        }
    }
    @Override
    public void loadingTaBooCache(){
        List<TaBooComparison> taBooComparisons=baseMapper.selectTaBooComparison();
        Map<String, Long> collect = taBooComparisons.stream()
                .collect(Collectors.toMap(
                        TaBooComparison::getUniqueCode,
                        TaBooComparison::getWarningType
                ));
        redisService.set(Constant.TABOO_UNIQUE_KEY,collect);
       // DictUtils.setTabooCache("TABOO_UNIQUE_KEY",collect);
    }
 
    @Override
    public void deleteTaBooComparisonById(Long taBooComparisonId) {
        checkUserAllowed();
        baseMapper.deleteById(taBooComparisonId);
        // 更新缓存
        loadingTaBooCache();
    }
 
    private void checkData(TaBooComparison taBooComparison){
        LambdaQueryWrapper<TaBooComparison> lambdaQueryWrapper = Wrappers.<TaBooComparison>lambdaQuery()
                .and(wrapper -> wrapper.eq(TaBooComparison::getLeftCode, taBooComparison.getLeftCode())
                        .or()
                        .eq(TaBooComparison::getLeftCode, taBooComparison.getTopCode()))
                .and(wrapper -> wrapper.eq(TaBooComparison::getTopCode, taBooComparison.getLeftCode())
                        .or()
                        .eq(TaBooComparison::getTopCode, taBooComparison.getTopCode()));
        if (taBooComparison.getId() != null){
            lambdaQueryWrapper.ne(TaBooComparison::getId, taBooComparison.getId());
        }
        Long l = baseMapper.selectCount(lambdaQueryWrapper);
        if (l > 0){
            throw new ApiException("该数据已存在");
        }
 
 
 
    }
 
}