package com.persagy.adm.server.custom.service.impl.check_update;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.persagy.adm.server.custom.client.RwdClient;
import com.persagy.adm.server.custom.common.AdmConst;
import com.persagy.adm.server.custom.service.ServiceUtil;
import com.persagy.dmp.basic.model.QueryCriteria;
import com.persagy.dmp.common.constant.ResponseCode;
import com.persagy.dmp.common.model.response.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 检查更新:元空间和空间的对应关系 bdtp库
 */
@Component
@Slf4j
public class MetaSpaceObjRelCheckUpdate implements CheckUpdate{
    @Autowired
    RwdClient rwdClient;

    @Autowired
    ObjectMapper objectMapper;

    @Override
    public boolean doCheck(CheckUpdateContext hasUpdateReq) {
        log.info("开始检查元空间和空间的对应关系是否有更新");
        QueryCriteria objRelCriteria2 = ServiceUtil.getQueryCriteria(objectMapper);
        objRelCriteria2.setOnlyCount(true);
        objRelCriteria2.setWithoutCount(true);
        objRelCriteria2.getCriteria().put("graphCode", "MechInArch").put("relCode", "Sp2Si").put("relValue", "GeneralZone").putObject("ts").put("$gt", hasUpdateReq.getDownLoadObjRelTs());
        CommonResult<List<ObjectNode>> query = rwdClient.queryRelation(hasUpdateReq.getGroupCode(), hasUpdateReq.getProjectId(), AdmConst.APP_ID, hasUpdateReq.getUserId(), objRelCriteria2);
        return query != null && ResponseCode.A00000.getCode().equals(query.getResult()) && query.getCount() > 0;
    }
}
