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

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.digital.entity.ObjectRelationProjectCal;
import com.persagy.proxy.adm.request.AdmResponse;
import com.persagy.proxy.adm.service.impl.AbstractAdmBaseServiceImpl;
import com.persagy.proxy.calculation.service.IAdmObjectRelationCalService;
import com.persagy.proxy.common.entity.InstanceUrlParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * 标记关系计算
 *
 * @author:linhuili
 * @date:2021/10/15
 */
@Service
@Slf4j
public class AdmObjectRelationCalServiceImpl extends AbstractAdmBaseServiceImpl<ObjectRelationProjectCal> implements IAdmObjectRelationCalService {

    /**
     * 根据关系类型查询标记计算关系
     * @param context
     * @param relationType
     * @return
     */
    @Override
    public List<ObjectRelationProjectCal> queryCalRelByRelationType(InstanceUrlParam context, Set<String> relationType){
        //设置查询条件
        QueryCriteria queryCriteria = new QueryCriteria();
        ObjectNode criteria = JsonNodeFactory.instance.objectNode();
        ArrayNode arrayNode = criteria.putObject("relCode").putArray("$in");
        relationType.stream().forEach(relCode -> arrayNode.add(relCode));
        queryCriteria.setCriteria(criteria);
        //查询标记计算关系
        AdmResponse admResponse = doQueryRelationProjectCal(context, queryCriteria, ObjectRelationProjectCal.class);
        return (List<ObjectRelationProjectCal>)admResponse.getContent();
    }

    /**
     * 更新标记计算关系
     * @param objectRelationProjectCals
     */
    @Override
    public void updateObjectCalRel(InstanceUrlParam context, List<ObjectRelationProjectCal> objectRelationProjectCals) {
        doUpdateRelationProjectCal(context, ObjectRelationProjectCal.class, objectRelationProjectCals);
    }
}
