From 90a1495b9d765c9e1d48f689dbcf977cbdb2768e Mon Sep 17 00:00:00 2001 From: dengzedong <dengzedong@email> Date: 星期二, 31 十二月 2024 17:23:13 +0800 Subject: [PATCH] 模型结果修改为存在唯一索引则修改,不存在插入。避免死锁问题 --- iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/MmItemResultServiceImpl.java | 130 +++++++++++++++++++------------------------ 1 files changed, 58 insertions(+), 72 deletions(-) diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/MmItemResultServiceImpl.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/MmItemResultServiceImpl.java index 0465a2d..26f58e5 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/MmItemResultServiceImpl.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/MmItemResultServiceImpl.java @@ -2,13 +2,12 @@ import com.alibaba.fastjson.JSONArray; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.iailab.framework.common.service.impl.BaseServiceImpl; -import com.iailab.module.data.api.point.dto.ApiPointDTO; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.iailab.framework.common.util.date.DateUtils; import com.iailab.module.model.mcs.pre.dao.MmItemResultDao; import com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity; import com.iailab.module.model.mcs.pre.entity.MmItemResultEntity; import com.iailab.module.model.mcs.pre.entity.MmItemResultJsonEntity; -import com.iailab.module.model.mcs.pre.service.MmItemOutputService; import com.iailab.module.model.mcs.pre.service.MmItemResultService; import com.iailab.module.model.mdk.vo.DataValueVO; import org.springframework.beans.factory.annotation.Autowired; @@ -16,8 +15,6 @@ import org.springframework.util.CollectionUtils; import java.math.BigDecimal; -import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; @@ -25,47 +22,21 @@ * @author PanZhibao * @date 2021年05月28日 10:34 */ -@Service("mmItemResultService") -public class MmItemResultServiceImpl extends BaseServiceImpl<MmItemResultDao, MmItemResultEntity> - implements MmItemResultService { +@Service +public class MmItemResultServiceImpl extends ServiceImpl<MmItemResultDao, MmItemResultEntity> implements MmItemResultService { private final int max_group_count = 100; private final String T_MM_ITEM_RESULT = "T_MM_ITEM_RESULT"; @Autowired - private MmItemOutputService mmItemOutputService; - - @Override - public List<MmItemResultEntity> getListByOutputId(String outputid, Map<String, Object> params) { - - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - Date startDateParam = null; - try { - startDateParam = sdf.parse((String)params.get("startTime")); - } catch (ParseException e) { - e.printStackTrace(); - } - Date endDateParam = null; - try { - endDateParam = sdf.parse((String)params.get("endTime")); - } catch (ParseException e) { - e.printStackTrace(); - } - - List<MmItemResultEntity> list = baseDao.selectList( - new QueryWrapper<MmItemResultEntity>() - .eq("outputid", outputid) - .between("datatime", startDateParam, endDateParam) - .orderByAsc("datatime") - ); - return list; - } + private MmItemResultDao mmItemResultDao; @Override public void savePredictValue(Map<String, List<DataValueVO>> predictValueMap, int t, String nIndex, Date predictTime) { List<MmItemResultEntity> importList = new ArrayList<>(); List<MmItemResultEntity> lastList = new ArrayList<>(); + for (Map.Entry<String, List<DataValueVO>> entry : predictValueMap.entrySet()) { for (DataValueVO dataVo : entry.getValue()) { MmItemResultEntity importData = new MmItemResultEntity(); @@ -78,9 +49,9 @@ List<DataValueVO> lastVoList = new ArrayList<>(); int size = entry.getValue().size(); - t = t > 0 ? t : 0; + t = Math.max(t, 0); int n = "n".equals(nIndex) ? size : Integer.parseInt(nIndex); - int length = (n - t) > 0 ? (n - t) : 0; //预测完不变的数据长度 + int length = Math.max((n - t), 0); //预测完不变的数据长度 if (size >= n) { for (int i = 0; i < (size - length); i ++) { int index = length + i; @@ -103,49 +74,29 @@ resultJson.setId(UUID.randomUUID().toString()); resultJson.setOutputid(entry.getKey()); resultJson.setPredicttime(predictTime); - resultJson.setJsonvalue(JSONArray.toJSONString(entry.getValue())); + List<Double> jsonValueList = entry.getValue().stream().map(valueVO -> { + return valueVO.getDataValue(); + }).collect(Collectors.toList()); + resultJson.setJsonvalue(JSONArray.toJSONString(jsonValueList)); Map<String, Object> map4 = new HashMap(2); map4.put("TABLENAME", "T_MM_ITEM_RESULT_JSON"); map4.put("entity", resultJson); - baseDao.savePredictJsonValue(map4); - } + mmItemResultDao.savePredictJsonValue(map4); - Map<String, Object> params = new HashMap(4); - params.put("TABLENAME", T_MM_ITEM_RESULT); - params.put("OUTPUTID", importList.get(0).getOutputid()); - params.put("STARTTIME", importList.get(0).getDatatime()); - params.put("ENDTIME", importList.get(importList.size() - 1).getDatatime()); - baseDao.deletePredictValue(params); - - int num1 = importList.size() / max_group_count; - int num2 = importList.size() % max_group_count; - if (num2 != 0) { - num1++; +// Map<String, Object> params = new HashMap(4); +// params.put("TABLENAME", T_MM_ITEM_RESULT); +// params.put("OUTPUTID", entry.getKey()); +// params.put("STARTTIME", importList.get(0).getDatatime()); +// params.put("ENDTIME", importList.get(importList.size() - 1).getDatatime()); +// mmItemResultDao.deletePredictValue(params); } - - List<MmItemResultEntity> tempList; - //先删除已经存在的数据,再插入新数据 - for (int i = 0; i < num1; i++) { - int startIndex = max_group_count * i; - int count = max_group_count; - if (num2!=0 && i == num1 - 1) { - count = num2; - } - tempList = new ArrayList<>(); - //获取某个索引范围内的对象集合 - for (int j = startIndex; j < startIndex + count; j++) { - tempList.add(importList.get(j)); - } - Map<String, Object> map2 = new HashMap<>(2); - map2.put("TABLENAME", T_MM_ITEM_RESULT); - map2.put("list", tempList); - baseDao.savePredictValue(map2); - } + // 存在则修改,不存在插入 + mmItemResultDao.saveOrUpdateItemResult(importList); Map<String, Object> map3 = new HashMap<>(2); map3.put("TABLENAME", "T_MM_ITEM_RESULT_LAST_POINT"); map3.put("list", lastList); - baseDao.savePredictValue(map3); + mmItemResultDao.savePredictValue(map3); } @Override @@ -155,7 +106,7 @@ .eq("outputid", outputid) .between("datatime", startTime, endTime) .orderByAsc("datatime"); - List<MmItemResultEntity> list = baseDao.selectList(queryWrapper); + List<MmItemResultEntity> list = mmItemResultDao.selectList(queryWrapper); if (CollectionUtils.isEmpty(list)) { return result; } @@ -167,4 +118,39 @@ }).collect(Collectors.toList()); return result; } + + @Override + public List<Object[]> getData(String outputid, Date startTime, Date endTime, String timeFormat) { + List<Object[]> result = new ArrayList<>(); + QueryWrapper<MmItemResultEntity> queryWrapper = new QueryWrapper<MmItemResultEntity>() + .eq("outputid", outputid) + .between("datatime", startTime, endTime) + .orderByAsc("datatime"); + List<MmItemResultEntity> list = mmItemResultDao.selectList(queryWrapper); + if (CollectionUtils.isEmpty(list)) { + return result; + } + list.forEach(item -> { + Object[] dataItem = new Object[2]; + dataItem[0] = DateUtils.format(item.getDatatime(), timeFormat); + dataItem[1] = item.getDatavalue().setScale(2, BigDecimal.ROUND_HALF_UP); + result.add(dataItem); + }); + return result; + } + + @Override + public void savePredictValue(Map<MmItemOutputEntity, Double> predictDoubleValues, Date predictTime) { + for (Map.Entry<MmItemOutputEntity, Double> entry : predictDoubleValues.entrySet()) { + MmItemResultJsonEntity resultJson = new MmItemResultJsonEntity(); + resultJson.setId(UUID.randomUUID().toString()); + resultJson.setOutputid(entry.getKey().getId()); + resultJson.setPredicttime(predictTime); + resultJson.setCumulant(String.valueOf(entry.getValue())); + Map<String, Object> map4 = new HashMap(2); + map4.put("TABLENAME", "T_MM_ITEM_RESULT_JSON"); + map4.put("entity", resultJson); + mmItemResultDao.savePredictJsonValue(map4); + } + } } -- Gitblit v1.9.3