/* * Copyright (c) 2018-2025, ztt All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the pig4cloud.com developer nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * Author: ztt */ package com.chinaztt.mes.technology.service.impl; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.date.DateUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.IoUtil; import cn.hutool.core.util.BooleanUtil; import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.toolkit.SqlHelper; import com.chinaztt.ifs.api.feign.IfsFeignClient; import com.chinaztt.mes.basic.entity.Part; import com.chinaztt.mes.basic.mapper.PartMapper; import com.chinaztt.mes.common.numgen.NumberGenerator; import com.chinaztt.mes.common.oa.OAProperty; import com.chinaztt.mes.quality.dto.TestStandardDTO; import com.chinaztt.mes.quality.entity.TestStandard; import com.chinaztt.mes.quality.entity.TestStandardBinding; import com.chinaztt.mes.quality.entity.TestStandardParam; import com.chinaztt.mes.quality.mapper.TestStandardBindingMapper; import com.chinaztt.mes.quality.mapper.TestStandardMapper; import com.chinaztt.mes.quality.mapper.TestStandardParamMapper; import com.chinaztt.mes.quality.service.impl.TestStandardServiceImpl; import com.chinaztt.mes.quality.state.standard.constant.TestStandardStateStringValues; import com.chinaztt.mes.technology.dto.*; import com.chinaztt.mes.technology.entity.*; import com.chinaztt.mes.technology.excel.DocumentTestStandardData; import com.chinaztt.mes.technology.excel.MaterialCostData; import com.chinaztt.mes.technology.feign.MesFileApi; import com.chinaztt.mes.technology.mapper.*; import com.chinaztt.mes.technology.service.DocumentJgtService; import com.chinaztt.mes.technology.service.DocumentService; import com.chinaztt.mes.technology.service.StructureService; import com.chinaztt.mes.technology.state.bom.constant.BomStateStringValues; import com.chinaztt.mes.technology.state.document.constant.DocumentStateStringValues; import com.chinaztt.mes.technology.state.document.constant.DocumentStates; import com.chinaztt.mes.technology.state.routing.constant.RoutingStateStringValues; import com.chinaztt.mes.technology.util.DocumentWordExportUtils; import com.chinaztt.mes.technology.util.TechnologyUtils; import com.chinaztt.ztt.common.core.util.R; import com.chinaztt.ztt.common.oss.service.OssTemplate; import com.chinaztt.ztt.common.security.service.ZttUser; import com.chinaztt.ztt.common.security.util.SecurityUtils; import lombok.AllArgsConstructor; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.math.BigDecimal; import java.net.URLEncoder; import java.time.LocalDateTime; import java.util.*; import java.util.stream.Collectors; /** * 工艺文件 * * @author zhangxy * @date 2021-05-07 09:45:58 */ @Slf4j @Service @AllArgsConstructor @Transactional(rollbackFor = Exception.class) public class DocumentServiceImpl extends ServiceImpl implements DocumentService { private static final String OSS_BUCKET = "technology"; private final OssTemplate minioTemplate; private BomMapper bomMapper; private BomServiceImpl bomService; private BomComponentMapper bomComponentMapper; private DocumentMaterialCostMapper documentMaterialCostMapper; private IfsFeignClient ifsFeignClient; private OAProperty oaProperty; private OperationMapper operationMapper; private PartMapper partMapper; private RoutingOperationTemplateMapper routingOperationTemplateMapper; private RoutingOperationMapper routingOperationMapper; private RoutingOperationParamMapper routingOperationParamMapper; private RoutingServiceImpl routingService; private RoutingMapper routingMapper; private StructureService structureService; private StructureMapper structureMapper; private TestStandardMapper testStandardMapper; private TestStandardParamMapper testStandardParamMapper; private TestStandardBindingMapper testStandardBindingMapper; private JoinDocumentBomRoutingMapper joinDocumentBomRoutingMapper; private JoinDocumentTestStandardMapper joinDocumentTestStandardMapper; private TestStandardServiceImpl testStandardService; private NumberGenerator numberGenerator; private DocumentStandardParamMapper documentStandardParamMapper; private DocumentTestStandardMapper documentTestStandardMapper; private TechnologyUtils technologyUtils; private DocumentJgtMapper documentJgtMapper; private DocumentJgtService documentJgtService; private DocumentSamplingRuleMapper documentSamplingRuleMapper; @Autowired private MesFileApi mesFileApi; private static final String locationImg="D:/file/img"; private static final String file_img_url = "http://127.0.0.1:6066/Image/img"; /** * @Author: Hans * @Description: 根据工艺文件ID查询相关库存件、BOM、工艺路线、检测标准 * @Date: 2022-06-09 */ @Override public DocumentDTO getDtoById(Long documentId) { Document document = baseMapper.selectById(documentId); DocumentDTO dto = new DocumentDTO(); BeanUtils.copyProperties(document, dto); dto.setBomRoutings(baseMapper.getBomRoutingDtoByDocId(documentId)); dto.setBomRoutingParts(baseMapper.getBomRoutingDtoPartByDocId(documentId)); dto.setTestStandards(joinDocumentTestStandardMapper.selectDtoListByDocumentId(documentId)); dto.setJgtList(documentJgtMapper.selectList(Wrappers.lambdaQuery() .eq(DocumentJgt::getDocumentId,documentId))); List costs = baseMapper.getMaterialCostByDocId(documentId); if (CollectionUtil.isNotEmpty(costs)) { dto.setMaterialCosts(costs.stream().collect(Collectors.groupingBy(DocumentMaterialCostDTO::getBomId))); } return dto; } @Override public DocumentDTO getDocumentById(Long id) { Document document = baseMapper.selectById(id); DocumentDTO dto = new DocumentDTO(); BeanUtils.copyProperties(document, dto); //bom结构的数据 dto.setBomRoutings(baseMapper.getBomRoutingDtoByDocId(id)); //检测规则与工艺文件的关联表 List joinDocumentTestStandardList = joinDocumentTestStandardMapper.selectDtoListByDocumentId(id); if (CollectionUtil.isNotEmpty(joinDocumentTestStandardList)) { for (JoinDocumentTestStandardDTO join : joinDocumentTestStandardList) { join.setStandardParams(testStandardParamMapper.selectList(Wrappers.lambdaQuery().eq(TestStandardParam::getTestStandardId, join.getTestStandardId()))); } //检测规则与检测规则参数 dto.setTestStandards(joinDocumentTestStandardList); } if (CollectionUtil.isNotEmpty(dto.getBomRoutings())) { for (JoinDocumentBomRoutingDTO joinDocumentBomRoutingDTO : dto.getBomRoutings()) { //工艺路线的数据(包含工序) joinDocumentBomRoutingDTO.setRoutingDTO(routingService.getRoutingById(joinDocumentBomRoutingDTO.getRoutingId())); if (CollectionUtil.isNotEmpty(joinDocumentBomRoutingDTO.getRoutingDTO().getOperations())) { //循环工序把工序参数塞进工序 for (RoutingOperationDTO routingOperationDTO : joinDocumentBomRoutingDTO.getRoutingDTO().getOperations()) { List routingOperationTemplateList = routingOperationTemplateMapper.selectList( Wrappers.lambdaQuery() .eq(RoutingOperationTemplate::getRoutingOperationId, routingOperationDTO.getId())); if (CollectionUtil.isNotEmpty(routingOperationTemplateList)) { Set idList = routingOperationTemplateList.stream().map(o -> o.getId()).collect(Collectors.toSet()); List routingOperationParamList = routingOperationParamMapper.selectList(Wrappers.lambdaQuery() .in(RoutingOperationParam::getOperationTemplateId, idList).eq(RoutingOperationParam::getRoutingOperationId, routingOperationDTO.getId()) .orderByAsc(RoutingOperationParam::getRoutingOperationId).orderByAsc(RoutingOperationParam::getId)); routingOperationDTO.setRoutingOperationParam(routingOperationParamList); } } } } } List costs = baseMapper.getMaterialCostByDocId(id); if (CollectionUtil.isNotEmpty(costs)) { dto.setMaterialCosts(costs.stream().collect(Collectors.groupingBy(DocumentMaterialCostDTO::getBomId))); } return dto; } /** * 修改工艺文件状态 * * @param ids * @param state * @return */ @Override public boolean updateStateByIds(List ids, DocumentStates state) { List documentList = baseMapper.selectList(Wrappers.lambdaQuery().in(Document::getId, ids)); //查询工艺路线和bom List djbList = joinDocumentBomRoutingMapper.selectList(Wrappers. lambdaQuery().in(JoinDocumentBomRouting::getDocumentId, ids)); //查询检测标准 List jdtsList = joinDocumentTestStandardMapper.selectList(Wrappers. lambdaQuery().in(JoinDocumentTestStandard::getDocumentId, ids)); if (state.equals(DocumentStates.ACCEPTED)) { //检查合法性 checkDocument(documentList); changeState(ids, djbList, jdtsList, DocumentStateStringValues.ACCEPTED, BomStateStringValues.ACCEPTED, RoutingStateStringValues.ACCEPTED, TestStandardStateStringValues.ACCEPTED); } else if (DocumentStates.DRAFT.equals(state)) { changeState(ids, djbList, jdtsList, DocumentStateStringValues.DRAFT, BomStateStringValues.DRAFT, RoutingStateStringValues.DRAFT, TestStandardStateStringValues.DRAFT); } else { changeDocState(ids, DocumentStateStringValues.REJECT); } List updateDocumentList = new ArrayList<>(); for (Long id:ids) { Document document = new Document(); document.setId(id); updateDocumentList.add(doUpdateRealUser(document)); } updateBatchById(updateDocumentList); return true; } /** * 检验工艺文件合法性 * * @param documents * @return */ private void checkDocument(List documents) { for (Document document : documents) { List jbrList = joinDocumentBomRoutingMapper.selectList(Wrappers.lambdaQuery() .eq(JoinDocumentBomRouting::getDocumentId, document.getId())); if (CollectionUtil.isEmpty(jbrList)) { throw new RuntimeException(document.getNumber() + "缺少工艺路线"); } for (JoinDocumentBomRouting jbr : jbrList) { List ids = routingOperationMapper.checkOperationPartByRoutingId(jbr.getRoutingId()); if (CollectionUtil.isNotEmpty(ids)) { throw new RuntimeException(document.getNumber() + "工艺路线上工序未全部绑定零件"); } } } } /** * 修改工艺文件和关联数据的状态 * * @param ids * @param djbList * @param jdtsList * @param docState 工艺文件状态 * @param bomState bom状态 * @param routingState 工艺路线状态 * @param standardState 检测标准状态 */ private void changeState(List ids, List djbList, List jdtsList, String docState, String bomState, String routingState, String standardState) { //修改主表状态 changeDocState(ids, docState); //更新BOM状态 List bomIds = djbList.stream().map(JoinDocumentBomRouting::getBomId).collect(Collectors.toList()); if (CollectionUtil.isNotEmpty(bomIds)) { UpdateWrapper bomUpdateWrapper = new UpdateWrapper(); bomUpdateWrapper.in("id", bomIds).set("state", bomState); bomMapper.update(null, bomUpdateWrapper); } //更新工艺状态 List routingIds = djbList.stream().map(JoinDocumentBomRouting::getRoutingId).collect(Collectors.toList()); if (CollectionUtil.isNotEmpty(routingIds)) { UpdateWrapper routingUpdateWrapper = new UpdateWrapper(); routingUpdateWrapper.in("id", routingIds).set("state", routingState); routingMapper.update(null, routingUpdateWrapper); } //更新检测标准 List standardids = jdtsList.stream().map(JoinDocumentTestStandard::getTestStandardId).collect(Collectors.toList()); if (CollectionUtil.isNotEmpty(standardids)) { UpdateWrapper standardUpdateWrapper = new UpdateWrapper(); standardUpdateWrapper.in("id", standardids).set("state", standardState); testStandardMapper.update(null, standardUpdateWrapper); } } /** * 修改工艺文件主表状态 * * @param ids * @param state */ private void changeDocState(List ids, String state) { if (CollectionUtil.isNotEmpty(ids)) { //设置工艺文件主表状态 UpdateWrapper updateWrapper = new UpdateWrapper<>(); updateWrapper.in("id", ids).set("state", state); baseMapper.update(null, updateWrapper); } } @Override public IPage> getTestStand(Page page, QueryWrapper ew, Long id) { return testStandardMapper.getTestStand(page, ew, id); } @Override public R>> calcMaterialCost(Long documentId) { //先删除老的 documentMaterialCostMapper.delete(Wrappers.lambdaQuery().eq(DocumentMaterialCost::getDocumentId, documentId)); List joinList = joinDocumentBomRoutingMapper.selectList (Wrappers.lambdaQuery().eq(JoinDocumentBomRouting::getDocumentId, documentId)); if (CollectionUtil.isEmpty(joinList)) { return R.failed("请先添加产品结构工序参数"); } Map> result = new HashMap<>(8); //循环每个BOM for (JoinDocumentBomRouting j : joinList) { //如果没有bomId 和 工艺路线id 都是null 就跳过当前循环 if (j.getBomId() == null || j.getRoutingId() == null) { continue; } List bomComponentTotallList = bomComponentMapper.selectList(Wrappers.lambdaQuery() .eq(BomComponent::getBomId, j.getBomId()).isNotNull(BomComponent::getParent)); List fatherIds = bomComponentTotallList.stream().map(e -> e.getParent()).collect(Collectors.toList()); RoutingDTO routing = routingMapper.getRoutingDtoById(j.getRoutingId()); Bom bom = bomMapper.selectById(j.getBomId()); Part bomPart = partMapper.selectById(bom.getPartId()); setIndex(routing); List dlist = new ArrayList<>(); for (RoutingOperationDTO routingOperationDTO : routing.getOperations()) { List bomComponentList = bomComponentMapper.selectList(Wrappers.lambdaQuery() .eq(BomComponent::getBomId, j.getBomId()) .eq(BomComponent::getPartId, routingOperationDTO.getPartId()) ); bomComponentList.forEach(e -> { List childList = bomMapper.getBomComponentsOrderByPartName(j.getBomId(), e.getId()); childList.forEach(ce -> { if (!fatherIds.contains(ce.getId())) { DocumentMaterialCostDTO dto = new DocumentMaterialCostDTO(); Part part = partMapper.selectById(ce.getPartId()); Operation operation = operationMapper.selectById(routingOperationDTO.getOperationId()); dto.setDocumentId(documentId); dto.setBomId(ce.getBomId()); dto.setPartId(ce.getPartId()); dto.setOperationName(operation.getName()); dto.setBomNumber(bom.getNumber()); dto.setFinishedProduct(bomPart.getPartName()); dto.setOperationId(routingOperationDTO.getOperationId()); dto.setIndex(routingOperationDTO.getIndex()); dto.setPartName(part.getPartName()); dto.setUnit(part.getUnit()); dto.setQuantity(ce.getQpa()); documentMaterialCostMapper.insert(dto); dlist.add(dto); } }); }); } result.put(bom.getId(), dlist); } return R.ok(result); } private void setIndex(RoutingDTO routing) { List operations = routing.getOperations(); if (CollectionUtil.isEmpty(operations)) { return; } if (operations.size() == 1) { operations.get(0).setIndex(1); return; } Set roIds = operations.stream().map(r -> r.getId()).collect(Collectors.toSet()); List refs = routing.getRefs(); Map refMap = refs.stream().collect(Collectors.toMap(RefDTO::getFromId, RefDTO::getToId)); //找到第一个节点 final Long first = roIds.stream().filter(id -> refMap.keySet().contains(id) && !refMap.values().contains(id)).findFirst().get(); operations.stream().filter(e -> e.getId().equals(first)).findFirst().get().setIndex(1); Long fromId = first; int index = 2; while (refMap.get(fromId) != null) { Long toId = refMap.get(fromId); if (toId == null) { break; } operations.stream().filter(o -> o.getId().equals(toId)).findAny().get().setIndex(index); fromId = toId; index++; } operations.sort(Comparator.comparing(RoutingOperationDTO::getIndex)); } @Override public R saveBom(JoinDocumentBomRouting join) { if (join.getDocumentId() == null) { return R.failed("请先保存基本信息"); } if (join.getRoutingId() == null) { return R.failed("请先选择工艺"); } //如果传了BomId 就用传的BomId if (join.getBomId() == null) { Routing routing = routingMapper.selectById(join.getRoutingId()); join.setBomId(routing.getBomId()); } joinDocumentBomRoutingMapper.updateById(join); if (join.getBomId() != null) { delRoutings(join.getId()); addDocTestStandard(join); Document document = getById(join.getDocumentId()); addBomRoutingPartByBomId(join.getId(), join.getBomId(), join.getDocumentId(), document.getDocType()); } Document document = new Document(); document.setId(join.getDocumentId()); baseMapper.updateById(doUpdateRealUser(document)); return R.ok(getDtoById(join.getDocumentId())); } private void addBomRoutingPartByBomId(Long parentId, Long bomId, Long documentId, String docType) { Bom bom = bomMapper.selectById(bomId); if (bom == null) { throw new RuntimeException("bom丢失id为" + bomId); } List parts = bomMapper.getBomCompontBomPart(bomId); for (Part part : parts) { JoinDocumentBomRouting joinDocumentBomRouting = new JoinDocumentBomRouting(); joinDocumentBomRouting.setPartId(part.getId()); joinDocumentBomRouting.setDocumentId(documentId); joinDocumentBomRouting.setParentId(parentId); // 根据PartID查找工艺路线中数据,并进行设置 List routings = routingMapper.selectList(Wrappers.lambdaQuery() .eq(Routing::getPartId, part.getId()) .eq(Routing::getBomTypeDb, docType) .orderByDesc(Routing::getId)); if (CollectionUtil.isEmpty(routings)) { throw new RuntimeException("保存失败,零件:" + part.getPartNo() + "无工艺路线"); } Routing routing = routings.get(0); joinDocumentBomRouting.setRoutingId(routing.getId()); joinDocumentBomRouting.setBomId(routing.getBomId()); joinDocumentBomRoutingMapper.insertAndGetId(joinDocumentBomRouting); // 添加检测标准 addDocTestStandard(joinDocumentBomRouting); // 当前零件有Bom数据且零件为制造件“A” if (routing.getBomId() != null && part.getPlanningMethod().equals("A") && part.getMaterialType().equals("1")) { addBomRoutingPartByBomId(joinDocumentBomRouting.getId(), routing.getBomId(), documentId, docType); } } } @Override public R saveTestStandard(List joins) { if (CollectionUtil.isNotEmpty(joins)) { joins.forEach(j -> { int count = joinDocumentTestStandardMapper.selectCount(Wrappers.lambdaQuery().eq(JoinDocumentTestStandard::getDocumentId, j.getDocumentId()) .eq(JoinDocumentTestStandard::getTestStandardId, j.getTestStandardId())); if (count == 0) { joinDocumentTestStandardMapper.insert(j); } }); } return R.ok(); } @Override public R deleteTestStandard(JoinDocumentTestStandardDTO join) { joinDocumentTestStandardMapper.delete(Wrappers.lambdaQuery().eq(JoinDocumentTestStandard::getDocumentId, join.getDocumentId()) .eq(JoinDocumentTestStandard::getTestStandardId, join.getTestStandardId())); return R.ok(); } @Override public R deleteBom(JoinDocumentBomRouting join) { if (join.getId() == null) { return R.failed("请选择需要删除数据"); } JoinDocumentBomRouting joinDocumentBomRouting = joinDocumentBomRoutingMapper.selectById(join.getId()); //工艺主表更新时间,更新人 Document document = new Document(); document.setId(joinDocumentBomRouting.getDocumentId()); baseMapper.updateById(doUpdateRealUser(document)); //先新增一个在删除 JoinDocumentBomRouting jadd = new JoinDocumentBomRouting(); jadd.setParentId(joinDocumentBomRouting.getParentId()); jadd.setPartId(joinDocumentBomRouting.getPartId()); jadd.setDocumentId(joinDocumentBomRouting.getDocumentId()); //再新增一个 joinDocumentBomRoutingMapper.insert(jadd); //刪除子节点和检测标准 if (joinDocumentBomRouting.getRoutingId() != null) { delRoutings(joinDocumentBomRouting.getId()); } //删除当前节点和检测标准 delJoinAndTest(joinDocumentBomRouting); joinDocumentBomRoutingMapper.deleteById(joinDocumentBomRouting.getId()); return R.ok(getDtoById(joinDocumentBomRouting.getDocumentId())); } //删除子节点 private void delRoutings(Long id) { List joinDocumentBomRoutingList = joinDocumentBomRoutingMapper.selectList(Wrappers.lambdaQuery() .eq(JoinDocumentBomRouting::getParentId, id)); for (JoinDocumentBomRouting joinDocumentBomRouting : joinDocumentBomRoutingList) { delJoinAndTest(joinDocumentBomRouting); joinDocumentBomRoutingMapper.deleteById(joinDocumentBomRouting.getId()); delRoutings(joinDocumentBomRouting.getId()); } } @Override public R routingChildCheck(Long id) { return R.ok(joinDocumentBomRoutingMapper.selectCount(Wrappers.lambdaQuery().eq(JoinDocumentBomRouting::getParentId, id))); } @SneakyThrows @Override public void exportMaterialCost(Long documentId, HttpServletResponse response) { List joinList = joinDocumentBomRoutingMapper.selectList (Wrappers.lambdaQuery().eq(JoinDocumentBomRouting::getDocumentId, documentId)); if (CollectionUtil.isEmpty(joinList)) { return; } response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("UTF-8"); String fileName = URLEncoder.encode("工艺文件原材料表", "UTF-8").replaceAll("\\+", "%20"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), MaterialCostData.class).build(); for (int i = 0; i < joinList.size(); i++) { Bom bom = bomMapper.selectById(joinList.get(i).getBomId()); WriteSheet sheet = EasyExcel.writerSheet(i, bom.getNumber()).head(MaterialCostData.class).build(); List data = baseMapper.exportList(documentId, bom.getId()); excelWriter.write(data, sheet); } excelWriter.finish(); } @Override public void updateStateByOa(String workflowId, DocumentStates state) { Document document = baseMapper.selectOne(Wrappers.lambdaQuery().eq(Document::getOaWorkflowId, workflowId)); if (document == null) { log.error("工艺文件审批回调", "workflowId=" + workflowId + "对应的工艺文件没有找到"); return; } List joinList1 = joinDocumentBomRoutingMapper.selectList(Wrappers. lambdaQuery().eq(JoinDocumentBomRouting::getDocumentId, document.getId())); List joinList2 = joinDocumentTestStandardMapper.selectList(Wrappers. lambdaQuery().eq(JoinDocumentTestStandard::getDocumentId, document.getId())); if (DocumentStates.ACCEPTED.equals(state)) { //批准 document.setState(DocumentStateStringValues.ACCEPTED); if (CollectionUtil.isNotEmpty(joinList1)) { //循环更新工艺、BOM状态 for (JoinDocumentBomRouting j : joinList1) { UpdateWrapper bomUpdateWrapper = new UpdateWrapper(); bomUpdateWrapper.set("state", BomStateStringValues.ACCEPTED); bomUpdateWrapper.eq("id", j.getBomId()); bomMapper.update(null, bomUpdateWrapper); UpdateWrapper routingUpdateWrapper = new UpdateWrapper(); routingUpdateWrapper.set("state", RoutingStateStringValues.ACCEPTED); routingUpdateWrapper.eq("id", j.getRoutingId()); routingMapper.update(null, routingUpdateWrapper); } } if (CollectionUtil.isNotEmpty(joinList2)) { //更新检测标准状态 for (JoinDocumentTestStandard jj : joinList2) { UpdateWrapper standardUpdateWrapper = new UpdateWrapper(); standardUpdateWrapper.set("state", TestStandardStateStringValues.ACCEPTED); standardUpdateWrapper.eq("id", jj.getTestStandardId()); testStandardMapper.update(null, standardUpdateWrapper); } } } else { //驳回 document.setState(DocumentStateStringValues.REJECT); } baseMapper.updateById(document); } @SneakyThrows @Override public R uploadJgt(MultipartFile file, Long documentId) { String requestUrl=""; String fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename()); //minioTemplate.putObject(OSS_BUCKET, fileName, multipartFile.getInputStream()); //创建文件夹 String timeForder= DateUtil.format(DateUtil.date(),"yyyy/MM/dd"); String forder=locationImg+"/"+OSS_BUCKET+"/"+timeForder; File filePath = new File(forder); if(!filePath.exists()){ filePath.mkdirs(); } //合并文件 RandomAccessFile raFile = null; BufferedInputStream inputStream=null; try{ File dirFile = new File(forder, fileName); //以读写的方式打开目标文件 raFile = new RandomAccessFile(dirFile, "rw"); raFile.seek(raFile.length()); inputStream = new BufferedInputStream(file.getInputStream()); byte[] buf = new byte[1024]; int length = 0; while ((length = inputStream.read(buf)) != -1) { raFile.write(buf, 0, length); } requestUrl=OSS_BUCKET+"/"+timeForder+"/"+fileName; }catch(Exception e){ e.printStackTrace(); }finally{ try { if (inputStream != null) { inputStream.close(); } if (raFile != null) { raFile.close(); } }catch(Exception ignored){ } } String url = requestUrl; QueryWrapperqueryWrapper=new QueryWrapper<>(); queryWrapper.lambda().eq(DocumentJgt::getDocumentId,documentId); DocumentJgt documentJgt = documentJgtMapper.selectOne(queryWrapper); if(Objects.nonNull(documentJgt)){ UpdateWrapperupdateWrapper=new UpdateWrapper<>(); updateWrapper.lambda().eq(DocumentJgt::getDocumentId,documentId).set(DocumentJgt::getFileName,locationImg+"/"+url); documentJgtMapper.update(null,updateWrapper); }else { DocumentJgt jgt=new DocumentJgt(); jgt.setDocumentId(documentId); jgt.setFileName(locationImg+"/"+url); documentJgtMapper.insert(jgt); } return R.ok(locationImg+"/"+url); } @SneakyThrows @Override public R uploadLct(MultipartFile file, Long documentId, String lctRemark) { Document document = baseMapper.selectById(documentId); if (StringUtils.isNotBlank(document.getLct())) { minioTemplate.removeObject(OSS_BUCKET, document.getLct()); } String fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename()); minioTemplate.putObject(OSS_BUCKET, fileName, file.getInputStream()); document.setLctRemark(lctRemark); document.setLct(fileName); baseMapper.updateById(doUpdateRealUser(document)); return R.ok("/document/" + OSS_BUCKET + "/" + fileName); } @SneakyThrows @Override public R removeJgt(Long jgtId) { DocumentJgt documentJgt = documentJgtMapper.selectById(jgtId); //工艺主表更新时间,更新人 Document document = new Document(); document.setId(documentJgt.getDocumentId()); baseMapper.updateById(doUpdateRealUser(document)); if (StringUtils.isNotBlank(documentJgt.getFileName())) { minioTemplate.removeObject(OSS_BUCKET, documentJgt.getFileName()); documentJgtMapper.deleteById(jgtId); } return R.ok(); } @SneakyThrows @Override public R removeLct(Long documentId) { Document document = baseMapper.selectById(documentId); if (StringUtils.isNotBlank(document.getLct())) { minioTemplate.removeObject(OSS_BUCKET, document.getLct()); document.setLct(null); document.setId(documentId); baseMapper.updateById(doUpdateRealUser(document)); } return R.ok(); } @Override public void getFile(String fileName, HttpServletResponse response) { try (InputStream inputStream = new FileInputStream(fileName)) { response.setContentType("application/octet-stream; charset=UTF-8"); IoUtil.copy(inputStream, response.getOutputStream()); } catch (Exception e) { log.error("文件读取异常: {}", e.getLocalizedMessage()); } } @Override public R copyDocument(List documentList) { return R.failed("相同零件不可有多个工艺文件"); // if (CollectionUtil.isNotEmpty(documentList)) { // for (Document document : documentList) { // Long id = document.getId(); // //新增工艺文件主表 // document.setId(null); // document.setOaWorkflowId(null); // document.setState(DocumentStateStringValues.DRAFT); // document.setNumber(numberGenerator.generateNumberWithPrefix(Document.DIGIT, Document.PREFIX, Document::getNumber)); // baseMapper.insert(document); // //查询工艺文件的检测和参数 // List testStandardList = testStandardMapper.getTestStandard(id); // for (TestStandard testStandard : testStandardList) { // //复制检测规则和参数 // TestStandard newTestStandard = testStandardService.copyTestStandard(testStandard); // JoinDocumentTestStandard joinDocumentTestStandard = new JoinDocumentTestStandard(); // joinDocumentTestStandard.setDocumentId(document.getId()); // joinDocumentTestStandard.setTestStandardId(newTestStandard.getId()); // joinDocumentTestStandardMapper.insert(joinDocumentTestStandard); // } // //查询工艺文件关联bom和工艺路线 // List joinDocumentBomRoutingList = joinDocumentBomRoutingMapper.selectList(Wrappers.lambdaQuery().eq(JoinDocumentBomRouting::getDocumentId, id)); // if (CollectionUtil.isNotEmpty(joinDocumentBomRoutingList)) { // for (JoinDocumentBomRouting joinDocumentBomRouting : joinDocumentBomRoutingList) { // //根据工艺路线id查询工艺路线 // List routingDTO = routingService.getByRoutingId(joinDocumentBomRouting.getRoutingId()); // //复制工艺路线 // R> routingList = routingService.copyRoutingSave(routingDTO); // List routing = routingList.getData(); // //复制的bom // Bom bom = bomService.copyBom(joinDocumentBomRouting.getBomId()); // //保存新的关联关系 // JoinDocumentBomRouting newJoinDocumentBomRouting = new JoinDocumentBomRouting(); // newJoinDocumentBomRouting.setDocumentId(document.getId()); // newJoinDocumentBomRouting.setBomId(bom.getId()); // newJoinDocumentBomRouting.setRoutingId(routing.get(0).getId()); // joinDocumentBomRoutingMapper.insert(newJoinDocumentBomRouting); // } // } // } // } // return R.ok(); } /** * @Author: Hans * @Description: 重写保存工艺文件 * @Date: 2022-06-10 */ @Override public R fullSave(Document document) { List documentList = baseMapper.selectList(Wrappers.lambdaQuery() .eq(Document::getVersion,document.getVersion()) .eq(Document::getPartId, document.getPartId()) .eq(Document::getDocType, document.getDocType())); if (CollectionUtil.isNotEmpty(documentList)) { return R.failed("该零件号同一版本已存在工艺文件"); } Document newDocument = new Document(); BeanUtils.copyProperties(document, newDocument); checkDocumentNumber(newDocument); newDocument.setState(DocumentStateStringValues.DRAFT); baseMapper.insert(newDocument); buildBomRoutingBom(newDocument); return R.ok(newDocument); } /** * @Author: Hans * @Description: 重写更新工艺文件 * @Date: 2022-06-10 */ @Override public R fullUpdate(Document document) { Document oriDocument = baseMapper.selectById(document.getId()); if (!oriDocument.getPartId().equals( document.getPartId()) || !org.apache.commons.lang3.StringUtils.equals(document.getDocType(), oriDocument.getDocType())) { List documentList = baseMapper.selectList(Wrappers.lambdaQuery() .ne(Document::getId, document.getId()) .eq(Document::getPartId, document.getPartId()) .eq(Document::getVersion, document.getVersion()) .eq(Document::getDocType, document.getDocType())); if (CollectionUtil.isNotEmpty(documentList)) { throw new RuntimeException("该零件号同一版本已存在工艺文件"); } // 先删除工艺文件、BOM、工艺路线关联表中数据 List joinDocumentBomRoutingList = joinDocumentBomRoutingMapper.selectList(Wrappers.lambdaQuery().eq(JoinDocumentBomRouting::getDocumentId, document.getId())); delJoinAndTest(joinDocumentBomRoutingList); buildBomRoutingBom(document); } baseMapper.updateById(doUpdateRealUser(document)); return R.ok(document); } /** * @Author: Hans * @Description: 构建工艺文件、工艺路线、BOM数据关联表 * @Date: 2022-06-10 */ private void buildBomRoutingBom(Document document) { JoinDocumentBomRouting joinDocumentBomRouting = new JoinDocumentBomRouting(); joinDocumentBomRouting.setDocumentId(document.getId()); joinDocumentBomRouting.setPartId(document.getPartId()); joinDocumentBomRouting.setQrate(BigDecimal.ONE); // 根据PartID查找工艺路线中数据,并进行设置 List routings = routingMapper.selectList(Wrappers.lambdaQuery() .eq(Routing::getPartId, document.getPartId()) .eq(Routing::getBomTypeDb, document.getDocType()) .orderByDesc(Routing::getId)); if (CollectionUtil.isEmpty(routings)) { throw new RuntimeException("保存失败,零件:" + document.getPartNo() + "无工艺路线"); } Routing routing = routings.get(0); joinDocumentBomRouting.setRoutingId(routing.getId()); joinDocumentBomRouting.setBomId(routing.getBomId()); Routing routing1 = routingMapper.selectById(joinDocumentBomRouting.getRoutingId()); if(Objects.equals(routing1.getState(),RoutingStateStringValues.DRAFT)){ throw new RuntimeException("工艺路线状态未通过!"); } joinDocumentBomRoutingMapper.insertAndGetId(joinDocumentBomRouting); // 添加检测标准 addDocTestStandard(joinDocumentBomRouting); // 设置子节点数据 addBomRoutingPartByBomId(joinDocumentBomRouting.getId(), routing.getBomId(), joinDocumentBomRouting.getDocumentId(),document.getDocType()); } @Override public boolean updateById(Document document) { checkDocumentNumber(document); return SqlHelper.retBool(baseMapper.updateById(document)); } private void checkDocumentNumber(Document document) { if (StringUtils.isBlank(document.getNumber())) { // 1. 自动生成编号 document.setNumber(numberGenerator.generateNumberWithPrefix(Document.DIGIT, Document.PREFIX, Document::getNumber)); } else { // 2.判断是否重复 List repeatNos = baseMapper.selectList(Wrappers.query().lambda().eq(Document::getNumber, document.getNumber()).ne(Document::getId, document.getId())); if (CollectionUtil.isNotEmpty(repeatNos)) { throw new RuntimeException("编号重复"); } } } /** * 构建检测参数 * * @param documentId * @param doc */ private void buildTestParam(Long documentId, XWPFDocument doc) { DocumentWordExportUtils.createText(doc, "1", "7 成品检测", true, "宋体"); List joinList = joinDocumentTestStandardMapper.selectList(Wrappers.lambdaQuery().eq(JoinDocumentTestStandard::getDocumentId, documentId)); for (int j = 1; j <= joinList.size(); j++) { List testStandardParams = testStandardParamMapper.selectList(Wrappers.lambdaQuery().eq(TestStandardParam::getTestStandardId, joinList.get(j - 1).getTestStandardId())); if (CollectionUtil.isNotEmpty(testStandardParams)) { for (int i = 1; i <= testStandardParams.size(); i++) { DocumentWordExportUtils.createText(doc, null, "7." + j + "." + i + testStandardParams.get(i - 1).getParameterItem(), false, "宋体"); DocumentWordExportUtils.createText(doc, null, " " + testStandardParams.get(i - 1).getReferenceValue(), false, "宋体"); } } } } /** * 构建工序参数 * * @param documentId * @param doc */ private void buildOperationParam(Long documentId, XWPFDocument doc) { List jbrList = joinDocumentBomRoutingMapper.selectList(Wrappers.lambdaQuery().eq(JoinDocumentBomRouting::getDocumentId, documentId)); if (CollectionUtil.isNotEmpty(jbrList)) { //工艺参数导出,多个BOM对应的工序步骤是一样的,但是每道的参数不一样 JoinDocumentBomRouting jbr = jbrList.get(0); List routingOperations = routingOperationMapper.selectList(Wrappers.lambdaQuery().eq(RoutingOperation::getRoutingId, jbr.getRoutingId())); if (CollectionUtil.isNotEmpty(routingOperations)) { for (int i = 0; i < routingOperations.size(); i++) { RoutingOperation routingOperation = routingOperations.get(i); Operation operation = operationMapper.selectById(routingOperation.getOperationId()); int num = i + 1; DocumentWordExportUtils.createText(doc, "2", "6." + num + operation.getName(), true, "宋体"); DocumentWordExportUtils.createText(doc, null, " 6." + num + ".1 生产设备: ", false, "宋体"); if (StringUtils.isNotBlank(routingOperation.getRemark())) { String[] remarkArray = DocumentWordExportUtils.returnStation(routingOperation.getRemark(), "\r\n"); for (int d = 0; d < remarkArray.length; d++) { DocumentWordExportUtils.createText(doc, null, " 6." + num + ".2." + remarkArray[d], false, "宋体"); } } Map> stringListMap = new LinkedHashMap<>(); for (int a = 0; a < jbrList.size(); a++) { List params = routingOperationParamMapper.selectList(Wrappers.lambdaQuery() .eq(RoutingOperationParam::getRoutingId, jbrList.get(0).getRoutingId()).eq(RoutingOperationParam::getOperationId, operation.getId())); Bom bom = bomMapper.selectById(jbrList.get(a).getBomId()); Part finalProduct = partMapper.selectById(bom.getPartId()); stringListMap.put(finalProduct.getPartName(), params); } DocumentWordExportUtils.createParaTable(doc, stringListMap); } } } } /** * 同步ifs * * @param id * @return */ @Override public R ifsSync(Long id) { String msg = ""; R routingIfsSyncResult = routingIfsSync(id); int code = 0; if (routingIfsSyncResult.getCode() == 0) { msg += "工艺路线同步成功!"; } else { msg += "工艺路线同步失败!原因:" + routingIfsSyncResult.getMsg() + " !"; code = 1; } R structureIfsSyncResult = structureIfsSync(id); if (structureIfsSyncResult.getCode() == 0) { msg += "产品结构同步成功!"; } else { msg += "产品结构同步失败!原因:" + structureIfsSyncResult.getMsg() + " !"; code = 1; } R routingIfsCalResult = routingIfsCal(id); if (routingIfsCalResult.getCode() == 0) { msg += "工艺路线替代制造提前期计算成功"; } else { msg += "工艺路线替代制造提前期计算失败!原因:" + routingIfsCalResult.getMsg() + " !"; code = 1; } Document document = new Document(); document.setId(id); baseMapper.updateById(doUpdateRealUser(document)); if (code == 0) { return R.ok().setMsg(msg); } else { return R.failed(msg); } } /** * 工艺路线的对接 * * @param id * @return */ private R routingIfsSync(Long id) { List joinList = joinDocumentBomRoutingMapper.selectList(Wrappers.lambdaQuery() .eq(JoinDocumentBomRouting::getDocumentId, id) .isNotNull(JoinDocumentBomRouting::getRoutingId)); List routingIds = joinList.stream().map(joinDocumentBomRouting -> joinDocumentBomRouting.getRoutingId()).collect(Collectors.toList()); return R.ok(routingService.routingIfsSync(routingIds)); } /** * 对接产品结构 * * @param id * @return */ private R structureIfsSync(Long id) { List structureDTOS = structureMapper.getStructureDtoByDocId(id); return R.ok(structureService.structureIfsSyncByDto(structureDTOS)); } /** * 工艺路线替代提前制造 * * @param id * @return */ private R routingIfsCal(Long id) { List joinList = joinDocumentBomRoutingMapper.selectList(Wrappers.lambdaQuery() .eq(JoinDocumentBomRouting::getDocumentId, id) .isNotNull(JoinDocumentBomRouting::getRoutingId)); List routingIds = joinList.stream().map(joinDocumentBomRouting -> joinDocumentBomRouting.getRoutingId()).collect(Collectors.toList()); return R.ok(routingService.routingIfsCal(routingIds)); } /** * 根据零件添加检测标准 * * @param joinDocumentBomRouting */ private void addDocTestStandard(JoinDocumentBomRouting joinDocumentBomRouting) { //查询工艺路线工序 List routingOperationList = routingOperationMapper.selectList(Wrappers.lambdaQuery() .eq(RoutingOperation::getRoutingId, joinDocumentBomRouting.getRoutingId())); //根据每个工艺路线工序的零件id 查询检测标准 for (RoutingOperation routingOperation : routingOperationList) { if (null != routingOperation.getPartId()) { List testStandardBindingList = testStandardBindingMapper.selectList(Wrappers.lambdaQuery() .eq(TestStandardBinding::getPartId, routingOperation.getPartId())); // 将所有的检测标准全部添加进来 for (TestStandardBinding testStandardBinding : testStandardBindingList) { addDocTestStandard(joinDocumentBomRouting.getId(), routingOperation.getId(), testStandardBinding.getStandardId()); } } } } @Override public boolean addDocTestStandard(Long docBomId, Long routingOperationId, Long testStandardId) { DocumentTestStandard documentTestStandard = documentTestStandardMapper.getDocumentTestStandardByStandardId(testStandardId); if (documentTestStandard != null) { String testType = documentTestStandard.getInspectionType(); Boolean isFinishedProduct = false; if (testType.equals("成品检验") || testType.equals("06finishedproduct")) { isFinishedProduct = true;//检测标准那边导入有问题,没有把name转成code } List documentStandardParamList = documentStandardParamMapper.getDocumentStandardParamListByStandardId(testStandardId); if (null != documentTestStandard) { documentTestStandard.setDocBomId(docBomId);//工艺文件与bom关系表id documentTestStandard.setRoutingOperationId(routingOperationId);//工艺工序id documentTestStandardMapper.insert(documentTestStandard); for (DocumentStandardParam documentStandardParam : documentStandardParamList) { documentStandardParam.setTestStandardId(documentTestStandard.getId());//主表id if (BooleanUtil.isTrue(documentStandardParam.getIsReference())) { //成品检的引用池范围:工艺文件中所有工艺路线下所有工序对应的工序参数;其他检引用池范围:该工序下的工艺参数 //若引用工艺参数,则根据参数编号判断工艺项是否存在 String paramValue; if (isFinishedProduct) { paramValue = technologyUtils.getRoutingOperationParamValueFinishedproduct(docBomId, routingOperationId, documentStandardParam.getCode(), documentStandardParam.getWireCore()); } else { paramValue = technologyUtils.getRoutingOperationParamValue(docBomId, routingOperationId, documentStandardParam.getCode()); } if (paramValue != null) { documentStandardParam.setReferenceValue(paramValue);//取到有效值后才引用 } } documentStandardParamMapper.insert(documentStandardParam); } // 查找是否存在抽检规则 RoutingOperation routingOperation = routingOperationMapper.selectById(routingOperationId); DocumentSamplingRule documentSamplingRule = documentSamplingRuleMapper.getSamplingRuleByOperationIdAndApplyType(routingOperation.getOperationId(), testType); addDocSamplingRule(docBomId, routingOperationId, null, documentSamplingRule, testType); } JoinDocumentBomRouting joinDocumentBomRouting = joinDocumentBomRoutingMapper.selectById(docBomId); JoinDocumentTestStandard joinDocumentTestStandard = new JoinDocumentTestStandard(); joinDocumentTestStandard.setDocumentId(joinDocumentBomRouting.getDocumentId()); joinDocumentTestStandard.setTestStandardId(documentTestStandard.getId()); joinDocumentTestStandardMapper.insert(joinDocumentTestStandard); } return true; } @Override public boolean delDocTestStandardById(Long id) { DocumentTestStandard documentTestStandard = documentTestStandardMapper.selectById(id); Long docId = joinDocumentBomRoutingMapper.selectById(documentTestStandard.getDocBomId()).getDocumentId(); //产出明细 documentStandardParamMapper.delete(Wrappers.lambdaQuery().eq(DocumentStandardParam::getTestStandardId, id)); //删主表 documentTestStandardMapper.deleteById(id); //删除工艺文件与检测标准关联表 joinDocumentTestStandardMapper.delete(Wrappers.lambdaQuery().eq(JoinDocumentTestStandard::getTestStandardId, id)); List joinDocumentTestStandardList = joinDocumentTestStandardMapper.selectList(Wrappers.lambdaQuery() .eq(JoinDocumentTestStandard::getDocumentId, docId)); Document document = new Document(); document.setId(docId); if(joinDocumentTestStandardList==null){ document.setTestStandard(false); } baseMapper.updateById(doUpdateRealUser(document)); return true; } @Override public R qryDocTestStandard(Long docBomId, Long routingOperationId) { List documentTestStandardList = documentTestStandardMapper.selectList(Wrappers.lambdaQuery().eq(DocumentTestStandard::getDocBomId, docBomId).eq(DocumentTestStandard::getRoutingOperationId, routingOperationId)); return R.ok(documentTestStandardList); } @Override public R delDocTestStandardDetailById(Long id) { updateDocumentByStandardParamId(id); documentStandardParamMapper.deleteById(id); return R.ok(); } @Override public R updDocTestStandardDetails(List documentStandardParamList) { if (CollectionUtils.isEmpty(documentStandardParamList)) { return R.failed("提交数据为空"); } else { for (DocumentStandardParam documentStandardParam : documentStandardParamList) { documentStandardParamMapper.updateById(documentStandardParam); } updateDocumentByStandardParamId(documentStandardParamList.get(0).getId()); return R.ok(); } } @Override public R qryDocTestStandardDetailsByStandardId(Long standardId) { List documentStandardParamList = documentStandardParamMapper.selectList(Wrappers.lambdaQuery().eq(DocumentStandardParam::getTestStandardId, standardId)); return R.ok(documentStandardParamList); } @Override public boolean batchDel(List ids) { //验证是否可以删除 List documentList = baseMapper.selectList(Wrappers.lambdaQuery() .eq(Document::getState, DocumentStateStringValues.ACCEPTED).in(Document::getId, ids)); if (CollectionUtil.isNotEmpty(documentList)) { throw new RuntimeException("存在已接收工艺文件删除失败"); } //删除对应检测标准数据 List joinDocumentBomRoutingList = joinDocumentBomRoutingMapper.selectList(Wrappers.lambdaQuery() .in(JoinDocumentBomRouting::getDocumentId, ids)); //删除中间表和检测标准 delJoinAndTest(joinDocumentBomRoutingList); //删除工艺文件 baseMapper.delete(Wrappers.lambdaQuery().in(Document::getId, ids)); //删除检测标准关系 joinDocumentTestStandardMapper.delete(Wrappers.lambdaQuery() .in(JoinDocumentTestStandard::getDocumentId, ids)); //删除工艺文件物料信息 documentMaterialCostMapper.delete(Wrappers.lambdaQuery() .in(DocumentMaterialCost::getDocumentId, ids)); return true; } private void delJoinAndTest(List joinDocumentBomRoutingList) { if (CollectionUtil.isNotEmpty(joinDocumentBomRoutingList)) { //查询工艺文件中间表 List jdbids = joinDocumentBomRoutingList.stream().map(JoinDocumentBomRouting::getId).collect(Collectors.toList()); //工艺文件关联的检测标准 List documentTestStandardList = documentTestStandardMapper.selectList(Wrappers.lambdaQuery() .in(DocumentTestStandard::getDocBomId, jdbids)); if (CollectionUtil.isNotEmpty(documentTestStandardList)) { //lambda 获取id List standardIds = documentTestStandardList.stream().map(DocumentTestStandard::getId).collect(Collectors.toList()); //删除检测标准数据 documentTestStandardMapper.deleteBatchIds(standardIds); documentStandardParamMapper.delete(Wrappers.lambdaQuery().in(DocumentStandardParam::getTestStandardId, standardIds)); } //删除关联关系 joinDocumentBomRoutingMapper.deleteBatchIds(jdbids); } } private void delJoinAndTest(JoinDocumentBomRouting joinDocumentBomRouting) { if (null != joinDocumentBomRouting) { List documentTestStandardList = documentTestStandardMapper.selectList(Wrappers.lambdaQuery() .eq(DocumentTestStandard::getDocBomId, joinDocumentBomRouting.getId())); if (CollectionUtil.isNotEmpty(documentTestStandardList)) { //lambda 获取id List standardIds = documentTestStandardList.stream().map(DocumentTestStandard::getId).collect(Collectors.toList()); //删除检测标准数据 documentTestStandardMapper.deleteBatchIds(standardIds); //删除子表 documentStandardParamMapper.delete(Wrappers.lambdaQuery().in(DocumentStandardParam::getTestStandardId, standardIds)); } } } @Override public Document changeTestStandard(Long docBomId) { Long docId = joinDocumentBomRoutingMapper.selectById(docBomId).getDocumentId(); Document document = new Document(); document.setId(docId); document.setTestStandard(true); baseMapper.updateById(doUpdateRealUser(document)); return document; } public void updateDocumentByStandardParamId(Long standardParamId){ DocumentStandardParam documentStandardParam = documentStandardParamMapper .selectById(standardParamId); List joinDocumentTestStandardList = joinDocumentTestStandardMapper.selectList(Wrappers.lambdaQuery() .eq(JoinDocumentTestStandard::getTestStandardId, documentStandardParam.getTestStandardId())); Document document = new Document(); document.setId(joinDocumentTestStandardList.get(0).getDocumentId()); baseMapper.updateById(doUpdateRealUser(document)); } public Document doUpdateRealUser(Document document){ ZttUser currentUser = SecurityUtils.getUser(); document.setUpdateRealUser(currentUser.getUsername()); document.setUpdateRealTime(LocalDateTime.now()); return document; } @Override public Boolean addDocSamplingRule(Long docBomId, Long routingOperationId, Long samplingRuleId, DocumentSamplingRule documentSamplingRule, String applyType) { if (samplingRuleId != null) { documentSamplingRule = documentSamplingRuleMapper.getSamplingRuleBySamplingRuleId(samplingRuleId); applyType = documentSamplingRule.getApplyType(); } RoutingOperation routingOperation = routingOperationMapper.selectById(routingOperationId); DocumentSamplingRule currentDocumentSamplingRule = documentSamplingRuleMapper.selectOne(Wrappers.lambdaQuery() .eq(DocumentSamplingRule::getDocBomId, docBomId) .eq(DocumentSamplingRule::getSamplingOperationId, routingOperation.getOperationId()) .eq(DocumentSamplingRule::getApplyType, applyType)); if (currentDocumentSamplingRule == null) { // 查找是否配置了基础抽检规则 if (documentSamplingRule != null) { documentSamplingRule.setDocBomId(docBomId); documentSamplingRule.setRoutingOperationId(routingOperationId); return SqlHelper.retBool(this.documentSamplingRuleMapper.insert(documentSamplingRule)); } } return false; } @Override public List qryDocSamplingRule(Long docBomId, Long routingOperationId) { return this.documentSamplingRuleMapper.selectList(Wrappers.lambdaQuery() .eq(DocumentSamplingRule::getDocBomId, docBomId) .eq(DocumentSamplingRule::getRoutingOperationId, routingOperationId)); } @Override public void export(HttpServletResponse response) throws IOException { //新建ExcelWriter ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build(); try { response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("UTF-8"); // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 String fileName = URLEncoder.encode("工艺文件检测标准导入模板", "UTF-8"); response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); //获取sheet0对象 WriteSheet mainSheet = EasyExcel.writerSheet(0, "导入模板").head(DocumentTestStandardData.class).build(); //向sheet0写入数据 传入空list这样只导出表头 List list = new ArrayList<>(); excelWriter.write(list, mainSheet); } catch (IOException e) { throw new RuntimeException("导出失败"); } finally { if (excelWriter != null) { excelWriter.finish(); } } } @Override public void importExcel(List list) { if (CollectionUtil.isNotEmpty(list)) { Set docIds = new HashSet<>(); for (DocumentTestStandardData standardData : list) { // 查找检测标准 DocumentTestStandard standard = documentTestStandardMapper.getTestStandardByStandardNo(standardData.getStandardNo()); if (standard == null) { throw new RuntimeException("检测标准编号为:" + standardData.getStandardNo() + "的检测标准不存在"); } // 查找工艺文件 Document document = this.getOne(Wrappers.lambdaQuery().eq(Document::getNumber, standardData.getDocumentNo())); if (document == null) { throw new RuntimeException("工艺文件编号为:" + standardData.getDocumentNo() + "的工艺文件不存在"); } docIds.add(document.getId()); // 查找工艺路线 Routing routing = routingMapper.selectOne(Wrappers.lambdaQuery().eq(Routing::getRoutingNo, standardData.getRoutingNo())); if (routing == null) { throw new RuntimeException("工艺路线编号为:" + standardData.getRoutingNo() + "的工艺路线不存在"); } // 查找工序 Operation operation = operationMapper.selectOne(Wrappers.lambdaQuery().eq(Operation::getOperationNo, standardData.getOperationNo())); if (operation == null) { throw new RuntimeException("工序编号为:" + standardData.getOperationNo() + "的工序不存在"); } // 查找零件 Part part = partMapper.selectOne(Wrappers.lambdaQuery().eq(Part::getPartNo, standardData.getPartNo())); if (part == null) { throw new RuntimeException("零件编号为:" + standardData.getPartNo() + "的零件不存在"); } // 查找工艺文件与工艺、BOM关系表数据 JoinDocumentBomRouting joinDocumentBomRouting = joinDocumentBomRoutingMapper.selectOne(Wrappers.lambdaQuery() .eq(JoinDocumentBomRouting::getDocumentId, document.getId()) .eq(JoinDocumentBomRouting::getRoutingId, routing.getId()) .eq(JoinDocumentBomRouting::getPartId, part.getId())); if (joinDocumentBomRouting == null) { throw new RuntimeException("工艺文件编号为:" + standardData.getDocumentNo() + "的工艺文件,工艺路线编号为:" + standardData.getRoutingNo() + "的工艺路线,零件编号为:" + standardData.getPartNo() + "的BOM结构不存在"); } // 查找工艺工序 RoutingOperation routingOperation = routingOperationMapper.selectOne(Wrappers.lambdaQuery() .eq(RoutingOperation::getRoutingId, routing.getId()) .eq(RoutingOperation::getOperationId, operation.getId())); if (routingOperation == null) { throw new RuntimeException("工艺路线编号为:" + standardData.getRoutingNo() + "的工艺路线,工序编号为:" + standardData.getOperationNo() + "的工序,零件编号为:" + standardData.getPartNo() + "的工艺工序不存在"); } // 查找是否已经绑定过相同检测标准 int count = documentTestStandardMapper.selectCount(Wrappers.lambdaQuery() .eq(DocumentTestStandard::getDocBomId, joinDocumentBomRouting.getId()) .eq(DocumentTestStandard::getRoutingOperationId, routingOperation.getId()) .eq(DocumentTestStandard::getStandardNo, standard.getStandardNo())); if (count > 0) { continue; } String testType = standard.getInspectionType(); boolean isFinishedProduct = testType.equals("06finishedproduct"); // 检测标准那边导入有问题,没有把name转成code List documentStandardParamList = documentStandardParamMapper.getDocumentStandardParamListByStandardId(standard.getId()); standard.setDocBomId(joinDocumentBomRouting.getId());// 工艺文件与bom关系表id standard.setRoutingOperationId(routingOperation.getId());// 工艺工序id documentTestStandardMapper.insert(standard); for (DocumentStandardParam documentStandardParam : documentStandardParamList) { documentStandardParam.setTestStandardId(standard.getId());// 主表id if (BooleanUtil.isTrue(documentStandardParam.getIsReference())) { // 成品检的引用池范围:工艺文件中所有工艺路线下所有工序对应的工序参数;其他检引用池范围:该工序下的工艺参数 // 若引用工艺参数,则根据参数编号判断工艺项是否存在 String paramValue; if (isFinishedProduct) { paramValue = technologyUtils.getRoutingOperationParamValueFinishedproduct(joinDocumentBomRouting.getId(), routingOperation.getId(), documentStandardParam.getCode(), documentStandardParam.getWireCore()); } else { paramValue = technologyUtils.getRoutingOperationParamValue(joinDocumentBomRouting.getId(), routingOperation.getId(), documentStandardParam.getCode()); } if (paramValue != null) { documentStandardParam.setReferenceValue(paramValue);// 取到有效值后才引用 } } documentStandardParamMapper.insert(documentStandardParam); } JoinDocumentTestStandard joinDocumentTestStandard = new JoinDocumentTestStandard(); joinDocumentTestStandard.setDocumentId(document.getId()); joinDocumentTestStandard.setTestStandardId(standard.getId()); joinDocumentTestStandardMapper.insert(joinDocumentTestStandard); } if (docIds.size() > 0) { this.update(Wrappers.lambdaUpdate().set(Document::getTestStandard, true).in(Document::getId, docIds)); } } } }