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.InverterInfo;
import com.cpp.web.domain.station.InverterStatusData;
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 {

            if (!this.parsingFjs.isEmpty()) {
                List<String> fileContent = ParsingFileUtil.getFileContent(file);
                ParsingFj parsingFjInfo = this.parsingFjs.get(0);

                if (parsingFjInfo.getDataType().equals(ParsingFieldUtil.SINGLE)) {
                    List<String> fjNames = parsingNames(parsingFjInfo.getEquipmentName(), fileContent);
                    Date time = parsingTime(parsingFjInfo.getTime(), fileContent);
                    if (fjNames.size() > 0) {
                        if (time != null){
                            int number = Integer.parseInt(parsingFjInfo.getEquipmentName().split("-")[0]);
                            String replace1 = parsingFjInfo.getActivePower().split("<=>")[0] + "<=>";
                            List<WindTurbineInfo> windTurbineInfoList = windTurbineInfos.stream().filter(i -> i.getStationCode().equals(stationCode)).collect(Collectors.toList());
                            for (String fjName : fjNames) {

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

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

                                    String replace2 = number + "<=>";
                                    WindTurbineStatusData windTurbineStatusData = new WindTurbineStatusData();
                                    windTurbineStatusData.setTime(time);
                                    windTurbineStatusData.setEquipmentId(eqId);
                                    windTurbineStatusData.setActivePower(parsingValue(parsingFjInfo.getActivePower().replace(replace1, replace2), fileContent));
                                    windTurbineStatusData.setCumulativeGeneratedEnergy(parsingValue(parsingFjInfo.getCumulativeGeneratedEnergy().replace(replace1, replace2), fileContent));
                                    windTurbineStatusData.setDayGridConnectedHours(parsingValue(parsingFjInfo.getDayGridConnectedHours().replace(replace1, replace2), fileContent));
                                    windTurbineStatusData.setWindWheelRatedSpeed(parsingValue(parsingFjInfo.getWindWheelRatedSpeed().replace(replace1, replace2), fileContent));
                                    windTurbineStatusData.setDayElectricQuantity(parsingValue(parsingFjInfo.getDayElectricQuantity().replace(replace1, replace2), fileContent));
                                    windTurbineStatusData.setElectricalCurrent(parsingValue(parsingFjInfo.getElectricalCurrent().replace(replace1, replace2), fileContent));
                                    windTurbineStatusData.setPitchAngle(parsingValue(parsingFjInfo.getPitchAngle().replace(replace1, replace2), fileContent));
                                    windTurbineStatusData.setPowerFactor(parsingValue(parsingFjInfo.getPowerFactor().replace(replace1, replace2), fileContent));
                                    windTurbineStatusData.setReactivePower(parsingValue(parsingFjInfo.getReactivePower().replace(replace1, replace2), fileContent));
                                    windTurbineStatusData.setVoltage(parsingValue(parsingFjInfo.getVoltage().replace(replace1, replace2), fileContent));
                                    windTurbineStatusData.setWd(parsingValue(parsingFjInfo.getWd().replace(replace1, replace2), fileContent));
                                    windTurbineStatusData.setWs(parsingValue(parsingFjInfo.getWs().replace(replace1, replace2), fileContent));
                                    windTurbineStatusData.setT(parsingValue(parsingFjInfo.getT().replace(replace1, replace2), fileContent));

                                    windTurbineStatusData.setStatus(parsingStatus(parsingFjInfo.getStatus().replace(replace1, replace2), fileContent));
                                    windTurbineStatusData.setIsSample(parsingStatus(parsingFjInfo.getIsSample().replace(replace1, replace2), fileContent));
                                    windTurbineStatusData.setStationCode(stationCode);

                                    windTurbineStatusDataService.save(windTurbineStatusData);
                                    log.info("解析FJ文件：" + file.getName() + " 成功！ O(∩_∩)O");
                                    parsingResultDto.setStatus("success");
                                } else {
                                    parsingResultDto.setStatus("0");
                                    parsingResultDto.setMessage("未找到对应风机：" + fjName);
                                    log.error("未找到对应风机：{}", fjName);
                                    break;
                                }
                                number++;
                            }
                        }else {
                            parsingResultDto.setStatus("0");
                            parsingResultDto.setMessage("解析风机数据时间失败：" + file.getName());
                            log.error("解析风机数据时间失败：" + file.getName());
                        }
                    }else {
                        parsingResultDto.setStatus("0");
                        parsingResultDto.setMessage("解析风机名称失败：" + file.getName());
                        log.error("解析风机名称失败：" + file.getName());
                    }
                }
            } else {
                parsingResultDto.setMessage("风机暂无多行解析方式");
                log.error("fj暂无多行解析方式");
            }


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

    }

    @Override
    public boolean activationParsingConf() {
        this.parsingFjs = list();
        this.windTurbineInfos = windTurbineInfoService.list();
        return true;
    }
}
