“djh”
2 days ago aad364bb323a1eaa0389ee5c6389bdc0ea7ed526
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
 
package com.gkhy.hazmat.system.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.hazmat.common.api.CommonPage;
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.HzHazmatBasic;
import com.gkhy.hazmat.system.domain.Subscribe;
import com.gkhy.hazmat.system.domain.SubscribeHazmat;
import com.gkhy.hazmat.system.mapper.HzHazmatBasicMapper;
import com.gkhy.hazmat.system.mapper.SubscribeHazmatMapper;
import com.gkhy.hazmat.system.mapper.SubscribeMapper;
import com.gkhy.hazmat.system.service.SubscribeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
 
@Service
public class SubscribeServiceImpl extends ServiceImpl<SubscribeMapper, Subscribe> implements SubscribeService {
 
    @Resource
    private SubscribeMapper subscribeMapper;
 
    @Autowired
    private SubscribeHazmatMapper subscribeHazmatMapper;
 
    @Autowired
    private HzHazmatBasicMapper hazmatBasicMapper;
    @Override
    public CommonPage<Subscribe> selectSubscribeList(Subscribe subscribe) {
        Integer currentUserId = SecurityUtils.getLoginUser().getUser().getUserType();
        boolean isAdmin = SecurityUtils.isSystemUser(currentUserId);
 
        if (!isAdmin) {
            if (subscribe.getSubscribePersonId() == null) {
                subscribe.setSubscribePersonId(SecurityUtils.getUserId());
            } else {
                if (!subscribe.getSubscribePersonId().equals(SecurityUtils.getUserId())) {
                    throw new ApiException("无权限查看其他用户的申购单");
                }
            }
        }
 
        PageUtils.startPage();
        List<Subscribe> subscribeList = subscribeMapper.selectSubscribeList(subscribe);
        for (Subscribe subscribe1 : subscribeList) {
            LambdaQueryWrapper<SubscribeHazmat> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SubscribeHazmat::getSubscribeId, subscribe1.getId());
            List<SubscribeHazmat> hazmatList = subscribeHazmatMapper.selectList(wrapper);
            subscribe1.setSubscribeHazmats(hazmatList);
        }
        return CommonPage.restPage(subscribeList);
    }
 
    @Override
    public Subscribe selectSubscribeById(Long subscribeId) {
        Subscribe subscribe = subscribeMapper.selectSubscribeById(subscribeId);
        if (subscribe == null) {
            throw new ApiException("申购单不存在");
        }
 
        Integer currentUserId = SecurityUtils.getLoginUser().getUser().getUserType();
        boolean isAdmin = SecurityUtils.isSystemUser(currentUserId);
 
        if (!isAdmin && !subscribe.getSubscribePersonId().equals(SecurityUtils.getUserId())
                && !SecurityUtils.getUserId().equals(subscribe.getCheckId())) {
            throw new ApiException("无权限查看该申购单");
        }
 
        LambdaQueryWrapper<SubscribeHazmat> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SubscribeHazmat::getSubscribeId, subscribeId);
        List<SubscribeHazmat> hazmatList = subscribeHazmatMapper.selectList(wrapper);
        subscribe.setSubscribeHazmats(hazmatList);
 
        return subscribe;
    }
 
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int insertSubscribe(Subscribe subscribe) {
        Long currentUserId = SecurityUtils.getUserId();
        String currentUserName = SecurityUtils.getLoginUser().getUser().getName();
 
        subscribe.setSubscribePersonId(currentUserId);
        subscribe.setSubscribePersonName(currentUserName);
        subscribe.setCreateBy(currentUserName);
        subscribe.setCreateTime(LocalDateTime.now());
        subscribe.setSubscribeNum(generateSubscribeNum());
        int row = subscribeMapper.insertSubscribe(subscribe);
        if (row < 1) {
            throw new ApiException("新增申购单失败");
        }
 
        if (subscribe.getSubscribeHazmats() != null && !subscribe.getSubscribeHazmats().isEmpty()) {
            for (SubscribeHazmat hazmat : subscribe.getSubscribeHazmats()) {
                HzHazmatBasic hazmatBasic=hazmatBasicMapper.selectById(hazmat.getBasicId());
                if(hazmat.getHazmatCount()>hazmatBasic.getMaxEntry()){
                    throw new ApiException(hazmatBasic.getName()+"数量超过单次入库最大数量<"+hazmatBasic.getMaxEntry()+">");
                }
                hazmat.setSubscribeId(subscribe.getId());
                hazmat.setDelFlag(0);
                subscribeHazmatMapper.insert(hazmat);
            }
        }
 
        return row;
    }
 
 
    private String generateSubscribeNum() {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        return now.format(formatter);
    }
 
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int updateSubscribe(Subscribe subscribe) {
        Subscribe existSubscribe = subscribeMapper.selectById(subscribe.getId());
        if (existSubscribe == null) {
            throw new ApiException("申购单不存在");
        }
 
        if (existSubscribe.getStatus() != 0) {
            throw new ApiException("只能修改待审核状态的申购单");
        }
 
        Long currentUserId = SecurityUtils.getUserId();
        boolean isAdmin = SecurityUtils.isAdmin(currentUserId);
 
        if (!isAdmin && !existSubscribe.getSubscribePersonId().equals(currentUserId)) {
            throw new ApiException("无权限修改该申购单");
        }
 
        subscribe.setUpdateBy(SecurityUtils.getUsername());
        subscribe.setUpdateTime(LocalDateTime.now());
 
        int row = subscribeMapper.updateSubscribe(subscribe);
        if (row < 1) {
            throw new ApiException("更新申购单失败");
        }
 
        if (subscribe.getSubscribeHazmats() != null) {
            LambdaQueryWrapper<SubscribeHazmat> deleteWrapper = new LambdaQueryWrapper<>();
            deleteWrapper.eq(SubscribeHazmat::getSubscribeId, subscribe.getId());
            subscribeHazmatMapper.delete(deleteWrapper);
 
            for (SubscribeHazmat hazmat : subscribe.getSubscribeHazmats()) {
                hazmat.setSubscribeId(subscribe.getId());
                hazmat.setDelFlag(0);
                subscribeHazmatMapper.insert(hazmat);
            }
        }
 
        return row;
    }
 
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int deleteSubscribeById(Long subscribeId) {
        Subscribe subscribe = subscribeMapper.selectSubscribeById(subscribeId);
        if (subscribe == null) {
            throw new ApiException("申购单不存在");
        }
 
        if (subscribe.getStatus() != 0) {
            throw new ApiException("只能删除待审核状态的申购单");
        }
 
        Long currentUserId = SecurityUtils.getUserId();
        boolean isAdmin = SecurityUtils.isAdmin(currentUserId);
 
        if (!isAdmin && !subscribe.getSubscribePersonId().equals(currentUserId)) {
            throw new ApiException("无权限删除该申购单");
        }
 
        LambdaQueryWrapper<SubscribeHazmat> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SubscribeHazmat::getSubscribeId, subscribeId);
        SubscribeHazmat updateHazmat = new SubscribeHazmat();
        updateHazmat.setDelFlag(1);
        subscribeHazmatMapper.update(updateHazmat, wrapper);
 
        subscribe.setDelFlag(1);
        subscribe.setUpdateBy(SecurityUtils.getUsername());
        subscribe.setUpdateTime(LocalDateTime.now());
 
        return subscribeMapper.updateSubscribe(subscribe);
    }
 
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void auditSubscribe(Long subscribeId, Integer status,String opinion) {
        if (status == null || (status != 1 && status != 2)) {
            throw new ApiException("审核状态不正确,1为通过,2为驳回");
        }
        if (status == 2 && opinion == null) {
            throw new ApiException("通过审核时,请填写审核意见");
        }
 
        Subscribe subscribe = subscribeMapper.selectSubscribeById(subscribeId);
        if (subscribe == null) {
            throw new ApiException("申购单不存在");
        }
 
        if (subscribe.getStatus() != 0) {
            throw new ApiException("该申购单已审核,不能重复审核");
        }
 
        Long currentUserId = SecurityUtils.getUserId();
        if (subscribe.getCheckId() != null && !currentUserId.equals(subscribe.getCheckId())) {
            throw new ApiException("非审批人不可审批");
        }
 
        String currentUserName = SecurityUtils.getUsername();
 
        subscribe.setStatus(status);
        subscribe.setOpinion(opinion);
        subscribe.setUpdateBy(currentUserName);
        subscribe.setUpdateTime(LocalDateTime.now());
 
        int row = subscribeMapper.updateSubscribe(subscribe);
        if (row < 1) {
            throw new ApiException("审核操作失败");
        }
    }
 
    @Override
    public CommonPage<Subscribe> selectAuditList(Subscribe subscribe) {
        Long currentUserId = SecurityUtils.getUserId();
        boolean isAdmin = SecurityUtils.isAdmin(currentUserId);
 
        if (!isAdmin) {
            subscribe.setCheckId(currentUserId);
        }
 
        PageUtils.startPage();
        List<Subscribe> subscribeList = subscribeMapper.selectSubscribeList(subscribe);
 
        for (Subscribe subscribe1 : subscribeList) {
            LambdaQueryWrapper<SubscribeHazmat> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SubscribeHazmat::getSubscribeId, subscribe1.getId());
            wrapper.eq(SubscribeHazmat::getDelFlag, 0);
            List<SubscribeHazmat> hazmatList = subscribeHazmatMapper.selectList(wrapper);
            subscribe1.setSubscribeHazmats(hazmatList);
        }
 
        return CommonPage.restPage(subscribeList);
    }
}