package com.persagy.proxy.object.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.persagy.dmp.basic.model.QueryCriteria;
import com.persagy.dmp.common.helper.SpringHelper;
import com.persagy.proxy.adm.constant.GraphCodeEnum;
import com.persagy.proxy.adm.constant.RelCodeEnum;
import com.persagy.proxy.adm.service.impl.AbstractAdmBaseServiceImpl;
import com.persagy.proxy.common.entity.InstanceUrlParam;
import com.persagy.proxy.object.model.AdmComponent;
import com.persagy.proxy.object.service.IAdmComAndEquipService;
import com.persagy.proxy.object.service.IAdmComponentService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ADM 组件 服务
 * @author lvxy
 * @date 2021/8/19
 */
@Service
public class AdmComponentServiceImpl extends AbstractAdmBaseServiceImpl<AdmComponent> implements IAdmComponentService, IAdmComAndEquipService {

    @Override
    public List<AdmComponent> doInsertExt(InstanceUrlParam context, Class<AdmComponent> clazz, List<AdmComponent> voList) {
        if(CollUtil.isEmpty(voList)) {
            return null;
        }
        return doInsert(context, clazz, voList);
    }

    @Override
    protected void processDmpCriteria(QueryCriteria dmpRequest) {
        ObjectNode criteria = dmpRequest.getCriteria();
        if (null==criteria){
            return;
        }
        if (!criteria.has("parentId")){
            return;
        }
        // 有父级id过滤
        ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class);
        ObjectNode relationTo = objectMapper.createObjectNode();
        relationTo.put("graphCode", GraphCodeEnum.MechSubset.name());
        relationTo.put("relCode", RelCodeEnum.Eq2Ec.name());
        relationTo.set("objFrom",criteria.get("parentId"));
        criteria.set("relationTo",relationTo);
        criteria.remove("parentId");
    }
}
