dengzedong
2025-06-04 7652e1d1223a76e0158a6a390a7b83c202c370b9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package com.iailab.module.model.mcs.sche.service.impl;
 
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.iailab.framework.common.service.impl.BaseServiceImpl;
import com.iailab.framework.common.util.object.BeanUtils;
import com.iailab.framework.common.util.object.ConvertUtils;
import com.iailab.module.data.api.ind.IndItemApi;
import com.iailab.module.data.api.ind.dto.ApiIndItemQueryDTO;
import com.iailab.module.data.api.ind.dto.ApiIndItemValueDTO;
import com.iailab.module.data.api.plan.PlanItemApi;
import com.iailab.module.data.api.point.DataPointApi;
import com.iailab.module.data.api.point.dto.ApiPointValueDTO;
import com.iailab.module.data.api.point.dto.ApiPointValueQueryDTO;
import com.iailab.module.data.common.ApiDataQueryDTO;
import com.iailab.module.data.common.ApiDataValueDTO;
import com.iailab.module.model.api.mcs.dto.PreDataBarLineReqVO;
import com.iailab.module.model.api.mcs.dto.PreDataBarLineRespVO;
import com.iailab.module.model.api.mcs.dto.StSuggestSnapshotRecordRespVO;
import com.iailab.module.model.common.enums.DataCategoryEnum;
import com.iailab.module.model.common.utils.DateUtils;
import com.iailab.module.model.mcs.pre.service.MmItemResultService;
import com.iailab.module.model.mcs.sche.dao.StSuggestSnapshotRecordDao;
import com.iailab.module.model.mcs.sche.entity.StSuggestSnapshotRecordEntity;
import com.iailab.module.model.mcs.sche.service.StSuggestSnapshotConfDetService;
import com.iailab.module.model.mcs.sche.service.StSuggestSnapshotConfMainService;
import com.iailab.module.model.mcs.sche.service.StSuggestSnapshotRecordService;
import com.iailab.module.model.mcs.sche.vo.StSuggestSnapshotConfDetRespVO;
import com.iailab.module.model.mcs.sche.vo.StSuggestSnapshotConfMainRespVO;
import com.iailab.module.model.mcs.sche.vo.StSuggestSnapshotRecordSaveReqVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
 
import static com.iailab.module.model.common.utils.DateUtils.DATE_TIME_PATTERN;
 
 
/**
 * @author Jay
 */
@Service
public class StSuggestSnapshotRecordServiceImpl extends BaseServiceImpl<StSuggestSnapshotRecordDao, StSuggestSnapshotRecordEntity>
        implements StSuggestSnapshotRecordService {
    @Autowired
    private DataPointApi dataPointApi;
 
    @Autowired
    private IndItemApi indItemApi;
 
    @Autowired
    private PlanItemApi planItemApi;
 
    @Autowired
    private MmItemResultService mmItemResultService;
 
 
 
    @Autowired
    private StSuggestSnapshotConfMainService stSuggestSnapshotConfMainService;
 
    @Autowired
    private StSuggestSnapshotConfDetService stSuggestSnapshotConfDetService;
 
    @Override
    @DSTransactional(rollbackFor = Exception.class)
    public void create(StSuggestSnapshotRecordSaveReqVO createReqVO) {
        StSuggestSnapshotRecordEntity entity = BeanUtils.toBean(createReqVO, StSuggestSnapshotRecordEntity.class);
        entity.setId(UUID.randomUUID().toString());
        baseDao.insert(entity);
    }
 
    @Override
    @DSTransactional(rollbackFor = Exception.class)
    public void update(StSuggestSnapshotRecordSaveReqVO updateReqVO) {
        StSuggestSnapshotRecordEntity entity = BeanUtils.toBean(updateReqVO, StSuggestSnapshotRecordEntity.class);
        baseDao.updateById(entity);
    }
 
    @Override
    @DSTransactional(rollbackFor = Exception.class)
    public void delete(String id) {
        baseDao.deleteById(id);
    }
 
    @Override
    public List<StSuggestSnapshotRecordRespVO> getListBySuggestId(String suggestId) {
        return ConvertUtils.sourceToTarget(baseDao.selectList("suggest_id",  suggestId), StSuggestSnapshotRecordRespVO.class);
    }
 
    @Override
    public void createSnapshotRecord(String modelId, String scheduleObj,Date scheduleTime, String suggestId) {
        if (StringUtils.isBlank(modelId) || StringUtils.isBlank(scheduleObj) || StringUtils.isBlank(suggestId)) {
            log.error("生成快照失败,参数错误!modelId:" + modelId + ",scheduleObj:" + scheduleObj + ",suggestId:" + suggestId);
            return;
        }
        //获取调度建议快照配置表信息
        StSuggestSnapshotConfMainRespVO confMain = stSuggestSnapshotConfMainService.getByModelIdAndScheduleObj(modelId, scheduleObj);
        if (confMain == null) {
            log.error("生成快照失败,confMain is null!modelId:" + modelId + ",scheduleObj:" + scheduleObj);
            return;
        }
        List<StSuggestSnapshotConfDetRespVO> confDetList = stSuggestSnapshotConfDetService.getByConfId(confMain.getId());
        if (CollectionUtils.isEmpty(confDetList)) {
            log.error("生成快照失败,confDetList is empty!modelId:" + modelId + ",scheduleObj:" + scheduleObj);
            return;
        }
        confDetList.forEach(confDet -> {
            StSuggestSnapshotRecordSaveReqVO snapshotRecordSaveReqVO = new StSuggestSnapshotRecordSaveReqVO();
            snapshotRecordSaveReqVO.setSuggestId(suggestId);
            snapshotRecordSaveReqVO.setModelId(modelId);
            snapshotRecordSaveReqVO.setDataNo(confDet.getDataNo());
            snapshotRecordSaveReqVO.setDataType(confDet.getDataType());
            snapshotRecordSaveReqVO.setDataName(confDet.getDataName());
            snapshotRecordSaveReqVO.setScheduleTime(scheduleTime);
            //计算开始时间,开始时间为当前调度时间减去快照配置表配置的左侧时间长度
            LocalDateTime localStartDateTime =  scheduleTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime().minusMinutes(confDet.getLeftLength() ==  null ? 60 : confDet.getLeftLength());
            Date startTime = Date.from(localStartDateTime.atZone(ZoneId.systemDefault()).toInstant());
            snapshotRecordSaveReqVO.setStartTime(startTime);
            //计算结束时间,结束时间为当前调度时间加上快照配置表配置的右侧时间长度
            LocalDateTime localEndDateTime =  scheduleTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime().plusMinutes(confDet.getRightLength() ==  null ? 60 : confDet.getLeftLength());
            Date endTime = Date.from(localEndDateTime.atZone(ZoneId.systemDefault()).toInstant());
            snapshotRecordSaveReqVO.setEndTime(endTime);
            this.create(snapshotRecordSaveReqVO);
        });
    }
 
    @Override
    public List<StSuggestSnapshotRecordRespVO> getChartData(List<StSuggestSnapshotRecordRespVO> reqList) {
        reqList.forEach(item -> {
            List<Object[]> dataList = new ArrayList<>();
            if(DataCategoryEnum.DATAPOINT.getCode().equals(item.getDataType())){
                ApiPointValueQueryDTO queryDTO = new ApiPointValueQueryDTO();
                queryDTO.setPointNo(item.getDataNo());
                queryDTO.setStart(item.getStartTime());
                queryDTO.setEnd(item.getEndTime());
                List<ApiPointValueDTO> valueList = dataPointApi.queryPointHistoryValue(queryDTO);
                if (valueList != null && !valueList.isEmpty()){
                    for (ApiPointValueDTO apiPointValueDTO : valueList) {
                        Object[] data = new Object[2];
                        data[0] = DateUtils.format(apiPointValueDTO.getT(), DATE_TIME_PATTERN);
                        data[1] = apiPointValueDTO.getV();
                        dataList.add(data);
                    }
                }
            } else if (DataCategoryEnum.IND.getCode().equals(item.getDataType())) {
                ApiIndItemQueryDTO queryDTO = new ApiIndItemQueryDTO();
                queryDTO.setItemNo(item.getDataNo());
                queryDTO.setStart(item.getStartTime());
                queryDTO.setEnd(item.getEndTime());
                List<ApiIndItemValueDTO> valueList = indItemApi.queryIndItemHistoryValue(queryDTO);
                if (valueList != null && !valueList.isEmpty()){
                    for (ApiIndItemValueDTO apiIndItemValueDTO : valueList) {
                        Object[] data = new Object[2];
                        data[0] = apiIndItemValueDTO.getDataTime();
                        data[1] = apiIndItemValueDTO.getDataValue();
                        dataList.add(data);
                    }
                }
            } else if (DataCategoryEnum.PLAN.getCode().equals(item.getDataType())) {
                ApiDataQueryDTO queryDTO = new ApiDataQueryDTO();
                queryDTO.setItemNo(item.getDataNo());
                queryDTO.setStart(item.getStartTime());
                queryDTO.setEnd(item.getEndTime());
                List<ApiDataValueDTO> valueList = planItemApi.queryPlanItemHistoryValue(queryDTO);
                if (valueList != null && !valueList.isEmpty()){
                    for (ApiDataValueDTO apiDataValueDTO : valueList) {
                        Object[] data = new Object[2];
                        data[0] = DateUtils.format(apiDataValueDTO.getDataTime(), DATE_TIME_PATTERN);
                        data[1] = apiDataValueDTO.getDataValue();
                        dataList.add(data);
                    }
                }
            } else if (DataCategoryEnum.PREDICT_ITEM_N.getCode().equals(item.getDataType())) {
                dataList = getPreDataList(item, "N");
            }else if (DataCategoryEnum.PREDICT_ITEM_L.getCode().equals(item.getDataType())) {
                dataList = getPreDataList(item, "L");
            }else if (DataCategoryEnum.PREDICT_ITEM_C.getCode().equals(item.getDataType())) {
                dataList = getPreDataList(item, "C");
            }
            item.setDataList(dataList);
 
        });
        return reqList;
    }
 
    @Override
    public List<StSuggestSnapshotRecordRespVO> getAllChartData(String suggestId) {
        List<StSuggestSnapshotRecordRespVO> records = this.getListBySuggestId(suggestId);
        if (CollectionUtils.isEmpty(records)) {
            return new ArrayList<>();
        }
        return this.getChartData(records);
    }
 
    private List<Object[]> getPreDataList(StSuggestSnapshotRecordRespVO resVo, String type){
        List<Object[]> dataList = new ArrayList<>();
        PreDataBarLineReqVO reqVO  = new PreDataBarLineReqVO();
        List<String> outIds = new ArrayList<>();
        outIds.add(resVo.getDataNo());
        reqVO.setOutIds(outIds);
        reqVO.setStartTime(resVo.getStartTime());
        reqVO.setEndTime(resVo.getEndTime());
        PreDataBarLineRespVO result = mmItemResultService.getPreDataCharts(reqVO);
        if (result == null || result.getDataViewList() == null || result.getDataViewList().isEmpty()){
            return dataList;
        }
        if ("N".equals(type) && result.getDataViewList().get(0).getPreDataN()!=null){
            dataList = result.getDataViewList().get(0).getPreDataN();
        }else if ("L".equals(type) && result.getDataViewList().get(0).getPreDataL()!=null){
            dataList = result.getDataViewList().get(0).getPreDataL();
        }else if ("C".equals(type) && result.getDataViewList().get(0).getCurData()!=null){
            dataList = result.getDataViewList().get(0).getCurData();
        }
        return dataList;
    }
}