jinpengyong
2023-10-30 0593108084bd368d512290053d81454df79e06e5
screen-api/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java
@@ -2,10 +2,12 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.moral.api.mapper.HistoryHourlyMapper;
import com.moral.api.mapper.OrganizationUnitAlarmMapper;
import com.moral.api.mapper.UnitConversionMapper;
@@ -57,11 +59,24 @@
    @Autowired
    HistoryHourlyMapper historyHourlyMapper;
    @Autowired
    HistoryFiveMinutelyMapper historyFiveMinutelyMapper;
    @Override
    public List<Device> getDevicesByMonitorPointId(Integer monitorPointId) {
        QueryWrapper<Device> wrapper = new QueryWrapper();
        wrapper.eq("monitor_point_id", monitorPointId);
        wrapper.eq("is_delete", Constants.NOT_DELETE);
        wrapper.orderByAsc("dev_num");
        return deviceMapper.selectList(wrapper);
    }
    @Override
    public List<Device> getDevicesByMonitorPointId(List<Integer> monitorPointId) {
        QueryWrapper<Device> wrapper = new QueryWrapper();
        wrapper.in("monitor_point_id", monitorPointId);
        wrapper.eq("is_delete", Constants.NOT_DELETE);
        wrapper.orderByAsc("dev_num");
        return deviceMapper.selectList(wrapper);
    }
@@ -191,6 +206,7 @@
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("mac", "name").in("mac", macs);
        queryWrapper.orderByAsc("dev_num","name","id");
        List<Device> devices = deviceMapper.selectList(queryWrapper);
        Map<String,Object> devicesInfo = new HashMap<>();
        for (Device device:devices) {
@@ -214,10 +230,11 @@
            Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_HH_EN);
            List<String> tableNames = MybatisPLUSUtils.getTableNamesByWrapper(startDate, endDate, SeparateTableType.MONTH);
            for (String mac:macs) {
                QueryWrapper<HistoryHourly> wrapper = new QueryWrapper<>();
                wrapper.eq("mac", mac);
                wrapper.between("time", startDate, endDate);
                List<HistoryHourly> historyHourlies = multiTableQuery(wrapper, tableNames);
                Map<String, Object> mapParams = new HashMap<>();
                mapParams.put("startDate",startDate);
                mapParams.put("endDate",endDate);
                mapParams.put("mac",mac);
                List<HistoryHourly> historyHourlies = multiTableQuery(mapParams, tableNames);
                historyHourlies = historyHourlies.stream().distinct().collect(Collectors.toList());
                List<HistoryHourly> distinctHistoryHourlies = new ArrayList<>();
                Map<String, Object> disMap = new HashMap<>();
@@ -232,8 +249,8 @@
                for (HistoryHourly historyHourly:distinctHistoryHourlies) {
                    Map<String,Object> historyHourlyMap = new HashMap<>();
                    historyHourlyMap.put("mac",mac);
                    JSONObject value = JSONObject.parseObject(historyHourly.getValue());
                    Double sensorValue = Double.parseDouble(value.get(sensorCode).toString());
                    JSONObject  value= JSONObject.parseObject(historyHourly.getValue());
                    Double sensorValue = Objects.nonNull(value)&&Objects.nonNull(value.get(sensorCode))?Double.parseDouble(value.get(sensorCode).toString()):0d;
                    historyHourlyMap.put(sensorCode,sensorValue);
                    Date time = historyHourly.getTime();
                    String timeStr = DateUtils.dateToDateString(time, DateUtils.yyyy_MM_dd_HH_EN);
@@ -290,7 +307,7 @@
                }
                middleDate = DateUtils.addDays(middleDate,1);
            }
        } else {
        } else if ("month".equals(type)){
            end = DateUtils.getDateAddMonth(endTime, 1);
            timeUnits = "monthly";
            dateFormat = "%Y-%m";
@@ -319,6 +336,45 @@
                    }
                }
                middleDate = DateUtils.addMonths(middleDate,1);
            }
        }else {
            Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_mm_ss_EN);
            Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_HH_mm_ss_EN);
            List<String> tableNames = MybatisPLUSUtils.getTableNamesByWrapper(startDate, endDate, SeparateTableType.MONTH);
            for (String mac:macs) {
                Map<String, Object> mapParams = new HashMap<>();
                mapParams.put("startDate",startDate);
                mapParams.put("endDate",endDate);
                mapParams.put("mac",mac);
                List<HistoryFiveMinutely> HistoryFiveMinutelys = FiveMinuteTableQuery(mapParams, tableNames);
                HistoryFiveMinutelys = HistoryFiveMinutelys.stream().distinct().collect(Collectors.toList());
                List<HistoryFiveMinutely> distinctHistoryHourlies = new ArrayList<>();
                Map<String, Object> disMap = new HashMap<>();
                for (HistoryFiveMinutely historyFiveMinutely:HistoryFiveMinutelys) {
                    Date time = historyFiveMinutely.getTime();
                    String timeStr = DateUtils.dateToDateString(time,DateUtils.yyyy_MM_dd_HH_mm_ss_EN);
                    if (!disMap.containsKey(timeStr)){
                        distinctHistoryHourlies.add(historyFiveMinutely);
                        disMap.put(timeStr,true);
                    }
                }
                for (HistoryFiveMinutely historyFiveMinutely:distinctHistoryHourlies) {
                    Map<String,Object> historyHourlyMap = new HashMap<>();
                    historyHourlyMap.put("mac",mac);
                    JSONObject value = JSONObject.parseObject(historyFiveMinutely.getValue());
                    if (value.get(sensorCode)==null){
                        historyHourlyMap.put(sensorCode,0.0);
                    }else {
                        Double sensorValue = Double.parseDouble(value.get(sensorCode).toString());
                        historyHourlyMap.put(sensorCode,sensorValue);
                    }
//                    Double sensorValue = Double.parseDouble(value.get(sensorCode).toString());
//                    historyHourlyMap.put(sensorCode,sensorValue);
                    Date time = historyFiveMinutely.getTime();
                    String timeStr = DateUtils.dateToDateString(time, DateUtils.yyyy_MM_dd_HH_mm_ss_EN);
                    historyHourlyMap.put("time",timeStr);
                    list.add(historyHourlyMap);
                }
            }
        }
        for (Map map:list) {
@@ -440,6 +496,40 @@
        return device;
    }
    /**
     * 模糊查询
     *
     * @param mac
     * @return
     */
    @Override
    public List<Device> getFuzzySearch(String mac) {
        LambdaQueryWrapper<Device> wrapper = new LambdaQueryWrapper<>();
        if (ObjectUtils.isEmpty(mac)){
            return null;
        }
        wrapper.eq(Device::getIsDelete,Constants.NOT_DELETE);
        wrapper.like(Device::getMac,mac);
        List<Device> devices = deviceMapper.selectList(wrapper);
        return devices;
    }
    /**
     * 根据组织查询设备
     *
     * @param id
     * @return
     */
    @Override
    public List<Device> getOrganizationDevice(Integer id) {
        LambdaQueryWrapper<Device> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Device::getOrganizationId,id);
        wrapper.eq(Device::getIsDelete,Constants.NOT_DELETE);
        wrapper.orderByAsc(Device::getCreateTime);
        List<Device> devices = deviceMapper.selectList(wrapper);
        return devices;
    }
    private Device getDeviceUnitAlramInforByMacFromDb(String mac){
        QueryWrapper<Device> wrapper = new QueryWrapper<>();
        wrapper.eq("mac",mac);
@@ -486,14 +576,23 @@
     * @Author: 陈凯裕
     * @Date: 2021/9/23
     */
    private List<HistoryHourly> multiTableQuery(QueryWrapper<HistoryHourly> wrapper, List<String> tableNames) {
    private List<HistoryHourly> multiTableQuery(Map<String, Object> params, List<String> tableNames) {
        List<HistoryHourly> result = new ArrayList<>();
        for (String tableName : tableNames) {
            MybatisPlusConfig.tableName.set(tableName);
            List<HistoryHourly> datas = historyHourlyMapper.selectList(wrapper);
            params.put("table",tableName);
            List<HistoryHourly> datas = historyHourlyMapper.listResult(params);
            result.addAll(datas);
        }
        MybatisPlusConfig.tableName.remove();
        return result;
    }
    private List<HistoryFiveMinutely> FiveMinuteTableQuery(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;
    }