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


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cpp.web.domain.datafactory.BaseParsing;
import com.cpp.web.domain.datafactory.ParsingCft;
import com.cpp.web.domain.datafactory.dto.ParsingResultDto;
import com.cpp.web.domain.datafactory.enums.FileTypeEnum;
import com.cpp.web.domain.station.ForecastPowerUltraShortTermStation;
import com.cpp.web.domain.station.WindTowerStatusData;
import com.cpp.web.mapper.datafactory.ParsingCftMapper;
import com.cpp.web.service.datafactory.DataStore;
import com.cpp.web.service.datafactory.ParsingCftService;
import com.cpp.web.service.datafactory.ParsingInterface;
import com.cpp.web.service.station.WindTowerInfoService;
import com.cpp.web.service.station.WindTowerStatusDataService;
import com.cpp.web.utils.ParsingFieldUtil;
import com.cpp.web.utils.ParsingFileUtil;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 测风塔解析业务层实现类
 *
 * @author tl
 * @date 2022-05-11 18:07:03
 */
@Service("cftParsing")
@RequiredArgsConstructor
@Slf4j
@Transactional
public class ParsingCftServiceImpl extends ServiceImpl<ParsingCftMapper, ParsingCft> implements ParsingCftService, ParsingInterface {

    private List<ParsingCft> parsingCfts = new ArrayList<>();

    private final WindTowerStatusDataService windTowerStatusDataService;

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

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

    @Override
    public ParsingResultDto parsing(File file, String stationCode) {
        ParsingResultDto parsingResultDto = new ParsingResultDto();
        parsingResultDto.setFileName(file.getName());
        parsingResultDto.setFileType(FileTypeEnum.cft.name());
        try {
            if (!this.parsingCfts.isEmpty()) {
                ParsingCft parsingCft = this.parsingCfts.get(0);
                List<String> fileContent = ParsingFileUtil.getFileContent(file);
                //判断文件名是否符合配置
                if (parsingCft.getDataType().equals(ParsingFieldUtil.SINGLE)) {
                    Date time = parsingTime(parsingCft.getTime(), fileContent);
                    if (time != null) {
                        WindTowerStatusData windTowerStatusData = new WindTowerStatusData();
                        windTowerStatusData.setTime(time);
                        windTowerStatusData.setT(parsingValue(parsingCft.gett(), fileContent));
                        windTowerStatusData.setPa(parsingValue(parsingCft.getPa(), fileContent));
                        windTowerStatusData.setRh(parsingValue(parsingCft.getRh(), fileContent));
                        windTowerStatusData.setWs10(parsingValue(parsingCft.getWs10(), fileContent));
                        windTowerStatusData.setWs30(parsingValue(parsingCft.getWs30(), fileContent));
                        windTowerStatusData.setWs50(parsingValue(parsingCft.getWs50(), fileContent));
                        windTowerStatusData.setWs60(parsingValue(parsingCft.getWs60(), fileContent));
                        windTowerStatusData.setWs70(parsingValue(parsingCft.getWs70(), fileContent));
                        windTowerStatusData.setWs80(parsingValue(parsingCft.getWs80(), fileContent));
                        windTowerStatusData.setWs90(parsingValue(parsingCft.getWs90(), fileContent));
                        windTowerStatusData.setWs100(parsingValue(parsingCft.getWs100(), fileContent));
                        windTowerStatusData.setWsHubHeight(parsingValue(parsingCft.getWsHubHeight(), fileContent));
                        windTowerStatusData.setWd10(parsingValue(parsingCft.getWd10(), fileContent));
                        windTowerStatusData.setWd30(parsingValue(parsingCft.getWd30(), fileContent));
                        windTowerStatusData.setWd50(parsingValue(parsingCft.getWd50(), fileContent));
                        windTowerStatusData.setWd60(parsingValue(parsingCft.getWd60(), fileContent));
                        windTowerStatusData.setWd70(parsingValue(parsingCft.getWd70(), fileContent));
                        windTowerStatusData.setWd80(parsingValue(parsingCft.getWd80(), fileContent));
                        windTowerStatusData.setWd90(parsingValue(parsingCft.getWd90(), fileContent));
                        windTowerStatusData.setWd100(parsingValue(parsingCft.getWd100(), fileContent));
                        windTowerStatusData.setWdHubHeight(parsingValue(parsingCft.getWdHubHeight(), fileContent));
                        windTowerStatusData.setStationCode(stationCode);
                        DataStore.getInstance().setData(Arrays.asList(windTowerStatusData), stationCode);
                        windTowerStatusDataService.remove(
                                new QueryWrapper<WindTowerStatusData>()
                                        .eq("time", time)
                                        .eq("station_code", stationCode));
                        windTowerStatusDataService.save(windTowerStatusData);
                        parsingResultDto.setStatus("success");
                        log.info("[" + stationCode + "]解析测风塔文件：{} 成功！ O(∩_∩)O", file.getName());
                    } else {
                        parsingResultDto.setMessage("解析测风塔文件时间错误");
                        log.error("[" + stationCode + "]解析测风塔文件时间错误，fileName：{}", file.getName());
                    }
                } else {
                    parsingResultDto.setMessage("测风塔暂无多行解析方式");
                    log.error("[" + stationCode + "]测风塔暂无多行解析方式，fileName：{}", file.getName());
                }
            }
        } catch (Exception e) {
            parsingResultDto.setMessage("解析测风塔数据失败！");
            log.error("[" + stationCode + "]解析测风塔数据失败！ /(ㄒoㄒ)/~~，fileName：{}", file.getName(), e);
        } finally {
            return parsingResultDto;
        }
    }

    @Override
    public boolean activationParsingConf() {
        this.parsingCfts = list();
        return true;
    }


}
