张晓波
2023-12-04 b58e87a6463ebf5109c49489c35260d7e8708653
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package com.thhy.mobile.modules.biz.mouldcheck.service.impl;
 
import com.alibaba.nacos.api.utils.StringUtils;
import com.thhy.general.common.BasicStatus;
import com.thhy.general.common.enums.MouldFreeType;
import com.thhy.general.common.enums.PipeOutModType;
import com.thhy.general.config.SysUserInfo;
import com.thhy.general.exception.BasicException;
import com.thhy.general.utils.UUIDUtils;
import com.thhy.general.utils.UserInfoUtils;
import com.thhy.mobile.modules.biz.mouldcheck.entity.*;
import com.thhy.mobile.modules.biz.mouldcheck.mapper.MouldCheckMapper;
import com.thhy.mobile.modules.biz.mouldcheck.mapper.MouldCheckRecordMapper;
import com.thhy.mobile.modules.biz.mouldcheck.mapper.MouldMapper;
import com.thhy.mobile.modules.biz.mouldcheck.service.ModCheckService;
import com.thhy.mobile.modules.biz.pipecheckrecord.entity.MaterialStockRecord;
import com.thhy.mobile.modules.biz.pipeinfo.entity.MaterialInfo;
import com.thhy.mobile.modules.biz.pipeinfo.entity.PipeInfo;
import com.thhy.mobile.modules.biz.pipeinfo.mapper.PipeInfoMapper;
import com.thhy.mobile.modules.biz.steelcheck.entity.SteelStockRecord;
import com.thhy.mobile.modules.biz.steelproduce.entity.ProduceDto;
import com.thhy.mobile.modules.biz.steelproduce.entity.SteelProduce;
import com.thhy.mobile.modules.biz.steelproduce.mapper.SteelProduceMapper;
import com.thhy.mobile.modules.biz.steelstock.entity.SteelStock;
import com.thhy.mobile.modules.biz.steelstock.mapper.SteelStockMapper;
import org.hibernate.validator.constraints.ModCheck;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
 
@Service
public class ModCheckServiceImpl implements ModCheckService {
 
    @Autowired
    private MouldMapper mouldMapper;
 
    @Autowired
    private MouldCheckMapper checkMapper;
 
    @Autowired
    private SteelProduceMapper steelProduceMapper;
 
    @Autowired
    private MouldCheckRecordMapper mcRecordMapper;
 
    @Autowired
    private SteelStockMapper steelStockMapper;
 
    @Autowired
    private PipeInfoMapper pipeInfoMapper;
 
    @Autowired
    private MouldCheckRecordMapper checkRecordMapper;
 
    @Autowired
    private RedissonClient redissonClient;
 
    @Override
    @Transactional
    public MouldCheck check(ModCheckDto modCheckDto) {
        SysUserInfo userInfo = UserInfoUtils.getInstance().getUserInfo();
        String mouldNum = modCheckDto.getMouldNum();  //这里是模具ID
        RLock lock = redissonClient.getLock("modcheck"+mouldNum);
        try {
            if(lock.tryLock(5,10,TimeUnit.SECONDS)){
                if(StringUtils.isEmpty(mouldNum)){
                    throw new BasicException(BasicStatus.SCAN_QR_IS_EORROR);
                }
                //Mould mould = mouldMapper.queryByMouldNum(modCheckDto.getMouldNum());
                Mould mould = mouldMapper.queryByMouldId(modCheckDto.getMouldNum());
                if(mould==null){
                    throw new BasicException(BasicStatus.SCAN_QR_IS_EORROR);
                }
                //记录
                if(mould.getFree()==MouldFreeType.AL_USE.getValue()){
                    throw new BasicException(BasicStatus.MOULD_ALREAY_USE);
                }
                int count = checkMapper.countByMouldNum(mould.getMouldId());
                if(count>0){
                    throw new BasicException(BasicStatus.MOULD_ALREAY_USE);
                }
 
                MouldCheck mouldCheck = new MouldCheck(mould.getMouldId(),userInfo.getUserId());
                checkMapper.insert(mouldCheck);
                return mouldCheck;
            }else{
                throw new BasicException(BasicStatus.OP_IS_TIMEOUT);
            }
        }catch (InterruptedException e){
            throw new BasicException(BasicStatus.OP_IS_REPEAT);
        }finally {
            if(lock.isLocked()){
                lock.unlock();
            }
        }
    }
 
    @Override
    @Transactional
    public void matchSteel(ModCheckDto modCheckDto) {
 
        RLock lock = redissonClient.getLock("matchsteel"+modCheckDto.getProduceNumber());
        try {
            if (lock.tryLock(5, 10, TimeUnit.SECONDS)) {
 
                RLock innerLock = redissonClient.getLock("matchsteel"+modCheckDto.getModCheckId());
 
                try {
                    if(innerLock.tryLock(5,10,TimeUnit.SECONDS)){
                        SysUserInfo userInfo = UserInfoUtils.getInstance().getUserInfo();
                        MouldCheck mouldCheck = checkMapper.queryById(modCheckDto.getModCheckId());
                        if(mouldCheck==null){
                            throw new BasicException(BasicStatus.SCAN_QR_IS_EORROR);
                        }
                        if(mouldCheck.getMatchStatus()!=1){
                            throw new BasicException(BasicStatus.MOULD_ALREAD_MATCH);
                        }
                        Mould mould = mouldMapper.queryById(mouldCheck.getModId());
 
                        int count = checkRecordMapper.steelCountByProNum(modCheckDto.getProduceNumber());
                        if(count>0){
                            throw new BasicException(BasicStatus.STEEL_IS_MATCHED);
                        }
                        SteelProduce steelProduce = steelProduceMapper.queryByProduceNum(modCheckDto.getProduceNumber());
                        if(steelProduce.getIsQualified()==null||steelProduce.getIsQualified()!=1){
                            throw new BasicException(BasicStatus.STEEL_IS_NOT_PASS);
                        }
                        boolean compareResult = CompareParam.compare(mould,steelProduce);
                        MouldCheckRecord mcr = new MouldCheckRecord();
                        mcr.setModCheckId(mouldCheck.getModCheckId()).setSteelProNum(modCheckDto.getProduceNumber()).setCreateUser(userInfo.getUserId());
                        mcr.setMatchResult(compareResult?2:3);
                        mcRecordMapper.insert(mcr);
                        checkMapper.updateMatchStatus(new MouldCheck(modCheckDto.getModCheckId(),compareResult?2:3));
 
                        if(compareResult){//匹配成功
                            //更改钢筋笼产品入模状态
                            steelProduceMapper.updateisModel(new ProduceDto(1,modCheckDto.getProduceNumber()));
                            //钢筋笼库存记录
                            SteelStockRecord steelStockRecord = new SteelStockRecord(steelProduce,true);
                            steelStockRecord.setModCheckId(mouldCheck.getModCheckId());
                            steelStockMapper.insertSteelStockRecord(steelStockRecord);
                            //减掉钢筋笼库存
                            steelStockMapper.updateMinusStock(new SteelStock(steelProduce));
                            //更新模具循环次数
                            mouldMapper.updateCycle(mould.getMouldId());
                            //管片信息
                            String proNumber = steelProduceMapper.queryProNumByProduce(modCheckDto.getProduceNumber());
                            int countPipeInPro = pipeInfoMapper.queryMaxPipeNumByProId(steelProduce.getProId());
                            PipeInfo pipeInfo = new PipeInfo(mould,steelProduce);
                            pipeInfo.setIntoModUser(userInfo.getUserId());
                            pipeInfo.setIntoModTime(new Date());
                            pipeInfo.setOutMod(PipeOutModType.IN.getValue()); //入模
                            pipeInfo.setPipeNum(UUIDUtils.createPipeNum(proNumber,countPipeInPro));
                            //pipeInfo.setRingNum();
                            RingCompleteCheck completeCheck = pipeInfoMapper.queryRingCompleteByMod(mould.getMouldNum(),pipeInfo.getProId());
                            if(completeCheck==null){
                                int maxRingNum = pipeInfoMapper.queryMaxRingNum(userInfo.getCompanyId());
                                pipeInfo.setRingNum(UUIDUtils.createRingNum(maxRingNum));
                            }else {
                                pipeInfo.setRingNum(completeCheck.getRingnum());
                            }
                            pipeInfoMapper.insert(pipeInfo);
                            pipeInfoMapper.updateSysMouldFree(new MouldFree(pipeInfo.getModId(), MouldFreeType.AL_USE.getValue()));
                        }
                    }else{
                        throw new BasicException(BasicStatus.OP_IS_TIMEOUT);
                    }
                }catch (InterruptedException e){
                    throw new BasicException(BasicStatus.OP_IS_REPEAT);
                }finally {
                    if (innerLock.isLocked()) {
                        innerLock.unlock();
                    }
                }
            } else {
                throw new BasicException(BasicStatus.OP_IS_TIMEOUT);
            }
        } catch (InterruptedException e) {
            throw new BasicException(BasicStatus.OP_IS_REPEAT);
        } finally {
            if (lock.isLocked()) {
                lock.unlock();
            }
        }
 
    }
 
    @Override
    @Transactional
    public void addPipe(ModCheckDto modCheckDto) {
        SysUserInfo userInfo = UserInfoUtils.getInstance().getUserInfo();
        if(StringUtils.isEmpty(userInfo.getCompanyId())){
            throw new BasicException(BasicStatus.USER_NOT_HAS_COMPANY);
        }
 
        String ringNum = modCheckDto.getRingNum();
        String proId = modCheckDto.getProId();
 
        try {
            if(ringNum.length()>4){
                throw new BasicException(BasicStatus.RING_NOT_DIS);
            }
            Integer.parseInt(ringNum);
        } catch (NumberFormatException e) {
            throw new BasicException(BasicStatus.RING_NOT_DIS);
        }
 
        String newRingNum = UUIDUtils.addZeroNoPlus(Integer.parseInt(ringNum),4);
 
        int count = pipeInfoMapper.countByRingNum(newRingNum);
        if(count>0){
            throw new BasicException(BasicStatus.RING_NUM_IS_REPEAT);
        }
 
        List<Mould> mouldList = pipeInfoMapper.queryMouldByNumType(proId,modCheckDto.getNumType());
        /*mouldList.forEach(ml->{
            ml = mouldMapper.queryById(ml.getMouldId());
        });*/
        //List<String> blockNums = pipeInfoMapper.queryBlockByPro(proId);
 
        SteelProduce steelProduce = new SteelProduce();
        steelProduce.setCompanyId(userInfo.getCompanyId());
        steelProduce.setProId(proId);
 
        steelProduce.setIsQualified(1);
        steelProduce.setQualityTime(new Date());
        steelProduce.setQualityUser(userInfo.getUserId());
        steelProduce.setIsModel(1);
        steelProduce.setCreateUser(userInfo.getUserId());
        steelProduce.setIsUse(1);
        steelProduce.setReinforcementId("02d336f47ea0eff7ec70c64f"); //配筋写死值
 
        for(Mould m : mouldList){
            //m = mouldMapper.queryById(m.getMouldId());
            String proNumber = steelProduceMapper.queryProNumByProject(proId);
            int maxSteelNum = pipeInfoMapper.queryMaxNumBySteelProduce();
            steelProduce.setProduceNumber(UUIDUtils.createSteelNum(proNumber,maxSteelNum));
            steelProduce.setSizeId(m.getMouldSize());
            steelProduce.setBlockNum(m.getMouldType());
            steelProduceMapper.insert(steelProduce);
 
            PipeInfo pipeInfo = new PipeInfo(m,steelProduce);
            pipeInfo.setIntoModUser(userInfo.getUserId());
            pipeInfo.setIntoModTime(new Date());
            pipeInfo.setOutMod(PipeOutModType.IN.getValue()); //入模
 
 
            int countPipeInPro = pipeInfoMapper.queryMaxPipeNumByProId(steelProduce.getProId());
            pipeInfo.setPipeNum(UUIDUtils.createPipeNum(proNumber,countPipeInPro));
            //pipeInfo.setRingNum();
            pipeInfo.setRingNum(newRingNum);
            pipeInfo.setCreateFlag(2);
            pipeInfoMapper.insert(pipeInfo);
        }
 
 
 
        /*SteelProduce steelProduce = steelProduceMapper.queryByProduceNum(modCheckDto.getProduceNumber());
        if(steelProduce.getIsQualified()==null||steelProduce.getIsQualified()!=1){
            throw new BasicException(BasicStatus.STEEL_IS_NOT_PASS);
        }
        boolean compareResult = CompareParam.compare(mould,steelProduce);
 
        if(compareResult){//匹配成功
            //更改钢筋笼产品入模状态
            steelProduceMapper.updateisModel(new ProduceDto(1,modCheckDto.getProduceNumber()));
            //减掉钢筋笼库存
            steelStockMapper.updateMinusStock(new SteelStock(steelProduce));
            //更新模具循环次数
            mouldMapper.updateCycle(mould.getMouldId());
            //管片信息
            String proNumber = steelProduceMapper.queryProNumByProduce(modCheckDto.getProduceNumber());
            int countPipeInPro = pipeInfoMapper.queryMaxPipeNumByProId(steelProduce.getProId());
            PipeInfo pipeInfo = new PipeInfo(mould,steelProduce);
            pipeInfo.setIntoModUser(userInfo.getUserId());
            pipeInfo.setIntoModTime(new Date());
            pipeInfo.setOutMod(PipeOutModType.IN.getValue()); //入模
            pipeInfo.setPipeNum(UUIDUtils.createPipeNum(proNumber,countPipeInPro));
            //pipeInfo.setRingNum();
            RingCompleteCheck completeCheck = pipeInfoMapper.queryRingComplete(pipeInfo);
            if(completeCheck==null){
                int maxRingNum = pipeInfoMapper.queryMaxRingNum(userInfo.getCompanyId());
                pipeInfo.setRingNum(UUIDUtils.createRingNum(maxRingNum));
            }else {
                pipeInfo.setRingNum(completeCheck.getRingnum());
            }
            pipeInfoMapper.insert(pipeInfo);
            //pipeInfoMapper.updateSysMouldFree(new MouldFree(pipeInfo.getModId(), MouldFreeType.AL_USE.getValue()));
        }*/
    }
 
 
 
    @Override
    public List<MouldCheckListVo> findModCheckList(ModCheckDto modCheckDto) {
        SysUserInfo userInfo = UserInfoUtils.getInstance().getUserInfo();
        if(modCheckDto==null)modCheckDto = new ModCheckDto();
        modCheckDto.setCompanyId(userInfo.getCompanyId());
        return checkMapper.ModCheckList(modCheckDto);
    }
 
    @Override
    @Transactional
    public void delete(ModCheckDto modCheckDto) {
        //checkMapper.deletelogic(modCheckDto.getModCheckId());
        MouldCheck mouldCheck = checkMapper.queryById(modCheckDto.getModCheckId());
        if(mouldCheck.getMatchStatus()==3||mouldCheck.getMatchStatus()==1){
            checkMapper.deletelogic(modCheckDto.getModCheckId());
            //记录删除标识
            checkRecordMapper.deleteByCheckId(modCheckDto.getModCheckId());
            return;
        }
        MouldCheckRecord mouldCheckRecord = checkRecordMapper.mouldCheckRecord(mouldCheck.getModCheckId());
        //判断管片状态
        int isCheck = pipeInfoMapper.queryIsCheckBySteelNum(mouldCheckRecord.getSteelProNum());
        if(isCheck!=0){
            throw new BasicException(BasicStatus.PIPE_IS_CHECK_COMPLETE);
        }
 
        //更改钢筋笼产品入模状态
        steelProduceMapper.updateisModel(new ProduceDto(2,mouldCheckRecord.getSteelProNum()));
        //加钢筋笼库存
        SteelProduce steelProduce = steelProduceMapper.queryByProduceNum(mouldCheckRecord.getSteelProNum());
        steelStockMapper.updatePlusStock(new SteelStock(steelProduce));
        //更新模具循环次数
        mouldMapper.updateCycleMinus(mouldCheck.getModId());
        //删除管片信息
        pipeInfoMapper.deleteBySteelProNum(mouldCheckRecord.getSteelProNum());
        //
        pipeInfoMapper.updateSysMouldFree(new MouldFree(mouldCheck.getModId(), MouldFreeType.FREE.getValue()));
        checkMapper.deletelogic(modCheckDto.getModCheckId());
        //记录删除标识
        checkRecordMapper.deleteByCheckId(modCheckDto.getModCheckId());
 
    }
 
    @Override
    public Map<String, Object> getH5MouldInfo(String mouldId) {
        return mouldMapper.getH5MouldInfo(mouldId);
    }
 
    @Override
    public List<Integer> allNumType() {
        return pipeInfoMapper.queryAllNumType();
    }
}