package com.persagy.proxy.dictionary.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.persagy.dmp.basic.model.QueryCriteria;
import com.persagy.proxy.adm.request.AdmQueryCriteria;
import com.persagy.proxy.dictionary.service.IAdmModelKeyService;
import com.persagy.proxy.adm.utils.AdmQueryCriteriaHelper;
import com.persagy.proxy.common.client.DmpRwdClient;
import com.persagy.proxy.common.entity.DmpResult;
import com.persagy.proxy.common.entity.InstanceUrlParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * ADM 模型下载 key 服务
 * @author lvxy
 * @date 2021/8/25
 *
 *
 * {
 *     "criteria":{
 *         "objType": "equipment",
 *   "bimId": {
 *       "$null": false
 *   }
 *   "relationFrom": {
 *       "graphCode": "MechInArch",
 *       "relCode": "Eq2Fl",
 *       "objTo": "#{floorId}"
 *   }
 *     }
 * }
 */
@Slf4j
@Service
public class AdmModelKeyServiceImpl implements IAdmModelKeyService {

    @Autowired
    private DmpRwdClient rwdClient;
    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 查询
     * 参见datacenter 中 ModelService 的 modelKey 方法
     * @Select("SELECT bim_id as bimIdPre,model_id as key FROM equipment WHERE project_id = #{projectId} AND floor_id = #{floorId} AND not bim_id ISNULL LIMIT 1")
     * @param request
     * @return
     */
    @Override
    public DmpResult<JSONArray> doQuery(InstanceUrlParam context, AdmQueryCriteria request) {
        // 转换为数据中台查询条件
        QueryCriteria dmpRequest = AdmQueryCriteriaHelper.toDmpCriteria(request);
        dmpRequest.setSize(1L);
        ObjectNode criteria = dmpRequest.getCriteria();
        String objTo = criteria.get("floorId").textValue();
        DmpResult<JSONArray> result = new DmpResult<>();
        if(objTo == null){
            result.setMessage("楼层 id 不能为空");
            result.setResult(DmpResult.FAILURE);
            return result;
        }

        //拼接中台查询条件
        criteria.remove("floorId");
        criteria.put("objType", "equipment");

        JsonNodeFactory factory = JsonNodeFactory.instance;
        ObjectNode relationFrom = factory.objectNode();
        relationFrom.put("objTo", objTo);
        relationFrom.put("relCode", "Eq2Fl");
        relationFrom.put("graphCode", "MechInArch");
        criteria.putPOJO("relationFrom", relationFrom);


        JSONObject jsonBimId = new JSONObject();
        jsonBimId.put("$null", false);
        criteria.putPOJO("bimId",jsonBimId);

        dmpRequest.setCriteria(criteria);
        dmpRequest.setSize(1L);
        dmpRequest.setPage(1L);
        dmpRequest.setWithoutCount(true);
        // 转换参数
        JSONObject paraEquip = null;
        try {
            String paraStr = objectMapper.writeValueAsString(dmpRequest);
            paraEquip = JSONObject.parseObject(paraStr);
        } catch (JsonProcessingException e) {
            log.error(e.getMessage(), e);
            return null;
        }
        result = rwdClient.queryObject(context, paraEquip);
        //处理成目标对象
        if(result.getResult().equals(DmpResult.SUCCESS)){
            JSONArray jsonArray = result.getData();
            JSONObject object = result.getData().getJSONObject(0);
            JSONObject model = new JSONObject();
            model.put("bimIdPre", object.get("bimId"));
            model.put("key", object.get("modelId"));
            jsonArray.remove(0);
            jsonArray.add(0, model);
            result.setData(jsonArray);
        }

        return result;
    }

}

