package com.chinaztt.mes.quality.service.impl; import cn.hutool.core.collection.CollectionUtil; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; 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.chinaztt.mes.basic.entity.Part; import com.chinaztt.mes.basic.entity.WorkstationLocation; import com.chinaztt.mes.basic.mapper.PartMapper; import com.chinaztt.mes.basic.mapper.WorkstationLocationMapper; import com.chinaztt.mes.quality.dto.*; import com.chinaztt.mes.quality.entity.Apply; import com.chinaztt.mes.quality.entity.ApplyPart; import com.chinaztt.mes.quality.entity.ReportSample; import com.chinaztt.mes.quality.entity.Result; import com.chinaztt.mes.quality.excel.ResultListV2Data; import com.chinaztt.mes.quality.mapper.ApplyPartMapper; import com.chinaztt.mes.quality.mapper.AuditingMapper; import com.chinaztt.mes.quality.mapper.ReportSampleMapper; import com.chinaztt.mes.quality.mapper.ResultMapper; import com.chinaztt.mes.quality.service.ReportSampleService; import com.chinaztt.mes.quality.service.ResultService; import com.chinaztt.mes.quality.state.result.constant.ResultStateStringValues; import com.chinaztt.mes.quality.utils.ResultUtils; import com.chinaztt.mes.warehouse.entity.Stock; import com.chinaztt.mes.warehouse.util.StockUtils; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.sql.Timestamp; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; /** * 检测结果表 * * @author cxf * @date 2021-04-21 13:21:49 */ @Service @AllArgsConstructor @Transactional(rollbackFor = Exception.class) public class ResultServiceImpl extends ServiceImpl implements ResultService { private StockUtils stockUtils; private ResultUtils resultUtils; private PartMapper partMapper; private ApplyPartMapper applyPartMapper; private WorkstationLocationMapper workstationLocationMapper; private ReportSampleService reportSampleService; private ReportSampleMapper reportSampleMapper; public static final Pattern QUOTATION_MARK_PATTERN = Pattern.compile("\"(.*?)\""); private AuditingMapper auditingMapper; @Override public IPage getResultPage(Page page, QueryWrapper gen) { return baseMapper.getResultPage(page, gen); } @Override public boolean batchUpdateResultById(List resultList) { resultList.stream().forEach(resultDTO -> { baseMapper.batchUpdateResultById(resultDTO); if (resultDTO.getIsUsed() != null && resultDTO.getIsUsed() == true) { if (resultDTO.getApplyPartId() != null) { // 做移库操作 String applyType = applyPartMapper.selectApplyTypeByApplyPartId(resultDTO.getApplyPartId()); // 产出报检才自动移库 if (StringUtils.isNotBlank(applyType) && Apply.OUTPUT_APPLY.equals(applyType)) { try { Long partId = applyPartMapper.selectPartIdBySystemNo(resultDTO.getSystemNo()); if (partId != null) { //检测申请 ApplyPart applyPart = applyPartMapper.selectById(resultDTO.getApplyPartId()); Long disqualifiedLocationId = workstationLocationMapper.selectBySystemNo(applyPart.getSystemNo(), WorkstationLocation.DISQUALIFIED_LOCATION); Long qualifiedLocationId = workstationLocationMapper.selectBySystemNo(applyPart.getSystemNo(), WorkstationLocation.QUALIFIED_LOCATION); stockUtils.localMove(partId, applyPart.getSystemNo(), applyPart.getLotBatchNo(), new BigDecimal(applyPart.getUnqualifiedArrived()), disqualifiedLocationId, qualifiedLocationId); } } catch (RuntimeException e) { } } } } }); return true; } @Override public boolean addResultByReceiving(List stockList) { if (CollectionUtil.isNotEmpty(stockList)) { stockList.stream().forEach(stock -> { // 添加检测结果表 Part part = partMapper.selectById(stock.getPartId()); Boolean isUsed = true; if (part != null && part.getUseSystemNo()) { isUsed = null; } resultUtils.addOrUpdateResult(stock.getSystemNo(), stock.getPartBatchNo(), ResultStateStringValues.UNTESTED, null, null, isUsed, null, stock.getPartId(), null, Apply.LOCAL_APPLY); }); } return true; } @Override public List getSampleItems(Long id) { Result result = this.baseMapper.selectById(id); List reportIds = partMapper.selectReportIdBySystemNoAndType(result.getSystemNo(), result.getApplyType()); List resultList = new ArrayList<>(); reportIds.forEach(reportId -> { List reportSamples = reportSampleService.list(Wrappers.lambdaQuery().eq(ReportSample::getReportId, reportId)); if (!reportSamples.isEmpty()) { for (ReportSample reportSample : reportSamples) { ReportSampleDTO dto = new ReportSampleDTO(); dto.setId(reportSample.getId()); List items = reportSampleService.getSampleItemsById(dto); resultList.addAll(items); } } }); return resultList; } @Override public ReportSampleDTO getResultPageTwo(Page page, ResultPageTwoQueryDTO queryDTO) { if (StringUtils.isBlank(queryDTO.getTestStandardNo())) { throw new RuntimeException("检测编号不能为空"); } if (queryDTO.getMoId() == null) { throw new RuntimeException("制造订单号不能为空"); } ReportSampleDTO result = new ReportSampleDTO(); //查询列名 List itemNameList = reportSampleMapper.getTestResult(queryDTO.getTestStandardNo(), queryDTO.getMoId()).stream().map(MoTestStandardParamDTO::getItemName).collect(Collectors.toList()); result.setItemNameList(itemNameList); String itemName = reportSampleMapper.getTest(queryDTO.getTestStandardNo(), queryDTO.getMoId()); IPage reportList = reportSampleMapper.getTestValue(page, queryDTO, itemName); result.setReportList(reportList); return result; } @Override public Object getResultPageV2(Page page, ResultPageV2QueryDTO queryDTO) { if (StringUtils.isBlank(queryDTO.getTestStandardNo())) { throw new RuntimeException("检测编号不能为空"); } ResultPageV2ResultDTO result = new ResultPageV2ResultDTO(); // 查询列名 List itemNameList = reportSampleMapper.getItemNameList(queryDTO.getTestStandardNo(), queryDTO.getTestType(), queryDTO.getPartId(), queryDTO.getPartBatchNo()); result.setItemNameList(itemNameList); String itemName = reportSampleMapper.getItemNameString(queryDTO.getTestStandardNo(), queryDTO.getTestType(), queryDTO.getPartId(), queryDTO.getPartBatchNo()); IPage reportList = reportSampleMapper.getTestValueV2(page, queryDTO, itemName); result.setValueList(reportList); return result; } @Override public Map>> getResultListV2(ResultPageV2QueryDTO queryDTO) { Map>> result = new HashMap<>(2); //获取列名 //拼接crosstab所需要的验证数据 String itemName = reportSampleMapper.getItemNameString(queryDTO.getTestStandardNo(), queryDTO.getTestType(), queryDTO.getPartId(), queryDTO.getPartBatchNo()); List> head = new ArrayList<>(); List headList = new ArrayList<>(); Matcher m = QUOTATION_MARK_PATTERN.matcher(itemName); while (m.find()) { String headValue = m.group().replace("\"", ""); // 过滤分组用的id字段 if (headValue.contains("id") || headValue.contains("Id")) { continue; } String label = ResultListV2Data.getValue(headValue); headList.add(headValue); List headName = new ArrayList<>(); if (StringUtils.isNotBlank(label)) { headName.add(label); } else { headName.add(headValue); } head.add(headName); } //获取数据 List list = reportSampleMapper.getResultListV2(queryDTO, itemName); List> data = new ArrayList<>(); if (CollectionUtil.isNotEmpty(list)) { for (JSONObject json : list) { if (json.containsKey("isQualified")) { if (json.getBoolean("isQualified")) { json.put("isQualified", "合格"); } else { json.put("isQualified", "不合格"); } } if (json.containsKey("reportType")) { if (Apply.FIRST_APPLY.equals(json.getString("reportType"))) { json.put("reportType", "首检"); } else if (Apply.OUTPUT_APPLY.equals(json.getString("reportType"))) { json.put("reportType", "尾检"); } else if (Apply.PROCESS_APPLY.equals(json.getString("reportType"))) { json.put("reportType", "工序检"); } else if (Apply.PRODUCT_APPLY.equals(json.getString("reportType"))) { json.put("reportType", "成品检"); } else { json.put("reportType", "其他"); } } List dataValue = new ArrayList<>(); for (String headName : headList) { dataValue.add(json.getString(headName)); } data.add(dataValue); } } result.put("data", data); result.put("head", head); return result; } @Override public Map>> getTestReportResult(ResultPageTwoQueryDTO reportSampleDTO) { Map>> result = new HashMap<>(2); //获取列名 //拼接crosstab所需要的验证数据 String itemName = reportSampleMapper.getTest(reportSampleDTO.getTestStandardNo(), reportSampleDTO.getMoId()); List> head = new ArrayList<>(); List headList = new ArrayList<>(); Matcher m = QUOTATION_MARK_PATTERN.matcher(itemName); while (m.find()) { String headValue = m.group().replace("\"", ""); // 过滤分组用的id字段 if (headValue.contains("id") || headValue.contains("Id")) { continue; } String label = com.chinaztt.mes.quality.excel.ReportSamoleData.getValue(headValue); headList.add(headValue); List headName = new ArrayList<>(); if (StringUtils.isNotBlank(label)) { headName.add(label); } else { headName.add(headValue); } head.add(headName); } //获取数据 List list = reportSampleMapper.getTestValue(reportSampleDTO, itemName); List> data = new ArrayList<>(); if (CollectionUtil.isNotEmpty(list)) { for (JSONObject json : list) { if (json.containsValue("isQualified")) { if (json.getBoolean("isQualified")) { json.put("isQualified", "合格"); } else { json.put("isQualified", "不合格"); } } List dataValue = new ArrayList<>(); for (String headName : headList) { dataValue.add(json.getString(headName)); } data.add(dataValue); } } result.put("data", data); result.put("head", head); return result; } @Override public List getAuditingDetailReport(Timestamp begin, Timestamp end) { return auditingMapper.getAuditingDetail(begin, end); } @Override public List getAuditingReport(Timestamp begin, Timestamp end) { List detailList = auditingMapper.getAuditingDetail(begin, end); List reportDTOS = new ArrayList<>(); if (CollectionUtil.isEmpty(detailList)) { return reportDTOS; } Map> detailMap = detailList.stream().collect(Collectors.groupingBy(AuditingDetailReportDTO::getPartNo)); for (Map.Entry> entry : detailMap.entrySet()) { List details = entry.getValue(); AuditingReportDTO reportDTO = new AuditingReportDTO(); reportDTO.setPartName(details.get(0).getPartName()); reportDTO.setPartNo(details.get(0).getPartNo()); reportDTO.setCreateTime(details.get(0).getCreateTime()); reportDTO.setUnit(details.get(0).getUnit()); BigDecimal productQuantity = details.stream().map(AuditingDetailReportDTO::getProductQuantity).reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal inspQuantity = details.stream().map(AuditingDetailReportDTO::getInspQuantity).reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal packagingQuantity = details.stream().filter(d -> org.apache.commons.lang3.StringUtils.equals("是", d.getIsPackaging())).map(AuditingDetailReportDTO::getProductQuantity).reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal stockQuantity = details.stream().filter(d -> org.apache.commons.lang3.StringUtils.equals("是", d.getIsStock())).map(AuditingDetailReportDTO::getProductQuantity).reduce(BigDecimal.ZERO, BigDecimal::add); reportDTO.setProductQuantity(productQuantity); reportDTO.setInspQuantity(inspQuantity); reportDTO.setPackagingQuantity(packagingQuantity); reportDTO.setStockQuantity(stockQuantity); reportDTOS.add(reportDTO); } return reportDTOS; } }