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

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
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.helper.SpringHelper;
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.*;
import com.persagy.proxy.report.model.AdmRelationObject;
import com.persagy.proxy.report.service.IRelationReportService;
import org.springframework.stereotype.Component;

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

/**
 * 
 * @version 1.0.0
 * @company persagy 
 * @author zhangqiankun
 * @date 2021年9月3日 上午9:42:46
 */
@Component(value = AdmRelationType.ARCHFORARCH_SP2SP)
public class ArchForArchSp2SpRelationObject extends AbstractReportRelationObject {

	public ArchForArchSp2SpRelationObject(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.ArchForArch.name());
		objectNode.put("relCode", RelCodeEnum.Sp2Sp.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.ArchForArch.name());
		requestData.setRelCode(RelCodeEnum.Sp2Sp.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) {
		// 获取建筑的信息
		ObjectNode floorInfo = this.relationReportService.getObjectByCodeAndId(groupCode, projectId, GraphCodeEnum.ArchForArch.name(),
				RelCodeEnum.Sp2Fl.name(), master.getId(), null);
		ObjectNode buildingInfo = this.relationReportService.getObjectByCodeAndId(groupCode, projectId, GraphCodeEnum.ArchForArch.name(),
				RelCodeEnum.Sp2Bd.name(), master.getId(), null);
		ObjectNode slaveFloorInfo = this.relationReportService.getObjectByCodeAndId(groupCode, projectId, GraphCodeEnum.ArchForArch.name(),
				RelCodeEnum.Sp2Fl.name(), slave.getId(), null);
		ObjectNode slaveBuildingInfo = this.relationReportService.getObjectByCodeAndId(groupCode, projectId, GraphCodeEnum.ArchForArch.name(),
				RelCodeEnum.Sp2Bd.name(), slave.getId(), null);

		AdmRelationObject convertObject = this.convertObject(master, "空间", slave, "空间");
		String masterBuildingName = buildingInfo.get("localName") == null ? AdmCommonConstant.EMPTY : buildingInfo.get("localName").asText();
		String masterFloorName = floorInfo.get("localName") == null ? AdmCommonConstant.EMPTY : floorInfo.get("localName").asText();
		String slaveBuildingName = slaveBuildingInfo.get("localName") == null ? AdmCommonConstant.EMPTY : slaveBuildingInfo.get("localName").asText();
		String slaveFloorName = slaveFloorInfo.get("localName") == null ? AdmCommonConstant.EMPTY : slaveFloorInfo.get("localName").asText();
		convertObject.setMasterBuildingName(masterBuildingName);
		convertObject.setMasterFloorName(masterFloorName);
		convertObject.setSlaveBuildingName(slaveBuildingName);
		convertObject.setSlaveBelong(slaveFloorName);
		
		if (StrUtil.isBlank(masterFloorName)) {
			convertObject.setMasterBelong(masterBuildingName);
		} else {
			convertObject.setMasterBelong((StrUtil.isBlank(masterBuildingName) ? AdmCommonConstant.EMPTY
					: StrUtil.join(AdmCommonConstant.LINE_THROUGH, masterBuildingName, masterFloorName)));
		}
		return convertObject;
	}

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