package com.persagy.adm.server.backstage.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.persagy.adm.server.algorithm.entity.AdmModelFile;
import com.persagy.adm.server.backstage.domain.AdmAnalysisModel;
import com.persagy.adm.server.backstage.domain.Level;
import com.persagy.dmp.common.constant.DigitalObjectType;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 处理Levels的数据处理类
 * @author : lijie
 * Update By 2022/1/22 17:20
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class LevelDataHandler extends AbstractDataHandler {
    /**
     * 获得对象分类
     * @param classCode :对象类型编码
     * @return : java.lang.String
     * @author : lijie
     * Update By 2022/1/22 17:21
     */
    @Override
    protected String getObjectType(String classCode) {
        return DigitalObjectType.FLOOR.getIndex();
    }

    @Override
    public void handleData(Map<String, ObjectNode> objectNodeMap, AdmAnalysisModel model, JSONArray objects,
                           String classCode,String planName) {
        AdmModelFile currentModelFile = model.getCurrentModelFile();
        if (null==currentModelFile|| StrUtil.isBlank(planName)){
            return;
        }
        List<Level> levels = objects.toJavaList(Level.class);
        if (CollUtil.isEmpty(levels)){
            return;
        }
        Optional<Level> first = levels.stream()
                .filter(level -> planName.equals(level.getName()))
                .findFirst();
        if(!first.isPresent()){
            return;
        }
        Level level = first.get();
        currentModelFile.setLevel(level.getValue());
    }
}
