heheng
2025-10-15 a4f1e1a9b97f7606347ba1b6a5c5957c3fc28a59
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
package com.gkhy.system.service.impl;
 
 
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.common.constant.Constants;
import com.gkhy.common.core.domain.entity.SysRole;
import com.gkhy.common.core.domain.entity.SysUser;
import com.gkhy.common.core.domain.model.LoginUser;
import com.gkhy.common.utils.SecurityUtils;
import com.gkhy.system.domain.Hazards;
import com.gkhy.system.domain.dto.HazardsEditDTO;
import com.gkhy.system.mapper.HazardsMapper;
import com.gkhy.system.service.HazardsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.time.LocalDateTime;
import java.util.List;
 
/**
 * <p>
 * 隐患上报/整改/台账 服务实现类
 * </p>
 *
 * @author hh
 * @since 2025-09-08 10:36:52
 */
@Service
public class HazardsServiceImpl extends ServiceImpl<HazardsMapper, Hazards> implements HazardsService {
 
 
    @Autowired
    private HazardsMapper hazardsMapper;
 
 
    @Override
    public List<Hazards> getHazardsList(Hazards hazards) {
        if (hazards != null && "2".equals(hazards.getHazardsType())){
            boolean b = check2Per();
            if (!b){
                hazards.setReformUserId(SecurityUtils.getUserId());
            }
        }
        return hazardsMapper.getHazardsList(hazards);
    }
 
    boolean check2Per(){
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId()) || SecurityUtils.hasRole(Constants.SYS_ADMIN) || SecurityUtils.hasRole(Constants.PLACE_SAFETY_OFFICER)){
            return true;
        }
        return false;
    }
 
 
    @Override
    public int saveHazards(Hazards inspection) {
        int result = 0;
        if (inspection.getId() == null){
            inspection.setCreateTime(LocalDateTime.now());
            inspection.setCreateBy(SecurityUtils.getUsername());
            result = hazardsMapper.insert(inspection);
        }else {
            inspection.setUpdateTime(LocalDateTime.now());
            inspection.setUpdateBy(SecurityUtils.getUsername());
            result = hazardsMapper.updateById(inspection);
        }
        return result;
    }
 
    @Override
    public int updateHazards(HazardsEditDTO dto) {
 
        checkPer();
        Hazards inspection = new Hazards();
        inspection.setId(dto.getId());
        inspection.setReformPeriod(dto.getReformPeriod());
        inspection.setRectificationMeasures(dto.getRectificationMeasures());
        inspection.setReasonAnalysis(dto.getReasonAnalysis());
        inspection.setReformPics(dto.getReformPics());
        inspection.setUpdateTime(LocalDateTime.now());
        inspection.setUpdateBy(SecurityUtils.getUsername());
        inspection.setState(1);
        return hazardsMapper.updateById(inspection);
    }
 
    private void checkPer(){
        boolean admin = SecurityUtils.isAdmin(SecurityUtils.getUserId());
        if (!admin){
            boolean placeSafetyOfficer = SecurityUtils.hasRole(Constants.SYS_ADMIN);
            if (!placeSafetyOfficer){
                throw new RuntimeException("没有权限");
            }
//            LoginUser loginUser = SecurityUtils.getLoginUser();
//            SysUser user = loginUser.getUser();
//            List<SysRole> roles = user.getRoles();
//            long sysAdmin = roles.stream().filter(role -> role.getRoleKey().equals(Constants.SYS_ADMIN)).count();
//            if (sysAdmin == 0){
//                throw new RuntimeException("没有权限");
//            }
        }
 
    }
 
    @Override
    public int deleteHazards(Long id) {
        return hazardsMapper.update(new Hazards(),
                new LambdaUpdateWrapper<Hazards>().eq(Hazards::getId, id)
                        .set(Hazards::getDelFlag, Constants.FAIL).set(Hazards::getUpdateTime, LocalDateTime.now())
                        .set(Hazards::getUpdateBy, SecurityUtils.getUsername()));
    }
}