package com.xydl.cac.spectrogram;

import com.xydl.cac.config.BizConfig;
import com.xydl.cac.entity.IedDlRecord;
import com.xydl.cac.entity.ModevType;
import com.xydl.cac.model.spectrogram.SouthPd;
import com.xydl.cac.model.spectrogram.SouthYsp;
import com.xydl.cac.model.spectrogram.SpectrogramModel;
import com.xydl.cac.service.ModevTypeService;
import com.xydl.cac.util.DateUtil;
import com.xydl.cac.util.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
@Slf4j
public class SpectrogramHandler {

    @Resource
    BizConfig bizConfig;
    @Resource
    ModevTypeService modevTypeService;
    @Resource
    ProcessorYsp processorYsp;
    @Resource
    ProcessorPd processorPd;

    public void processFile(IedDlRecord record) {
        record.setCreateTime(new Date());
        if (record.getTypeId() == null) {
            return;
        }
        String localFilePath = record.getPath().replaceFirst(bizConfig.getDataNginxPath(), bizConfig.getDatapath());
        try {
            ModevType modevType = modevTypeService.detail(record.getTypeId());

            // 谱图解析
            if (modevType.getMc().contains("油色谱")) {
                SouthYsp model = processorYsp.process(localFilePath);
                this.setData(record, model);
                log.info("解析油色谱谱图文件成功");
            } else if (modevType.getMc().contains("局部放电")) {
                SouthPd model = processorPd.process(localFilePath);
                this.setData(record, model);
                log.info("解析局部放电谱图文件成功");
            } else {
                log.error("缺少该类型" + record.getTypeId() + "的谱图处理模块");
            }
        } catch (Exception ex) {
            log.error("解析谱图文件失败, typeId=" + record.getTypeId() + ", devId=" + record.getDevId()
                    + ", file=" + localFilePath, ex);
        }
    }

    private void setData(IedDlRecord record, SpectrogramModel model) {
        String json = JSONUtil.object2Json(model);
        record.setData(json);
        try {
            String str = String.valueOf(model.getCreateTime());
            Date dtime = DateUtil.parse(str, "yyyyMMddHHmmss");
            record.setDTime(dtime);
        } catch (Exception ignore) {
        }
    }

    public void jsonToModel(IedDlRecord record) {
        if (record.getTypeId() == null) {
            return;
        }
        if (record.getData() == null) {
            return;
        }
        try {
            ModevType modevType = modevTypeService.detail(record.getTypeId());
            if (modevType.getMc().contains("油色谱")) {
                SouthYsp model = JSONUtil.json2Object(record.getData(), SouthYsp.class);
                record.setModel(model);
                record.setData(null);
            } else if (modevType.getMc().contains("局部放电")) {
                SouthPd model = JSONUtil.json2Object(record.getData(), SouthPd.class);
                record.setModel(model);
                record.setData(null);
            }
        } catch (Exception ex) {
            log.error("谱图数据data内容存在异常, typeId=" + record.getTypeId() + ", devId=" + record.getDevId()
                    + ", file=" + record.getPath());
        }
    }

}
