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


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.WindTowerInfo;
import com.cpp.web.domain.station.WindTowerStatusData;
import com.cpp.web.mapper.datafactory.ParsingCftMapper;
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.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("cftParsing")
@AllArgsConstructor
@Slf4j
@Transactional
public class ParsingCftServiceImpl extends ServiceImpl<ParsingCftMapper, ParsingCft> implements ParsingCftService, ParsingInterface {


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

    private List<WindTowerInfo> windTowerInfos = new ArrayList<>();

    private final WindTowerStatusDataService windTowerStatusDataService;

    private final WindTowerInfoService windTowerInfoService;


    @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()) {
                    List<WindTowerInfo> windTowerInfoList = this.windTowerInfos.stream().filter(w -> w.getStationCode().equals(stationCode)).collect(Collectors.toList());
                    ParsingCft parsingCft = this.parsingCfts.get(0);
                    if (windTowerInfoList.size() > 0) {
                        Long eqId = windTowerInfoList.get(0).getId();//通常每个场站只上报一个测风塔数据，所有配置多个的情况下只取第一个存储
                        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.gettInst(), fileContent));
                                    windTowerStatusData.setPa(parsingValue(parsingCft.getPaInst(), fileContent));
                                    windTowerStatusData.setRh(parsingValue(parsingCft.getRhInst(), fileContent));

                                    windTowerStatusData.setWs10(parsingValue(parsingCft.getWsInst10(), fileContent));
                                    windTowerStatusData.setWs30(parsingValue(parsingCft.getWsInst30(), fileContent));
                                    windTowerStatusData.setWs50(parsingValue(parsingCft.getWsInst50(), fileContent));
                                    windTowerStatusData.setWs60(parsingValue(parsingCft.getWsInst60(), fileContent));
                                    windTowerStatusData.setWs70(parsingValue(parsingCft.getWsInst70(), fileContent));
                                    windTowerStatusData.setWs80(parsingValue(parsingCft.getWsInst80(), fileContent));
                                    windTowerStatusData.setWs90(parsingValue(parsingCft.getWsInst90(), fileContent));
                                    windTowerStatusData.setWs100(parsingValue(parsingCft.getWsInst100(), fileContent));
                                    windTowerStatusData.setWsHubHeight(parsingValue(parsingCft.getWsInstHubHeight(), fileContent));

                                    windTowerStatusData.setWd10(parsingValue(parsingCft.getWdInst10(), fileContent));
                                    windTowerStatusData.setWd30(parsingValue(parsingCft.getWdInst30(), fileContent));
                                    windTowerStatusData.setWd50(parsingValue(parsingCft.getWdInst50(), fileContent));
                                    windTowerStatusData.setWd60(parsingValue(parsingCft.getWdInst60(), fileContent));
                                    windTowerStatusData.setWd70(parsingValue(parsingCft.getWdInst70(), fileContent));
                                    windTowerStatusData.setWd80(parsingValue(parsingCft.getWdInst80(), fileContent));
                                    windTowerStatusData.setWd90(parsingValue(parsingCft.getWdInst90(), fileContent));
                                    windTowerStatusData.setWd100(parsingValue(parsingCft.getWdInst100(), fileContent));
                                    windTowerStatusData.setWdHubHeight(parsingValue(parsingCft.getWdInstHubHeight(), fileContent));
                                    windTowerStatusData.setStationCode(parsingCft.getStationCode());

                                    windTowerStatusDataService.save(windTowerStatusData);
                                    parsingResultDto.setStatus("success");
                                    log.info("解析CFT文件：{} 成功！ O(∩_∩)O", file.getName());
                                } else {
                                    parsingResultDto.setMessage("解析测风塔文件时间错误");
                                    log.error("解析CFT文件时间错误");
                                }
                            } else {
                                parsingResultDto.setMessage("测风塔暂无多行解析方式");
                                log.error("cft暂无多行解析方式");
                            }

                        }

                }
            } catch (Exception e) {
                parsingResultDto.setMessage("解析测风塔数据失败！");
                log.error("解析测风塔数据失败！ /(ㄒoㄒ)/~~", e);
            } finally {
                return parsingResultDto;
            }
        }
    }

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


}
