package com.jiayue.ipp.idp.service.impl.an;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiayue.ipp.common.data.entity.WeatherStationInfo;
import com.jiayue.ipp.common.data.entity.WeatherStationStatusData;
import com.jiayue.ipp.common.data.entity.an.ParsingInfo;
import com.jiayue.ipp.common.data.entity.an.ParsingQxzInfo;
import com.jiayue.ipp.common.data.entity.an.ParsingUrl;
import com.jiayue.ipp.common.data.enums.FileTypeEnum;
import com.jiayue.ipp.idp.dto.ParsingConstant;
import com.jiayue.ipp.idp.mapper.an.ParsingQxzInfoMapper;
import com.jiayue.ipp.idp.service.WeatherStationInfoService;
import com.jiayue.ipp.idp.service.WeatherStationStatusDataService;
import com.jiayue.ipp.idp.service.an.ParsingInterface;
import com.jiayue.ipp.idp.service.an.ParsingQxzInfoService;
import com.jiayue.ipp.idp.util.FieldUtil;
import com.jiayue.ipp.idp.util.FileUtil;
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.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * 气象站解析业务层实现类
 *
 * @author tl
 * @date 2022-05-11 18:07:03
 */
@Service
@Slf4j
@AllArgsConstructor
@Transactional
public class ParsingQxzInfoServiceImpl extends ServiceImpl<ParsingQxzInfoMapper, ParsingQxzInfo> implements ParsingQxzInfoService, ParsingInterface {


    private final Long intervalTime = 300000L;

    private final WeatherStationStatusDataService weatherStationStatusDataService;

    private final WeatherStationInfoService weatherStationInfoService;

    @Override
    public boolean save(ParsingInfo parsingInfo) {
        return save((ParsingQxzInfo) parsingInfo);
    }

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

    @Override
    public String parsing(File file, ParsingUrl parsingUrl) {
        String parsingStatus = ParsingConstant.FAIL;

        try {

            List<ParsingQxzInfo> parsingQxzInfos = baseMapper.selectList(Wrappers.lambdaQuery(ParsingQxzInfo.class).like(ParsingInfo::getStationCode, parsingUrl.getStationCode()));
            if (parsingQxzInfos.size() > 0) {
                List<WeatherStationInfo> weatherStationInfoList = weatherStationInfoService.list(Wrappers.lambdaQuery(WeatherStationInfo.class)
                        .eq(WeatherStationInfo::getStationCode, parsingUrl.getStationCode()));
                if (weatherStationInfoList.size() > 0) {
                    List<String> fileContent = FileUtil.getFileContent(file);
                    for (ParsingQxzInfo parsingQxzInfo : parsingQxzInfos) {
                        //判断文件名是否符合配置
                        if (file.getName().indexOf(parsingQxzInfo.getFileName()) >= 0) {
                            if (parsingQxzInfo.getDataType().equals(FieldUtil.SINGLE)) {
                                Long time = parsingTime(parsingQxzInfo.getTime(), fileContent);
                                if (time != 0L) {
                                    WeatherStationStatusData w = new WeatherStationStatusData();
                                    w.setTime(new Date(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(weatherStationInfoList.get(0).getEquipmentNo());
                                    weatherStationStatusDataService.save(w);
                                    parsingStatus = FileTypeEnum.QXZ.name();
                                    log.info("解析QXZ文件：{} 成功！ O(∩_∩)O", file.getName());
                                } else {
                                    parsingStatus += "解析QXZ文件时间错误";
                                    log.error("解析QXZ文件时间错误");
                                }
                            } else {
                                parsingStatus += "qxz暂无多行解析方式";
                                log.info("qxz暂无多行解析方式");
                            }
                        }
                    }
                } else {
                    parsingStatus += "qxz未配置作为解析使用设备";
                    log.info("qxz未配置作为解析使用设备");
                }
            }
        } catch (Exception e) {
            parsingStatus += "解析气象站数据失败";
            log.error("解析气象站数据失败！ /(ㄒoㄒ)/~~", e);
        } finally {
            return parsingStatus;
        }
    }
}
