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

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
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.AdmRelationTypeEnum;
import com.persagy.proxy.report.model.AdmRelationObject;
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年11月1日 上午11:42:26
 */
@Component(value = AdmRelationType.DEFAULT_RELATION_OBJECT)
public class DeafultRelationObject extends AbstractReportRelationObject {

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

	@Override
	public long countRelationObjects(String groupCode, String projectId, AdmRelationTypeEnum typeEnum) {
		QueryCriteria queryCriteria = new QueryCriteria();
		ObjectNode objectNode = OBJECT_MAPPER.createObjectNode();
		objectNode.put("graphCode", typeEnum.getGraphCode());
		objectNode.put("relCode", typeEnum.getRelCode());
		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, AdmRelationTypeEnum typeEnum) {
		RequestData requestData = new RequestData();
		requestData.setGraphCode(typeEnum.getGraphCode());
		requestData.setRelCode(typeEnum.getRelCode());
		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;
	}
	
	@Override
	protected AdmRelationObject handleObjectDigital(ObjectDigital master, ObjectDigital slave, String groupCode, String projectId) {
		throw new UnsupportedOperationException("未实现的策略方法");
	}
	
	@Override
	public Object beforeSaveRelationObject(AdmRelationObject relationObject, String groupCode, String projectId, String code, AdmRelationTypeEnum typeEnum) {
		return this.beforeSaveRelationObject(relationObject, typeEnum, groupCode, projectId, code);
	}
	@Override
	public Map<String,Object> beforeSaveRelationObjects(List<AdmRelationObject> relationObjects, String groupCode, String projectId, String code, AdmRelationTypeEnum typeEnum) {
		return this.beforeSaveRelationObjects(relationObjects, typeEnum, groupCode, projectId, code);
	}

}
