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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.WeatherStationInfo;
import com.cpp.web.domain.station.WeatherStationStatusData;
import com.cpp.web.mapper.datafactory.ParsingQxzMapper;
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.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.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 气象站解析业务层实现类
 *
 * @author tl
 * @date 2022-05-11 18:07:03
 */
@Service("qxzParsing")
@Slf4j
@AllArgsConstructor
@Transactional
public class ParsingQxzServiceImpl extends ServiceImpl<ParsingQxzMapper, ParsingQxz> implements ParsingQxzService, ParsingInterface {

    private final WeatherStationStatusDataService weatherStationStatusDataService;

    private final WeatherStationInfoService weatherStationInfoService;

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

    private List<WeatherStationInfo> weatherStationInfos = 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 {

            List<ParsingQxz> parsingQxzInfos = selectByStationCode(stationCode);
            if (parsingQxzInfos.size() > 0) {
                List<WeatherStationInfo> weatherStationInfoList = weatherStationInfos.stream().filter(w->w.getStationCode().equals(stationCode)).collect(Collectors.toList());
                if (weatherStationInfoList.size() > 0) {
                    Long eqId = weatherStationInfoList.get(0).getId();
                    List<String> fileContent = ParsingFileUtil.getFileContent(file);
                    for (ParsingQxz parsingQxzInfo : parsingQxzInfos) {
                        //判断文件名是否符合配置
                            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.setDiffuseRDA(parsingValue(parsingQxzInfo.getDiffuseRDA(), fileContent));
                                    w.setDirectR(parsingValue(parsingQxzInfo.getDirectR(), fileContent));
                                    w.setDirectRDA(parsingValue(parsingQxzInfo.getDirectRDA(), fileContent));
                                    w.setGlobalR(parsingValue(parsingQxzInfo.getGlobalR(), fileContent));
                                    w.setGlobalRDA(parsingValue(parsingQxzInfo.getGlobalRDA(), fileContent));
                                    w.setHourDA(parsingValue(parsingQxzInfo.getHourDA(), fileContent));
                                    w.setObliqueR(parsingValue(parsingQxzInfo.getObliqueR(), fileContent));
                                    w.setObliqueRDA(parsingValue(parsingQxzInfo.getObliqueRDA(), 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.setStatus(parsingStatus(parsingQxzInfo.getStatus(), fileContent));
                                    w.setStationCode(parsingQxzInfo.getStationCode());
                                    w.setEquipmentId(eqId);
                                    weatherStationStatusDataService.save(w);
                                    parsingResultDto.setStatus("success");
                                    log.info("解析QXZ文件：{} 成功！ O(∩_∩)O", file.getName());
                                } else {
                                    parsingResultDto.setMessage("解析气象站文件时间错误");
                                    log.error("解析QXZ文件时间错误");
                                }
                            } else {
                                parsingResultDto.setMessage("气象站暂无多行解析方式");
                                log.error("qxz暂无多行解析方式");
                            }

                    }
                } else {
                    parsingResultDto.setMessage("气象站未配置作为解析使用设备");
                    log.error("qxz未配置作为解析使用设备");
                }
            }
        } catch (Exception e) {
            parsingResultDto.setMessage("解析气象站数据失败！");
            log.error("解析气象站数据失败！ /(ㄒoㄒ)/~~", e);
        } finally {
            return parsingResultDto;
        }
    }

    @Override
    public boolean activationParsingConf() {
        this.parsingQxzs = baseMapper.selectList(null);
        this.weatherStationInfos = weatherStationInfoService.list();
        return false;
    }

    @Override
    public List selectByStationCode(String stationCode) {
        return null;
    }

}
