From 289dbe69d68fcfed09ec8a10b7a81495e6416ffc Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Tue, 17 May 2022 10:14:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A3=80=E9=AA=8C=E6=95=B0=E6=8D=AE=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E4=B8=8A=E4=BC=A0=E6=97=B6=E9=97=B4=E5=92=8C=E6=89=B9?= =?UTF-8?q?=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/xkrs/dao/QcSourceDao.java | 9 +- .../service/impl/QcSourceServiceImpl.java | 185 ++++++++++++++---- src/main/java/com/xkrs/util/Constant.java | 2 + 3 files changed, 150 insertions(+), 46 deletions(-) diff --git a/src/main/java/com/xkrs/dao/QcSourceDao.java b/src/main/java/com/xkrs/dao/QcSourceDao.java index ce16aef..8866738 100644 --- a/src/main/java/com/xkrs/dao/QcSourceDao.java +++ b/src/main/java/com/xkrs/dao/QcSourceDao.java @@ -7,7 +7,6 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Component; import java.util.List; -import java.util.Map; /** * 检验数据Dao @@ -15,10 +14,10 @@ import java.util.Map; @Component public interface QcSourceDao extends JpaRepository, JpaSpecificationExecutor { - @Query(value = "SELECT id, batch_no AS batchNo, machine_no AS machineNo, material_no AS materialNo, mould_no AS mouldNo, variety_no AS varietyNo, craft_item_no AS craftItemNo, qc_item_no AS qcItemNo, qc_value AS qcValue FROM qc_source WHERE batch_no LIKE %?1% AND machine_no LIKE %?2% AND material_no LIKE %?3% AND mould_no LIKE %?4% AND variety_no LIKE %?5% ORDER BY create_time DESC", nativeQuery = true) - List> queryQcSource(String batchNo, String machineNo, String materialNo, String mouldNo, String varietyNo); + @Query(value = "SELECT * FROM qc_source WHERE batch_no LIKE %?1% AND machine_no LIKE %?2% AND material_no LIKE %?3% AND mould_no LIKE %?4% AND variety_no LIKE %?5% AND craft_item_no LIKE %?6% ORDER BY create_time DESC", nativeQuery = true) + List queryQcSource(String batchNo, String machineNo, String materialNo, String mouldNo, String varietyNo, String craftItemNo); - @Query(value = "SELECT id, batch_no AS batchNo, machine_no AS machineNo, material_no AS materialNo, mould_no AS mouldNo, variety_no AS varietyNo, craft_item_no AS craftItemNo, qc_item_no AS qcItemNo, qc_value AS qcValue FROM qc_source WHERE batch_no LIKE %?1% AND machine_no LIKE %?2% AND material_no LIKE %?3% AND mould_no LIKE %?4% AND variety_no LIKE %?5% AND craft_item_no = ?6 ORDER BY create_time DESC", nativeQuery = true) - List> queryQcSource(String batchNo, String machineNo, String materialNo, String mouldNo, String varietyNo, String craftItemNo); + @Query(value = "SELECT create_time FROM qc_source WHERE batch_no LIKE %?1% AND machine_no LIKE %?2% AND material_no LIKE %?3% AND mould_no LIKE %?4% AND variety_no LIKE %?5% AND craft_item_no LIKE %?6% ORDER BY create_time ASC", nativeQuery = true) + List queryQcSourceAsc(String batchNo, String machineNo, String materialNo, String mouldNo, String varietyNo, String craftItemNo); } \ No newline at end of file diff --git a/src/main/java/com/xkrs/service/impl/QcSourceServiceImpl.java b/src/main/java/com/xkrs/service/impl/QcSourceServiceImpl.java index 5802025..5795de5 100644 --- a/src/main/java/com/xkrs/service/impl/QcSourceServiceImpl.java +++ b/src/main/java/com/xkrs/service/impl/QcSourceServiceImpl.java @@ -57,8 +57,8 @@ public class QcSourceServiceImpl implements QcSourceService { * 导出excel样式一(表头顺序) * 批次号 机器号 物料号 模具号 机种号 */ - List EXCEL_HEAD_STYLE_ONE_NO = Arrays.asList("batchNo", "machineNo", "materialNo", "mouldNo", "varietyNo", "craftItemNo"); - List EXCEL_HEAD_STYLE_ONE_NAME = Arrays.asList("批次", "机器", "物料", "模具", "机种", "工艺项目"); + List EXCEL_HEAD_STYLE_ONE_NO = Arrays.asList("batchNo", "machineNo", "materialNo", "mouldNo", "varietyNo", "craftItemNo", "createTime", "index"); + List EXCEL_HEAD_STYLE_ONE_NAME = Arrays.asList("批次", "机器", "物料", "模具", "机种", "工艺项目", "上传时间", "批次"); /** * 添加检验数据 @@ -148,6 +148,7 @@ public class QcSourceServiceImpl implements QcSourceService { */ @Override public String queryQcSource(String batchNo, String machineNo, String materialNo, String mouldNo, String varietyNo, String craftItemNo) { + long startTimeMillis = System.currentTimeMillis(); List craftItemList = craftItemDao.findAll(); if (craftItemList.isEmpty()) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "未添加工艺项目,请先添加工艺项目!", locale); @@ -161,37 +162,61 @@ public class QcSourceServiceImpl implements QcSourceService { String finalMaterialNo = LocalStringUtils.formatEmptyValue(materialNo).trim(); String finalMouldNo = LocalStringUtils.formatEmptyValue(mouldNo).trim(); String finalVarietyNo = LocalStringUtils.formatEmptyValue(varietyNo).trim(); - List> sourceList; - if (TextUtils.isEmpty(craftItemNo)) { - sourceList = qcSourceDao.queryQcSource(finalBatchNo, finalMachineNo, finalMaterialNo, finalMouldNo, finalVarietyNo); - } else { - sourceList = qcSourceDao.queryQcSource(finalBatchNo, finalMachineNo, finalMaterialNo, finalMouldNo, finalVarietyNo, craftItemNo.trim()); - } - List> resultList = generateResultList(sourceList); + String finalCraftItemNo = LocalStringUtils.formatEmptyValue(craftItemNo).trim(); + //检验数据结果集 + List sourceList = qcSourceDao.queryQcSource(finalBatchNo, finalMachineNo, finalMaterialNo, finalMouldNo, finalVarietyNo, finalCraftItemNo); + //检验规格 + List qcSpecList = qcSpecDao.findAll(); + List> resultList = generateResultList(sourceList, qcSpecList); + System.out.println("查询检验数据耗时:" + formatDuring(System.currentTimeMillis() - startTimeMillis)); if (resultList.isEmpty()) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时没有该检验数据的信息!", locale); } return outputEncapsulationObject(PromptMessageEnum.SUCCESS, resultList, locale); } + public static String formatDuring(long mss) { + long days = mss / (1000 * 60 * 60 * 24); + long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60); + long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60); + long seconds = (mss % (1000 * 60)) / 1000; + if (days <= 0 && hours <= 0 && minutes <= 0) { + return seconds + " 秒 "; + } + if (days <= 0 && hours <= 0) { + return minutes + " 分钟 " + seconds + " 秒 "; + } + if (days <= 0) { + return hours + " 小时 " + minutes + " 分钟 " + seconds + " 秒 "; + } + return days + " 天 " + hours + " 小时 " + minutes + " 分钟 " + seconds + " 秒 "; + } + /** * 生成返回结果 + * + * @param sourceList 检验数据结果集 + * @param qcSpecList 检验规格结果集 + * @return */ - private List> generateResultList(List> sourceList) { + private List> generateResultList(List sourceList, List qcSpecList) { List> resultList = new ArrayList<>(); if (sourceList == null || sourceList.isEmpty()) { return resultList; } - for (Map source : sourceList) { - String batchNo = (String) source.getOrDefault("batchNo", ""); - String machineNo = (String) source.getOrDefault("machineNo", ""); - String materialNo = (String) source.getOrDefault("materialNo", ""); - String mouldNo = (String) source.getOrDefault("mouldNo", ""); - String varietyNo = (String) source.getOrDefault("varietyNo", ""); - String craftItemNo = (String) source.getOrDefault("craftItemNo", ""); - String qcItemNo = (String) source.getOrDefault("qcItemNo", ""); - String qcValue = (String) source.getOrDefault("qcValue", ""); - String formatValue = getFormatValue(varietyNo, craftItemNo, qcItemNo, qcValue); + long startTimeMillis = System.currentTimeMillis(); + //这个for循环完成后:一维结果集被格式化为二维结果集,每一行的上传时间是本行所有数据的最早时间 + for (QcSourceEntity sourceEntity : sourceList) { + String batchNo = LocalStringUtils.formatEmptyValue(sourceEntity.getBatchNo()); + String machineNo = LocalStringUtils.formatEmptyValue(sourceEntity.getMachineNo()); + String materialNo = LocalStringUtils.formatEmptyValue(sourceEntity.getMaterialNo()); + String mouldNo = LocalStringUtils.formatEmptyValue(sourceEntity.getMouldNo()); + String varietyNo = LocalStringUtils.formatEmptyValue(sourceEntity.getVarietyNo()); + String craftItemNo = LocalStringUtils.formatEmptyValue(sourceEntity.getCraftItemNo()); + String createTime = LocalStringUtils.formatEmptyValue(sourceEntity.getCreateTime()); + String qcItemNo = LocalStringUtils.formatEmptyValue(sourceEntity.getQcItemNo()); + String qcValue = LocalStringUtils.formatEmptyValue(sourceEntity.getQcValue()); + String formatValue = getFormatValue(qcSpecList, varietyNo, craftItemNo, qcItemNo, qcValue); Map emptyRoom = findEmptyRoom(resultList, qcItemNo, batchNo, machineNo, materialNo, mouldNo, varietyNo, craftItemNo); if (emptyRoom == null) { Map newRoom = new HashMap<>(); @@ -201,32 +226,113 @@ public class QcSourceServiceImpl implements QcSourceService { newRoom.put(EXCEL_HEAD_STYLE_ONE_NO.get(3), mouldNo); newRoom.put(EXCEL_HEAD_STYLE_ONE_NO.get(4), varietyNo); newRoom.put(EXCEL_HEAD_STYLE_ONE_NO.get(5), craftItemNo); + newRoom.put(EXCEL_HEAD_STYLE_ONE_NO.get(6), createTime);//新行的上传时间使用第一个数据的上传时间 newRoom.put(qcItemNo, formatValue); resultList.add(newRoom); } else { emptyRoom.put(qcItemNo, formatValue); + long oldTime = Long.parseLong((String) emptyRoom.get(EXCEL_HEAD_STYLE_ONE_NO.get(6))); + if (Long.parseLong(createTime) < oldTime) {//已存在行的上传时间用更早的时间替换更晚的时间 + emptyRoom.put(EXCEL_HEAD_STYLE_ONE_NO.get(6), createTime); + } } } + //这个for循环完成后:二维结果集每一行的上传时间是本分组所有数据的最早时间 + for (Map result : resultList) { + String createTime = (String) result.get(EXCEL_HEAD_STYLE_ONE_NO.get(6)); + List createTimeTagList = getCreateTimeTagList(resultList, result); + int createTimeTagIndex = getCreateTimeTagIndex(createTimeTagList, Long.parseLong(createTime)); + result.put(EXCEL_HEAD_STYLE_ONE_NO.get(6), String.valueOf(createTimeTagList.get(createTimeTagIndex))); + result.put(EXCEL_HEAD_STYLE_ONE_NO.get(7), "第" + (createTimeTagIndex + 1) + "批"); + } + System.out.println("generateResultList耗时:" + formatDuring(System.currentTimeMillis() - startTimeMillis)); return resultList; } + /** + * 获取上传时间节点索引 + * + * @param createTimeTagList 上传时间节点集合 + * @param createTimeLong 当前行上传时间(其实也是当前行所有数据的最早上传时间) + * @return 上传时间节点索引 + */ + private int getCreateTimeTagIndex(List createTimeTagList, long createTimeLong) { + for (int i = 0; i < createTimeTagList.size(); i++) { + long dur = createTimeLong - createTimeTagList.get(i); + if (dur >= 0 && dur <= Constant.QC_TIME_INTERVAL) { + return i; + } + } + throw new RuntimeException("获取上传时间节点索引时发生异常"); + } + + /** + * 获取上传时间节点集合 + * + * @param resultList 二维结果集 + * @param currentResult 当前行 + * @return 当前行所在分组的时间节点集合 + */ + private List getCreateTimeTagList(List> resultList, Map currentResult) { + String currentBatchNo = (String) currentResult.get(EXCEL_HEAD_STYLE_ONE_NO.get(0)); + String currentMachineNo = (String) currentResult.get(EXCEL_HEAD_STYLE_ONE_NO.get(1)); + String currentMaterialNo = (String) currentResult.get(EXCEL_HEAD_STYLE_ONE_NO.get(2)); + String currentMouldNo = (String) currentResult.get(EXCEL_HEAD_STYLE_ONE_NO.get(3)); + String currentVarietyNo = (String) currentResult.get(EXCEL_HEAD_STYLE_ONE_NO.get(4)); + String currentCraftItemNo = (String) currentResult.get(EXCEL_HEAD_STYLE_ONE_NO.get(5)); + List createTimeList = new ArrayList<>(); + //这个for循环的作用是在二维结果集中过滤出和当前行数据属于同一分组的所有行上传时间集合 + for (Map result : resultList) { + String batchNo = (String) result.get(EXCEL_HEAD_STYLE_ONE_NO.get(0)); + String machineNo = (String) result.get(EXCEL_HEAD_STYLE_ONE_NO.get(1)); + String materialNo = (String) result.get(EXCEL_HEAD_STYLE_ONE_NO.get(2)); + String mouldNo = (String) result.get(EXCEL_HEAD_STYLE_ONE_NO.get(3)); + String varietyNo = (String) result.get(EXCEL_HEAD_STYLE_ONE_NO.get(4)); + String craftItemNo = (String) result.get(EXCEL_HEAD_STYLE_ONE_NO.get(5)); + String createTime = (String) result.get(EXCEL_HEAD_STYLE_ONE_NO.get(6)); + if (batchNo.equals(currentBatchNo) && machineNo.equals(currentMachineNo) && materialNo.equals(currentMaterialNo) && mouldNo.equals(currentMouldNo) && varietyNo.equals(currentVarietyNo) && craftItemNo.equals(currentCraftItemNo)) { + createTimeList.add(Long.parseLong(createTime)); + } + } + //对上传时间集合进行排序 + createTimeList.sort(Long::compareTo); + //根据上传时间集合和时间间隔生成上传时间节点集合 + List createTimeTagList = new ArrayList<>();//时间节点列表 + for (int i = 0; i < createTimeList.size(); i++) { + Long createTime = createTimeList.get(i); + if (createTimeTagList.isEmpty()) { + createTimeTagList.add(createTime); + continue; + } + Long newCreateTimeTag = createTimeTagList.get(createTimeTagList.size() - 1);//最新的时间节点 + if (createTime >= newCreateTimeTag + Constant.QC_TIME_INTERVAL) { + createTimeTagList.add(createTime); + } + } + return createTimeTagList; + } + /** * 获取添加了 ##H 或 ##L 后缀的测量值(如果可能的话) */ - private String getFormatValue(String varietyNo, String craftItemNo, String qcItemNo, String qcValue) { - Optional existsQcSpecOptional = qcSpecDao.findExistsQcSpec(varietyNo, craftItemNo, qcItemNo); - if (existsQcSpecOptional.isEmpty()) { + private String getFormatValue(List qcSpecList, String varietyNo, String craftItemNo, String qcItemNo, String qcValue) { + if (qcSpecList == null || qcSpecList.isEmpty()) { return qcValue; } - QcSpecEntity existsQcSpec = existsQcSpecOptional.get(); - double valueMax = stringToDouble(existsQcSpec.getMax()); - double valueMin = stringToDouble(existsQcSpec.getMin()); - double value = stringToDouble(qcValue); - if (valueMax > -99_000D && value > -99_000D && value >= valueMax) { - return qcValue + "##H"; - } - if (valueMin > -99_000D && value > -99_000D && value <= valueMin) { - return qcValue + "##L"; + for (int i = 0; i < qcSpecList.size(); i++) { + QcSpecEntity qcSpec = qcSpecList.get(i); + if (varietyNo.equals(qcSpec.getVarietyNo()) && craftItemNo.equals(qcSpec.getCraftItemNo()) && qcItemNo.equals(qcSpec.getQcItemNo())) { + double valueMax = stringToDouble(qcSpec.getMax()); + double valueMin = stringToDouble(qcSpec.getMin()); + double value = stringToDouble(qcValue); + if (valueMax > -99_000D && value > -99_000D && value >= valueMax) { + return qcValue + "##H"; + } + if (valueMin > -99_000D && value > -99_000D && value <= valueMin) { + return qcValue + "##L"; + } + return qcValue; + } } return qcValue; } @@ -266,13 +372,13 @@ public class QcSourceServiceImpl implements QcSourceService { */ @Override public String exportExcel(ExportSourceExcelQo exportSourceExcelQo) throws Exception { - String host = exportSourceExcelQo.getHost(); + String host = LocalStringUtils.formatEmptyValue(exportSourceExcelQo.getHost()).trim(); String finalBatchNo = LocalStringUtils.formatEmptyValue(exportSourceExcelQo.getBatchNo()).trim(); String finalMachineNo = LocalStringUtils.formatEmptyValue(exportSourceExcelQo.getMachineNo()).trim(); String finalMaterialNo = LocalStringUtils.formatEmptyValue(exportSourceExcelQo.getMaterialNo()).trim(); String finalMouldNo = LocalStringUtils.formatEmptyValue(exportSourceExcelQo.getMouldNo()).trim(); String finalVarietyNo = LocalStringUtils.formatEmptyValue(exportSourceExcelQo.getVarietyNo()).trim(); - String craftItemNo = exportSourceExcelQo.getCraftItemNo(); + String finalCraftItemNo = LocalStringUtils.formatEmptyValue(exportSourceExcelQo.getCraftItemNo()).trim(); List craftItemList = craftItemDao.findAll(); if (craftItemList.isEmpty()) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "未添加工艺项目,请先添加工艺项目!", locale); @@ -281,13 +387,9 @@ public class QcSourceServiceImpl implements QcSourceService { if (qcItemList.isEmpty()) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "未添加检验项目,请先添加检验项目!", locale); } - List> sourceList; - if (TextUtils.isEmpty(craftItemNo)) { - sourceList = qcSourceDao.queryQcSource(finalBatchNo, finalMachineNo, finalMaterialNo, finalMouldNo, finalVarietyNo); - } else { - sourceList = qcSourceDao.queryQcSource(finalBatchNo, finalMachineNo, finalMaterialNo, finalMouldNo, finalVarietyNo, craftItemNo.trim()); - } - List> resultList = generateResultList(sourceList); + List sourceList = qcSourceDao.queryQcSource(finalBatchNo, finalMachineNo, finalMaterialNo, finalMouldNo, finalVarietyNo, finalCraftItemNo); + List qcSpecList = qcSpecDao.findAll(); + List> resultList = generateResultList(sourceList, qcSpecList); String finalHost = host.endsWith("/") ? host : host + "/"; List> formattedResultList = formatResultList(finalHost, resultList, qcItemList); if (formattedResultList.isEmpty()) { @@ -318,6 +420,7 @@ public class QcSourceServiceImpl implements QcSourceService { dataList.add((String) dataMap.getOrDefault(EXCEL_HEAD_STYLE_ONE_NO.get(3), "")); dataList.add((String) dataMap.getOrDefault(EXCEL_HEAD_STYLE_ONE_NO.get(4), "")); dataList.add((String) dataMap.getOrDefault(EXCEL_HEAD_STYLE_ONE_NO.get(5), "")); + dataList.add((String) dataMap.getOrDefault(EXCEL_HEAD_STYLE_ONE_NO.get(6), "")); for (QcItemEntity entity : qcItemList) { String value = (String) dataMap.getOrDefault(entity.getQcItemNo(), ""); if (Constant.QC_ITEM_TYPE_STRING.equals(entity.getQcItemType())) { diff --git a/src/main/java/com/xkrs/util/Constant.java b/src/main/java/com/xkrs/util/Constant.java index 7102fbb..887ad5b 100644 --- a/src/main/java/com/xkrs/util/Constant.java +++ b/src/main/java/com/xkrs/util/Constant.java @@ -12,4 +12,6 @@ public interface Constant { */ String QC_ITEM_TYPE_PICTURE = "1"; + long QC_TIME_INTERVAL = 1; + }