危化品全生命周期管理后端
“djh”
2025-04-21 437f8e2b89a18363a1073fdbb3ab99bcd840a757
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
package com.gkhy.hazmat.system.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.hazmat.common.api.CommonPage;
import com.gkhy.hazmat.common.config.IdTableNameHandler;
import com.gkhy.hazmat.common.domain.entity.SysUser;
import com.gkhy.hazmat.common.enums.CodePrexEnum;
import com.gkhy.hazmat.common.enums.UserTypeEnum;
import com.gkhy.hazmat.common.exception.ApiException;
import com.gkhy.hazmat.common.utils.PageUtils;
import com.gkhy.hazmat.common.utils.SecurityUtils;
import com.gkhy.hazmat.system.domain.HzHazmat;
import com.gkhy.hazmat.system.domain.HzHazmatFlow;
import com.gkhy.hazmat.system.mapper.HzHazmatFlowMapper;
import com.gkhy.hazmat.system.mapper.HzHazmatMapper;
import com.gkhy.hazmat.system.service.HzHazmatFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 危化品流向表 服务实现类
 * </p>
 *
 * @author kzy
 * @since 2024-08-05 14:41:40
 */
@Service
public class HzHazmatFlowServiceImpl extends ServiceImpl<HzHazmatFlowMapper, HzHazmatFlow> implements HzHazmatFlowService {
    @Autowired
    private HzHazmatMapper hazmatMapper;
 
    @Override
    public CommonPage selectHazmatFlowList(HzHazmatFlow hazmatFlow) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        if (currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())){
            IdTableNameHandler.setCurrentId(hazmatFlow.getCompanyId());
        }else {
            hazmatFlow.setCompanyId(currentUser.getCompanyId());
            //设置分表id
            //todo
            IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
        }
        PageUtils.startPage();
        List<HzHazmatFlow> flowList = baseMapper.selectHazmatFlowList(hazmatFlow);
        IdTableNameHandler.removeCurrentId();
        return CommonPage.restPage(flowList);
    }
 
    @Override
    public HzHazmatFlow selectHazmatFlowById(Long hazmatFlowId) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        //设置分表id
        IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
        HzHazmatFlow hazmatFlow = baseMapper.selectById(hazmatFlowId);
        IdTableNameHandler.removeCurrentId();
        if (!hazmatFlow.getCompanyId().equals(currentUser.getCompanyId())) {
            throw new ApiException("无权限查看其它企业数据");
        }
        return hazmatFlow;
    }
 
    @Override
    public int insertHazmatFlow(HzHazmatFlow hazmatFlow) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        hazmatFlow.setCreateBy(currentUser.getUsername());
        hazmatFlow.setCompanyId(currentUser.getCompanyId());
        checkUserAllowed(null,currentUser);
        //设置分表id
        IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
        int row = baseMapper.insert(hazmatFlow);
        IdTableNameHandler.removeCurrentId();
        if (row < 1) {
            throw new ApiException("新增危化品流向失败");
        }
        return row;
    }
 
    @Override
    public int deleteHazmatFlowById(Long hazmatFlowId) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        //设置分表id
        IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
        HzHazmatFlow hazmatFlow=baseMapper.selectById(hazmatFlowId);
        IdTableNameHandler.removeCurrentId();
        if(hazmatFlow==null){
            throw new ApiException("流向信息不存在");
        }
        checkUserAllowed(hazmatFlow,currentUser);
 
        baseMapper.deleteById(hazmatFlow);
        return 0;
    }
 
    @Override
    public List<HzHazmatFlow> selectAllHazmatFlowById(Long hazmatFlowId) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        //设置分表id
        IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
        HzHazmatFlow hazmatFlow=getById(hazmatFlowId);
        if(hazmatFlow==null){
            throw new ApiException("流向不存在");
        }
        List<HzHazmatFlow> hazmatFlowList = baseMapper.selectAllHazmatFlowByHazmatId(hazmatFlow.getHazmatId());
        IdTableNameHandler.removeCurrentId();
        return hazmatFlowList;
    }
 
    @Override
    public List<HzHazmatFlow> selectAllHazmatFlowByCode(String code,Long companyId) {
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        if(!code.startsWith(CodePrexEnum.MATERIAL.getCode())){
            throw new ApiException("条码格式不正确");
        }
        if (currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())){
            IdTableNameHandler.setCurrentId(companyId);
        }else {
            //设置分表id
            IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
        }
        HzHazmat hazmat=hazmatMapper.selectHazmatByCode(code,currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())?companyId:currentUser.getCompanyId());
        if(hazmat==null){
            throw new ApiException("危化品条码数据不存在");
        }
        List<HzHazmatFlow> hazmatFlowList = baseMapper.selectAllHazmatFlowByHazmatId(hazmat.getId());
        IdTableNameHandler.removeCurrentId();
        return hazmatFlowList;
    }
 
    @Override
    public CommonPage<HzHazmatFlow> selectAllHazmatFlowByUser() {
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        HzHazmatFlow hazmat=new HzHazmatFlow()
                .setCreateId(currentUser.getId())
                .setCompanyId(currentUser.getCompanyId());
        //设置分表id
        IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
        PageUtils.startPage();
        List<HzHazmatFlow> hazmatFlowList = baseMapper.selectAllHazmatFlowByUser(hazmat);
        IdTableNameHandler.removeCurrentId();
        return CommonPage.restPage(hazmatFlowList);
    }
 
    @Override
    public List<HzHazmatFlow> getAllHazmatFlowByHazmatId(Long hazmatId,Long companyId) {
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        if (currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())){
            IdTableNameHandler.setCurrentId(companyId);
        }else {
            //设置分表id
            IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
        }
        HzHazmat hazmat=hazmatMapper.selectById(hazmatId);
        if(hazmat==null){
            throw new ApiException("危化品数据不存在");
        }
        checkUserAllowed(new HzHazmatFlow().setCompanyId(hazmat.getCompanyId()),currentUser);
        List<HzHazmatFlow> hazmatFlowList = baseMapper.selectAllHazmatFlowByHazmatId(hazmatId);
        IdTableNameHandler.removeCurrentId();
        return hazmatFlowList;
    }
 
    public void checkUserAllowed(HzHazmatFlow hazmatFlow,SysUser user) {
        if (user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
            throw new ApiException("管理员不能操作");
        }
        if(hazmatFlow!=null){
            if (!user.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())){
                if(!Objects.equals(user.getCompanyId(), hazmatFlow.getCompanyId())){
                    throw new ApiException("无权限操作其他企业数据");
                }
            }
        }
    }
}