lyfO_o
2022-03-30 3ae57f026d9199f587fd25160e98d19572a30541
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package com.nanometer.smartlab.controller;
 
 
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.nanometer.smartlab.entity.*;
import com.nanometer.smartlab.entity.dto.ContainerStatusMngDto;
import com.nanometer.smartlab.model.SelectItemModel;
import com.nanometer.smartlab.service.SysContainerSensorsService;
import com.nanometer.smartlab.service.SysLaboratoryContainerService;
import com.nanometer.smartlab.service.SysLaboratoryService;
import com.nanometer.smartlab.service.SysWarehouseContainerService;
import com.nanometer.smartlab.util.FacesUtils;
import com.nanometer.smartlab.util.Utils;
import org.apache.commons.collections4.map.SingletonMap;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.log4j.Logger;
import org.primefaces.context.RequestContext;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.primefaces.model.chart.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
 
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.faces.model.SelectItem;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
 
 
@Controller
@Scope("session")
public class ContainerStatusMngController extends BaseController {
 
    private static Logger logger = Logger.getLogger(ContainerStatusMngController.class);
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
    private static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
 
    @Resource
    private SysContainerSensorsService sysContainerSensorsService;
 
    private String laboratoryName;
    private String laboratoryContainerName;
 
    private LazyDataModel<ContainerStatusMngDto> dataModel;
    private LineChartModel lineModel;
 
    private SysContainerSensors viewSensors;
 
    private List<Map<String,String>> containerSelectorList = new ArrayList<Map<String,String>>();
    private String selectedContainerId;
    private Integer periodType =1;  //1:day; 2:week; 3:month
    private Integer lineType =1;  //1:温度; 2:湿度; 3:浓度
    private Integer model = 0;  //1是查询
 
    private List<ContainerStatusMngDto> selectContainerList;
    private SysContainerSensors selectedSysContainerSensors;
 
    private List<SysContainerSensors> chartSensorsList;
 
    private Map<String,BigDecimal> temperatureMap = new HashMap<>();
    private Map<String,BigDecimal> humidityMap = new HashMap<>();
    private Map<String,BigDecimal> vocMap = new HashMap<>();
 
 
    private List<SelectItemModel> dateRangeList = new ArrayList<SelectItemModel>(){{
        Calendar instance = Calendar.getInstance();
        int year = instance.get(Calendar.YEAR);
        add(new SelectItemModel(){{setName("本日");setValue("1");}});
        add(new SelectItemModel(){{setName("本周");setValue("2");}});
        add(new SelectItemModel(){{setName("本月");setValue("3");}});
        for (int i = 1; i <= 12; i++) {
            SelectItemModel item = new SelectItemModel();
            item.setName(year +"年"+ i + "月");
            item.setValue(year + String.format("%02d",i));
            add(item);
        }
    }};
 
 
    public LazyDataModel<ContainerStatusMngDto> getDataModel() {
        if (this.dataModel == null) {
            this.dataModel = new LazyDataModel<ContainerStatusMngDto>() {
 
                public List<ContainerStatusMngDto> load(int first, int pageSize, String sortField, SortOrder sortOrder,
                                                Map<String, Object> filters) {
                    List<ContainerStatusMngDto> list = null;
                    try {
                        Map<String,Object> params = new HashMap<>();
                        //查询把index置为0
                        if(model == 1){
                            first = 0;
                        }
                        params.put("offset", first);
                        params.put("limit",pageSize);
                        params.put("laboratoryName",laboratoryName);
                        params.put("laboratoryContainerName",laboratoryContainerName);
 
                        int pageCount = sysContainerSensorsService.countAll(params);
                        //设置页码
                        this.setRowCount(pageCount);
                        if(pageCount>0)
                            list = sysContainerSensorsService.selectInfo(params);
 
                    } catch (Exception e) {
                        logger.error("error occured.", e);
                    }
                    selectContainerList = null;
                    return list;
                }
 
                @Override
                public ContainerStatusMngDto getRowData(String rowKey) {
//                    Iterator<ContainerStatusMngDto> iterator = this.iterator();
//                    if (iterator != null) {
//                        ContainerStatusMngDto su = null;
//                        while (iterator.hasNext()) {
//                            su = iterator.next();
//                            if (rowKey.equals(su.getId())) {
//                                return su;
//                            }
//                        }
//                    }
                    return sysContainerSensorsService.getRowData(rowKey);
                }
            };
        }
        return dataModel;
    }
 
    public void onViewBtnClick() throws ParseException {
        if (this.selectContainerList == null
                || this.selectContainerList.size() == 0) {
            FacesUtils.warn("请选择数据。");
            return;
        }
        if (this.selectContainerList.size() > 1) {
            FacesUtils.warn("只能选择一个数据进行查看。");
            return;
        }
        containerSelectorList.clear();
        selectedContainerId = selectContainerList.get(0).getId();
        fetchChartSensorsList();
        renderLineChart();
        RequestContext.getCurrentInstance().execute("PF('dialog').show()");
    }
 
 
    public void search(){
        dataModel = null;
        model = 1;
    }
 
    public String getLaboratoryName() {
        return laboratoryName;
    }
 
    public void setLaboratoryName(String laboratoryName) {
        this.laboratoryName = laboratoryName;
    }
 
    public String getLaboratoryContainerName() {
        return laboratoryContainerName;
    }
 
    public void setLaboratoryContainerName(String laboratoryContainerName) {
        this.laboratoryContainerName = laboratoryContainerName;
    }
 
    public List<ContainerStatusMngDto> getSelectContainerList() {
        return selectContainerList;
    }
 
    public void setSelectContainerList(List<ContainerStatusMngDto> selectContainerList) {
        this.selectContainerList = selectContainerList;
    }
 
    public SysContainerSensors getViewSensors() {
        return viewSensors;
    }
 
    public void setViewSensors(SysContainerSensors viewSensors) {
        this.viewSensors = viewSensors;
    }
 
    public LineChartModel getLineModel() {
        return lineModel;
    }
 
    public void setLineModel(LineChartModel lineModel) {
        this.lineModel = lineModel;
    }
 
    public String getSelectedContainerId() {
        return selectedContainerId;
    }
 
    public void setSelectedContainerId(String selectedContainerId) {
        this.selectedContainerId = selectedContainerId;
    }
 
    public Integer getLineType() {
        return lineType;
    }
 
    public void setLineType(Integer lineType) {
        this.lineType = lineType;
    }
 
    public Integer getPeriodType() {
        return periodType;
    }
 
    public void setPeriodType(Integer periodType) {
        this.periodType = periodType;
    }
 
    public SysContainerSensors getSelectedSysContainerSensors() {
        return selectedSysContainerSensors;
    }
 
    public void setSelectedSysContainerSensors(SysContainerSensors selectedSysContainerSensors) {
        this.selectedSysContainerSensors = selectedSysContainerSensors;
    }
 
    public void onSelectLineType(){
        renderLineChart();
    }
 
    public void onSelectPeriodType() throws ParseException {
        fetchChartSensorsList();
        renderLineChart();
    }
 
    public void onSelectContainer() throws ParseException {
        fetchChartSensorsList();
        renderLineChart();
    }
 
    public List<Map<String, String>> getContainerSelectorList() {
        return containerSelectorList;
    }
 
    public void setContainerSelectorList(List<Map<String, String>> containerSelectorList) {
        this.containerSelectorList = containerSelectorList;
    }
 
    public void fetchChartSensorsList() throws ParseException {
        String beginDate = null;
        String endDate = null;
 
        switch (periodType) {
            case 1: beginDate = DateFormatUtils.ISO_DATE_FORMAT.format(new Date());break;
            case 2:  beginDate = Utils.getWeekMonday("yyyy-MM-dd");break;
            case 3:  beginDate =  DateFormatUtils.ISO_DATE_FORMAT.format(DateUtils.truncate(new Date(),Calendar.MONTH));break;
            default:
 
                Date parse = sdf.parse(periodType + "");
                Calendar instance = Calendar.getInstance();
                instance.setTime(parse);
                instance.set(Calendar.DAY_OF_MONTH, 1);
                beginDate = sdf2.format(instance.getTime()) +" 00:00:00";
 
                instance.set(Calendar.DAY_OF_MONTH,instance.getActualMaximum(Calendar.DAY_OF_MONTH));;
                endDate = sdf2.format(instance.getTime()) +" 23:59:59";
 
                //1月
        }
 
 
 
        if (endDate != null) {
            Map params = ImmutableMap.of("containerId",selectedContainerId,"beginUpdateTime", beginDate,"endUpdateTime", endDate,"orderby","update_time asc");
            chartSensorsList = sysContainerSensorsService.getListBySelective(params);
        }else{
            Map params = ImmutableMap.of("containerId",selectedContainerId,"beginUpdateTime", beginDate,"orderby","update_time asc");
            chartSensorsList = sysContainerSensorsService.getListBySelective(params);
        }
 
        temperatureMap = sysContainerSensorsService.selectMaxTemperature(selectedContainerId, beginDate, endDate);
        humidityMap = sysContainerSensorsService.selectMaxHumidity(selectedContainerId,beginDate,endDate);
        vocMap = sysContainerSensorsService.selectMaxVoc(selectedContainerId, beginDate, endDate);
 
//        this.maxHumidity = sysContainerSensorsService.selectMaxHumidity(selectedContainerId,beginDate,endDate);
//        this.maxVoc = sysContainerSensorsService.selectMaxVoc(selectedContainerId,beginDate,endDate);
    }
 
    public void renderLineChart() {
        String minY = null;
        String maxY = null;
 
        lineModel = new LineChartModel();
        LineChartSeries series1 = new LineChartSeries();
        series1.setFill(true);
 
        for (SysContainerSensors sensors : chartSensorsList) {
            if(lineType == 1) {
                series1.setLabel("温度");
                series1.set(DateFormatUtils.format(sensors.getUpdateTime(),"yyyy-MM-dd HH:mm"),sensors.getTemp());
            } else if(lineType == 2){
                series1.setLabel("湿度");
                series1.set(DateFormatUtils.format(sensors.getUpdateTime(),"yyyy-MM-dd HH:mm"),sensors.getHumidity());
            } else {
                series1.setLabel("浓度");
                series1.set(DateFormatUtils.format(sensors.getUpdateTime(),"yyyy-MM-dd HH:mm"),sensors.getVoc1());
            }
        }
 
//        DateAxis axis = new DateAxis("Dates");
//        Axis xAxis = lineModel.getAxis(AxisType.X);
 
        Axis xAxis = new DateAxis();
 
        lineModel.getAxes().put(AxisType.X, xAxis);
        lineModel.setShowDatatip(true);
        if (periodType == 1) {
            xAxis.setTickFormat("%m-%d:%H");
        }
        if (periodType == 2) {
            xAxis.setTickFormat("%m-%d");
        }
        if (periodType >= 3) {
            xAxis.setTickFormat("%m-%d");
        }
        xAxis.setTickAngle(-50);
//        axis.setMax(DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm"));
//        axis.setMin(minDate);
//        axis.setTickFormat("%Y-%m-%d %H:%M");
 
        lineModel.addSeries(series1);
        lineModel.setSeriesColors("5e9cd9");
        lineModel.setLegendPosition("e");
        Axis yAxis = lineModel.getAxis(AxisType.Y);
//        yAxis.setMin(0);
//        yAxis.setMax(10);
    }
 
 
 
    public List<SysContainerSensors> getChartSensorsList() {
        return chartSensorsList;
    }
 
    public void setChartSensorsList(List<SysContainerSensors> chartSensorsList) {
        this.chartSensorsList = chartSensorsList;
    }
 
 
    public List<SelectItemModel> getDateRangeList() {
        return dateRangeList;
    }
 
    public void setDateRangeList(List<SelectItemModel> dateRangeList) {
        this.dateRangeList = dateRangeList;
    }
 
    public Map<String, BigDecimal> getTemperatureMap() {
        return temperatureMap;
    }
 
    public void setTemperatureMap(Map<String, BigDecimal> temperatureMap) {
        this.temperatureMap = temperatureMap;
    }
 
    public Map<String, BigDecimal> getHumidityMap() {
        return humidityMap;
    }
 
    public void setHumidityMap(Map<String, BigDecimal> humidityMap) {
        this.humidityMap = humidityMap;
    }
 
    public Map<String, BigDecimal> getVocMap() {
        return vocMap;
    }
 
    public void setVocMap(Map<String, BigDecimal> vocMap) {
        this.vocMap = vocMap;
    }
}