CZT
2023-09-06 71c4fa1e27f75ae4b765c95c67a3069c84dc72ba
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
package com.ld.igds.es.service;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import com.bstek.bdf2.core.orm.hibernate.HibernateDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
 
import com.bstek.dorado.data.provider.Page;
import com.ld.igds.models.Energy;
import com.ld.igds.util.ContextUtil;
import com.ld.igds.util.DateUtil;
 
@Component
public class HEsService extends HibernateDao {
 
    public List<Energy> listEsMeter(Map<String, Object> param) {
        if (null == param) {
            param = new HashMap<String, Object>();
            param.put("time", new Date());
        }
        Date time = (Date) param.get("time");
 
        String hql = " from "
                + Energy.class.getName()
                + " where companyId=:companyId and updateTime>=:time and updateTime <:end order by updateTime desc";
 
        param.put("companyId", ContextUtil.getCompanyId());
        param.put("time", DateUtil.getCurZero(time));
        param.put("end", DateUtil.getNextZero(time));
 
        // 获取所有满足条件的数据
        List<Energy> list = this.query(hql, param);
 
        if (null == list || list.isEmpty())
            return list;
 
        // 数据筛选,只要最新一条数据
        Map<String, Energy> tempMap = new HashMap<String, Energy>();
        
        list.stream().forEachOrdered(data -> {
            if(StringUtils.isEmpty(data.getDeviceId())){
                data.setDeviceId("0");
                data.setDeviceName("电表");
            }
            if (null == tempMap.get(data.getDepotId())) {
                tempMap.put(data.getDepotId() + "-" + data.getDeviceId(), data);
            }
        });
 
        // MAP转换为LIST
        return new ArrayList<Energy>(tempMap.values());
    }
 
    public void queryData(Page<Energy> page, Map<String, Object> param) throws Exception {
        
        String hql = " from "
                + Energy.class.getName()
                + " where companyId=:companyId ";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("companyId", ContextUtil.getCompanyId());
        
        if(null != param){
            Date start = (Date) param.get("start");
            if(null != start){
                start =  DateUtil.getCurZero(start);
                hql += " and updateTime >=:start ";
                args.put("start", start);
            }
            
            Date end = (Date) param.get("end");
            if(null != end){
                start =  DateUtil.getNextZero(end);
                hql += " and updateTime <:end ";
                args.put("end", end);
            }
            
            String depotId = (String) param.get("depotId");
            if(StringUtils.isNotEmpty(depotId)){
                hql += " and depotId =:depotId ";
                args.put("depotId", depotId);
            }
        }
        
        String coutHql = "select count(1) "+ hql;
        
        hql += " order by updateTime desc";
        
        this.pagingQuery(page, hql, coutHql, args);
    }
 
}