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.adm.constant.GraphCodeEnum;
import com.persagy.proxy.adm.constant.RelCodeEnum;
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年9月3日 上午9:42:46
 */
@Component(value = AdmRelationType.MECHINARCH_EQ2SP)
public class MechInArchEq2SpRelationObject extends AbstractReportRelationObject {

	public MechInArchEq2SpRelationObject(IRelationReportService relationReportService) {
		super(relationReportService);
	}

	@Override
	public long countRelationObjects(String groupCode, String projectId) {
		QueryCriteria queryCriteria = new QueryCriteria();
		ObjectNode objectNode = OBJECT_MAPPER.createObjectNode();
		objectNode.put("graphCode", GraphCodeEnum.MechInArch.name());
		objectNode.put("relCode", RelCodeEnum.Eq2Sp.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.MechInArch.name());
		requestData.setRelCode(RelCodeEnum.Eq2Sp.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;
	}

	@Override
	protected AdmRelationObject handleObjectDigital(ObjectDigital master, ObjectDigital slave, String groupCode, String projectId) {
		return this.convertObject(master, "设备", slave, "空间");
	}

	@Override
	public Object beforeSaveRelationObject(AdmRelationObject relationObject, String groupCode, String projectId, String code) {
		return this.beforeSaveRelationObject(relationObject, AdmRelationTypeEnum.EQ2SP_IN, groupCode, projectId, code);
	}

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