危化品全生命周期管理后端
“djh”
2025-02-24 34f448ffe2aacb496c15ab5da44a24128e0682be
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
258
259
260
261
262
263
264
265
package com.gkhy.hazmat.system.service.impl;
 
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
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.EntryStateEnum;
import com.gkhy.hazmat.common.enums.HazmatStatusEnum;
import com.gkhy.hazmat.common.enums.UserTypeEnum;
import com.gkhy.hazmat.common.exception.ApiException;
import com.gkhy.hazmat.common.listener.CustomEventPublisher;
import com.gkhy.hazmat.common.utils.PageUtils;
import com.gkhy.hazmat.common.utils.SecurityUtils;
import com.gkhy.hazmat.common.utils.StringUtils;
import com.gkhy.hazmat.system.domain.*;
import com.gkhy.hazmat.system.mapper.*;
import com.gkhy.hazmat.system.service.HzProductEntryRecordService;
import com.gkhy.hazmat.system.service.HzProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
 
/**
 * <p>
 * 成品入库记录表 服务实现类
 * </p>
 *
 * @author kzy
 * @since 2024-08-06 16:06:49
 */
@Service
public class HzProductEntryRecordServiceImpl extends ServiceImpl<HzProductEntryRecordMapper, HzProductEntryRecord> implements HzProductEntryRecordService {
    @Autowired
    private HzProductBasicMapper productBasicMapper;
    @Autowired
    private HzProductMapper productMapper;
    @Autowired
    private HzProductService productService;
    @Autowired
    private HzProductWarehouseRecordMapper productWarehouseRecordMapper;
    @Autowired
    private CustomEventPublisher customEventPublisher;
    @Autowired
    private SysCompanyMapper companyMapper;
 
    @Override
    public CommonPage selectEntryRecordList(HzProductEntryRecord entryRecord) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        if (!currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
            entryRecord.setCompanyId(currentUser.getCompanyId());
        }
        PageUtils.startPage();
        List<HzProductEntryRecord> entryRecordList = baseMapper.selectEntryRecordList(entryRecord);
        return CommonPage.restPage(entryRecordList);
    }
 
    @Override
    public HzProductEntryRecord selectEntryRecordById(Long entryRecordId) {
        HzProductEntryRecord entryRecord = baseMapper.selectById(entryRecordId);
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        if (currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
            return entryRecord;
        } else if (!entryRecord.getCompanyId().equals(currentUser.getCompanyId())) {
            throw new ApiException("无权限查看其它企业数据");
        }
        return entryRecord;
    }
 
    @Override
    public int insertEntryRecord(HzProductEntryRecord entryRecord) {
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        HzProductBasic productBasic=productBasicMapper.selectById(entryRecord.getBasicId());
        if(productBasic==null){
            throw new ApiException("成品基础数据不存在");
        }
        if(entryRecord.getNum()>productBasic.getMaxEntry()){
            throw new ApiException("数量超过单次入库最大数量<"+productBasic.getMaxEntry()+">");
        }
        entryRecord.setCompanyId(currentUser.getCompanyId());
        entryRecord.setCreateBy(currentUser.getUsername());
        checkUserAllowed(null,currentUser);
        int row=0;
        synchronized (this) {
            //生成条码
            generateCode(entryRecord);
            row = baseMapper.insert(entryRecord);
            if (row < 1) {
                throw new ApiException("新增入库记录失败");
            }
        }
        return row;
    }
 
    public void generateCode(HzProductEntryRecord entryRecord){
        SysCompany company=companyMapper.selectById(entryRecord.getCompanyId());
        String code=company.getCode();
        if(StringUtils.isBlank(code)){
            throw new ApiException("公司两位编码为空");
        }
        String currentDate= DateUtil.format(new Date(), DatePattern.PURE_DATE_FORMAT);
        StringBuilder prefixBuilder=new StringBuilder().append(CodePrexEnum.GOOD.getCode())
                .append(code)
                .append(currentDate);
        HzProductEntryRecord per=baseMapper.selectLastEntryRecord(prefixBuilder.toString(),entryRecord.getCompanyId());
        int startCode=1;
        int endCode=startCode+entryRecord.getNum()-1;
        if(per!=null){
            startCode=per.getEndCode()+1;
            endCode=startCode+entryRecord.getNum()-1;
        }
        entryRecord.setCodePrex(prefixBuilder.toString());
        entryRecord.setStartCode(startCode);
        entryRecord.setEndCode(endCode);
        entryRecord.setState(EntryStateEnum.UNENTER.getCode());
    }
 
    @Override
    public int deleteEntryRecordById(Long entryRecordId) {
        HzProductEntryRecord entryRecord=baseMapper.selectById(entryRecordId);
        if(entryRecord==null){
            throw new ApiException("入库记录不存在");
        }
        if(entryRecord.getState().equals(EntryStateEnum.ENTER.getCode())){
            throw new ApiException("已入库的记录不能删除");
        }
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(entryRecord,currentUser);
        baseMapper.deleteById(entryRecordId);
        return 0;
    }
 
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void doEntry(Long entryRecordId) {
        HzProductEntryRecord entryRecord=getById(entryRecordId);
        if(entryRecord.getState().equals(EntryStateEnum.ENTER.getCode())){
            throw new ApiException("已完成入库,不能再操作");
        }
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(entryRecord,currentUser);
        HzProductBasic productBasic=productBasicMapper.selectById(entryRecord.getBasicId());
        if(productBasic==null){
            throw new ApiException("成品基础数据不存在");
        }
        //设置分表id
        IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
  //      synchronized (this) {
            //获取当前仓库库存
            int count = productMapper.selectProductCountOfWarehouse(entryRecord.getWarehouseId(), productBasic.getId(), currentUser.getCompanyId());
            //新增危化品变动记录
            HzProductWarehouseRecord warehouseRecord = new HzProductWarehouseRecord()
                    .setWarehouseId(entryRecord.getWarehouseId())
                    .setCupboardId(entryRecord.getCupboardId())
                    .setBasicId(productBasic.getId())
                    .setNum(entryRecord.getNum())
                    .setCompanyId(currentUser.getCompanyId())
                    .setCreateId(currentUser.getId())
                    .setRemaining(entryRecord.getNum() + count);
            productWarehouseRecordMapper.insert(warehouseRecord);
            int startCode=entryRecord.getStartCode();
            int endCode=entryRecord.getEndCode();
            List<HzProduct> productList = new ArrayList<>();
            for (int i = startCode; i <=endCode; i++) {
                String lastCode= StringUtils.addZeroForNum(String.valueOf(i),4);
                String code=String.format("%s%s",entryRecord.getCodePrex(),lastCode);
                productList.add(new HzProduct().setWarehouseId(entryRecord.getWarehouseId())
                                .setCupboardId(entryRecord.getCupboardId())
                        .setBasicId(entryRecord.getBasicId())
                        .setEntryId(entryRecord.getId())
                        .setRemaining(productBasic.getMetering())
                        .setCompanyId(currentUser.getCompanyId())
                        .setState(HazmatStatusEnum.WAREHOUSEIN.getCode())
                        .setCode(code));
            }
            //批量创建危化品
            if (productList.size() > 100) {
                while (!productList.isEmpty()) {
                    int endIndex = Math.min(productList.size(), 100);
                    List<HzProduct> subList = productList.subList(0, endIndex);
                    productService.saveBatch(subList);
                    productList = productList.subList(endIndex, productList.size());
                }
            } else {
                productService.saveBatch(productList);
            }
   //     }
        //更新入库记录状态
        entryRecord.setState(EntryStateEnum.ENTER.getCode());
        updateById(new HzProductEntryRecord().setId(entryRecord.getId()).setState(EntryStateEnum.ENTER.getCode()));
        IdTableNameHandler.removeCurrentId();
        //异步执行
        // 注册一个事务完成后执行的回调
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
            @Override
            public void afterCommit() {
                customEventPublisher.publishEntry(entryRecord.getId(), CodePrexEnum.GOOD.getCode(), currentUser.getCompanyId(), currentUser.getId());
            }
        });
    }
 
 
 
 
    @Override
    public int updateEntryRecord(HzProductEntryRecord entryRecord) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(entryRecord,currentUser);
        HzProductBasic productBasic=productBasicMapper.selectById(entryRecord.getBasicId());
        if(productBasic==null){
            throw new ApiException("成品基础数据不存在");
        }
        if(entryRecord.getNum()>productBasic.getMaxEntry()){
            throw new ApiException("数量超过单次入库最大数量<"+productBasic.getMaxEntry()+">");
        }
        HzProductEntryRecord existEr=baseMapper.selectById(entryRecord.getId());
        if(existEr.getState().equals(EntryStateEnum.ENTER.getCode())){
            throw new ApiException("已经入库,不能再修改");
        }
 
        entryRecord.setUpdateBy(currentUser.getUsername());
        int row=baseMapper.updateById(entryRecord);
        if(row<1){
            throw new ApiException("更新入库信息失败");
        }
        return row;
    }
 
    @Override
    public CommonPage selectProductListByEntryId(Long entryId) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        HzProductEntryRecord entryRecord=getById(entryId);
        if(entryRecord==null){
            throw new ApiException("入库信息不存在");
        }
        checkUserAllowed(entryRecord,currentUser);
        //设置分表id
        IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
        PageUtils.startPage();
        List<HzProduct> productList = productMapper.selectProductList(new HzProduct().setEntryId(entryId));
        IdTableNameHandler.removeCurrentId();
        return CommonPage.restPage(productList);
 
    }
 
    public void checkUserAllowed(HzProductEntryRecord entryRecord,SysUser user) {
        if (user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
            throw new ApiException("管理员不能操作");
        }
        if(entryRecord!=null){
            if(!Objects.equals(user.getCompanyId(), entryRecord.getCompanyId())){
                throw new ApiException("无权限操作其他企业数据");
            }
        }
    }
}