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

import cn.hutool.json.JSONUtil;
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.domain.overhaulplan.OverhaulPlan;
import com.cpp.web.service.datafactory.ParsingInterface;
import com.cpp.web.service.overhaulplan.OverhaulPlanService;
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.Date;
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 OverhaulPlanService overhaulPlanService;

    @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");
        } 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;
        private List<RestrictedPlan> restrictedPlans;


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

            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);
                }
            }
            if (this.restrictedPlans != null && restrictedPlans.size() > 0) {
                List<OverhaulPlan> overhaulPlans = new ArrayList<>();
                for (RestrictedPlan restrictedPlan : restrictedPlans) {
                    OverhaulPlan overhaulPlan = new OverhaulPlan();
                    overhaulPlan.setDescription(restrictedPlan.getDescribe());
                    overhaulPlan.setEndTime(restrictedPlan.getEndTime());
                    overhaulPlan.setStartTime(restrictedPlan.startTime);
                    overhaulPlan.setOverhaulCapacity(restrictedPlan.getCapacity());
                    overhaulPlan.setStationCode(stationCode);
                    overhaulPlan.setName(restrictedPlan.getPlanName());
                    overhaulPlans.add(overhaulPlan);
                }

                overhaulPlanService.saveBatch(overhaulPlans);
            }

        }
    }


    @Data
    class ChannelError {
        private String msg;
    }

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

    @Data
    class RestrictedPlan {
        private String planName;
        private Date startTime;
        private Date endTime;
        private BigDecimal capacity;
        private String describe;
    }
}
