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

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

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.persagy.dmp.basic.dto.RequestData;
import com.persagy.dmp.common.helper.SpringHelper;
import com.persagy.dmp.common.model.response.CommonResult;
import com.persagy.dmp.digital.client.DigitalObjectFacade;
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.AdmRelationTypeEnum;
import com.persagy.proxy.report.model.AdmRelationObject;
import com.persagy.proxy.report.service.IRelationReportService;

import cn.hutool.core.collection.CollectionUtil;

/**
 * 普通操作抽象类
 * 
 * @version 1.0.0
 * @company persagy 
 * @author zhangqiankun
 * @date 2021年9月2日 下午11:00:26
 */
public abstract class AbstractRelationObject implements RelationObjectStrategy {

	protected IRelationReportService relationReportService;
	
	public AbstractRelationObject(IRelationReportService reportDownloadService) {
		this.relationReportService = reportDownloadService;
	}
	
	@Override
	public long countRelationObjects(String groupCode, String projectId, AdmRelationTypeEnum typeEnum) {
		return this.countRelationObjects(groupCode, projectId);
	}
	
	/**
	 * 统计关系总数
	 * 
	 * @param groupCode
	 * @param projectId
	 * @return
	 */
	protected abstract long countRelationObjects(String groupCode, String projectId);
	
	@Override
	public List<ObjectDigital> queryAllRelations(String groupCode, String projectId, String mainContent, Set<String> slaveContent, AdmRelationTypeEnum typeEnum) {
		return this.queryAllRelations(groupCode, projectId, mainContent, slaveContent);
	}
	
	/**
	 * 查询出所有的关系对象数据
	 * 
	 * @param groupCode 集团编码
	 * @param projectId 项目ID
	 * @param mainContent	主对象的筛选关键字,筛选范围为id,name,local_id,local_name,cADID.为关系左侧对象
	 * @param slaveContent	从对象的筛选关键字.objTo对应的对象,筛选范围为id,name,local_id,local_name.为关系右侧对象
	 * @return 不会返回null
	 */
	protected abstract List<ObjectDigital> queryAllRelations(String groupCode, String projectId, String mainContent, Set<String> slaveContent);
	
	
	@Override
	public void queryPageRelations(List<ObjectDigital> allDigitals, RequestData requestData, String groupCode, String projectId) {
		CommonResult<List<ObjectDigital>> queryPrototype = DigitalObjectFacade.queryObjectListByGraphCodeAndRelCodePrototype(groupCode, projectId, AdmCommonConstant.APP_ID, AdmCommonConstant.USER_ID, requestData);
		Long count = queryPrototype.getCount();
		List<ObjectDigital> infos = queryPrototype.getData();
		if (count == null || CollectionUtil.isEmpty(infos)) {
			return;
		}
		
		allDigitals.addAll(infos);
		if (count < SpringHelper.getLong("persagy.common.sql.limit",500L)) {
			return;
		}
		if ((infos.size()*requestData.getPage()) > count) {
			return;
		}
		requestData.setPage((requestData.getPage() + 1));
		this.queryPageRelations(allDigitals, requestData, groupCode, projectId);
	}
	
	@Override
	public boolean saveRelationObject(ObjectNode relationObject, String groupCode, String projectId) {
		ObjectRelation objectRelation = DigitalRelationFacade.createOne(groupCode, projectId, AdmCommonConstant.APP_ID, AdmCommonConstant.USER_ID, relationObject);
		return objectRelation != null;
	}
	
	@Override
	public boolean saveRelationObjects(List<ObjectNode> relationObjects, String groupCode, String projectId) {
		List<ObjectRelation> create = DigitalRelationFacade.create(groupCode, projectId, AdmCommonConstant.APP_ID, AdmCommonConstant.USER_ID, relationObjects);
		return CollectionUtil.isNotEmpty(create);
	}
	
	/**************************************************** 默认不实现的类 *******************************************************/
	
	@Override
	public List<AdmRelationObject> exportRelationObject(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, groupCode, projectId, code);
	}

	protected Object beforeSaveRelationObject(AdmRelationObject relationObject, String groupCode, String projectId, String code) {
		throw new UnsupportedOperationException("未实现的策略方法");
	}

	@Override
	public Map<String,Object> beforeSaveRelationObjects(List<AdmRelationObject> relationObjects, String groupCode, String projectId, String code, AdmRelationTypeEnum typeEnum) {
		return this.beforeSaveRelationObjects(relationObjects, groupCode, projectId, code);
	}

	protected Map<String,Object> beforeSaveRelationObjects(List<AdmRelationObject> relationObjects, String groupCode, String projectId, String code) {
		throw new UnsupportedOperationException("未实现的策略方法");
	}

}
