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.ParsingFj;
import com.cpp.web.domain.datafactory.dto.ParsingResultDto;
import com.cpp.web.domain.datafactory.enums.FileTypeEnum;
import com.cpp.web.domain.station.WindTurbineInfo;
import com.cpp.web.domain.station.WindTurbineStatusData;
import com.cpp.web.mapper.datafactory.ParsingFjMapper;
import com.cpp.web.service.datafactory.ParsingFjService;
import com.cpp.web.service.datafactory.ParsingInterface;
import com.cpp.web.service.station.WindTurbineInfoService;
import com.cpp.web.service.station.WindTurbineStatusDataService;
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("fjParsing")
@AllArgsConstructor
@Slf4j
@Transactional
public class ParsingFjServiceImpl extends ServiceImpl<ParsingFjMapper, ParsingFj> implements ParsingFjService, ParsingInterface {

    private final WindTurbineStatusDataService windTurbineStatusDataService;

    private final WindTurbineInfoService windTurbineInfoService;

    private List<WindTurbineInfo> windTurbineInfos = new ArrayList<>();

    private List<ParsingFj> parsingFjs = new ArrayList<>();

    @Override
    public boolean save(BaseParsing parsingInfo) {
        return save((ParsingFj) 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.fj.name());

        try {


            List<ParsingFj> parsingFjInfos = selectByStationCode(stationCode);
            if (parsingFjInfos.size() > 0) {

                List<String> fileContent = ParsingFileUtil.getFileContent(file);

                for (ParsingFj parsingFjInfo : parsingFjInfos) {
                    if (parsingFjInfo.getDataType().equals(ParsingFieldUtil.SINGLE)) {

                        String fjName = parsingName(parsingFjInfo.getEquipmentName(), fileContent);

                        List<WindTurbineInfo> filterWindTurbineInfos = windTurbineInfos.stream().filter(i -> i.getName().equals(fjName)).collect(Collectors.toList());

                        if (filterWindTurbineInfos.size() > 0) {
                            Long eqId = filterWindTurbineInfos.get(0).getId();

                            Date time = parsingTime(parsingFjInfo.getTime(), fileContent);
                            if (time != null) {
                                WindTurbineStatusData windTurbineStatusData = new WindTurbineStatusData();
                                windTurbineStatusData.setTime(time);
                                windTurbineStatusData.setEquipmentId(eqId);
                                windTurbineStatusData.setActivePower(parsingValue(parsingFjInfo.getActivePower(), fileContent));
                                windTurbineStatusData.setCumulativeGeneratedEnergy(parsingValue(parsingFjInfo.getCumulativeGeneratedEnergy(), fileContent));
                                windTurbineStatusData.setDayGridConnectedHours(parsingValue(parsingFjInfo.getDayGridConnectedHours(), fileContent));
                                windTurbineStatusData.setWindWheelRatedSpeed(parsingValue(parsingFjInfo.getWindWheelRatedSpeed(), fileContent));
                                windTurbineStatusData.setDayElectricQuantity(parsingValue(parsingFjInfo.getDayElectricQuantity(), fileContent));
                                windTurbineStatusData.setElectricalCurrent(parsingValue(parsingFjInfo.getElectricalCurrent(), fileContent));
                                windTurbineStatusData.setPitchAngle(parsingValue(parsingFjInfo.getPitchAngle(), fileContent));
                                windTurbineStatusData.setPowerFactor(parsingValue(parsingFjInfo.getPowerFactor(), fileContent));
                                windTurbineStatusData.setReactivePower(parsingValue(parsingFjInfo.getReactivePower(), fileContent));
                                windTurbineStatusData.setVoltage(parsingValue(parsingFjInfo.getVoltage(), fileContent));
                                windTurbineStatusData.setWd(parsingValue(parsingFjInfo.getWd(), fileContent));
                                windTurbineStatusData.setWs(parsingValue(parsingFjInfo.getWs(), fileContent));
                                windTurbineStatusData.setT(parsingValue(parsingFjInfo.getT(), fileContent));

                                windTurbineStatusData.setStatus(parsingStatus(parsingFjInfo.getStatus(), fileContent));
                                windTurbineStatusData.setStationCode(parsingFjInfo.getStationCode());

                                windTurbineStatusDataService.save(windTurbineStatusData);
                                log.info("解析FJ文件：" + file.getName() + " 成功！ O(∩_∩)O");
                                parsingResultDto.setStatus("success");
                            } else {
                                parsingResultDto.setMessage("解析风机文件时间错误");
                                log.error("解析FJ文件时间错误");
                            }
                        } else {
                            parsingResultDto.setMessage("风机在本地无配置无法解析");
                            log.error("fj {} 在本地无配置无法解析", fjName);
                        }
                    } else {
                        parsingResultDto.setMessage("风机暂无多行解析方式");
                        log.error("fj暂无多行解析方式");
                    }

                }
            }

        } catch (Exception e) {
            parsingResultDto.setMessage("解析风机数据错误！");
            log.error("解析风机数据错误", e);
        } finally {
            return parsingResultDto;
        }

    }

    @Override
    public boolean activationParsingConf() {
        this.parsingFjs = baseMapper.selectList(null);
        this.windTurbineInfos = windTurbineInfoService.list();
        return false;
    }

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