package com.persagy.proxy.object.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.persagy.dmp.basic.model.QueryCriteria;
import com.persagy.dmp.define.entity.ObjectTypeDefine;
import com.persagy.proxy.common.entity.DmpResult;
import com.persagy.proxy.common.entity.InstanceUrlParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author lvxy
 * @date 2021/9/2
 */

public interface IAdmComAndEquipService {
    default QueryCriteria getDmpCriteria(InstanceUrlParam context, Set<String> classCodes, String objType){
        QueryCriteria criteriaObject = new QueryCriteria();
        ObjectNode criteriaEq = JsonNodeFactory.instance.objectNode();

        ArrayNode array = criteriaEq.putObject("code").putArray("$in");
        classCodes.forEach(code -> array.add(code));

        criteriaEq.put("objType", objType);
        criteriaObject.setCriteria(criteriaEq);
        criteriaObject.setSize(classCodes.size() * 1L);
        criteriaObject.setPage(1L);
        criteriaObject.setWithoutCount(true);
        return criteriaObject;
    }

    default Map<String, ObjectTypeDefine> getDefineMap(DmpResult<JSONArray> resultDef){
        Map<String, ObjectTypeDefine> defClassMap = new HashMap<String, ObjectTypeDefine>();
        if(resultDef.getResult().equals(DmpResult.SUCCESS)){
            JSONArray defArr = resultDef.getData();
            List<ObjectTypeDefine> defineList = JSONUtil.toList(defArr.toString(), ObjectTypeDefine.class);
            defClassMap = CollUtil.fieldValueMap(defineList, "code");
        }
        return defClassMap;
    }
}
