package com.chinaztt.mes.quality.service.impl; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.http.HttpRequest; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.toolkit.SqlHelper; import com.chinaztt.mes.basic.entity.Part; import com.chinaztt.mes.basic.mapper.PartMapper; import com.chinaztt.mes.basic.util.AesUtils; import com.chinaztt.mes.common.numgen.NumberGenerator; import com.chinaztt.mes.quality.dto.ApplyDTO; import com.chinaztt.mes.quality.dto.ApplyPartDTO; import com.chinaztt.mes.quality.entity.*; import com.chinaztt.mes.quality.mapper.ApplyMapper; import com.chinaztt.mes.quality.mapper.ApplyPartMapper; import com.chinaztt.mes.quality.mapper.ResultMapper; import com.chinaztt.mes.quality.service.ApplyService; import com.chinaztt.mes.quality.state.result.constant.ResultStateStringValues; import com.chinaztt.mes.quality.utils.ResultUtils; import com.chinaztt.mes.quality.utils.TestApplyRuleUtils; import com.chinaztt.mes.warehouse.entity.Stock; import com.chinaztt.mes.warehouse.mapper.StockMapper; import com.chinaztt.ztt.admin.api.feign.RemoteParamService; import com.chinaztt.ztt.common.core.constant.SecurityConstants; import com.chinaztt.ztt.common.security.util.SecurityUtils; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import java.math.BigDecimal; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; /** * 检测申请表 * * @author cxf * @date 2021-04-01 13:34:45 */ @Service @AllArgsConstructor @Transactional(rollbackFor = Exception.class) public class ApplyServiceImpl extends ServiceImpl implements ApplyService { private NumberGenerator applyNumberGenerator; private ApplyPartMapper applyPartMapper; private ResultMapper resultMapper; private StockBean stockBean; private ResultUtils resultUtils; private static final String STATE = "To be Inspected"; private TestApplyRuleUtils testApplyRuleUtils; private RemoteParamService remoteParamService; private StockMapper stockMapper; private PartMapper partMapper; @Override public boolean saveDto(ApplyDTO applyDTO) { checkApplyNo(applyDTO); String ruleApply = remoteParamService.getByKey(Apply.RULE_APPLY, SecurityConstants.FROM_IN).getData(); //系统参数开关打开&&明细列表不为空&&不是系统规则触发的自动创建成品检 if(ruleApply.equals("true") && null != applyDTO.getApplyPartList() && !applyDTO.getFinishedProductAutoInsp()){ testApplyRuleUtils.adjustApplyDTONew(applyDTO); if(!applyDTO.getAutoCreateApplyDoor()){ //不自动报检 return false; } } //剔除掉相同批次号已申请的对象(工作台批量提交时有两个嵌套循环,第一轮循环是产出列表,第二轮循环是遍历检测结果为null的检测结果列表) testApplyRuleUtils.removeAlExistObj(applyDTO.getApplyType(),applyDTO.getApplyPartList()); baseMapper.insert(applyDTO); if (CollectionUtil.isNotEmpty(applyDTO.getApplyPartList())) { applyDTO.getApplyPartList().stream().forEach(applyPartDTO -> { // 添加申请id applyPartDTO.setApplyId(applyDTO.getId()); }); saveApplyPartList(applyDTO.getApplyPartList(), applyDTO.getApplyType()); return true; }else{ //如果是工作台自动报检的则删除空单据,否则判断为手动新增申请单主表 if (StringUtils.isNotBlank(applyDTO.getRemark()) && applyDTO.getRemark().contains("自动报检")) { baseMapper.deleteById(applyDTO); } return false;//这里返回false可以屏蔽工作台将测结果的申请id更新为null【2022-09-20】 } } @Override public boolean saveApplyPartList(List applyPartDTOList) { if (!CollectionUtils.isEmpty(applyPartDTOList)) { Long id = applyPartDTOList.get(0).getApplyId(); Apply apply = baseMapper.selectById(id); saveApplyPartList(applyPartDTOList, apply.getApplyType()); } return true; } public boolean saveApplyPartList(List applyPartDTOList, String applyType) { applyPartDTOList.stream().forEach(applyPartDTO -> { // 初始化 Boolean isQualified = null; Boolean isUsed = false; applyPartDTO.setIsQualified(null); //产出报检就这几个? if (Apply.FIRST_APPLY.equals(applyType) || Apply.OUTPUT_APPLY.equals(applyType) || Apply.PRODUCT_APPLY.equals(applyType) || Apply.PROCESS_APPLY.equals(applyType)) { // 产出报检: // 查检测标准编号(计划) List testStandardList = applyPartMapper.findTestStandardNoBySystemNo(applyPartDTO.getSystemNo());//这里没有根据检测类型取查对应的检测标准 if (CollectionUtil.isNotEmpty(testStandardList) && testStandardList.size() == 1) { applyPartDTO.setTestStandardNo(testStandardList.get(0).getStandardNo()); applyPartDTO.setMoTestStandardId(testStandardList.get(0).getId()); } // 添加显示字段信息,例如批号,零件号,零件描述等 JSONObject showInfo = applyPartMapper.selectShowInfoBySystemNo(applyPartDTO.getSystemNo()); if (showInfo != null) { if (StringUtils.isBlank(applyPartDTO.getOutBatchNo())) { applyPartDTO.setLotBatchNo(showInfo.getString("out_batch_no"));//批号 } else { applyPartDTO.setLotBatchNo(applyPartDTO.getOutBatchNo()); } if (StringUtils.isBlank(applyPartDTO.getPartNo())) { applyPartDTO.setPartNo(showInfo.getString("part_no"));//零件编号 } if (StringUtils.isBlank(applyPartDTO.getPartName())) { applyPartDTO.setPartDesc(showInfo.getString("part_name"));//零件描述 } else { applyPartDTO.setPartDesc(applyPartDTO.getPartName()); } if (applyPartDTO.getPartId() == null) { applyPartDTO.setPartId(showInfo.getLong("part_id"));//零件id } if (applyPartDTO.getCheckLength() == null) { applyPartDTO.setQtyArrived(showInfo.getBigDecimal("product_qty").stripTrailingZeros().toPlainString()); applyPartDTO.setTotalLength(new BigDecimal(applyPartDTO.getQtyArrived())); } else { applyPartDTO.setQtyArrived(applyPartDTO.getCheckLength().stripTrailingZeros().toPlainString()); applyPartDTO.setTotalLength(new BigDecimal(applyPartDTO.getQtyArrived())); } } else { Stock stock = stockMapper.selectById(applyPartDTO.getStockId()); if (stock == null) { throw new RuntimeException("不存在该零件的报检信息"); } applyPartDTO.setLotBatchNo(stock.getPartBatchNo()); Part part = partMapper.selectById(stock.getPartId()); applyPartDTO.setPartNo(part.getPartNo()); applyPartDTO.setPartDesc(part.getPartName()); applyPartDTO.setPartId(part.getId()); applyPartDTO.setQtyArrived(stock.getStockQuantity().stripTrailingZeros().toPlainString()); applyPartDTO.setTotalLength(stock.getStockQuantity()); applyPartDTO.setStockId(stock.getId()); applyPartDTO.setSourceLocationId(stock.getLocationId()); } } else if (Apply.LOCAL_APPLY.equals(applyType)) { // 本地库存产出报检: if (applyPartDTO.getIsUsed() != null) { // 在检测结果登记前是否允许该物料被使用,否:待检测/检测不合格/不允许使用,是:待检测,其他不变 if (applyPartDTO.getIsUsed()) { isUsed = null; } else { isQualified = false; } } } applyPartDTO.setUnqualifiedArrived(BigDecimal.ZERO.toString()); Long operationId = applyPartMapper.selectOperationIdBySystemNo(applyPartDTO.getSystemNo());//获取工序id applyPartDTO.setOperationId(operationId); applyPartMapper.insert(applyPartDTO); // 添加检测结果表 // resultUtils.addOrUpdateResult(applyPartDTO.getSystemNo(), applyPartDTO.getLotBatchNo(), ResultStateStringValues.UNTESTED, // null, isQualified, isUsed, null, applyPartDTO.getPartId(), null, applyType); resultUtils.addOrUpdateResult(applyPartDTO.getSystemNo(), applyPartDTO.getLotBatchNo(), ResultStateStringValues.UNTESTED, null, isQualified, isUsed, null, applyPartDTO.getPartId(), applyPartDTO.getApplyId(), applyType); }); return true; } @Override public boolean updateById(Apply apply) { checkApplyNo(apply); return SqlHelper.retBool(baseMapper.updateById(apply)); } private void checkApplyNo(Apply apply) { if (StringUtils.isBlank(apply.getApplyNo())) { // 1. 自动生成编号 apply.setApplyNo(applyNumberGenerator.generateNumberWithPrefix(Apply.DIGIT, Apply.PREFIX, Apply::getApplyNo)); } else { // 2.判断是否重复 List repeatNos = baseMapper.selectList(Wrappers.query().lambda().eq(Apply::getApplyNo, apply.getApplyNo()).ne(Apply::getId, apply.getId())); if (CollectionUtil.isNotEmpty(repeatNos)) { throw new RuntimeException("编号重复"); } } } @Override public boolean deleteById(Long id) { applyPartMapper.delete(Wrappers.query().lambda().eq(ApplyPart::getApplyId, id)); return SqlHelper.retBool(baseMapper.deleteById(id)); } @Override public JSONObject getStock(Apply apply) { UUID uuid = UUID.randomUUID(); String s = UUID.randomUUID().toString(); JSONObject inAttr = new JSONObject(); inAttr.put("stateDb", STATE); Map stockMap = new HashMap<>(16); stockMap.put("contract", stockBean.getRegion()); try { System.out.println(stockBean.getSecretKey()); System.out.println(stockBean.getCleartextSecretKey()); if (StringUtils.isNotBlank(stockBean.getSecretKey())) { stockMap.put("contractKey", stockBean.getSecretKey()); } else if (StringUtils.isNotBlank(stockBean.getCleartextSecretKey())) { stockMap.put("contractKey", AesUtils.encrypt(stockBean.getCleartextSecretKey())); } } catch (Exception e) { throw new RuntimeException("明文密钥加密失败"); } stockMap.put("userId", SecurityUtils.getUser().getUsername()); stockMap.put("inAttr", inAttr); String result = HttpRequest.get(stockBean.getIp()).form(stockMap).execute().body(); JSONObject jsonObject1 = JSONObject.parseObject(result); return jsonObject1; } }