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.ParsingRp;
import com.cpp.web.domain.datafactory.dto.ParsingConfParam;
import com.cpp.web.domain.datafactory.dto.ParsingResultDto;
import com.cpp.web.domain.datafactory.enums.FileTypeEnum;
import com.cpp.web.domain.station.PowerStationStatusData;
import com.cpp.web.mapper.datafactory.ParsingRpMapper;
import com.cpp.web.service.datafactory.ParsingInterface;
import com.cpp.web.service.datafactory.ParsingRpService;
import com.cpp.web.service.station.PowerStationStatusDataService;
import com.cpp.web.utils.ParsingFieldUtil;
import com.cpp.web.utils.ParsingFileUtil;
import com.cpp.web.utils.ParsingUtil;
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.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 实际功率解析业务层实现类
 *
 * @author tl
 * @date 2022-05-11 18:07:03
 */
@Service("rpParsing")
@Slf4j
@AllArgsConstructor
@Transactional
public class ParsingRpServiceImpl extends ServiceImpl<ParsingRpMapper, ParsingRp> implements ParsingRpService, ParsingInterface {

    private final PowerStationStatusDataService powerStationStatusDataService;

    private List<ParsingRp> parsingRps = new ArrayList<>();

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

        try {
            List<ParsingRp> parsingRpInfos = selectByStationCode(stationCode);
            if (!parsingRpInfos.isEmpty()) {
                ParsingRp parsingRpInfo = parsingRpInfos.get(0);

                List<String> fileContent = ParsingFileUtil.getFileContent(file);
                if (parsingRpInfo.getDataType().equals(ParsingFieldUtil.SINGLE)) {

                    Date time = parsingForecastTime(parsingRpInfo.getTime(), fileContent);

                    if (time != null) {

                        BigDecimal realValue = new BigDecimal(-99);
                        if (parsingRpInfo.getRealValue() != null)
                            realValue = parsingValue(parsingRpInfo.getRealValue(), fileContent);

                        BigDecimal ableValue = new BigDecimal(-99);
                        if (parsingRpInfo.getAbleValue() != null)
                            ableValue = parsingValue(parsingRpInfo.getAbleValue(), fileContent);

                        BigDecimal theoryValue = new BigDecimal(-99);
                        if (parsingRpInfo.getTheoryValue() != null)
                            theoryValue = parsingValue(parsingRpInfo.getTheoryValue(), fileContent);


                        PowerStationStatusData powerStationStatusData = new PowerStationStatusData();
                        powerStationStatusData.setStationCode(stationCode);
                        powerStationStatusData.setTheoryValue(theoryValue);
                        powerStationStatusData.setAbleValue(ableValue);
                        powerStationStatusData.setRealValue(realValue);
                        powerStationStatusData.setTime(time);

                        powerStationStatusDataService.save(powerStationStatusData);

                        log.info("解析RP文件：{} 成功！ O(∩_∩)O", file.getName());
                        parsingResultDto.setStatus("success");
                    } else {
                        parsingResultDto.setMessage("解析实际功率文件时间错误");
                        log.error("解析RP文件时间错误");
                    }
                } else {
                    parsingResultDto.setMessage("实际功率暂无单行解析方式");
                    log.error("RP暂无单行解析方式");
                }

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

    @Override
    public boolean activationParsingConf() {
        return false;
    }

    @Override
    public List selectByStationCode(String stationCode) {
        return null;
    }


    protected Date parsingForecastTime(String sign, List<String> fileContent) {
        Date time = null;
        try {
            ParsingConfParam config = ParsingFieldUtil.getConfig(sign);
            String s = fileContent.get(ParsingUtil.noParseInt(config.getLineNumber()));
            int signIndex = s.indexOf(config.getDataSign());
            String result;
            if (signIndex > 0) {
                int startLength = config.getDataSign().length() + signIndex + 1;
                int length = config.getFormat().length();
                if (String.valueOf(s.charAt(startLength + 1)).equals("'")) {
                    startLength += 1;
                }
                result = s.substring(startLength, startLength + length);

                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(config.getFormat());

                time = simpleDateFormat.parse(result);

                log.info("解析实际功率【时间】成功==》开始时间为：{}", result);
            } else {
                log.error("解析实际功率【时间】时公式标识不存在所在行");
            }
        } catch (RuntimeException e) {
            log.error("解析实际功率【时间】失败");
            e.printStackTrace();
        } finally {
            return time;
        }

    }



}
