package com.jiayue.ipp.idp.service.impl.an;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiayue.ipp.common.data.entity.WindTurbineInfo;

import com.jiayue.ipp.common.data.entity.WindTurbineStatusData;
import com.jiayue.ipp.common.data.entity.an.ParsingFjInfo;
import com.jiayue.ipp.common.data.entity.an.ParsingInfo;
import com.jiayue.ipp.common.data.entity.an.ParsingUrl;
import com.jiayue.ipp.common.data.enums.FileTypeEnum;
import com.jiayue.ipp.idp.dto.ParsingConstant;
import com.jiayue.ipp.idp.mapper.an.ParsingFjInfoMapper;
import com.jiayue.ipp.idp.service.WindTurbineInfoService;
import com.jiayue.ipp.idp.service.WindTurbineStatusDataService;
import com.jiayue.ipp.idp.service.an.ParsingFjInfoService;
import com.jiayue.ipp.idp.service.an.ParsingInterface;
import com.jiayue.ipp.idp.util.FieldUtil;
import com.jiayue.ipp.idp.util.FileUtil;
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.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 风机解析业务层实现类
 *
 * @author tl
 * @date 2022-05-11 18:07:03
 */
@Service
@AllArgsConstructor
@Slf4j
@Transactional
public class ParsingFjInfoServiceImpl extends ServiceImpl<ParsingFjInfoMapper, ParsingFjInfo> implements ParsingFjInfoService, ParsingInterface {


    private final Long intervalTime = 300000l;

    private final WindTurbineStatusDataService windTurbineStatusDataService;

    private final WindTurbineInfoService windTurbineInfoService;


    @Override
    public boolean save(ParsingInfo parsingInfo) {
        return save((ParsingFjInfo) parsingInfo);
    }

    @Override
    public boolean deleteById(String id) {
        return removeById(id);
    }

    @Override
    public String parsing(File file, ParsingUrl ftpParsingUrl) {
        String parsingStatus = ParsingConstant.FAIL;

        try {


            List<ParsingFjInfo> parsingFjInfos = baseMapper.selectList(Wrappers.lambdaQuery(ParsingFjInfo.class).like(ParsingInfo::getStationCode, ftpParsingUrl.getStationCode()));
            if (parsingFjInfos.size() > 0) {

                List<String> fileContent = FileUtil.getFileContent(file);
                List<WindTurbineInfo> windTurbineInfoList = windTurbineInfoService.list(Wrappers.lambdaQuery(WindTurbineInfo.class).eq(WindTurbineInfo::getStationCode, ftpParsingUrl.getStationCode()));

                for (ParsingFjInfo parsingFjInfo : parsingFjInfos) {
                    //判断文件名是否符合配置
                    if (file.getName().indexOf(parsingFjInfo.getFileName()) >= 0) {

                        WindTurbineStatusData windTurbineStatusData = new WindTurbineStatusData();

                        if (parsingFjInfo.getDataType().equals(FieldUtil.SINGLE)) {

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

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

                            if (filterWindTurbineInfos.size() > 0) {
                                windTurbineStatusData.setEquipmentId(filterWindTurbineInfos.get(0).getEquipmentNo());
                                Long time = parsingTime(parsingFjInfo.getTime(), fileContent);
                                if (time != 0l) {
                                    LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault());
                                    windTurbineStatusData.setTime(localDateTime);

                                    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");
                                    parsingStatus = FileTypeEnum.FJXX.name();
                                } else {
                                    parsingStatus += "解析FJ文件时间错误";
                                    log.error("解析FJ文件时间错误");
                                }
                            } else {
                                parsingStatus += "fj {" + fjName + "} 在本地无配置无法解析";
                                log.info("fj {} 在本地无配置无法解析", fjName);
                            }
                        } else {
                            parsingStatus += "fj暂无多行解析方式";
                            log.info("fj暂无多行解析方式");
                        }


                    }
                }
            }

        } catch (Exception e) {
            parsingStatus += "解析风机数据错误！ /(ㄒoㄒ)/~~";
            log.error("解析风机数据错误",e);
        } finally {
            return parsingStatus;
        }

    }


}
