package com.casic.missiles.modular.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.casic.missiles.core.application.context.AbstractPermissionContext;
import com.casic.missiles.core.application.service.AbstractDictService;
import com.casic.missiles.core.datascope.DataScope;
import com.casic.missiles.model.response.ResponseData;
import com.casic.missiles.modular.system.constant.DeviceConst;
import com.casic.missiles.modular.system.dao.DataAnalysisMapper;
import com.casic.missiles.modular.system.dto.*;
import com.casic.missiles.modular.system.dto.ExcelModel.*;
import com.casic.missiles.modular.system.dto.statistic.DataH2s;
import com.casic.missiles.modular.system.enums.HarmfulGasBusiness;
import com.casic.missiles.modular.system.enums.TemphumBusiness;
import com.casic.missiles.modular.system.model.BusWellInfo;
import com.casic.missiles.modular.system.service.IBusDeviceSupportService;
import com.casic.missiles.modular.system.service.IDataAnalysisService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * 数据统计分析
 *
 * @author lwh
 */
@Slf4j
@Component
@DS("log")
public class IDataAnalysisServiceImpl extends ServiceImpl<DataAnalysisMapper, BusWellInfo> implements IDataAnalysisService {
    @Resource
    private AbstractDictService dictService;
    @Resource
    private IBusDeviceSupportService deviceSupportService;
    @Resource
    private AbstractPermissionContext permissionService;

    @Override
    public List<BusWellInfoDto> getAlarmWellList(DataScope dataScope, String keywords, String wellType, String deptid) {
        return this.baseMapper.getAlarmWellList(dataScope, keywords, wellType, StrUtil.isEmpty(deptid) ? -1 : Long.valueOf(deptid));
    }

    @Override
    public List<BusWellInfoDto> getNotAlarmWellList(DataScope dataScope, String keywords, String wellType, String deptid) {
        return this.baseMapper.getNotAlarmWellList(dataScope, keywords, wellType, StrUtil.isEmpty(deptid) ? -1 : Long.valueOf(deptid));
    }

    @Override
    public List<DeviceDataDto> getDeviceDatas(String devTypeName, String devcode, String status) {
        List<DeviceDataDto> deviceDataDtoList = new ArrayList<>();
        DeviceDataDto deviceDataDto = new DeviceDataDto();
        DeviceData deviceData;
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        switch (devTypeName) {
            case "井盖状态监测仪":
                deviceData = this.baseMapper.wellcoverData(devcode);
                deviceDataDto.setTypeName("井盖状态");
                if (ObjectUtil.isNotEmpty(deviceData)) {
                    //井盖离线
                    if ("0".equals(status)) {
                        deviceDataDto.setValue("离线");
                        deviceDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                    } else if ("心跳".equals(deviceData.getData())) {
                        deviceDataDto.setValue("井盖状态正常");
                        deviceDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                    } else {
                        deviceDataDto.setValue(deviceData.getData());
                        deviceDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                    }
                } else {
                    deviceDataDto.setValue("无数据");
                }
                deviceDataDtoList.add(deviceDataDto);
                break;

            case "液位监测仪":
                deviceData = this.baseMapper.liquidData(devcode);
                if (ObjectUtil.isNotEmpty(deviceData)) {
                    deviceDataDto.setTypeName("井内水位");
                    deviceDataDto.setValue(deviceData.getData() + "m");
                    deviceDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                    deviceDataDtoList.add(deviceDataDto);
                }
                break;

            case "有害气体监测仪":
                deviceData = this.baseMapper.harmfulgasData(devcode);
                if (ObjectUtil.isNotEmpty(deviceData)) {
                    String[] harmfulGasDatas = deviceData.getData().split(",");
                    HarmfulGasBusiness[] harmfulGasBusinesses = HarmfulGasBusiness.values();
                    for (int i = 0; i < harmfulGasDatas.length; i++) {
                        deviceDataDto = new DeviceDataDto();
                        deviceDataDto.setTypeName(harmfulGasBusinesses[i].getDesc());
                        deviceDataDto.setValue(harmfulGasDatas[i] + harmfulGasBusinesses[i].getUnit());
                        deviceDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                        deviceDataDtoList.add((DeviceDataDto) deviceDataDto.clone());
                    }
                }
                break;
            case "燃气智能监测终端":
                deviceData = this.baseMapper.gasData(devcode);
                if (ObjectUtil.isNotEmpty(deviceData)) {
                    deviceDataDto.setTypeName("燃气浓度");
                    deviceDataDto.setValue(deviceData.getData() + "%LEL");
                    deviceDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                    deviceDataDtoList.add(deviceDataDto);
                }
                break;

            case "温湿度监测仪":
                deviceData = this.baseMapper.temphumData(devcode);
                if (ObjectUtil.isNotEmpty(deviceData)) {
                    String[] temphumDatas = deviceData.getData().split(",");
                    TemphumBusiness[] temphumBusinesses = TemphumBusiness.values();
                    for (int i = 0; i < temphumDatas.length; i++) {
                        deviceDataDto = new DeviceDataDto();
                        deviceDataDto.setTypeName(temphumBusinesses[i].getDesc());
                        deviceDataDto.setValue(temphumDatas[i] + temphumBusinesses[i].getUnit());
                        deviceDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                        deviceDataDtoList.add((DeviceDataDto) deviceDataDto.clone());
                    }
                }
                break;

            case "开挖监测仪":
                deviceData = this.baseMapper.digData(devcode);
                if (ObjectUtil.isNotEmpty(deviceData)) {
                    deviceDataDto.setTypeName("开挖状态");
                    deviceDataDto.setValue(deviceData.getData() + "mg");
                    deviceDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                    deviceDataDtoList.add(deviceDataDto);
                }
                break;

            case "噪声记录仪":
                deviceData = this.baseMapper.noiseData(devcode);
                if (ObjectUtil.isNotEmpty(deviceData)) {
                    deviceDataDto.setTypeName("管线噪声");
                    deviceDataDto.setValue(deviceData.getData() + "mg");
                    deviceDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                    deviceDataDtoList.add(deviceDataDto);
                }
                break;
            case "井盖定位监测仪":
                deviceData = this.baseMapper.wellcoverlocaData(devcode);
                if (ObjectUtil.isNotEmpty(deviceData)) {
                    deviceDataDto.setTypeName("井盖位置");
                    deviceDataDto.setValue(deviceData.getData());
                    deviceDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                    deviceDataDtoList.add(deviceDataDto);
                }
                break;

            case "燃气智能监测终端(一体化)":
                deviceData = this.baseMapper.lgGasData(devcode);
                deviceDataDto.setTypeName("燃气浓度");
                if (ObjectUtil.isNotEmpty(deviceData)) {

                    Double dValue = Double.parseDouble(deviceData.getData());
                    if (dValue <= 20.0) {
                        deviceDataDto.setValue(deviceData.getData() + "%LEL");
                    } else {
                        DecimalFormat df = new DecimalFormat("0.00");
                        dValue = dValue / 20.0;
                        deviceDataDto.setValue(df.format(dValue) + "%VOL");
                    }
                    deviceDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                } else {
                    deviceDataDto.setValue("无数据");
                }
                deviceDataDtoList.add(deviceDataDto);

                deviceData = this.baseMapper.lgLiquidData(devcode);
                deviceDataDto.setTypeName("井内水位");
                if (ObjectUtil.isNotEmpty(deviceData)) {

                    deviceDataDto.setValue(deviceData.getData() + "m");
                    deviceDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                } else {
                    deviceDataDto.setValue("无数据");
                }
                deviceDataDtoList.add((DeviceDataDto) deviceDataDto.clone());
                break;
            case "管盯":
                deviceData = this.baseMapper.tubeData(devcode);
                if (ObjectUtil.isNotEmpty(deviceData)) {
                    deviceDataDto.setTypeName("燃气泄漏");
                    deviceDataDto.setValue(deviceData.getData() + "%LEL");
                    deviceDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                    deviceDataDtoList.add(deviceDataDto);
                }
                break;
            case "硫化氢泄漏检测仪":
                deviceData = this.baseMapper.H2sData(devcode);
                if (ObjectUtil.isNotEmpty(deviceData)) {
                    deviceDataDto.setTypeName("H2S浓度");
                    deviceDataDto.setValue(deviceData.getData() + "%LEL");
                    deviceDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                    deviceDataDtoList.add(deviceDataDto);
                }
                break;
            case "可燃气体":
                deviceData = this.baseMapper.gasDector(devcode);
                if (ObjectUtil.isNotEmpty(deviceData)) {
                    deviceDataDto.setTypeName("液化气浓度");
                    deviceDataDto.setValue(deviceData.getData() + "%LEL");
                    deviceDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                    deviceDataDtoList.add(deviceDataDto);
                }

                break;
            case "消防栓防盗水监测仪":
                deviceData = this.baseMapper.hydrantData(devcode);
                if (ObjectUtil.isNotEmpty(deviceData)) {
                    deviceDataDto.setTypeName("消防栓状态");
                    deviceDataDto.setValue(deviceData.getData());
                    deviceDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                    deviceDataDtoList.add(deviceDataDto);
                }
                break;

            default://未知设备
                log.info("未知设备！");
                break;
        }
        return deviceDataDtoList;
    }

    @Override
    public BfztCountDataDto getBfztCountData(List<Long> deptIds, Boolean isApp) {
        return this.baseMapper.bfztCountData(deptIds, isApp);
    }

    @Override
    public List<WatchDataDto> getWatchDatas(Long id) {

        List<DeviceInWellInfo> deviceInWellInfos = deviceSupportService.getDeviceInWellInfo(id);
        List<WatchDataDto> watchDataDtos = new ArrayList<>();

        for (DeviceInWellInfo deviceInWellInfo : deviceInWellInfos) {
            WatchDataDto watchDataDto = new WatchDataDto();
            watchDataDto.setDeviceId(deviceInWellInfo.getDeviceId());
            watchDataDto.setDevcode(deviceInWellInfo.getDevcode());
            watchDataDto.setInstallDate(deviceInWellInfo.getInstallDate());
            watchDataDto.setOnlineState(deviceInWellInfo.getOnlineState());
            watchDataDto.setOnlineStateName(dictService.getDictNameByCode("onlineStatus", deviceInWellInfo.getOnlineState()));

            DeviceData deviceData;
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            switch (deviceInWellInfo.getTypeName()) {
                case "井盖状态监测仪":
                    deviceData = this.baseMapper.wellcoverData(deviceInWellInfo.getDevcode());
                    watchDataDto.setWatchType("井盖状态");
                    watchDataDto.setType("1");
                    if (ObjectUtil.isNotEmpty(deviceData)) {
                        //井盖离线
                        if ("0".equals(watchDataDto.getOnlineState())) {
                            watchDataDto.setValue("离线");
                            watchDataDto.setData("离线");
                            watchDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                        } else if ("心跳".equals(deviceData.getData())) {
                            watchDataDto.setValue("井盖状态正常");
                            watchDataDto.setData("井盖状态正常");
                            watchDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                        } else {
                            watchDataDto.setValue(deviceData.getData());
                            watchDataDto.setData(deviceData.getData());
                            watchDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                        }
                    } else {
                        watchDataDto.setValue("无数据");
                        watchDataDto.setData("无数据");
                    }
                    watchDataDtos.add(watchDataDto);
                    break;

                case "液位监测仪":
                    deviceData = this.baseMapper.liquidData(deviceInWellInfo.getDevcode());
                    watchDataDto.setWatchType("井内水位");
                    if (ObjectUtil.isNotEmpty(deviceData)) {
                        watchDataDto.setType("2");

                        watchDataDto.setValue(deviceData.getData() + "m");
                        watchDataDto.setData(deviceData.getData() + "m");
                        watchDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                        watchDataDtos.add(watchDataDto);
                    } else {
                        watchDataDto.setValue("无数据");
                        watchDataDto.setData("无数据");
                    }
                    break;

                case "有害气体监测仪":
                    deviceData = this.baseMapper.harmfulgasData(deviceInWellInfo.getDevcode());
                    if (ObjectUtil.isNotEmpty(deviceData)) {
                        String[] harmfulGasDatas = deviceData.getData().split(",");
                        HarmfulGasBusiness[] harmfulGasBusinesses = HarmfulGasBusiness.values();

                        for (int i = 0; i < harmfulGasDatas.length; i++) {
                            watchDataDto.setWatchType(harmfulGasBusinesses[i].getDesc());
                            watchDataDto.setType("3");
                            watchDataDto.setValue(harmfulGasDatas[i] + harmfulGasBusinesses[i].getUnit());
                            watchDataDto.setData(harmfulGasDatas[i] + harmfulGasBusinesses[i].getUnit());
                            watchDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                            watchDataDtos.add((WatchDataDto) watchDataDto.clone());
                        }
                    } else {
                        watchDataDto.setValue("无数据");
                        watchDataDto.setData("无数据");
                    }
                    break;

                case "燃气智能监测终端":
                    deviceData = this.baseMapper.gasData(deviceInWellInfo.getDevcode());
                    watchDataDto.setWatchType("燃气浓度");
                    if (ObjectUtil.isNotEmpty(deviceData)) {
                        watchDataDto.setType("4");
                        watchDataDto.setValue(deviceData.getData() + "%LEL");
                        watchDataDto.setData(deviceData.getData() + "%LEL");
                        watchDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                        watchDataDtos.add(watchDataDto);
                    } else {
                        watchDataDto.setValue("无数据");
                        watchDataDto.setData("无数据");
                    }
                    break;

                case "温湿度监测仪":
                    deviceData = this.baseMapper.temphumData(deviceInWellInfo.getDevcode());
                    if (ObjectUtil.isNotEmpty(deviceData)) {
                        String[] temphumDatas = deviceData.getData().split(",");
                        TemphumBusiness[] temphumBusinesses = TemphumBusiness.values();

                        for (int i = 0; i < temphumDatas.length; i++) {
                            watchDataDto.setWatchType(temphumBusinesses[i].getDesc());
                            watchDataDto.setType("5");
                            watchDataDto.setValue(temphumDatas[i] + temphumBusinesses[i].getUnit());
                            watchDataDto.setData(temphumDatas[i] + temphumBusinesses[i].getUnit());
                            watchDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                            watchDataDtos.add((WatchDataDto) watchDataDto.clone());
                        }
                    } else {
                        watchDataDto.setValue("无数据");
                        watchDataDto.setData("无数据");
                    }
                    break;

                case "开挖监测仪":
                    deviceData = this.baseMapper.digData(deviceInWellInfo.getDevcode());
                    watchDataDto.setWatchType("开挖状态");
                    if (ObjectUtil.isNotEmpty(deviceData)) {
                        watchDataDto.setType("6");
                        watchDataDto.setValue(deviceData.getData() + "mg");
                        watchDataDto.setData(deviceData.getData() + "mg");
                        watchDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                        watchDataDtos.add(watchDataDto);
                    } else {
                        watchDataDto.setValue("无数据");
                        watchDataDto.setData("无数据");
                    }
                    break;

                case "噪声记录仪":
                    deviceData = this.baseMapper.noiseData(deviceInWellInfo.getDevcode());
                    watchDataDto.setWatchType("管线噪声");
                    if (ObjectUtil.isNotEmpty(deviceData)) {
                        watchDataDto.setType("8");
                        watchDataDto.setValue(deviceData.getData() + "mg");
                        watchDataDto.setData(deviceData.getData() + "mg");
                        watchDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                        watchDataDtos.add(watchDataDto);
                    } else {
                        watchDataDto.setValue("无数据");
                        watchDataDto.setData("无数据");
                    }
                    break;

                case "井盖定位监测仪":
                    deviceData = this.baseMapper.wellcoverlocaData(deviceInWellInfo.getDevcode());
                    watchDataDto.setWatchType("井盖位置");
                    if (ObjectUtil.isNotEmpty(deviceData)) {
                        watchDataDto.setType("7");

                        watchDataDto.setValue(deviceData.getData());
                        watchDataDto.setData(deviceData.getData());
                        watchDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                        watchDataDtos.add(watchDataDto);
                    } else {
                        watchDataDto.setValue("无数据");
                        watchDataDto.setData("无数据");
                    }
                    break;

                case "燃气智能监测终端(一体化)":
                    deviceData = this.baseMapper.lgGasData(deviceInWellInfo.getDevcode());
                    watchDataDto.setWatchType("燃气浓度");
                    watchDataDto.setType("10");
                    if (ObjectUtil.isNotEmpty(deviceData)) {

                        Double dValue = Double.parseDouble(deviceData.getData());
                        if (dValue <= 20.0) {
                            watchDataDto.setValue(deviceData.getData() + "%LEL");
                            watchDataDto.setData(deviceData.getData() + "%LEL");
                        } else {
                            DecimalFormat df = new DecimalFormat("0.00");
                            dValue = dValue / 20.0;
                            watchDataDto.setValue(df.format(dValue) + "%VOL");
                            watchDataDto.setData(df.format(dValue) + "%VOL");
                        }
                        watchDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                    } else {
                        watchDataDto.setValue("无数据");
                        watchDataDto.setData("无数据");
                    }
                    watchDataDtos.add((WatchDataDto) watchDataDto.clone());

                    deviceData = this.baseMapper.lgLiquidData(deviceInWellInfo.getDevcode());
                    watchDataDto.setWatchType("井内水位");
                    watchDataDto.setType("10");
                    if (ObjectUtil.isNotEmpty(deviceData)) {
                        watchDataDto.setValue(deviceData.getData() + "m");
                        watchDataDto.setData(deviceData.getData() + "m");
                        watchDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                    } else {
                        watchDataDto.setValue("无数据");
                        watchDataDto.setData("无数据");
                    }
                    watchDataDtos.add((WatchDataDto) watchDataDto.clone());
                    break;
                case "管盯":
                    deviceData = this.baseMapper.tubeData(deviceInWellInfo.getDevcode());
                    watchDataDto.setWatchType("燃气泄漏");
                    if (ObjectUtil.isNotEmpty(deviceData)) {
                        watchDataDto.setType("12");
                        watchDataDto.setValue(deviceData.getData() + "%LEL");
                        watchDataDto.setData(deviceData.getData() + "%LEL");
                        watchDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                        watchDataDtos.add(watchDataDto);
                    } else {
                        watchDataDto.setValue("无数据");
                        watchDataDto.setData("无数据");
                    }
                    break;
                case "硫化氢泄漏检测仪":
                    deviceData = this.baseMapper.H2sData(deviceInWellInfo.getDevcode());
                    if (ObjectUtil.isNotEmpty(deviceData)) {
                        watchDataDto.setWatchType("H2S浓度");
                        watchDataDto.setType("14");
                        watchDataDto.setValue(deviceData.getData() + "%LEL");
                        watchDataDto.setData(deviceData.getData() + "%LEL");
                        watchDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                        watchDataDtos.add(watchDataDto);
                    } else {
                        watchDataDto.setValue("无数据");
                        watchDataDto.setData("无数据");
                    }
                    break;
                case "可燃气体":
                    deviceData = this.baseMapper.gasDector(deviceInWellInfo.getDevcode());
                    watchDataDto.setWatchType("液化气浓度");
                    if (ObjectUtil.isNotEmpty(deviceData)) {
                        watchDataDto.setType("101");
                        watchDataDto.setValue(deviceData.getData() + "%LEL");
                        watchDataDto.setData(deviceData.getData() + "%LEL");
                        watchDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                        watchDataDtos.add(watchDataDto);
                    } else {
                        watchDataDto.setValue("无数据");
                        watchDataDto.setData("无数据");
                    }

                    break;
                case "消防栓防盗水监测仪":
                    deviceData = this.baseMapper.hydrantData(deviceInWellInfo.getDevcode());
                    watchDataDto.setWatchType("消防栓状态");
                    if (ObjectUtil.isNotEmpty(deviceData)) {
                        watchDataDto.setType("11");
                        watchDataDto.setValue(deviceData.getData());
                        watchDataDto.setData(deviceData.getData());
                        watchDataDto.setLogtime(sdf.format(deviceData.getLogtime()));

                    } else {
                        watchDataDto.setValue("无数据");
                        watchDataDto.setData("无数据");
                    }
                    watchDataDtos.add(watchDataDto);
                    break;

                default://未知设备
                    log.info("未知设备！");
                    break;
            }
        }
        return watchDataDtos;
    }

    //数据相关统计------------------------------------------------------------
    @Override
    public List<WellCoverDataDto> selectWellDataScopePage(DataScope dataScope, Page<WellCoverDataDto> page, String deptid, String beginTime, String endTime, String keywords) {
        return this.baseMapper.selectWellDataScopePage(dataScope, page, StrUtil.isEmpty(deptid) ? -1 : Long.valueOf(deptid), beginTime, endTime, keywords);
    }

    @Override
    public List<LiquidDataExcelDto> selectLiquidDataScopePage(DataScope dataScope, Page<LiquidDataExcelDto> page, String deptid, String beginTime, String endTime, String keywords) {
        return this.baseMapper.selectLiquidDataScopePage(dataScope, page, StrUtil.isEmpty(deptid) ? -1 : Long.valueOf(deptid), beginTime, endTime, keywords);
    }

    @Override
    public List<DataWellLiquidDto> getWellliquiddataList(DataScope dataScope, Page<DataWellLiquidDto> page, String deptid, String beginTime, String endTime, String keywords) {
        return this.baseMapper.getWellliquiddataList(dataScope, page, StrUtil.isEmpty(deptid) ? -1 : Long.valueOf(deptid), beginTime, endTime, keywords);
    }

    @Override
    public List<DeviceDataExcelDto> selectGasDataScopePage(DataScope dataScope, Page<DeviceDataExcelDto> page, String deptid, String beginTime, String endTime, String keywords) {
        List<DeviceDataExcelDto> deviceDataExcelDtoList = this.baseMapper.selectGasDataScopePage(dataScope, page, StrUtil.isEmpty(deptid) ? -1 : Long.valueOf(deptid), beginTime, endTime, keywords);
        return deviceDataExcelDtoList;
    }

    @Override
    public List<DeviceDataExcelDto> selectTubeDataScopePage(DataScope dataScope, Page<DeviceDataExcelDto> page, String deptid, String beginTime, String endTime, String keywords) {
        List<DeviceDataExcelDto> mapList = this.baseMapper.selectTubeDataScopePage(dataScope, page, StrUtil.isEmpty(deptid) ? -1 : Long.valueOf(deptid), beginTime, endTime, keywords);
        return mapList;
    }

    @Override
    public List<HarmfulDataDto> selectHarmfulDataScopePage(DataScope dataScope, Page<HarmfulDataDto> page, String deptid, String beginTime, String endTime, String keywords) {
        List<HarmfulDataDto> mapList = this.baseMapper.selectHarmfulDataScopePage(dataScope, page, StrUtil.isEmpty(deptid) ? -1 : Long.valueOf(deptid), beginTime, endTime, keywords);
        return mapList;
    }

    @Override
    public List<TemhumDataDto> selectTempDataScopePage(DataScope dataScope, Page<TemhumDataDto> page, String deptid, String beginTime, String endTime, String keywords) {
        List<TemhumDataDto> mapList = this.baseMapper.selectTempDataScopePage(dataScope, page, StrUtil.isEmpty(deptid) ? -1 : Long.valueOf(deptid), beginTime, endTime, keywords);
        return mapList;
    }


    @Override
    public List<TemppressDataDto> selectTempPressDataScopePage(DataScope dataScope, Page<TemppressDataDto> page, String deptid, String beginTime, String endTime, String keywords) {
        List<TemppressDataDto> mapList = this.baseMapper.selectTempPressDataScopePage(dataScope, page, StrUtil.isEmpty(deptid) ? -1 : Long.valueOf(deptid), beginTime, endTime, keywords);
        return mapList;
    }

    @Override
    public List<DigNoiseDataDto> selectDigDataScopePage(DataScope dataScope, Page<DigNoiseDataDto> page, String deptid, String beginTime, String endTime, String keywords) {
        List<DigNoiseDataDto> mapList = this.baseMapper.selectDigDataScopePage(dataScope, page, StrUtil.isEmpty(deptid) ? -1 : Long.valueOf(deptid), beginTime, endTime, keywords);
        return mapList;
    }

    @Override
    public List<WellAppointDataDto> selectWellLocDataScopePage(DataScope dataScope, Page<WellAppointDataDto> page, String deptid, String beginTime, String endTime, String keywords) {
        List<WellAppointDataDto> mapList = this.baseMapper.selectWellLocDataScopePage(dataScope, page, StrUtil.isEmpty(deptid) ? -1 : Long.valueOf(deptid), beginTime, endTime, keywords);
        return mapList;
    }

    @Override
    public List<DigNoiseDataDto> selectNoiseDataScopePage(DataScope dataScope, Page<DigNoiseDataDto> page, String deptid, String beginTime, String endTime, String keywords) {
        List<DigNoiseDataDto> mapList = this.baseMapper.selectNoiseDataScopePage(dataScope, page, StrUtil.isEmpty(deptid) ? -1 : Long.valueOf(deptid), beginTime, endTime, keywords);
        return mapList;
    }

    @Override
    public List<AlarmDeptStaticsDto> alarmsStaticsByDept(Long deviceType, String beginTime, String endTime, DataScope dataScope, Long deptId) {
        List<Long> deptIds = permissionService.getDeptService().getIdsByPid(deptId, DeviceConst.DEPT_TYPE_ORG);
        return this.baseMapper.alarmsStaticsByDept(deviceType, beginTime, endTime, dataScope, deptId, CollectionUtil.isEmpty(deptIds));
    }

    @Override
    public List<GasLiquidDataExcelDto> selectGasLiquidDataScopePage(DataScope dataScope, Page<GasLiquidDataExcelDto> page, String sensorType, String deptid, String beginTime, String endTime, String keywords) {
        List<Long> deptIds = null;
        if (StrUtil.isNotEmpty(deptid)) {
            deptIds = permissionService.getDeptService().getIdsByPid(deptid, null);
            deptIds.add(Long.valueOf(deptid));
        }
        List<GasLiquidDataExcelDto> deviceDataExcelDtoList = new ArrayList<>();
        if ("liquid".equals(sensorType)) {
            deviceDataExcelDtoList = this.baseMapper.selectGasLiquidDataLiquidScopePage(dataScope, page, deptIds, beginTime, endTime, keywords);
        }
        if ("gas".equals(sensorType)) {
            deviceDataExcelDtoList = this.baseMapper.selectGasLiquidDataGasScopePage(dataScope, page, deptIds, beginTime, endTime, keywords);
        }
        deviceDataExcelDtoList.forEach(deviceDataExcelDto -> {
                    deviceDataExcelDto.setDeptName(permissionService.getDeptService().getDeptName(Convert.toLong(deviceDataExcelDto.getDeptid())));
//                    setCompany(map);
                    if ("liquid".equals(sensorType)) {
                        float liquiddata = Float.valueOf(StrUtil.isEmpty(deviceDataExcelDto.getData()) ? "0" : deviceDataExcelDto.getData());
                        if (liquiddata <= 0) {
                            deviceDataExcelDto.setData("正常水位");
                        }
                    }
                }
        );
        return deviceDataExcelDtoList;
    }

    @Override
    public List<Map<String, Object>> selectDataList(DataScope dataScope, String wellCode, String watchType, String beginTime, String endTime) {
        if (StrUtil.isAllEmpty(beginTime, endTime)) {
            Calendar calendar = Calendar.getInstance();
            endTime = DateUtil.format(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
            calendar.add(Calendar.DATE, -7);
            beginTime = DateUtil.format(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
        }
        List<Map<String, Object>> list = new ArrayList<>();
        switch (watchType) {
            case "liquid":
                list = this.baseMapper.selectLiquidDataList(dataScope, wellCode, beginTime, endTime);
                break;
            case "gas":
                list = this.baseMapper.selectGasDataList(dataScope, wellCode, beginTime, endTime);
                break;
            case "temp":
                list = this.baseMapper.selectTempDataList(dataScope, wellCode, beginTime, endTime);
                break;
            case "harmful":
                list = this.baseMapper.selectHarmfulDataList(dataScope, wellCode, beginTime, endTime);
                break;
            default:
                break;
        }
        for (Map<String, Object> map : list) {
            map.put("deptName", permissionService.getDeptService().getDeptName(Long.valueOf(map.get("deptid").toString())));
            setCompany(map);
        }
        return list;
    }

    /**
     * 查询公司名称、ID
     *
     * @param dataMap
     */
    private void setCompany(Map<String, Object> dataMap) {
        if (ObjectUtil.isNotEmpty(dataMap.get("deptid"))) {
            List<Long> parentDepts = permissionService.getDeptService().getParentListById(Convert.toLong(dataMap.get("deptid")));
            if (CollectionUtil.isNotEmpty(parentDepts)) {
                Long companyId = parentDepts.get(parentDepts.size() - 1);
                dataMap.put("companyId", companyId);
                dataMap.put("companyName", permissionService.getDeptService().getDeptName(companyId));
            }
        }
    }

    public ResponseData getHydrantlist(DataScope dataScope, Page<HydrantDataDto> page, String deptid, String beginTime, String endTime, String keywords) {
        ResponseData responseData = new ResponseData();
        try {
            List<HydrantDataDto> hydrantDataDtoList = this.baseMapper.getHydrantlist(dataScope, page, StrUtil.isEmpty(deptid) ? -1 : Long.valueOf(deptid), beginTime, endTime, keywords);
            hydrantDataDtoList.forEach(gasDectorDataDto -> {
                gasDectorDataDto.setDeptName(permissionService.getDeptService().getDeptName(gasDectorDataDto.getDeptid()));
            });
            page.setRecords(hydrantDataDtoList);
            responseData.setData(page);
            responseData.setCode(200);
            responseData.setMessage("查询成功");
        } catch (DataAccessException dex) {
            log.error("主题：消防栓数据库异常，关键字{}，开始时间{}，结束时间{}，部门编号{}，异常信息",
                    keywords, beginTime, endTime, deptid, dex);
            responseData.setCode(500);
            responseData.setMessage("数据库查询异常");
        }
        return responseData;
    }

    public ResponseData getH2slist(DataScope dataScope, Page<H2SDataDto> page, String keywords, String beginTime, String endTime, String deptid) {
        ResponseData responseData = new ResponseData();
        try {
            List<H2SDataDto> h2sDataDtoList = this.baseMapper.selectH2sListDataScopePage(dataScope, page, keywords, beginTime, endTime, StrUtil.isEmpty(deptid) ? -1 : Long.valueOf(deptid));
            h2sDataDtoList.forEach(
                    h2sDataDto -> {
                        h2sDataDto.setDeptName(permissionService.getDeptService().getDeptName(h2sDataDto.getDeptid()));
                        h2sDataDto.setStrength(!StringUtils.isEmpty(h2sDataDto.getStrength()) &&
                                Float.valueOf(h2sDataDto.getStrength()) >= 100 ? "浓度溢出" : h2sDataDto.getStrength());
                    }
            );
            page.setRecords(h2sDataDtoList);
            responseData.setData(page);
            responseData.setCode(200);
            responseData.setMessage("查询成功");
        } catch (DataAccessException dex) {
            log.error("主题：硫化氢数据库异常，关键字{}，开始时间{}，结束时间{}，部门编号{}，异常信息{}",
                    keywords, beginTime, endTime, deptid, dex);
            responseData.setCode(500);
            responseData.setMessage("数据库查询异常");
        }
        return responseData;
    }
}
