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

import com.esotericsoftware.kryo.util.ObjectMap;
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;

/**
 * 检查app端创建的关系有无更新
 */
@Component
@Slf4j
public class BdtpObjRelCreateByAppCheckUpdate implements CheckUpdate{
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    RwdClient rwdClient;
    @Override
    public boolean doCheck(CheckUpdateContext hasUpdateReq) {
        try{
            log.info("开始检查app端创建的关系有无更新");
            QueryCriteria objRelCriteria = ServiceUtil.getQueryCriteria(objectMapper);
            objRelCriteria.getCriteria().putObject("ts").put("$gt", hasUpdateReq.getDownLoadObjRelTs());
            objRelCriteria.getCriteria().put("createApp", AdmConst.CREATOR_APP);
            objRelCriteria.setOnlyCount(true);
            objRelCriteria.setWithoutCount(true);
            CommonResult<List<ObjectNode>> query = rwdClient.queryRelation(hasUpdateReq.getGroupCode(), hasUpdateReq.getProjectId(), AdmConst.APP_ID, hasUpdateReq.getUserId(), objRelCriteria);
            return query != null && ResponseCode.A00000.getCode().equals(query.getResult()) && query.getCount() > 0;
        }catch (Exception e){
            log.error("检查app端创建的关系数据时异常",e);
            return false;
        }

    }
}
