jinpengyong
2023-10-20 17c774c9c13febdcff654ffd6bbabd313c37a3ee
screen-api/src/main/java/com/moral/api/service/impl/HistoryFiveMinutelyServiceImpl.java
@@ -2,12 +2,10 @@
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.core.conditions.segments.NormalSegmentList;
import com.moral.api.config.mybatis.MybatisPlusConfig;
import com.moral.api.entity.*;
import com.moral.api.mapper.DeviceMapper;
import com.moral.api.mapper.HistoryFiveMinutelyMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.mapper.HistoryHourlyMapper;
@@ -24,7 +22,9 @@
import com.moral.constant.SeparateTableType;
import com.moral.util.DateUtils;
import com.moral.util.MybatisPLUSUtils;
import com.moral.util.RegionCodeUtils;
import io.lettuce.core.GeoCoordinates;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@@ -41,6 +41,7 @@
 * @since 2021-07-15
 */
@Service
@Slf4j
public class HistoryFiveMinutelyServiceImpl extends ServiceImpl<HistoryFiveMinutelyMapper, HistoryFiveMinutely> implements HistoryFiveMinutelyService {
    @Autowired
@@ -54,15 +55,30 @@
    HistoryHourlyMapper historyHourlyMapper;
    @Autowired
    DeviceService deviceService;
    @Autowired
    DeviceMapper deviceMapper;
    @Override
    public List<DeviceAndFiveMinuteDataDTO> queryDeviceAndFiveMinuteData(QueryDeviceAndFiveMinuteDataForm form) {
    public List<Map<String, Object>> queryDeviceAndFiveMinuteData(QueryDeviceAndFiveMinuteDataForm form) {
        //取参
        String times = DateUtils.dateToDateString(new Date(),DateUtils.yyyyMM_EN);
        Integer organizationId = form.getOrganizationId();
        Integer regionCode = form.getRegionCode();
        String sensorCode = form.getSensorCode();
        String type = "$."+ sensorCode;
        String region = null;
        String endHourlyTime = null;
        if (regionCode != null && organizationId!=24) {
            region = RegionCodeUtils.regionCodeConvertToName(regionCode);
        }
        if (form.getChooseTime().equals("true")) {
            endHourlyTime = form.getTime();
            Date time = DateUtils.convertDate(endHourlyTime);
            times = DateUtils.dateToDateString(time,DateUtils.yyyyMM_EN);
        }
        List<Map<String, Object>> listAll = deviceMapper.deviceList(organizationId,regionCode,region,type,sensorCode,times,endHourlyTime);
        //查询组织在对应地区下的站点以及设备
        List<MonitorPoint> monitorPoints = monitorPointService.queryByOrgIdAndRegionCode(new MonitorPointQueryForm(organizationId, regionCode));
        /*List<MonitorPoint> monitorPoints = monitorPointService.queryByOrgIdAndRegionCode(new MonitorPointQueryForm(organizationId, regionCode));
        List<Device> devices = new ArrayList<>();
        for (MonitorPoint monitorPoint : monitorPoints) {
            List<Device> monitorPointDevices = monitorPoint.getDevices();
@@ -74,10 +90,11 @@
                }
            }
        }
        }*/
        //查询所有设备对应的数据
        /*List<Device> devices = new ArrayList<>();
        List<DeviceAndFiveMinuteDataDTO> dtos = new ArrayList<>();
        if (form.getChooseTime().equals("true")){
        if (form.getChooseTime().equals("true")) {
            String time = form.getTime();
            String timeUnits = DateUtils.stringToDateString(time, "yyyy-MM-dd HH:mm", DateUtils.yyyyMM_EN);
            for (Device device : devices) {
@@ -97,10 +114,12 @@
            }
            return dtos;
        }
        //List<Map<String, Object>> sensorValuess = (List<Map<String, Object>>) redisTemplate.opsForValue().multiGet(Arrays.asList("data_five_minutes:p5dnd7a0245400","data_five_minutes:p5dnd7a0245412"));
        for (Device device : devices) {
            DeviceAndFiveMinuteDataDTO dto = new DeviceAndFiveMinuteDataDTO();
            String mac = device.getMac();
            Map<String, Object> sensorValues = queryLastDataByMac(mac);
            String dbDataStr = JSON.toJSONString(sensorValues);
            sensorValues = JSON.parseObject(dbDataStr, HashMap.class);
            Map<String, Object> sensorValue = new HashMap<>();
@@ -111,8 +130,8 @@
            dto.setDevice(device);
            dto.setSensorValue(sensorValue);
            dtos.add(dto);
        }
        return dtos;
        }*/
        return listAll;
    }
    @Override
@@ -122,14 +141,12 @@
        //如果没有数据从数据库查询
        if (sensorValues != null)
            return sensorValues;
        QueryWrapper<HistoryFiveMinutely> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mac", mac);
        queryWrapper.orderByDesc("time");
        queryWrapper.last("limit 0,1");
        Map<String, Object> mapParams = new HashMap<>();
        mapParams.put("mac",mac);
        //获取当月的表名
        Date date = new Date();
        List<String> tableNames = MybatisPLUSUtils.getTableNamesByWrapper(date, date, SeparateTableType.MONTH);
        List<HistoryFiveMinutely> datas = multiTableQuery(queryWrapper, tableNames);
        List<HistoryFiveMinutely> datas = multiTableQuery(mapParams, tableNames);
        if (ObjectUtils.isEmpty(datas))
            return null;
        HistoryFiveMinutely historyFiveMinutely = datas.get(0);
@@ -147,19 +164,18 @@
//        List<WindData> windData = historyFiveMinutelyMapper.getAreaWindData(params);
//        return getWindData(windData);
        String chooseTime = params.get("chooseTime").toString();
        List<WindData> windData=null;
        if (chooseTime.equals("true")){
        List<WindData> windData = null;
        if (chooseTime.equals("true")) {
            String time = params.get("time").toString();
            String timeUnits = DateUtils.stringToDateString(time, "yyyy-MM-dd HH:mm", DateUtils.yyyyMM_EN);
            params.put("timeUnits", timeUnits);
            windData = historyHourlyMapper.getArea(params);
        }else {
        } else {
            String timeUnits = DateUtils.dateToDateString(new Date(), DateUtils.yyyyMM_EN);
            params.put("timeUnits", timeUnits);
            //风场数据
            windData = historyFiveMinutelyMapper.getAreaWindData(params);
        }
        return getWindData(windData);
    }
@@ -170,24 +186,27 @@
//        Map<String, Object> datas = queryLastDataByMac(mac);
        String chooseTime = params.get("chooseTime").toString();
        String mac = params.get("mac").toString();
        Map<String, Object> datas=null;
        if (chooseTime.equals("true")){
        Map<String, Object> datas = null;
        if (chooseTime.equals("true")) {
            String time = params.get("time").toString();
            String timeUnits = DateUtils.stringToDateString(time, "yyyy-MM-dd HH:mm", DateUtils.yyyyMM_EN);
            String dbDataStr = historyHourlyMapper.queryLastHourlyMac(mac, time, timeUnits);
            if (dbDataStr==null){
            if (dbDataStr == null) {
                return null;
            }
//            String dbDataStr = historyFiveMinutelyMapper.queryLastFiveMinutely(mac, time, timeUnits);
            datas = JSON.parseObject(dbDataStr, HashMap.class);
            datas.put("dataTime",time);
        }else {
            datas.put("dataTime", time);
        } else {
            datas = queryLastDataByMac(mac);
            if (ObjectUtils.isEmpty(datas)){
                return null;
            }
        }
        //构建返回对象
        Map<String, Object> result = new HashMap<>();
        //创建返回因子code集合
        List<String> sensorCodes = Arrays.asList("a34004", "a34002", "a21005", "a21004", "a21026", "a05024", "a99054","a01008","a01007");
        List<String> sensorCodes = Arrays.asList("a34004", "a34002", "a21005", "a21004", "a21026", "a05024", "a99054", "a01008", "a01007");
        //获取设备信息
        Device device = deviceService.getDeviceUnitAlramInforByMac(mac);
        //转换数据单位和名称
@@ -198,25 +217,30 @@
            if (dataO != null) {
                String data = String.valueOf(dataO);
                //转换单位
                if (!sensor.getShowUnitKey().equals(sensor.getUnitKey()) )
                if (!sensor.getShowUnitKey().equals(sensor.getUnitKey()))
                    data = UnitConvertUtils.calculate(data, sensor.getFormula());
                //拼接单位
                data = data +" "+ sensor.getShowUnit();
                data = data + " " + sensor.getShowUnit();
                result.put(sensor.getCode(), data);
            }else{
                result.put(sensor.getCode(),"-");
            } else {
                if (sensor.getCode().equals("a01008") ||sensor.getCode().equals("a01007")){
                    result.put(sensor.getCode(), "-");
                }else {
                    result.put(sensor.getCode(), "0"+" "+sensor.getShowUnit());
                }
            }
        }
        //添加设备名称
        result.put("name",device.getName());
        result.put("name", device.getName());
        //添加时间
        result.put("dataTime",datas.get("dataTime"));
        result.put("dataTime", datas.get("dataTime"));
        //排序
        return orderSixParam(result);
    }
    //对六参以及时间进行排序
    private Map<String,Object> orderSixParam(Map<String,Object> data){
    private Map<String, Object> orderSixParam(Map<String, Object> data) {
        LinkedHashMap result = new LinkedHashMap();
        Object PM2_5 = data.get("a34004");
        Object PM10 = data.get("a34002");
@@ -228,26 +252,26 @@
        Object a01007 = data.get("a01007");
        Object name = data.get("name");
        Object time = data.get("dataTime");
        if(name!=null)
            result.put("名称",name);
        if(PM2_5!=null)
            result.put("PM2.5",PM2_5);
        if(PM10!=null)
            result.put("PM10",PM10);
        if(SO2!=null)
            result.put("二氧化硫(SO₂)",SO2);
        if(NO2!=null)
            result.put("二氧化氮(NO₂)",NO2);
        if(CO!=null)
            result.put("一氧化碳(CO)",CO);
        if(O3!=null)
            result.put("臭氧(O₃)",O3);
        if(time!=null)
            result.put("时间",time);
        if (a01008!=null)
            result.put("风向",a01008);
        if (a01007!=null)
            result.put("风速",a01007);
        if (name != null)
            result.put("名称", name);
        if (PM2_5 != null)
            result.put("PM2.5", PM2_5);
        if (PM10 != null)
            result.put("PM10", PM10);
        if (SO2 != null)
            result.put("二氧化硫(SO₂)", SO2);
        if (NO2 != null)
            result.put("二氧化氮(NO₂)", NO2);
        if (CO != null)
            result.put("一氧化碳(CO)", CO);
        if (O3 != null)
            result.put("臭氧(O₃)", O3);
        if (time != null)
            result.put("时间", time);
        if (a01008 != null)
            result.put("风向", a01008);
        if (a01007 != null)
            result.put("风速", a01007);
        return result;
    }
@@ -348,32 +372,31 @@
        laLaMap.put("minLo", lomi);
        laLaMap.put("maxLa", lama);
        laLaMap.put("minLa", lami);
        Double lo1 = lomi - length * 0.00001141;
//        Double lo1 = lomi - length * 0.000001141 * 2;
       //Double lo1 = lomi - length * 0.000001141 * 2;
        Double lo2 = loma + length * 0.00001141;
//        Double lo2 = loma + length * 0.000001141 * 2;
//        Double la2 = lami - length * 0.000000899 * 2;
       //Double lo2 = loma + length * 0.000001141 * 2;
       //Double la2 = lami - length * 0.000000899 * 2;
        Double la2 = lami - length * 0.00000899;
//        Double la1 = lama + length * 0.000000899 * 2;
        //Double la1 = lama + length * 0.000000899 * 2;
        Double la1 = lama + length * 0.00000899;
        double dx = 0.00001141 * perdlen;
        double dy = 0.00000899 * perdlen;
//        double dx = 0.000001141 * perdlen * 2;
//        double dy = 0.000000899 * perdlen * 2;
       //double dx = 0.000001141 * perdlen * 2;
       //double dy = 0.000000899 * perdlen * 2;
        int nx = (int) Math.floor((lo2 - lo1) / dx);
        int ny = (int) Math.floor((la1 - la2) / dy);
        List<Double> uList = new ArrayList<>();
        List<Double> vList = new ArrayList<>();
        int nums = Objects.nonNull(nx*ny)?nx*ny+100:100;
        List<Double> uList = new ArrayList<>(nums);
        List<Double> vList = new ArrayList<>(nums);
        int x;
        int y;
        for (int i = 0; i < mapList.size(); i++) {
            Double lo = (Double) mapList.get(i).get("lo");
            Double la = (Double) mapList.get(i).get("la");
            x = (int) Math.floor((lo - lo1) / dx);
@@ -423,6 +446,8 @@
                }
            }
        }
        String uData = "\"" + "data" + "\"" + ": " + uList;
        String vData = "\"" + "data" + "\"" + ": " + vList;
@@ -440,6 +465,7 @@
        String s1 = "[" + "{" + header1 + ", " + uData + "}" + ", " + "{" + header2 + ", " + vData + "}" + "]";
        //System.out.println(s1);
        JSONArray jsonArray = JSONArray.parseArray(s1);
        list.add(jsonArray);
@@ -457,11 +483,12 @@
     * @Date: 2021/9/23
     */
    public List<HistoryFiveMinutely> getValueByMacAndTime(String mac, Date startDate, Date endDate) {
        QueryWrapper<HistoryFiveMinutely> wrapper = new QueryWrapper<>();
        wrapper.eq("mac", mac);
        wrapper.between("time", startDate, endDate);
        Map<String, Object> mapParams = new HashMap<>();
        mapParams.put("startDate",startDate);
        mapParams.put("endDate",endDate);
        mapParams.put("mac",mac);
        List<String> tableNames = MybatisPLUSUtils.getTableNamesByWrapper(startDate, endDate, SeparateTableType.MONTH);
        List<HistoryFiveMinutely> datas = multiTableQuery(wrapper, tableNames);
        List<HistoryFiveMinutely> datas = multiTableQueryResult(mapParams, tableNames);
        return datas;
    }
@@ -473,16 +500,22 @@
     * @Author: 陈凯裕
     * @Date: 2021/9/23
     */
    private List<HistoryFiveMinutely> multiTableQuery(QueryWrapper<HistoryFiveMinutely> wrapper, List<String> tableNames) {
    private List<HistoryFiveMinutely> multiTableQuery(Map<String, Object> params, List<String> tableNames) {
        List<HistoryFiveMinutely> result = new ArrayList<>();
        for (String tableName : tableNames) {
            MybatisPlusConfig.tableName.set(tableName);
            List<HistoryFiveMinutely> datas = historyFiveMinutelyMapper.selectList(wrapper);
            params.put("table",tableName);
            List<HistoryFiveMinutely> datas = historyFiveMinutelyMapper.listResultOne(params);
            result.addAll(datas);
        }
        MybatisPlusConfig.tableName.remove();
        return result;
    }
    private List<HistoryFiveMinutely> multiTableQueryResult(Map<String, Object> params, List<String> tableNames) {
        List<HistoryFiveMinutely> result = new ArrayList<>();
        for (String tableName : tableNames) {
            params.put("table",tableName);
            List<HistoryFiveMinutely> datas = historyFiveMinutelyMapper.listResult(params);
            result.addAll(datas);
        }
        return result;
    }
}