package com.persagy.proxy.adm.strategy.relationdata;

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

import com.persagy.dmp.common.helper.SpringHelper;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.persagy.dmp.basic.dto.RequestData;
import com.persagy.dmp.basic.model.QueryCriteria;
import com.persagy.dmp.common.model.response.CommonResult;
import com.persagy.dmp.digital.client.DigitalRelationFacade;
import com.persagy.dmp.digital.entity.ObjectDigital;
import com.persagy.dmp.digital.entity.ObjectRelation;
import com.persagy.proxy.adm.constant.AdmCommonConstant;
import com.persagy.proxy.adm.constant.AdmRelationType;
import com.persagy.proxy.adm.constant.GraphCodeEnum;
import com.persagy.proxy.adm.constant.RelCodeEnum;
import com.persagy.proxy.report.service.IRelationReportService;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;

/**
 * 
 * @version 1.0.0
 * @company persagy 
 * @author zhangqiankun
 * @date 2021年9月9日 下午5:28:21
 */
@Component(value = AdmRelationType.ARCHSUBSET_BD2FL)
public class ArchSubsetBd2FlRelationObject extends AbstractRelationObject {

	public ArchSubsetBd2FlRelationObject(IRelationReportService reportDownloadService) {
		super(reportDownloadService);
	}

	@Override
	public long countRelationObjects(String groupCode, String projectId) {
		QueryCriteria queryCriteria = new QueryCriteria();
		ObjectNode objectNode = OBJECT_MAPPER.createObjectNode();
		objectNode.put("graphCode", GraphCodeEnum.ArchSubset.name());
		objectNode.put("relCode", RelCodeEnum.Bd2Fl.name());
		objectNode.put("valid", 1);
		queryCriteria.setCriteria(objectNode);
		queryCriteria.setOnlyCount(true);
		
		CommonResult<List<ObjectRelation>> result = DigitalRelationFacade.queryPrototype(groupCode, projectId, AdmCommonConstant.APP_ID, AdmCommonConstant.USER_ID, queryCriteria);
		return result == null ? 0 : (result.getCount() == null ? 0 : result.getCount());
	}
	
	@Override
	public List<ObjectDigital> queryAllRelations(String groupCode, String projectId, String mainContent, Set<String> slaveContent) {
		RequestData requestData = new RequestData();
		requestData.setGraphCode(GraphCodeEnum.ArchSubset.name());
		requestData.setRelCode(RelCodeEnum.Bd2Fl.name());
		if (StrUtil.isNotBlank(mainContent)) {
			requestData.setMainContent(mainContent);
		}
		if (CollectionUtil.isNotEmpty(slaveContent)) {
			requestData.setSlaveContent(slaveContent);
		}
		requestData.setPage(1L);
		requestData.setSize(SpringHelper.getLong("persagy.common.sql.limit",500L));
		
		List<ObjectDigital> allDigitals = new ArrayList<ObjectDigital>();
		this.queryPageRelations(allDigitals, requestData, groupCode, projectId);
		return allDigitals;
	}

}
