|
@@ -1,12 +1,24 @@
|
|
|
package org.jeecg.modules.equipmentOnoff.service.impl;
|
|
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import org.jeecg.common.api.vo.Result;
|
|
|
+import org.jeecg.modules.collectdata.entity.Collectdata;
|
|
|
import org.jeecg.modules.equipmentOnoff.entity.EquipmentOnoff;
|
|
|
+import org.jeecg.modules.equipmentOnoff.entity.HistoryParamDto;
|
|
|
import org.jeecg.modules.equipmentOnoff.mapper.EquipmentOnoffMapper;
|
|
|
import org.jeecg.modules.equipmentOnoff.service.IEquipmentOnoffService;
|
|
|
+import org.jeecg.modules.tpmEquipment.service.ITpmEquipmentService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
+import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
+import org.springframework.data.mongodb.core.query.Query;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
/**
|
|
|
* @Description: tpm_equipment_onoff
|
|
|
* @Author: jeecg-boot
|
|
@@ -16,4 +28,78 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
@Service
|
|
|
public class EquipmentOnoffServiceImpl extends ServiceImpl<EquipmentOnoffMapper, EquipmentOnoff> implements IEquipmentOnoffService {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private MongoTemplate mongoTemplate;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ @SuppressWarnings("all")
|
|
|
+ private ITpmEquipmentService tpmEquipmentService;
|
|
|
+
|
|
|
+ public Map<String, Date> splitByMonth(Date beginTime, Date endTime){
|
|
|
+ Map<String, Date> dateMap = new HashMap<>();
|
|
|
+ Calendar calB = Calendar.getInstance();
|
|
|
+ Calendar calE = Calendar.getInstance();
|
|
|
+ calB.setTime(beginTime);
|
|
|
+ calB.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
|
|
|
+ calE.setTime(endTime);
|
|
|
+ calE.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
|
|
|
+ System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calB.getTime()));
|
|
|
+ System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calE.getTime()));
|
|
|
+ int i = 1;
|
|
|
+ while (calB.before(calE)){
|
|
|
+ if(calB.get(Calendar.YEAR)==calE.get(Calendar.YEAR) && calB.get(Calendar.MONTH)==calE.get(Calendar.MONTH)){
|
|
|
+ dateMap.put("beginTime"+ i, calB.getTime());
|
|
|
+ dateMap.put("endTime"+ i, calE.getTime());
|
|
|
+// dateList.add(dateMap);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ //当前的时间段是跨月或跨年的,需要进行切分
|
|
|
+ dateMap.put("beginTime"+i, calB.getTime());
|
|
|
+ //将开始时间挪到下个月的第一天
|
|
|
+ calB.add(Calendar.MONTH, 1);
|
|
|
+ calB.set(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ calB.set(Calendar.HOUR_OF_DAY, 0);// 时
|
|
|
+ calB.set(Calendar.MINUTE, 0);// 分
|
|
|
+ calB.set(Calendar.SECOND, 0);// 秒
|
|
|
+ calB.set(Calendar.MILLISECOND, 0);// 毫秒
|
|
|
+ dateMap.put("endTime"+i, calB.getTime());
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+
|
|
|
+ return dateMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Result<List<Map<String, Object>>> getEquipmentHistoryData(HistoryParamDto historyParamDto){
|
|
|
+ List<Map<String, Object>> historyList = new ArrayList<>();
|
|
|
+ List<Collectdata> orgDataList = new ArrayList<>();
|
|
|
+ //根据设备id找到设备编号
|
|
|
+ String equipmentcode;
|
|
|
+ try {
|
|
|
+ equipmentcode = (tpmEquipmentService.getById(historyParamDto.getEquipmentid())).getEquipmentcode();
|
|
|
+ }catch (NullPointerException e){
|
|
|
+ return Result.error("id为" + historyParamDto.getEquipmentid() + "的设备找不到设备编号,无法进行数据查询!");
|
|
|
+ }
|
|
|
+
|
|
|
+ //根据开始时间和结束时间确定要读取的表
|
|
|
+ Map<String, Date> dateMap = splitByMonth(historyParamDto.getBeginTime(), historyParamDto.getEndTime());
|
|
|
+ for(int i=1;i<=dateMap.size()/2;i++){
|
|
|
+ //创建查询对象
|
|
|
+ Query HistoryQuery = new Query();
|
|
|
+ //添加查询条件
|
|
|
+// HistoryQuery.addCriteria(Criteria.where("equipmentcode").is(equipmentcode));
|
|
|
+ HistoryQuery.addCriteria(Criteria.where("logtime").gte(dateMap.get("beginTime"+i)).lte(dateMap.get("endTime"+i))); // 大于等于startDate且小于等于endDate
|
|
|
+ HistoryQuery.addCriteria(Criteria.where("equipmentcode").is(equipmentcode));
|
|
|
+ //执行查询
|
|
|
+ try{
|
|
|
+ orgDataList.addAll(mongoTemplate.find(HistoryQuery, Collectdata.class, "data_"+new SimpleDateFormat("yyyyMM").format(dateMap.get("beginTime"+i))));
|
|
|
+ }catch (NullPointerException e){}
|
|
|
+ }
|
|
|
+ for(Collectdata collectdata:orgDataList){
|
|
|
+ Map<String, Object> dataMap = new ObjectMapper().convertValue(collectdata.getProperties(), Map.class);
|
|
|
+ dataMap.put("logtime", collectdata.getLogtime());
|
|
|
+ historyList.add(dataMap);
|
|
|
+ }
|
|
|
+ return Result.ok(historyList);
|
|
|
+ }
|
|
|
+
|
|
|
}
|