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(); List completeCheckList = pipeInfoMapper.queryRingCompleteByMod(mould.getMouldNum(),pipeInfo.getProId()); if(completeCheckList.isEmpty()){ int maxRingNum = pipeInfoMapper.queryMaxRingNum(userInfo.getCompanyId()); pipeInfo.setRingNum(UUIDUtils.createRingNum(maxRingNum)); }else { pipeInfo.setRingNum(completeCheckList.get(0).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 mouldList = pipeInfoMapper.queryMouldByNumType(proId,modCheckDto.getNumType()); /*mouldList.forEach(ml->{ ml = mouldMapper.queryById(ml.getMouldId()); });*/ //List 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 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 getH5MouldInfo(String mouldId) { return mouldMapper.getH5MouldInfo(mouldId); } @Override public List allNumType() { return pipeInfoMapper.queryAllNumType(); } }