package com.xkrs.service.impl; import com.xkrs.dao.QcItemDao; import com.xkrs.dao.QcSourceDao; import com.xkrs.dao.QcSpecDao; import com.xkrs.encapsulation.PromptMessageEnum; import com.xkrs.model.entity.QcItemEntity; import com.xkrs.model.entity.QcSourceEntity; import com.xkrs.model.entity.QcSpecEntity; import com.xkrs.model.qo.QcSourceQoDelete; import com.xkrs.model.qo.QcSourceQoInsert; import com.xkrs.service.QcSourceService; import com.xkrs.util.Constant; import com.xkrs.util.ExcelUploadUtil; import com.xkrs.util.LocalDateUtils; import com.xkrs.util.LocalStringUtils; import org.apache.http.util.TextUtils; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.transaction.Transactional; import java.io.IOException; import java.util.*; import static com.xkrs.encapsulation.OutputEncapsulation.outputEncapsulationObject; @Service public class QcSourceServiceImpl implements QcSourceService { private Locale locale = LocaleContextHolder.getLocale(); @Resource private QcItemDao qcItemDao; @Resource private QcSourceDao qcSourceDao; @Resource private QcSpecDao qcSpecDao; /** * 添加检验数据 */ @Override public String insertQcSource(QcSourceQoInsert insertQo, MultipartFile picture) { String batchNo = insertQo.getBatchNo(); String machineNo = insertQo.getMachineNo(); String materialNo = insertQo.getMaterialNo(); String mouldNo = insertQo.getMouldNo(); String varietyNo = insertQo.getVarietyNo(); String qcItemNo = insertQo.getQcItemNo(); String qcValue = insertQo.getQcValue(); Optional qcItemEntityByNo = qcItemDao.findByNo(qcItemNo); if (qcItemEntityByNo.isEmpty()) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "不存在该检验项目,请先添加检验项目!", locale); } if (Constant.QC_ITEM_TYPE_STRING.equals(qcItemEntityByNo.get().getType())) { QcSourceEntity entity = new QcSourceEntity(); entity.setCreateTime(LocalDateUtils.getCurrentSecond()); entity.setBatchNo(LocalStringUtils.formatEmptyValue(batchNo)); entity.setMachineNo(LocalStringUtils.formatEmptyValue(machineNo)); entity.setMaterialNo(LocalStringUtils.formatEmptyValue(materialNo)); entity.setMouldNo(LocalStringUtils.formatEmptyValue(mouldNo)); entity.setVarietyNo(LocalStringUtils.formatEmptyValue(varietyNo)); entity.setQcItemNo(LocalStringUtils.formatEmptyValue(qcItemNo)); entity.setQcValue(LocalStringUtils.formatEmptyValue(qcValue)); qcSourceDao.save(entity); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "添加成功!", locale); } if (Constant.QC_ITEM_TYPE_PICTURE.equals(qcItemEntityByNo.get().getType())) { if (picture == null) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "上传图片不能为空!", locale); } String picturePath; try { picturePath = ExcelUploadUtil.memoryFile(picture, 2); } catch (IOException e) { e.printStackTrace(); return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "上传图片失败!", locale); } QcSourceEntity entity = new QcSourceEntity(); entity.setCreateTime(LocalDateUtils.getCurrentSecond()); entity.setBatchNo(LocalStringUtils.formatEmptyValue(batchNo)); entity.setMachineNo(LocalStringUtils.formatEmptyValue(machineNo)); entity.setMaterialNo(LocalStringUtils.formatEmptyValue(materialNo)); entity.setMouldNo(LocalStringUtils.formatEmptyValue(mouldNo)); entity.setVarietyNo(LocalStringUtils.formatEmptyValue(varietyNo)); entity.setQcItemNo(LocalStringUtils.formatEmptyValue(qcItemNo)); entity.setQcValue(LocalStringUtils.formatEmptyValue(picturePath)); qcSourceDao.save(entity); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "添加成功!", locale); } return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "添加失败!", locale); } /** * 删除检验数据 */ @Transactional(rollbackOn = Exception.class) @Override public String deleteQcSource(QcSourceQoDelete deleteQo) { Integer id = deleteQo.getId(); Optional entityById = qcSourceDao.findById(id); if (entityById.isEmpty()) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "不存在该检验数据,请先添加检验数据!", locale); } qcSourceDao.deleteById(id); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "删除成功!", locale); } /** * 查询检验数据 */ @Override public String queryQcSource(String batchNo, String machineNo, String materialNo, String mouldNo, String varietyNo) { List qcItemList = qcItemDao.findAll(); if (qcItemList.isEmpty()) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "未添加检验项目,请先添加检验项目!", locale); } List> sourceList = qcSourceDao.queryQcSource(LocalStringUtils.formatEmptyValue(batchNo), LocalStringUtils.formatEmptyValue(machineNo), LocalStringUtils.formatEmptyValue(materialNo), LocalStringUtils.formatEmptyValue(mouldNo), LocalStringUtils.formatEmptyValue(varietyNo)); List> resultList = generateResultList(sourceList); if (resultList.isEmpty()) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时没有该检验数据的信息!", locale); } return outputEncapsulationObject(PromptMessageEnum.SUCCESS, resultList, locale); } /** * 生成返回结果 */ private List> generateResultList(List> sourceList) { 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 qcItemNo = (String) source.getOrDefault("qcItemNo", ""); String qcValue = (String) source.getOrDefault("qcValue", ""); if (TextUtils.isEmpty(qcItemNo) || TextUtils.isEmpty(qcValue)) { continue; } String formatValue = getFormatValue(varietyNo, qcItemNo, qcValue); Map emptyRoom = findEmptyRoom(resultList, batchNo, qcItemNo); if (emptyRoom == null) { Map newRoom = new HashMap<>(); newRoom.put("batchNo", batchNo); newRoom.put("machineNo", machineNo); newRoom.put("materialNo", materialNo); newRoom.put("mouldNo", mouldNo); newRoom.put("varietyNo", varietyNo); newRoom.put(qcItemNo, formatValue); resultList.add(newRoom); } else { emptyRoom.put(qcItemNo, formatValue); } } return resultList; } /** * 获取添加了 ##H 或 ##L 后缀的测量值(如果可能的话) */ private String getFormatValue(String varietyNo, String qcItemNo, String qcValue) { Optional entityByVarietyNoAndQcItemNo = qcSpecDao.findByVarietyNoAndQcItemNo(varietyNo, qcItemNo); if (entityByVarietyNoAndQcItemNo.isEmpty()) { return qcValue; } QcSpecEntity qcSpec = entityByVarietyNoAndQcItemNo.get(); double valueMax = stringToDouble(qcSpec.getMax()); double valueMin = stringToDouble(qcSpec.getMin()); double value = stringToDouble(qcValue); if (valueMax >= 0 && value >= 0 && value >= valueMax) { return qcValue + "##H"; } if (valueMin >= 0 && value >= 0 && value <= valueMin) { return qcValue + "##L"; } return qcValue; } /** * 格式化字符串为Double */ private Double stringToDouble(String stringValue) { try { return Double.parseDouble(stringValue); } catch (Exception e) { e.printStackTrace(); return -1D; } } /** * 尝试找到一个qcItemNo有空位置的Map */ private Map findEmptyRoom(List> mapList, String batchNo, String qcItemNo) { if (mapList == null || mapList.isEmpty() || TextUtils.isEmpty(batchNo) || TextUtils.isEmpty(qcItemNo)) { return null; } for (Map room : mapList) { if (room.getOrDefault("batchNo", "").equals(batchNo)) { if (!room.containsKey(qcItemNo)) { return room; } } } return null; } }