yangys
2024-10-30 c27b939fa5fa6ce4d712f7e9ced2ad811d69d5ec
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
package com.qianwen.smartman.modules.mdc.service;
 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
 
import com.baomidou.dynamic.datasource.annotation.DS;
import com.qianwen.smartman.modules.mdc.dto.ProcessParameterVO;
 
@DS("iotdb")
@Service
public class ProcessParameterHelperService {
    private static final Logger log = LoggerFactory.getLogger(ProcessParameterHelperService.class);
    
    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    @DS("iotdb")
    public ProcessParameterVO queryLastParameterLessThenTime(long workstationId,String item,Long time) {
        /**
         *tdengine实现:oldFirstStatue
         * select last(ts) as realTime,
               last(v)  as value_collect
        from iot_data.super_collect_data
        where n = #{item}
          and ts < #{startTime}
          and workstation_id = #{workstationId}
         */
        ProcessParameterVO vo = null;
        String sql ="select max_time(n) as ts,last_value(n) as n,last_value(v) as v,last_value(workstation_id) as workstationId from root.f2.process_param_"+workstationId+"_"+item+" where time<"+time+" limit 1 align by device";
        List<ProcessParameterVO> list =  jdbcTemplate.query(sql, new RowMapper<ProcessParameterVO>() {
 
            @Override
            public ProcessParameterVO mapRow(ResultSet rs, int rowNum) throws SQLException {
                ProcessParameterVO p = new ProcessParameterVO();
                Long time = rs.getLong("ts");
                p.setRealTime(time);
                p.setCollectItem(rs.getString("n"));
                p.setValueCollect(rs.getString("v"));
                
                return p;
            }
            
        });
        if(!list.isEmpty()) {
            vo = list.get(0);
        }
        return vo;
    }
    
    /**
     * 获取工位最新参数值(采集时间应大于指定时间)
     * @param workstationId
     * @param item  参数名称,如DeviceStatus/Output
     * @param time 指定的时间
     * @return
     */
    @DS("iotdb")
    public ProcessParameterVO getLastParameterGreaterThenTime(long workstationId,String item,Long time) {
        /*
         oldLastStatue
        select last(ts) as realTime,
               last(v)  as value_collect
        from iot_data.super_collect_data
        where n = #{item}
          and ts &gt; #{endTime}
          and workstation_id = #{workstationId}
         * */
        /*
        LastProcessParam lp = this.parameterMapper.lastParameterGreaterThanTime(workstationId,item,time);
        if(lp == null) {
            return null;
        }
        ProcessParameterVO vo = new ProcessParameterVO();
        //解析json为对象列表
        JSONObject paramsObj = JSONObject.parseObject(lp.getParamJson());
        
        if(paramsObj.containsKey(item)) {
            JSONObject itemObj = paramsObj.getJSONObject(item);
            
            Long lastTime = itemObj.getLong("time");
            vo.setTime(new Timestamp(lastTime));
            vo.setRealTime(lastTime);
            vo.setValueCollect(itemObj.getString("value"));
        }
        
        return vo;*/
        
        ProcessParameterVO vo = null;
        String sql ="select max_time(n) as ts,last_value(n) as n,last_value(v) as v,last_value(workstation_id) as workstationId from root.f2.process_param_"+workstationId+"_"+item+" where time>"+time+" limit 1 align by device";
        List<ProcessParameterVO> list =  jdbcTemplate.query(sql, new RowMapper<ProcessParameterVO>() {
 
            @Override
            public ProcessParameterVO mapRow(ResultSet rs, int rowNum) throws SQLException {
                ProcessParameterVO p = new ProcessParameterVO();
                p.setRealTime(rs.getLong("ts"));
                p.setCollectItem(rs.getString("n"));
                p.setValueCollect(rs.getString("v"));
                
                return p;
            }
            
        });
        if(!list.isEmpty()) {
            vo = list.get(0);
        }
        return vo;
        
        
    }
 
}