package com.xydl.cac.spectrogram;

import com.xydl.cac.config.BizConfig;
import com.xydl.cac.entity.IedDlRecord;
import com.xydl.cac.entity.constants.Constants;
import com.xydl.cac.model.spectrogram.SouthYsp;
import com.xydl.cac.util.DataUtil;
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
    ProcessorYsp processorYsp;

    public void processFile(IedDlRecord record) {
        if (record.getTypeId() == null) {
            return;
        }
        String localFilePath = record.getPath().replaceFirst(bizConfig.getDataNginxPath(), bizConfig.getDatapath());
        try {
            // 油色谱谱图
            if (record.getTypeId() == Constants.TypeYSP) {
                SouthYsp model = processorYsp.process(localFilePath);
                String json = JSONUtil.object2Json(model);
                record.setData(json);
                record.setCreateTime(new Date());
                try {
                    String str = String.valueOf(model.getCreateTime());
                    Date dtime = DateUtil.parse(str, "yyyyMMddHHmmss");
                    record.setDTime(dtime);
                } catch (Exception ignore) {
                }
                log.info("解析油色谱谱图文件成功");
            } else {
                log.error("缺少该类型" + record.getTypeId() + "的谱图处理模块");
            }
        } catch (Exception ex) {
            log.error("解析谱图文件失败, typeId=" + record.getTypeId() + ", devId=" + record.getDevId()
                    + ", file=" + localFilePath, ex);
        }
    }

    public void jsonToModel(IedDlRecord record) {
        if (record.getTypeId() == null) {
            return;
        }
        if (record.getData() == null) {
            return;
        }
        try {
            if (Constants.TypeYSP == record.getTypeId()) {
                SouthYsp model = JSONUtil.json2Object(record.getData(), SouthYsp.class);
                record.setModel(model);
                record.setData(null);
            }
        } catch (Exception ex) {
            log.error("谱图数据data内容存在异常, typeId=" + record.getTypeId() + ", devId=" + record.getDevId()
                    + ", file=" + record.getPath());
        }
    }

}
