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.WindTowerInfo;
import com.jiayue.ipp.common.data.entity.WindTowerStatusData;
import com.jiayue.ipp.common.data.entity.an.ParsingCftInfo;
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.ParsingCftInfoMapper;
import com.jiayue.ipp.idp.service.WindTowerInfoService;
import com.jiayue.ipp.idp.service.WindTowerStatusDataService;
import com.jiayue.ipp.idp.service.an.ParsingCftInfoService;
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.Date;
import java.util.List;

/**
 * 测风塔解析业务层实现类
 *
 * @author tl
 * @date 2022-05-11 18:07:03
 */
@Service
@AllArgsConstructor
@Slf4j
@Transactional
public class ParsingCftInfoServiceImpl extends ServiceImpl<ParsingCftInfoMapper, ParsingCftInfo> implements ParsingCftInfoService, ParsingInterface {


    private final Long intervalTime = 300000L;

    private final WindTowerStatusDataService windTowerStatusDataService;

    private final WindTowerInfoService windTowerInfoService;


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

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

    @Override
    public String parsing(File file, ParsingUrl parsingUrl) {

        String parsingStatus = ParsingConstant.FAIL;

        try {


            List<ParsingCftInfo> parsingCftInfos = baseMapper.selectList(Wrappers.lambdaQuery(ParsingCftInfo.class).like(ParsingInfo::getStationCode, parsingUrl.getStationCode()));
            if (parsingCftInfos.size() > 0) {
                List<WindTowerInfo> windTowerInfoList = windTowerInfoService.list(Wrappers.lambdaQuery(WindTowerInfo.class)
                        .eq(WindTowerInfo::getStationCode, parsingUrl.getStationCode()));
                if (windTowerInfoList.size() > 0) {
                    List<String> fileContent = FileUtil.getFileContent(file);
                    for (ParsingCftInfo parsingCftInfo : parsingCftInfos) {
                        //判断文件名是否符合配置
                        if (file.getName().indexOf(parsingCftInfo.getFileName()) >= 0) {

                            if (parsingCftInfo.getDataType().equals(FieldUtil.SINGLE)) {
                                Long time = parsingTime(parsingCftInfo.getTime(), fileContent);
                                if (time != 0L) {
                                    WindTowerStatusData windTowerStatusData = new WindTowerStatusData();
                                    windTowerStatusData.setTime(new Date(time));

                                    windTowerStatusData.setTInst(parsingValue(parsingCftInfo.gettInst(), fileContent));
                                    windTowerStatusData.setPaInst(parsingValue(parsingCftInfo.getPaInst(), fileContent));
                                    windTowerStatusData.setRhInst(parsingValue(parsingCftInfo.getRhInst(), fileContent));
                                    windTowerStatusData.setAirDensity(parsingValue(parsingCftInfo.getAirDensity(), fileContent));

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

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

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

                                    windTowerStatusData.setEquipmentId(windTowerInfoList.get(0).getEquipmentNo());

                                    windTowerStatusDataService.save(windTowerStatusData);
                                    parsingStatus = FileTypeEnum.CFT.name();
                                    log.info("解析CFT文件：{} 成功！ O(∩_∩)O", file.getName());
                                } else {
                                    parsingStatus += "解析CFT文件时间错误";
                                    log.error("解析CFT文件时间错误");
                                }
                            } else {
                                parsingStatus += "cft暂无多行解析方式";
                                log.info("cft暂无多行解析方式");
                            }
                        }
                    }
                } else {
                    parsingStatus += "cft未配置作为解析使用设备";
                    log.info("cft未配置作为解析使用设备");
                }

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

    }
}
