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

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.casic.missiles.core.application.service.AbstractDictService;
import com.casic.missiles.core.datascope.DataScope;
import com.casic.missiles.modular.system.dao.DataAnalysisMapper;
import com.casic.missiles.modular.system.dto.*;
import com.casic.missiles.modular.system.enmus.HarmfulGasBusiness;
import com.casic.missiles.modular.system.enmus.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.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.List;

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

    @Override
    public List<BusWellInfoDto> getAlarmWellList(DataScope dataScope, String keywords, String wellType, String deptid) {
        return this.baseMapper.getAlarmWellList(dataScope, keywords, wellType, deptid);
    }

    @Override
    public List<BusWellInfoDto> getNotAlarmWellList(DataScope dataScope, String keywords, String wellType, String deptid) {
        return this.baseMapper.getNotAlarmWellList(dataScope, keywords, wellType, 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;

            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());
                    if (ObjectUtil.isNotEmpty(deviceData)) {
                        watchDataDto.setWatchType("井内水位");
                        watchDataDto.setType("2");

                        watchDataDto.setValue(deviceData.getData() + "m");
                        watchDataDto.setData(deviceData.getData() + "m");
                        watchDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                        watchDataDtos.add(watchDataDto);
                    }
                    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());
                        }
                    }
                    break;

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

                        watchDataDto.setValue(deviceData.getData() + "%LEL");
                        watchDataDto.setData(deviceData.getData() + "%LEL");
                        watchDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                        watchDataDtos.add(watchDataDto);
                    }
                    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());
                        }
                    }
                    break;

                case "开挖监测仪":
                    deviceData = this.baseMapper.digData(deviceInWellInfo.getDevcode());
                    if (ObjectUtil.isNotEmpty(deviceData)) {
                        watchDataDto.setWatchType("开挖状态");
                        watchDataDto.setType("6");

                        watchDataDto.setValue(deviceData.getData() + "mg");
                        watchDataDto.setData(deviceData.getData() + "mg");
                        watchDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                        watchDataDtos.add(watchDataDto);
                    }
                    break;

                case "噪声记录仪":
                    deviceData = this.baseMapper.noiseData(deviceInWellInfo.getDevcode());
                    if (ObjectUtil.isNotEmpty(deviceData)) {
                        watchDataDto.setWatchType("管线噪声");
                        watchDataDto.setType("8");

                        watchDataDto.setValue(deviceData.getData() + "mg");
                        watchDataDto.setData(deviceData.getData() + "mg");
                        watchDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                        watchDataDtos.add(watchDataDto);
                    }
                    break;

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

                        watchDataDto.setValue(deviceData.getData());
                        watchDataDto.setData(deviceData.getData());
                        watchDataDto.setLogtime(sdf.format(deviceData.getLogtime()));
                        watchDataDtos.add(watchDataDto);
                    }
                    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;

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

        return watchDataDtos;
    }

}
