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 {
                List<ParsingCft> parsingCfts = selectByStationCode(stationCode);
                if (!parsingCfts.isEmpty()) {
                    List<WindTowerInfo> windTowerInfoList = this.windTowerInfos.stream().filter(w -> w.getStationCode().equals(stationCode)).collect(Collectors.toList());
                    ParsingCft parsingCft = 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.setTInst(parsingValue(parsingCft.gettInst(), fileContent));
                                    windTowerStatusData.setPaInst(parsingValue(parsingCft.getPaInst(), fileContent));
                                    windTowerStatusData.setRhInst(parsingValue(parsingCft.getRhInst(), fileContent));
                                    windTowerStatusData.setAirDensity(parsingValue(parsingCft.getAirDensity(), fileContent));

                                    windTowerStatusData.setWsInst10(parsingValue(parsingCft.getWsInst10(), fileContent));
                                    windTowerStatusData.setWsInst30(parsingValue(parsingCft.getWsInst30(), fileContent));
                                    windTowerStatusData.setWsInst50(parsingValue(parsingCft.getWsInst50(), fileContent));
                                    windTowerStatusData.setWsInst60(parsingValue(parsingCft.getWsInst60(), fileContent));
                                    windTowerStatusData.setWsInst70(parsingValue(parsingCft.getWsInst70(), fileContent));
                                    windTowerStatusData.setWsInst80(parsingValue(parsingCft.getWsInst80(), fileContent));
                                    windTowerStatusData.setWsInst90(parsingValue(parsingCft.getWsInst90(), fileContent));
                                    windTowerStatusData.setWsInst100(parsingValue(parsingCft.getWsInst100(), fileContent));
                                    windTowerStatusData.setWsInst110(parsingValue(parsingCft.getWsInst110(), fileContent));
                                    windTowerStatusData.setWsInstHubHeight(parsingValue(parsingCft.getWsInstHubHeight(), fileContent));

                                    windTowerStatusData.setWdInst10(parsingValue(parsingCft.getWdInst10(), fileContent));
                                    windTowerStatusData.setWdInst30(parsingValue(parsingCft.getWdInst30(), fileContent));
                                    windTowerStatusData.setWdInst50(parsingValue(parsingCft.getWdInst50(), fileContent));
                                    windTowerStatusData.setWdInst60(parsingValue(parsingCft.getWdInst60(), fileContent));
                                    windTowerStatusData.setWdInst70(parsingValue(parsingCft.getWdInst70(), fileContent));
                                    windTowerStatusData.setWdInst80(parsingValue(parsingCft.getWdInst80(), fileContent));
                                    windTowerStatusData.setWdInst90(parsingValue(parsingCft.getWdInst90(), fileContent));
                                    windTowerStatusData.setWdInst100(parsingValue(parsingCft.getWdInst100(), fileContent));
                                    windTowerStatusData.setWdInst110(parsingValue(parsingCft.getWdInst110(), fileContent));
                                    windTowerStatusData.setWdInstHubHeight(parsingValue(parsingCft.getWdInstHubHeight(), fileContent));

                                    windTowerStatusData.setStatus(parsingStatus(parsingCft.getStatus(), fileContent));
                                    windTowerStatusData.setStationCode(parsingCft.getStationCode());

                                    windTowerStatusData.setEquipmentId(eqId);

                                    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 = baseMapper.selectList(null);
        this.windTowerInfos = windTowerInfoService.list(null);
        return false;
    }


    @Override
    public List<ParsingCft> selectByStationCode(String stationCode) {
        return this.parsingCfts.stream().filter(p -> p.getStationCode().equals(stationCode)).collect(Collectors.toList());
    }
}
