yangys
2025-05-09 e44c211aab8da89e89657426460d44285de6959c
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
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) {
        
        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) {
        
        
        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;
        
        
    }
 
}