| | |
| | | package com.moral.api.service.impl; |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.ArrayList; |
| | | import java.util.Calendar; |
| | | import java.util.Collections; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.OptionalDouble; |
| | | import java.util.function.Supplier; |
| | | import java.util.stream.Collectors; |
| | | import java.util.stream.DoubleStream; |
| | | import java.util.stream.Stream; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.moral.api.config.mybatis.MybatisPlusConfig; |
| | | import com.moral.api.entity.*; |
| | | import com.moral.api.entity.Device; |
| | | import com.moral.api.entity.GeoCoordinate; |
| | | import com.moral.api.entity.HistoryHourly; |
| | | import com.moral.api.entity.Organization; |
| | | import com.moral.api.mapper.DeviceMapper; |
| | | import com.moral.api.mapper.HistoryHourlyMapper; |
| | | import com.moral.api.service.HistoryHourlyService; |
| | | import com.moral.api.service.OrganizationService; |
| | | import com.moral.api.service.SensorService; |
| | | import com.moral.api.utils.GetCenterPointFromListOfCoordinates; |
| | | import com.moral.constant.Constants; |
| | | import com.moral.constant.SeparateTableType; |
| | |
| | | import com.moral.util.DateUtils; |
| | | import com.moral.util.MybatisPLUSUtils; |
| | | import com.moral.util.PollutantUtils; |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import java.sql.Array; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | String mac = map.get("mac").toString(); |
| | | String sensorCode = map.get("sensor_code").toString(); |
| | | String date = map.get("date").toString(); |
| | | String dateTime = date.replace("-",""); |
| | | String timeUnits = dateTime.substring(0,6)+"_complete"; |
| | | Map<String,Object> params = new HashMap<>(); |
| | | params.put("timeUnits",timeUnits); |
| | | params.put("mac",mac); |
| | | String dateTime = date.replace("-", ""); |
| | | String timeUnits = dateTime.substring(0, 6) + "_complete"; |
| | | Map<String, Object> params = new HashMap<>(); |
| | | params.put("timeUnits", timeUnits); |
| | | params.put("mac", mac); |
| | | List resultList = new ArrayList(); |
| | | for (int i = 0; i < 24; i++) { |
| | | Map<String,Object> oneHourDateMap = new HashMap<>(); |
| | | Map<String, Object> oneHourDateMap = new HashMap<>(); |
| | | String j; |
| | | if (i<10){ |
| | | j = " 0"+i+":00:00"; |
| | | }else { |
| | | j = " "+i+":00:00"; |
| | | if (i < 10) { |
| | | j = " 0" + i + ":00:00"; |
| | | } else { |
| | | j = " " + i + ":00:00"; |
| | | } |
| | | String time = date+j; |
| | | params.put("time",time); |
| | | String resultTime = time.substring(0,13); |
| | | if (ObjectUtils.isEmpty(historyHourlyMapper.selectHourlyData(params))){ |
| | | oneHourDateMap.put("time",resultTime); |
| | | oneHourDateMap.put("values",new ArrayList<>()); |
| | | String time = date + j; |
| | | params.put("time", time); |
| | | String resultTime = time.substring(0, 13); |
| | | if (ObjectUtils.isEmpty(historyHourlyMapper.selectHourlyData(params))) { |
| | | oneHourDateMap.put("time", resultTime); |
| | | oneHourDateMap.put("values", new ArrayList<>()); |
| | | resultList.add(oneHourDateMap); |
| | | continue; |
| | | } |
| | |
| | | oneHourlyData = historyHourlyMapper.selectHourlyData(params); |
| | | JSONObject js = JSONObject.parseObject(oneHourlyData); |
| | | String sensorDate = js.get(sensorCode).toString(); |
| | | sensorDate = sensorDate.replace("[",""); |
| | | sensorDate = sensorDate.replace("]",""); |
| | | sensorDate = sensorDate.replace("[", ""); |
| | | sensorDate = sensorDate.replace("]", ""); |
| | | String[] split = sensorDate.split(","); |
| | | oneHourDateMap.put("time",resultTime); |
| | | oneHourDateMap.put("values",split); |
| | | oneHourDateMap.put("time", resultTime); |
| | | oneHourDateMap.put("values", split); |
| | | resultList.add(oneHourDateMap); |
| | | } |
| | | return resultList; |
| | |
| | | return multiTableQuery(queryWrapper, tableNames); |
| | | } |
| | | |
| | | @Override |
| | | public Double calculatedValue(List<HistoryHourly> list, String sensorCode, String type, Double lower, Double upper) { |
| | | Supplier<Stream<HistoryHourly>> supplier = list::stream; |
| | | DoubleStream doubleStream = supplier.get() |
| | | .flatMapToDouble(v -> { |
| | | Map<String, Object> dataValue = JSONObject.parseObject(v.getValue(), Map.class); |
| | | Object sensorValue = dataValue.get(sensorCode); |
| | | |
| | | if (ObjectUtils.isEmpty(sensorValue)) { |
| | | return null; |
| | | } |
| | | |
| | | //数据有效性判断 |
| | | Object flag = dataValue.get(sensorCode + "-" + Constants.MARKER_BIT_KEY); |
| | | if (!Constants.MARKER_BIT_TRUE.equals(flag)) { |
| | | return null; |
| | | } |
| | | |
| | | double aDouble = Double.parseDouble(sensorValue.toString()); |
| | | |
| | | //剔除超出上下限的数据 |
| | | if (!ObjectUtils.isEmpty(lower)) { |
| | | if (aDouble < lower) { |
| | | return null; |
| | | } |
| | | } |
| | | if (!ObjectUtils.isEmpty(upper)) { |
| | | if (aDouble > upper) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | return DoubleStream.of(aDouble); |
| | | }); |
| | | Double result = null; |
| | | OptionalDouble optionalDouble = null; |
| | | if ("sum".equals(type)) { |
| | | result = doubleStream.sum(); |
| | | } else { |
| | | if ("min".equals(type)) { |
| | | optionalDouble = doubleStream.average(); |
| | | |
| | | } else if ("max".equals(type)) { |
| | | optionalDouble = doubleStream.min(); |
| | | |
| | | } else if ("avg".equals(type)) { |
| | | optionalDouble = doubleStream.max(); |
| | | } |
| | | |
| | | if (optionalDouble.isPresent()) { |
| | | result = optionalDouble.getAsDouble(); |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 多表查询,传入表名集合,以及条件wrapper,返回数据 |
| | | * @Param: [wrapper, tableNames] |