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.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 com.persagy.dmp.digital.client.DigitalObjectClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 检查对象数据是否有更新
 */
@Component
@Slf4j
public class BdtpObjectCheckUpdate implements CheckUpdate{
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    private DigitalObjectClient objectClient;
    @Override
    public boolean doCheck(CheckUpdateContext hasUpdateReq) {
        try{
            log.info("检查对象数据是否有更新");
            QueryCriteria criteria = ServiceUtil.getQueryCriteria(objectMapper, AdmConst.OBJ_TYPE_BUILDING, AdmConst.OBJ_TYPE_FLOOR,AdmConst.OBJ_TYPE_SPACE, AdmConst.OBJ_TYPE_SHAFT, AdmConst.OBJ_TYPE_SYSTEM, AdmConst.OBJ_TYPE_EQUIPMENT, "ispace");
            criteria.setOnlyCount(true);
            criteria.setWithoutCount(true);
            criteria.getCriteria().putObject("ts").put("$gt", hasUpdateReq.getDownloadBdtpTs());
            CommonResult<List<ObjectNode>> query = objectClient.query(hasUpdateReq.getGroupCode(), hasUpdateReq.getProjectId(), AdmConst.APP_ID, hasUpdateReq.getUserId(), criteria);
            return query != null && ResponseCode.A00000.getCode().equals(query.getResult()) && query.getCount() > 0;
        }catch (Exception e){
            log.error("检查对象数据是否有更新时异常",e);
            return false;
        }

    }
}
