From c5d3f6e322ff9e49d661262e9060a15fcddcc61b Mon Sep 17 00:00:00 2001 From: "zangch@mesnac.com" Date: Mon, 16 Mar 2026 17:05:35 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E5=8F=82=E6=95=B0=E8=A1=A8=E6=9F=A5=E8=AF=A2=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E4=BB=A5=E6=94=AF=E6=8C=81=E5=88=86=E8=A1=A8=E8=B7=AF?= =?UTF-8?q?=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BaseDeviceParamValController.java | 5 +- .../aucma/base/domain/BaseDeviceParamVal.java | 19 +- .../base/mapper/BaseDeviceParamValMapper.java | 31 +- .../service/IBaseDeviceParamValService.java | 6 +- .../impl/BaseDeviceParamValServiceImpl.java | 365 +++++------ .../impl/BaseOrderInfoServiceImpl.java | 35 ++ .../mapper/base/BaseDeviceParamValMapper.xml | 581 ++++++++++++------ .../mapper/base/BaseMaterialinfoMapper.xml | 2 +- .../dms/controller/DmsMobileController.java | 14 +- .../impl/AndonDashboardServiceImpl.java | 30 +- .../service/impl/Board4ServiceImpl.java | 20 +- .../resources/mapper/report/Board4Mapper.xml | 129 ++-- 12 files changed, 729 insertions(+), 508 deletions(-) diff --git a/aucma-base/src/main/java/com/aucma/base/controller/BaseDeviceParamValController.java b/aucma-base/src/main/java/com/aucma/base/controller/BaseDeviceParamValController.java index 00bf5a5..bca027b 100644 --- a/aucma-base/src/main/java/com/aucma/base/controller/BaseDeviceParamValController.java +++ b/aucma-base/src/main/java/com/aucma/base/controller/BaseDeviceParamValController.java @@ -4,7 +4,6 @@ import java.util.List; import javax.servlet.http.HttpServletResponse; import com.aucma.common.annotation.Anonymous; -import com.aucma.common.utils.DateUtils; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; @@ -65,7 +64,7 @@ public class BaseDeviceParamValController extends BaseController { */ @PreAuthorize("@ss.hasPermi('baseDeviceParamVal:val:query')" ) @GetMapping(value = "/{recordId}" ) - public AjaxResult getInfo(@PathVariable("recordId" ) Long recordId) { + public AjaxResult getInfo(@PathVariable("recordId" ) String recordId) { return success(baseDeviceParamValService.selectBaseDeviceParamValByRecordId(recordId)); } @@ -96,7 +95,7 @@ public class BaseDeviceParamValController extends BaseController { @PreAuthorize("@ss.hasPermi('baseDeviceParamVal:val:remove')" ) @Log(title = "设备工艺参数当前值" , businessType = BusinessType.DELETE) @DeleteMapping("/{recordIds}" ) - public AjaxResult remove(@PathVariable Long[] recordIds) { + public AjaxResult remove(@PathVariable String[] recordIds) { return toAjax(baseDeviceParamValService.deleteBaseDeviceParamValByRecordIds(recordIds)); } diff --git a/aucma-base/src/main/java/com/aucma/base/domain/BaseDeviceParamVal.java b/aucma-base/src/main/java/com/aucma/base/domain/BaseDeviceParamVal.java index a4c2fb2..37911ac 100644 --- a/aucma-base/src/main/java/com/aucma/base/domain/BaseDeviceParamVal.java +++ b/aucma-base/src/main/java/com/aucma/base/domain/BaseDeviceParamVal.java @@ -1,6 +1,7 @@ package com.aucma.base.domain; import java.util.Date; +import java.util.List; import com.fasterxml.jackson.annotation.JsonFormat; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; @@ -18,7 +19,7 @@ public class BaseDeviceParamVal extends BaseEntity private static final long serialVersionUID=1L; /** 主键 */ - private Long recordId; + private String recordId; /** 参数编号 */ @Excel(name = "参数编号") @@ -55,12 +56,15 @@ private static final long serialVersionUID=1L; /** 查询结束时间(仅查询用) */ private Date endTime; - public void setRecordId(Long recordId) + /** 查询涉及的月分表后缀(仅查询用) */ + private List tableSuffixes; + + public void setRecordId(String recordId) { this.recordId = recordId; } - public Long getRecordId() + public String getRecordId() { return recordId; } @@ -144,6 +148,14 @@ private static final long serialVersionUID=1L; this.endTime = endTime; } + public List getTableSuffixes() { + return tableSuffixes; + } + + public void setTableSuffixes(List tableSuffixes) { + this.tableSuffixes = tableSuffixes; + } + @Override public String toString(){ return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) @@ -157,6 +169,7 @@ public String toString(){ .append("recordTime",getRecordTime()) .append("beginTime",getBeginTime()) .append("endTime",getEndTime()) + .append("tableSuffixes", getTableSuffixes()) .toString(); } } diff --git a/aucma-base/src/main/java/com/aucma/base/mapper/BaseDeviceParamValMapper.java b/aucma-base/src/main/java/com/aucma/base/mapper/BaseDeviceParamValMapper.java index ad478be..72092b0 100644 --- a/aucma-base/src/main/java/com/aucma/base/mapper/BaseDeviceParamValMapper.java +++ b/aucma-base/src/main/java/com/aucma/base/mapper/BaseDeviceParamValMapper.java @@ -4,6 +4,7 @@ import java.util.List; import java.util.Map; import com.aucma.base.domain.BaseDeviceParamVal; +import org.apache.ibatis.annotations.Param; /** * 设备工艺参数当前值Mapper接口 @@ -19,7 +20,7 @@ public interface BaseDeviceParamValMapper * @param recordId 设备工艺参数当前值主键 * @return 设备工艺参数当前值 */ - public BaseDeviceParamVal selectBaseDeviceParamValByRecordId(Long recordId); + public BaseDeviceParamVal selectBaseDeviceParamValByRecordId(String recordId); /** * 查询设备工艺参数当前值列表 @@ -45,6 +46,16 @@ public interface BaseDeviceParamValMapper */ public int insertBaseDeviceParamVal(BaseDeviceParamVal baseDeviceParamVal); + /** + * 向自动采集月分表插入一条设备参数记录 + * + * @param tableName 目标月表 + * @param baseDeviceParamVal 参数记录 + * @return 结果 + */ + public int insertPartitionBaseDeviceParamVal(@Param("tableName") String tableName, + @Param("entity") BaseDeviceParamVal baseDeviceParamVal); + /** * 修改设备工艺参数当前值 * @@ -59,7 +70,7 @@ public interface BaseDeviceParamValMapper * @param recordId 设备工艺参数当前值主键 * @return 结果 */ - public int deleteBaseDeviceParamValByRecordId(Long recordId); + public int deleteBaseDeviceParamValByRecordId(String recordId); /** * 批量删除设备工艺参数当前值 @@ -67,28 +78,28 @@ public interface BaseDeviceParamValMapper * @param recordIds 需要删除的数据主键集合 * @return 结果 */ - public int deleteBaseDeviceParamValByRecordIds(Long[] recordIds); + public int deleteBaseDeviceParamValByRecordIds(String[] recordIds); /** * 查询设备状态统计(基于三色灯参数) * * @return 设备状态统计Map */ - public Map selectDeviceStatusStatistics(); + public Map selectDeviceStatusStatistics(Map params); /** * 获取每个设备的编号、名称、三色灯状态 * * @return 设备状态列表 */ - public List> selectDeviceStatusList(); + public List> selectDeviceStatusList(Map params); /** * 获取每个设备的开机时间当天最新值 * * @return 设备开机时间列表 */ - public List> selectDeviceStartTimeList(); + public List> selectDeviceStartTimeList(Map params); /** * 参数追溯查询(按时间范围) @@ -106,6 +117,14 @@ public interface BaseDeviceParamValMapper */ public List selectParamHistoryValues(Map params); + /** + * 查询双源条件下的参数名称 + * + * @param params 查询参数 + * @return 参数名称 + */ + public String selectParamNameByCodeFromSources(Map params); + /** * 查询参数名称 * diff --git a/aucma-base/src/main/java/com/aucma/base/service/IBaseDeviceParamValService.java b/aucma-base/src/main/java/com/aucma/base/service/IBaseDeviceParamValService.java index 0c4dccc..6fc0e09 100644 --- a/aucma-base/src/main/java/com/aucma/base/service/IBaseDeviceParamValService.java +++ b/aucma-base/src/main/java/com/aucma/base/service/IBaseDeviceParamValService.java @@ -19,7 +19,7 @@ public interface IBaseDeviceParamValService * @param recordId 设备工艺参数当前值主键 * @return 设备工艺参数当前值 */ - public BaseDeviceParamVal selectBaseDeviceParamValByRecordId(Long recordId); + public BaseDeviceParamVal selectBaseDeviceParamValByRecordId(String recordId); /** * 查询设备工艺参数当前值列表 @@ -59,7 +59,7 @@ public interface IBaseDeviceParamValService * @param recordIds 需要删除的设备工艺参数当前值主键集合 * @return 结果 */ - public int deleteBaseDeviceParamValByRecordIds(Long[] recordIds); + public int deleteBaseDeviceParamValByRecordIds(String[] recordIds); /** * 删除设备工艺参数当前值信息 @@ -67,7 +67,7 @@ public interface IBaseDeviceParamValService * @param recordId 设备工艺参数当前值主键 * @return 结果 */ - public int deleteBaseDeviceParamValByRecordId(Long recordId); + public int deleteBaseDeviceParamValByRecordId(String recordId); /** * 查询设备状态统计(基于三色灯参数) diff --git a/aucma-base/src/main/java/com/aucma/base/service/impl/BaseDeviceParamValServiceImpl.java b/aucma-base/src/main/java/com/aucma/base/service/impl/BaseDeviceParamValServiceImpl.java index 9d2aa72..fc0dcfa 100644 --- a/aucma-base/src/main/java/com/aucma/base/service/impl/BaseDeviceParamValServiceImpl.java +++ b/aucma-base/src/main/java/com/aucma/base/service/impl/BaseDeviceParamValServiceImpl.java @@ -1,231 +1,184 @@ package com.aucma.base.service.impl; -import java.util.*; - +import com.aucma.base.domain.BaseDeviceParamVal; +import com.aucma.base.mapper.BaseDeviceParamValMapper; +import com.aucma.base.service.IBaseDeviceParamValService; +import com.aucma.base.service.IRtDailyProdStateService; +import com.aucma.base.support.DeviceParamTableRouter; import com.aucma.common.utils.DateUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import com.aucma.base.mapper.BaseDeviceParamValMapper; -import com.aucma.base.domain.BaseDeviceParamVal; -import com.aucma.base.service.IBaseDeviceParamValService; +import org.springframework.transaction.annotation.Transactional; + +import java.math.BigDecimal; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; /** * 设备工艺参数当前值Service业务层处理 - * - * @author zch - * @date 2025-10-15 */ @Service -public class BaseDeviceParamValServiceImpl implements IBaseDeviceParamValService -{ +public class BaseDeviceParamValServiceImpl implements IBaseDeviceParamValService { + private static final Logger log = LoggerFactory.getLogger(BaseDeviceParamValServiceImpl.class); + private static final String DAILY_OUTPUT_PARAM_NAME = "机台状态-实际产出数量"; + private static final DateTimeFormatter QUERY_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); @Autowired private BaseDeviceParamValMapper baseDeviceParamValMapper; - /** - * 查询设备工艺参数当前值 - * - * @param recordId 设备工艺参数当前值主键 - * @return 设备工艺参数当前值 - */ + @Autowired + private DeviceParamTableRouter deviceParamTableRouter; + + @Autowired + private IRtDailyProdStateService rtDailyProdStateService; + @Override - public BaseDeviceParamVal selectBaseDeviceParamValByRecordId(Long recordId) - { + public BaseDeviceParamVal selectBaseDeviceParamValByRecordId(String recordId) { return baseDeviceParamValMapper.selectBaseDeviceParamValByRecordId(recordId); } - /** - * 查询设备工艺参数当前值列表 - * - * @param baseDeviceParamVal 设备工艺参数当前值 - * @return 设备工艺参数当前值 - */ @Override - public List selectBaseDeviceParamValList(BaseDeviceParamVal baseDeviceParamVal) - { + public List selectBaseDeviceParamValList(BaseDeviceParamVal baseDeviceParamVal) { return baseDeviceParamValMapper.selectBaseDeviceParamValList(baseDeviceParamVal); } - /** - * 查询每设备每参数的最新记录列表(聚合最新值) - * - * @param baseDeviceParamVal 查询条件 - * @return 最新记录集合 - */ @Override - public List selectLatestBaseDeviceParamValList(BaseDeviceParamVal baseDeviceParamVal) - { + public List selectLatestBaseDeviceParamValList(BaseDeviceParamVal baseDeviceParamVal) { try { - // 入参为空直接返回,调用侧可据此判定为无效请求 - if (baseDeviceParamVal == null) { + if (baseDeviceParamVal == null) { baseDeviceParamVal = new BaseDeviceParamVal(); } - // 防止全表扫描:若未指定时间范围,默认查询最近24小时 if (baseDeviceParamVal.getBeginTime() == null && baseDeviceParamVal.getEndTime() == null) { Date end = new Date(); Date begin = DateUtils.addHours(end, -24); baseDeviceParamVal.setBeginTime(begin); baseDeviceParamVal.setEndTime(end); } + baseDeviceParamVal.setTableSuffixes(deviceParamTableRouter.resolveReadTableSuffixes( + baseDeviceParamVal.getBeginTime(), baseDeviceParamVal.getEndTime())); List list = baseDeviceParamValMapper.selectLatestBaseDeviceParamValList(baseDeviceParamVal); return list != null ? list : Collections.emptyList(); } catch (Exception e) { - log.error("查询设备参数最新值失败: {}", e.getMessage()); + log.error("查询设备参数最新值失败: {}", e.getMessage(), e); return Collections.emptyList(); } } - /** - * 新增设备工艺参数当前值 - * - * @param baseDeviceParamVal 设备工艺参数当前值 - * @return 结果 - */ @Override - public int insertBaseDeviceParamVal(BaseDeviceParamVal baseDeviceParamVal) - { - // 当天无记录时再插入,兼容首次上报场景 - return baseDeviceParamValMapper.insertBaseDeviceParamVal(baseDeviceParamVal); + @Transactional(rollbackFor = Exception.class) + public int insertBaseDeviceParamVal(BaseDeviceParamVal baseDeviceParamVal) { + if (baseDeviceParamVal == null) { + return 0; + } + fillTimeIfAbsent(baseDeviceParamVal); + String tableName = deviceParamTableRouter.resolveWriteTable( + baseDeviceParamVal.getDeviceCode(), baseDeviceParamVal.getCollectTime()); + + if ("BASE_DEVICE_PARAM_VAL".equals(tableName)) { + int rows = baseDeviceParamValMapper.insertBaseDeviceParamVal(baseDeviceParamVal); + syncRtStateIfNeeded(baseDeviceParamVal, rows); + return rows; + } + + fillPartitionRecordIdIfAbsent(baseDeviceParamVal); + return baseDeviceParamValMapper.insertPartitionBaseDeviceParamVal(tableName, baseDeviceParamVal); } - /** - * 修改设备工艺参数当前值 - * - * @param baseDeviceParamVal 设备工艺参数当前值 - * @return 结果 - */ @Override - public int updateBaseDeviceParamVal(BaseDeviceParamVal baseDeviceParamVal) - { + public int updateBaseDeviceParamVal(BaseDeviceParamVal baseDeviceParamVal) { return baseDeviceParamValMapper.updateBaseDeviceParamVal(baseDeviceParamVal); } - /** - * 批量删除设备工艺参数当前值 - * - * @param recordIds 需要删除的设备工艺参数当前值主键 - * @return 结果 - */ @Override - public int deleteBaseDeviceParamValByRecordIds(Long[] recordIds) - { + public int deleteBaseDeviceParamValByRecordIds(String[] recordIds) { return baseDeviceParamValMapper.deleteBaseDeviceParamValByRecordIds(recordIds); } - /** - * 删除设备工艺参数当前值信息 - * - * @param recordId 设备工艺参数当前值主键 - * @return 结果 - */ @Override - public int deleteBaseDeviceParamValByRecordId(Long recordId) - { + public int deleteBaseDeviceParamValByRecordId(String recordId) { return baseDeviceParamValMapper.deleteBaseDeviceParamValByRecordId(recordId); } - /** - * 查询设备状态统计(基于三色灯参数) - * - * @return 设备状态统计Map - */ @Override - public Map selectDeviceStatusStatistics() - { + public Map selectDeviceStatusStatistics() { try { - Map result = baseDeviceParamValMapper.selectDeviceStatusStatistics(); + Map params = buildRangeParams(DateUtils.addHours(new Date(), -2), new Date()); + Map result = baseDeviceParamValMapper.selectDeviceStatusStatistics(params); if (result == null) { result = new HashMap<>(); - result.put("totalCount", 0);//从base_deviceledger中获取is_flag = 1 - result.put("runningCount", 0);//机台状态-三色灯机器运行 - result.put("stoppedCount", 0);//三色灯机器暂停 - result.put("standbyCount", 0);//三色灯机器待机 - result.put("alarmCount", 0);//三色灯机器报警 - result.put("notStartedCount", 0);//t.totalCount - s.runningCount - s.stoppedCount - s.standbyCount - s.alarmCount AS notStartedCount - result.put("runningRate", "0%"); - return result; + result.put("TOTALCOUNT", 0); + result.put("RUNNINGCOUNT", 0); + result.put("STOPPEDCOUNT", 0); + result.put("STANDBYCOUNT", 0); + result.put("ALARMCOUNT", 0); + result.put("NOTSTARTEDCOUNT", 0); } - // 计算开机率 - Object totalObj = result.get("TOTALCOUNT"); - Object runningObj = result.get("RUNNINGCOUNT"); - int total = totalObj != null ? ((Number) totalObj).intValue() : 0; - int running = runningObj != null ? ((Number) runningObj).intValue() : 0; + int total = getIntValue(result.get("TOTALCOUNT")); + int running = getIntValue(result.get("RUNNINGCOUNT")); String runningRate = total > 0 ? String.format("%.2f%%", (running * 100.0 / total)) : "0%"; result.put("runningRate", runningRate); return result; } catch (Exception e) { - log.error("查询设备状态统计失败: {}", e.getMessage()); + log.error("查询设备状态统计失败: {}", e.getMessage(), e); Map result = new HashMap<>(); - result.put("totalCount", 0); - result.put("runningCount", 0); - result.put("stoppedCount", 0); - result.put("standbyCount", 0); - result.put("notStartedCount", 0); + result.put("TOTALCOUNT", 0); + result.put("RUNNINGCOUNT", 0); + result.put("STOPPEDCOUNT", 0); + result.put("STANDBYCOUNT", 0); + result.put("ALARMCOUNT", 0); + result.put("NOTSTARTEDCOUNT", 0); result.put("runningRate", "0%"); return result; } } - /** - * 获取每个设备的编号、名称、三色灯状态 - * - * @return 设备状态列表 - */ @Override - public List> selectDeviceStatusList() - { + public List> selectDeviceStatusList() { try { - List> list = baseDeviceParamValMapper.selectDeviceStatusList(); + Map params = buildRangeParams(DateUtils.addHours(new Date(), -2), new Date()); + List> list = baseDeviceParamValMapper.selectDeviceStatusList(params); return list != null ? list : Collections.emptyList(); } catch (Exception e) { - log.error("查询设备状态列表失败: {}", e.getMessage()); + log.error("查询设备状态列表失败: {}", e.getMessage(), e); return Collections.emptyList(); } } - /** - * 获取每个设备的开机时间当天最新值 - * - * @return 设备开机时间列表 - */ @Override - public List> selectDeviceStartTimeList() - { + public List> selectDeviceStartTimeList() { try { - List> list = baseDeviceParamValMapper.selectDeviceStartTimeList(); + Date now = new Date(); + Map params = buildRangeParams(now, now); + List> list = baseDeviceParamValMapper.selectDeviceStartTimeList(params); return list != null ? list : Collections.emptyList(); } catch (Exception e) { - log.error("查询设备开机时间列表失败: {}", e.getMessage()); + log.error("查询设备开机时间列表失败: {}", e.getMessage(), e); return Collections.emptyList(); } } - /** - * 参数追溯查询(按时间范围) - */ @Override public List selectTraceList(String deviceCode, String paramCode, String startTime, String endTime) { try { - // mapper层使用Map入参,便于只携带本次upsert所需字段 - Map params = new HashMap<>(); - params.put("deviceCode", deviceCode); - params.put("paramCode", paramCode); - params.put("startTime", startTime); - params.put("endTime", endTime); + Map params = buildStringRangeParams(deviceCode, paramCode, startTime, endTime); List list = baseDeviceParamValMapper.selectTraceList(params); return list != null ? list : Collections.emptyList(); } catch (Exception e) { - log.error("参数追溯查询失败: {}", e.getMessage()); + log.error("参数追溯查询失败: {}", e.getMessage(), e); return Collections.emptyList(); } } - /** - * 获取SPC分析数据 - */ @Override public Map getSPCData(String deviceCode, String paramCode, String startTime, String endTime) { Map result = new HashMap<>(); @@ -233,21 +186,13 @@ public class BaseDeviceParamValServiceImpl implements IBaseDeviceParamValService result.put("paramCode", paramCode); try { - // 查询参数名称 - String paramName = baseDeviceParamValMapper.selectParamNameByCode(paramCode); + Map params = buildStringRangeParams(deviceCode, paramCode, startTime, endTime); + String paramName = baseDeviceParamValMapper.selectParamNameByCodeFromSources(params); result.put("paramName", paramName != null ? paramName : paramCode); - // 查询参数历史值 - // mapper层使用Map入参,便于只携带本次upsert所需字段 - Map params = new HashMap<>(); - params.put("deviceCode", deviceCode); - params.put("paramCode", paramCode); - params.put("startTime", startTime); - params.put("endTime", endTime); List values = baseDeviceParamValMapper.selectParamHistoryValues(params); - if (values == null || values.isEmpty()) { - result.put("values", new ArrayList<>()); + result.put("values", Collections.emptyList()); result.put("sampleSize", 0); result.put("mean", 0.0); result.put("stdDev", 0.0); @@ -261,7 +206,6 @@ public class BaseDeviceParamValServiceImpl implements IBaseDeviceParamValService result.put("values", values); result.put("sampleSize", values.size()); - // 计算统计指标 double mean = calculateMean(values); double stdDev = calculateStdDev(values, mean); double ucl = mean + 3 * stdDev; @@ -273,72 +217,139 @@ public class BaseDeviceParamValServiceImpl implements IBaseDeviceParamValService result.put("lcl", Math.round(lcl * 1000.0) / 1000.0); result.put("cl", Math.round(mean * 1000.0) / 1000.0); - // 计算CPK(假设规格限为均值±10%) double usl = mean * 1.1; double lsl = mean * 0.9; double cpk = calculateCPK(mean, stdDev, usl, lsl); result.put("cpk", Math.round(cpk * 100.0) / 100.0); result.put("usl", Math.round(usl * 1000.0) / 1000.0); result.put("lsl", Math.round(lsl * 1000.0) / 1000.0); - return result; } catch (Exception e) { - log.error("获取SPC分析数据失败: {}", e.getMessage()); + log.error("获取SPC分析数据失败: {}", e.getMessage(), e); result.put("error", e.getMessage()); return result; } } - - /** - * 按设备与参数直接新增一条数据(不覆盖当天历史记录) - */ @Override - public int upsertTodayParamValue(BaseDeviceParamVal baseDeviceParamVal) - { - // 入参为空直接返回,调用侧可据此判定为无效请求 + @Transactional(rollbackFor = Exception.class) + public int upsertTodayParamValue(BaseDeviceParamVal baseDeviceParamVal) { if (baseDeviceParamVal == null) { return 0; } - // 若调用方未传时间,统一回填服务端当前时间,避免空时间导致SQL匹配偏差 - Date now = new Date(); - if (baseDeviceParamVal.getCollectTime() == null) { - baseDeviceParamVal.setCollectTime(now); - } - if (baseDeviceParamVal.getRecordTime() == null) { - baseDeviceParamVal.setRecordTime(now); - } - - // Why:PDA上报需要沉淀完整过程数据,后续分析依赖时间序列,不能覆盖当天已有记录 - return baseDeviceParamValMapper.insertBaseDeviceParamVal(baseDeviceParamVal); + fillTimeIfAbsent(baseDeviceParamVal); + return insertBaseDeviceParamVal(baseDeviceParamVal); } + + private void fillTimeIfAbsent(BaseDeviceParamVal entity) { + Date now = new Date(); + if (entity.getCollectTime() == null) { + entity.setCollectTime(now); + } + if (entity.getRecordTime() == null) { + entity.setRecordTime(now); + } + } + + private void fillPartitionRecordIdIfAbsent(BaseDeviceParamVal entity) { + if (entity.getRecordId() == null || entity.getRecordId().trim().isEmpty()) { + // 为什么这样做:自动采集月表要求字符串型业务唯一标识,手工补录时也要保持同一语义。 + entity.setRecordId(UUID.randomUUID().toString()); + } + } + + private void syncRtStateIfNeeded(BaseDeviceParamVal entity, int rows) { + if (rows <= 0) { + return; + } + if (!deviceParamTableRouter.isOldDevice(entity.getDeviceCode())) { + return; + } + if (!DAILY_OUTPUT_PARAM_NAME.equals(entity.getParamName())) { + return; + } + try { + BigDecimal newVal = new BigDecimal(entity.getParamValue()); + // 为什么这样做:OLD 设备只走 Java 写入链路,不在这里同步 RT 就会让 Board4 永远少算 OLD 设备产量。 + rtDailyProdStateService.incrementProduction( + entity.getDeviceCode(), entity.getParamName(), newVal, entity.getCollectTime()); + } catch (NumberFormatException ex) { + log.warn("OLD设备产量值不是合法数字,跳过RT同步 | deviceCode={}, paramValue={}", + entity.getDeviceCode(), entity.getParamValue()); + } + } + + private Map buildRangeParams(Date startTime, Date endTime) { + Map params = new HashMap<>(); + params.put("tableSuffixes", deviceParamTableRouter.resolveReadTableSuffixes(startTime, endTime)); + return params; + } + + private Map buildStringRangeParams(String deviceCode, String paramCode, String startTime, String endTime) { + Map params = new HashMap<>(); + params.put("deviceCode", deviceCode); + params.put("paramCode", paramCode); + params.put("startTime", startTime); + params.put("endTime", endTime); + params.put("tableSuffixes", deviceParamTableRouter.resolveReadTableSuffixes( + parseQueryDate(startTime), parseQueryDate(endTime))); + return params; + } + + private Date parseQueryDate(String dateTime) { + if (dateTime == null || dateTime.trim().isEmpty()) { + return null; + } + LocalDateTime localDateTime = LocalDateTime.parse(dateTime.trim(), QUERY_TIME_FORMATTER); + return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); + } + + private int getIntValue(Object value) { + if (value instanceof Number) { + return ((Number) value).intValue(); + } + if (value == null) { + return 0; + } + try { + return Integer.parseInt(value.toString()); + } catch (NumberFormatException ex) { + return 0; + } + } + private double calculateMean(List values) { - if (values == null || values.isEmpty()) return 0.0; + if (values == null || values.isEmpty()) { + return 0.0; + } double sum = 0.0; - for (Double v : values) { - if (v != null) sum += v; + for (Double value : values) { + if (value != null) { + sum += value; + } } return sum / values.size(); } private double calculateStdDev(List values, double mean) { - if (values == null || values.size() < 2) return 0.0; + if (values == null || values.size() < 2) { + return 0.0; + } double sumSquares = 0.0; - for (Double v : values) { - if (v != null) { - sumSquares += Math.pow(v - mean, 2); + for (Double value : values) { + if (value != null) { + sumSquares += Math.pow(value - mean, 2); } } return Math.sqrt(sumSquares / (values.size() - 1)); } private double calculateCPK(double mean, double stdDev, double usl, double lsl) { - if (stdDev == 0) return 0.0; - double cpupper = (usl - mean) / (3 * stdDev); - double cplower = (mean - lsl) / (3 * stdDev); - return Math.min(cpupper, cplower); + if (stdDev == 0) { + return 0.0; + } + double cpUpper = (usl - mean) / (3 * stdDev); + double cpLower = (mean - lsl) / (3 * stdDev); + return Math.min(cpUpper, cpLower); } } - - - diff --git a/aucma-base/src/main/java/com/aucma/base/service/impl/BaseOrderInfoServiceImpl.java b/aucma-base/src/main/java/com/aucma/base/service/impl/BaseOrderInfoServiceImpl.java index ae96329..4e8d186 100644 --- a/aucma-base/src/main/java/com/aucma/base/service/impl/BaseOrderInfoServiceImpl.java +++ b/aucma-base/src/main/java/com/aucma/base/service/impl/BaseOrderInfoServiceImpl.java @@ -9,6 +9,7 @@ import java.util.Set; import com.aucma.base.domain.BaseMaterialInfo; import com.aucma.base.domain.BaseOrderInfo; import com.aucma.base.mapper.BaseOrderInfoMapper; +import com.aucma.base.service.IBusinessCodeGeneratorService; import com.aucma.base.service.IBaseMaterialInfoService; import com.aucma.base.service.IBaseOrderInfoService; import com.aucma.common.exception.ServiceException; @@ -26,12 +27,17 @@ import org.springframework.transaction.annotation.Transactional; */ @Service public class BaseOrderInfoServiceImpl implements IBaseOrderInfoService { + private static final String DEFAULT_WORK_CENTER_CODE = "7"; + @Autowired private BaseOrderInfoMapper baseOrderInfoMapper; @Autowired private IBaseMaterialInfoService baseMaterialInfoService; + @Autowired + private IBusinessCodeGeneratorService businessCodeGeneratorService; + /** * 查询工单信息 * @@ -97,6 +103,7 @@ public class BaseOrderInfoServiceImpl implements IBaseOrderInfoService { */ @Override public int insertBaseOrderInfo(BaseOrderInfo baseOrderInfo) { + fillInsertDefaults(baseOrderInfo); syncMaterialMasterFields(baseOrderInfo); baseOrderInfo.setCreatedTime(DateUtils.getNowDate()); return baseOrderInfoMapper.insertBaseOrderInfo(baseOrderInfo); @@ -110,6 +117,7 @@ public class BaseOrderInfoServiceImpl implements IBaseOrderInfoService { */ @Override public int updateBaseOrderInfo(BaseOrderInfo baseOrderInfo) { + preserveOrderCodeOnUpdate(baseOrderInfo); syncMaterialMasterFields(baseOrderInfo); baseOrderInfo.setUpdatedTime(DateUtils.getNowDate()); return baseOrderInfoMapper.updateBaseOrderInfo(baseOrderInfo); @@ -361,4 +369,31 @@ public class BaseOrderInfoServiceImpl implements IBaseOrderInfoService { baseOrderInfo.setMaterialName(materialInfo.getMaterialName()); baseOrderInfo.setMatkl(materialInfo.getMaterialMatkl()); } + + private void fillInsertDefaults(BaseOrderInfo baseOrderInfo) { + if (baseOrderInfo == null) { + throw new ServiceException("工单信息不能为空"); + } + // 为什么新增时一律由后端生成编号:只有服务端统一串行分配,才能在并发新增时避免同一编号被重复使用。 + baseOrderInfo.setOrderCode(businessCodeGeneratorService.generateSapPlanCode(DateUtils.getNowDate())); + if (StringUtils.isBlank(baseOrderInfo.getWorkCenterCode())) { + // 为什么默认给工作中心 7:满足当前业务默认值要求,同时保留前端可改动能力,未显式选择时也能稳定落库。 + baseOrderInfo.setWorkCenterCode(DEFAULT_WORK_CENTER_CODE); + } + } + + private void preserveOrderCodeOnUpdate(BaseOrderInfo baseOrderInfo) { + if (baseOrderInfo == null || baseOrderInfo.getObjId() == null) { + return; + } + if (StringUtils.isNotBlank(baseOrderInfo.getOrderCode())) { + baseOrderInfo.setOrderCode(baseOrderInfo.getOrderCode().trim()); + return; + } + BaseOrderInfo existedOrder = baseOrderInfoMapper.selectBaseOrderInfoByObjId(baseOrderInfo.getObjId()); + if (existedOrder == null) { + throw new ServiceException("工单不存在,无法更新"); + } + baseOrderInfo.setOrderCode(existedOrder.getOrderCode()); + } } diff --git a/aucma-base/src/main/resources/mapper/base/BaseDeviceParamValMapper.xml b/aucma-base/src/main/resources/mapper/base/BaseDeviceParamValMapper.xml index b5ef5d5..508b9ba 100644 --- a/aucma-base/src/main/resources/mapper/base/BaseDeviceParamValMapper.xml +++ b/aucma-base/src/main/resources/mapper/base/BaseDeviceParamValMapper.xml @@ -5,280 +5,461 @@ - - - - - - - - + + + + + + + + + + record_id || '' AS record_id, param_code, device_code, device_id, + param_name, param_value, collect_time, record_time + + - select record_id, param_code, device_code, device_id, param_name, param_value, - collect_time, record_time - from base_device_param_val + SELECT + FROM base_device_param_val + + + + + SELECT + FROM BASE_DEVICE_PARAM_VAL + WHERE device_code LIKE 'OLD-%' + + AND device_code = #{deviceCode} + + + AND param_code = #{paramCode} + + + AND device_id = #{deviceId} + + + AND param_name LIKE '%' || #{paramName} || '%' + + + AND NVL(record_time, collect_time) BETWEEN #{beginTime} AND #{endTime} + + UNION ALL + + + SELECT + FROM BASE_DEVICE_PARAM_VAL_${suffix} + WHERE device_code NOT LIKE 'OLD-%' + + AND device_code = #{deviceCode} + + + AND param_code = #{paramCode} + + + AND device_id = #{deviceId} + + + AND param_name LIKE '%' || #{paramName} || '%' + + + AND NVL(record_time, collect_time) BETWEEN #{beginTime} AND #{endTime} + + + + - + + WHERE record_id = #{recordId} - - SELECT PARAMRECORD_SEQ_ID.NEXTVAL as recordId FROM DUAL - - insert into base_device_param_val + + SELECT TO_CHAR(PARAMRECORD_SEQ_ID.NEXTVAL) AS recordId FROM DUAL + + INSERT INTO base_device_param_val - record_id, - param_code, - device_code, - device_id, - param_name, - param_value, - collect_time, - record_time, + record_id, + param_code, + device_code, + device_id, + param_name, + param_value, + collect_time, + record_time, - #{recordId}, - #{paramCode}, - #{deviceCode}, - #{deviceId}, - #{paramName}, - #{paramValue}, - #{collectTime}, - #{recordTime}, + #{recordId}, + #{paramCode}, + #{deviceCode}, + #{deviceId}, + #{paramName}, + #{paramValue}, + #{collectTime}, + #{recordTime}, + + + + + INSERT INTO ${tableName} + + record_id, + param_code, + device_code, + device_id, + param_name, + param_value, + collect_time, + record_time, + + + #{entity.recordId}, + #{entity.paramCode}, + #{entity.deviceCode}, + #{entity.deviceId}, + #{entity.paramName}, + #{entity.paramValue}, + #{entity.collectTime}, + #{entity.recordTime}, - update base_device_param_val + UPDATE base_device_param_val - param_code = #{paramCode}, - device_code = #{deviceCode}, - device_id = #{deviceId}, - param_name = #{paramName}, - param_value = #{paramValue}, - collect_time = #{collectTime}, - record_time = #{recordTime}, + param_code = #{paramCode}, + device_code = #{deviceCode}, + device_id = #{deviceId}, + param_name = #{paramName}, + param_value = #{paramValue}, + collect_time = #{collectTime}, + record_time = #{recordTime}, - where record_id = #{recordId} + WHERE record_id = #{recordId} - - delete from base_device_param_val where record_id = #{recordId} + + DELETE FROM base_device_param_val WHERE record_id = #{recordId} - - delete from base_device_param_val where record_id in + + DELETE FROM base_device_param_val + WHERE record_id IN #{recordId} - - - - + WITH merged_param AS ( + + SELECT device_code, param_name, collect_time, record_time + FROM BASE_DEVICE_PARAM_VAL + WHERE device_code LIKE 'OLD-%' + AND param_name IN ('机台状态-三色灯机器运行', '机台状态-三色灯机器暂停', '机台状态-三色灯机器待机', '机台状态-三色灯机器报警') + AND UPPER(param_value) = 'TRUE' + AND collect_time >= SYSDATE - (2/24) + AND collect_time < SYSDATE + UNION ALL + + + SELECT device_code, param_name, collect_time, record_time + FROM BASE_DEVICE_PARAM_VAL_${suffix} + WHERE device_code NOT LIKE 'OLD-%' + AND param_name IN ('机台状态-三色灯机器运行', '机台状态-三色灯机器暂停', '机台状态-三色灯机器待机', '机台状态-三色灯机器报警') + AND UPPER(param_value) = 'TRUE' + AND collect_time >= SYSDATE - (2/24) + AND collect_time < SYSDATE + + + + ), + today_latest AS ( + SELECT device_code, + param_name, + ROW_NUMBER() OVER ( + PARTITION BY device_code + ORDER BY NVL(record_time, collect_time) DESC, + DECODE(param_name, + '机台状态-三色灯机器报警', 1, + '机台状态-三色灯机器暂停', 2, + '机台状态-三色灯机器待机', 3, + '机台状态-三色灯机器运行', 4, + 9) ASC + ) AS rn + FROM merged_param ), device_status AS ( - SELECT device_code, param_name FROM today_latest WHERE rn = 1 + SELECT device_code, param_name + FROM today_latest + WHERE rn = 1 ), status_sum AS ( - SELECT - NVL(SUM(CASE WHEN param_name = '机台状态-三色灯机器运行' THEN 1 ELSE 0 END), 0) AS runningCount, - NVL(SUM(CASE WHEN param_name = '机台状态-三色灯机器暂停' THEN 1 ELSE 0 END), 0) AS stoppedCount, - NVL(SUM(CASE WHEN param_name = '机台状态-三色灯机器待机' THEN 1 ELSE 0 END), 0) AS standbyCount, - NVL(SUM(CASE WHEN param_name = '机台状态-三色灯机器报警' THEN 1 ELSE 0 END), 0) AS alarmCount + SELECT NVL(SUM(CASE WHEN param_name = '机台状态-三色灯机器运行' THEN 1 ELSE 0 END), 0) AS runningCount, + NVL(SUM(CASE WHEN param_name = '机台状态-三色灯机器暂停' THEN 1 ELSE 0 END), 0) AS stoppedCount, + NVL(SUM(CASE WHEN param_name = '机台状态-三色灯机器待机' THEN 1 ELSE 0 END), 0) AS standbyCount, + NVL(SUM(CASE WHEN param_name = '机台状态-三色灯机器报警' THEN 1 ELSE 0 END), 0) AS alarmCount FROM device_status ), total_cnt AS ( SELECT COUNT(1) AS totalCount FROM base_deviceledger WHERE is_flag = 1 ) - SELECT - t.totalCount, - s.runningCount, - s.stoppedCount, - s.standbyCount, - s.alarmCount, - t.totalCount - s.runningCount - s.stoppedCount - s.standbyCount - s.alarmCount AS notStartedCount + SELECT t.totalCount, + s.runningCount, + s.stoppedCount, + s.standbyCount, + s.alarmCount, + t.totalCount - s.runningCount - s.stoppedCount - s.standbyCount - s.alarmCount AS notStartedCount FROM status_sum s, total_cnt t - - + WITH merged_param AS ( + + SELECT device_code, param_name, collect_time, record_time + FROM BASE_DEVICE_PARAM_VAL + WHERE device_code LIKE 'OLD-%' + AND param_name IN ('机台状态-三色灯机器运行', '机台状态-三色灯机器暂停', '机台状态-三色灯机器待机', '机台状态-三色灯机器报警') + AND UPPER(param_value) = 'TRUE' + AND collect_time >= SYSDATE - (2/24) + AND collect_time < SYSDATE + UNION ALL + + + SELECT device_code, param_name, collect_time, record_time + FROM BASE_DEVICE_PARAM_VAL_${suffix} + WHERE device_code NOT LIKE 'OLD-%' + AND param_name IN ('机台状态-三色灯机器运行', '机台状态-三色灯机器暂停', '机台状态-三色灯机器待机', '机台状态-三色灯机器报警') + AND UPPER(param_value) = 'TRUE' + AND collect_time >= SYSDATE - (2/24) + AND collect_time < SYSDATE + + + + ), + today_latest AS ( + SELECT device_code, + param_name, + ROW_NUMBER() OVER (PARTITION BY device_code ORDER BY NVL(record_time, collect_time) DESC) AS rn + FROM merged_param ), device_status AS ( - SELECT device_code, param_name FROM today_latest WHERE rn = 1 + SELECT device_code, param_name + FROM today_latest + WHERE rn = 1 ) - SELECT - d.device_code AS deviceCode, - d.device_name AS deviceName, - d.product_line_code AS productLineCode, - CASE - WHEN s.param_name = '机台状态-三色灯机器运行' THEN '运行' - WHEN s.param_name = '机台状态-三色灯机器暂停' THEN '停机' - WHEN s.param_name = '机台状态-三色灯机器待机' THEN '待机' - WHEN s.param_name = '机台状态-三色灯机器报警' THEN '报警' - ELSE '未开机' - END AS deviceStatus, - CASE - WHEN s.param_name = '机台状态-三色灯机器运行' THEN 1 - WHEN s.param_name = '机台状态-三色灯机器暂停' THEN 2 - WHEN s.param_name = '机台状态-三色灯机器待机' THEN 3 - WHEN s.param_name = '机台状态-三色灯机器报警' THEN 4 - ELSE 0 - END AS statusCode + SELECT d.device_code AS deviceCode, + d.device_name AS deviceName, + d.product_line_code AS productLineCode, + CASE + WHEN s.param_name = '机台状态-三色灯机器运行' THEN '运行' + WHEN s.param_name = '机台状态-三色灯机器暂停' THEN '停机' + WHEN s.param_name = '机台状态-三色灯机器待机' THEN '待机' + WHEN s.param_name = '机台状态-三色灯机器报警' THEN '报警' + ELSE '未开机' + END AS deviceStatus, + CASE + WHEN s.param_name = '机台状态-三色灯机器运行' THEN 1 + WHEN s.param_name = '机台状态-三色灯机器暂停' THEN 2 + WHEN s.param_name = '机台状态-三色灯机器待机' THEN 3 + WHEN s.param_name = '机台状态-三色灯机器报警' THEN 4 + ELSE 0 + END AS statusCode FROM base_deviceledger d LEFT JOIN device_status s ON d.device_code = s.device_code WHERE d.is_flag = 1 ORDER BY d.product_line_code, d.device_code - - + WITH merged_param AS ( + + SELECT device_code, param_value, collect_time, record_time + FROM BASE_DEVICE_PARAM_VAL + WHERE device_code LIKE 'OLD-%' + AND param_name = '机台状态-开机时间' + AND collect_time >= TRUNC(SYSDATE) + AND collect_time < TRUNC(SYSDATE) + 1 + UNION ALL + + + SELECT device_code, param_value, collect_time, record_time + FROM BASE_DEVICE_PARAM_VAL_${suffix} + WHERE device_code NOT LIKE 'OLD-%' + AND param_name = '机台状态-开机时间' + AND collect_time >= TRUNC(SYSDATE) + AND collect_time < TRUNC(SYSDATE) + 1 + + + + ), + today_latest AS ( + SELECT device_code, + param_value, + ROW_NUMBER() OVER (PARTITION BY device_code ORDER BY NVL(record_time, collect_time) DESC) AS rn + FROM merged_param ) - SELECT - d.device_code AS deviceCode, - d.device_name AS deviceName, - d.product_line_code AS productLineCode, - NVL(t.param_value, '') AS startTime + SELECT d.device_code AS deviceCode, + d.device_name AS deviceName, + d.product_line_code AS productLineCode, + NVL(t.param_value, '') AS startTime FROM base_deviceledger d LEFT JOIN today_latest t ON d.device_code = t.device_code AND t.rn = 1 WHERE d.is_flag = 1 ORDER BY d.product_line_code, d.device_code - - - - + SELECT param_name + FROM ( + SELECT param_name, collect_time + FROM ( + + SELECT param_name, collect_time + FROM BASE_DEVICE_PARAM_VAL + WHERE param_code = #{paramCode} + + AND device_code = #{deviceCode} + + + AND collect_time BETWEEN TO_DATE(#{startTime}, 'YYYY-MM-DD HH24:MI:SS') + AND TO_DATE(#{endTime}, 'YYYY-MM-DD HH24:MI:SS') + + UNION ALL + + + SELECT param_name, collect_time + FROM BASE_DEVICE_PARAM_VAL_${suffix} + WHERE param_code = #{paramCode} + + AND device_code = #{deviceCode} + + + AND collect_time BETWEEN TO_DATE(#{startTime}, 'YYYY-MM-DD HH24:MI:SS') + AND TO_DATE(#{endTime}, 'YYYY-MM-DD HH24:MI:SS') + + + + + ) + ORDER BY collect_time DESC + ) + WHERE ROWNUM = 1 + + + - \ No newline at end of file + diff --git a/aucma-base/src/main/resources/mapper/base/BaseMaterialinfoMapper.xml b/aucma-base/src/main/resources/mapper/base/BaseMaterialinfoMapper.xml index 537d7b5..0798190 100644 --- a/aucma-base/src/main/resources/mapper/base/BaseMaterialinfoMapper.xml +++ b/aucma-base/src/main/resources/mapper/base/BaseMaterialinfoMapper.xml @@ -58,7 +58,7 @@ - - - - - - - - - @@ -115,15 +69,14 @@ - - + @@ -197,25 +150,15 @@ ) WHERE ROWNUM <= 10 - - - - +