package com.cpp.web.service.datafactory.impl;

import cn.hutool.json.JSONUtil;
import com.cpp.web.domain.Alarm.AbnormalAlarm;
import com.cpp.web.domain.datafactory.BaseParsing;
import com.cpp.web.domain.datafactory.dto.ParsingResultDto;
import com.cpp.web.domain.datafactory.enums.FileTypeEnum;
import com.cpp.web.domain.enums.AlarmEnum;
import com.cpp.web.domain.enums.DataSourcesEnum;
import com.cpp.web.service.alarm.AbnormalAlarmService;
import com.cpp.web.service.datafactory.ParsingInterface;
import com.cpp.web.utils.LogUtil;
import com.cpp.web.utils.ParsingFileUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 气象站解析业务层实现类
 *
 * @author tl
 * @date 2022-05-11 18:07:03
 */
@Service("statusParsing")
@Slf4j
@AllArgsConstructor
@Transactional
public class ParsingStatusServiceImpl implements ParsingInterface {

    private final AbnormalAlarmService abnormalAlarmService;

    @Override
    public boolean save(BaseParsing parsingInfo) {
        return true;
    }

    @Override
    public boolean deleteById(Long id) {
        return true;
    }


    @Override
    public ParsingResultDto parsing(File file, String stationCode) {

        ParsingResultDto parsingResultDto = new ParsingResultDto();
        parsingResultDto.setFileName(file.getName());
        parsingResultDto.setFileType(FileTypeEnum.status.name());
        try {

            String json = ParsingFileUtil.getStr(file);
            StationStatusDto stationStatusDto = JSONUtil.parse(json).toBean(StationStatusDto.class);
            stationStatusDto.generateAbnormalAlarm(stationCode);


            log.info("解析RP文件：{} 成功！ O(∩_∩)O", file.getName());
            parsingResultDto.setStatus("success");
            parsingResultDto.setMessage("解析站端状态数据失败！");
        } catch (Exception e) {
            parsingResultDto.setMessage("解析站端状态数据失败！");
            log.error("解析站端状态数据失败！ /(ㄒoㄒ)/~~", e);
        } finally {
            return parsingResultDto;
        }
    }

    @Override
    public boolean activationParsingConf() {
        return true;
    }

    @Data
    class StationStatusDto {
        private List<ChannelError> channelErrors;
        private List<UploadFile> uploadFiles;
        private BigDecimal hardDiskCapacity;


        /**
         * 生成告警
         *
         * @return
         */
        private void generateAbnormalAlarm(String stationCode) {

            List<AbnormalAlarm> list = new ArrayList<>();
            if (this.channelErrors != null) {
                for (ChannelError channelError : this.channelErrors) {
                    LogUtil.info(DataSourcesEnum.E1, AlarmEnum.E1, channelError.getMsg(), stationCode);
                }
            }


            if (this.uploadFiles != null) {
                for (UploadFile uploadFile : this.uploadFiles) {
                    LogUtil.info(DataSourcesEnum.E1, AlarmEnum.E2, LogUtil.format("站端[{}]类型文件：【{}】未上报！", uploadFile.getType(), uploadFile.getName()), stationCode);
                }
            }

            if (this.hardDiskCapacity != null) {
                if (this.hardDiskCapacity.compareTo(new BigDecimal(95)) > -1) {
                    LogUtil.info(DataSourcesEnum.E1, AlarmEnum.E3, LogUtil.format("站端磁盘空间达到 {}%，请关注！", this.hardDiskCapacity), stationCode);
                }
            }

        }
    }


    @Data
    class ChannelError {
        private String msg;
    }

    @Data
    class UploadFile {
        private String type;
        private String name;
    }
}
