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.ParsingQxz;
import com.cpp.web.domain.datafactory.dto.ParsingResultDto;
import com.cpp.web.domain.datafactory.enums.FileTypeEnum;
import com.cpp.web.domain.station.InverterStatusData;
import com.cpp.web.domain.station.WeatherStationStatusData;
import com.cpp.web.mapper.datafactory.ParsingQxzMapper;
import com.cpp.web.service.datafactory.DataStore;
import com.cpp.web.service.datafactory.ParsingInterface;
import com.cpp.web.service.datafactory.ParsingQxzService;
import com.cpp.web.service.station.WeatherStationInfoService;
import com.cpp.web.service.station.WeatherStationStatusDataService;
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("qxzParsing")
@Slf4j
@RequiredArgsConstructor
@Transactional
public class ParsingQxzServiceImpl extends ServiceImpl<ParsingQxzMapper, ParsingQxz> implements ParsingQxzService, ParsingInterface {

    private final WeatherStationStatusDataService weatherStationStatusDataService;

    private List<ParsingQxz> parsingQxzs = new ArrayList<>();

    @Override
    public boolean save(BaseParsing parsingInfo) {
        return save((ParsingQxz) 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.qxz.name());
        try {
            if (!this.parsingQxzs.isEmpty()) {
                ParsingQxz parsingQxzInfo = this.parsingQxzs.get(0);
                List<String> fileContent = ParsingFileUtil.getFileContent(file);
                //判断文件名是否符合配置
                if (parsingQxzInfo.getDataType().equals(ParsingFieldUtil.SINGLE)) {
                    Date time = parsingTime(parsingQxzInfo.getTime(), fileContent);
                    if (time != null) {
                        WeatherStationStatusData w = new WeatherStationStatusData();
                        w.setTime(time);
                        w.setAirT(parsingValue(parsingQxzInfo.getAirT(), fileContent));
                        w.setCellT(parsingValue(parsingQxzInfo.getCellT(), fileContent));
                        w.setDiffuseR(parsingValue(parsingQxzInfo.getDiffuseR(), fileContent));
                        w.setDirectR(parsingValue(parsingQxzInfo.getDirectR(), fileContent));
                        w.setGlobalR(parsingValue(parsingQxzInfo.getGlobalR(), fileContent));
                        w.setP(parsingValue(parsingQxzInfo.getP(), fileContent));
                        w.setRh(parsingValue(parsingQxzInfo.getRh(), fileContent));
                        w.setWd(parsingValue(parsingQxzInfo.getWd(), fileContent));
                        w.setWs(parsingValue(parsingQxzInfo.getWs(), fileContent));
                        w.setStationCode(stationCode);
                        DataStore.getInstance().setData(Arrays.asList(w), stationCode);
                        weatherStationStatusDataService.remove(
                                new QueryWrapper<WeatherStationStatusData>()
                                        .eq("time", time)
                                        .eq("station_code", stationCode));
                        weatherStationStatusDataService.save(w);
                        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());
                }
            } 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.parsingQxzs = list();
        return true;
    }

}
