package com.persagy.proxy.relation.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.AdmResponse;
import com.persagy.proxy.adm.service.IAdmRelationService;
import com.persagy.proxy.adm.utils.AdmContextUtil;
import com.persagy.proxy.common.client.DmpRwdClient;
import com.persagy.proxy.common.entity.InstanceUrlParam;
import com.persagy.proxy.common.entity.RelationDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:025、关系-业务空间所在的建筑楼层
 * @author:lgy
 * @data:2021/9/24 19:46
 */
@RestController
@RequestMapping("/rel/sp-in")
public class RelationSpInBdOrFlController {
    @Autowired
    private IAdmRelationService service;
    @Autowired
    DmpRwdClient rwdClient;

    /**
     * 默认图类型
     */
    public static String DEFAULT_GRAPH_CODE = "ArchSubset";
    /**
     * 默认边类型
     */
    public static String DEFAULT_REL_CODE = "Bd2Sp";
    /**
     * 创建业务空间和建筑的关系
     * @param param
     * @return
     * @throws Exception
     */
    @PostMapping(value = {"/bd-create"})
    public AdmResponse spInBdCreate(@RequestBody JSONObject param) throws Exception {
        String buildingId = param.getString("buildingId");
        String spaceId = param.getString("spaceId");
        String type = param.getString("objectType");
        if(!StrUtil.isAllNotEmpty(buildingId,spaceId,type)) {
            return AdmResponse.failure("必填项：buildingId（竖井id）、spaceId(业务空间id),objectType");
        }
        // 创建关系对象
        List<RelationDTO> voList = new ArrayList<>();
        voList.add(new RelationDTO(null, DEFAULT_GRAPH_CODE, DEFAULT_REL_CODE, type, buildingId , spaceId));
        service.doSave(AdmContextUtil.toDmpContext(), voList);
        return AdmResponse.success();
    }

    /**
     * 删除业务空间和建筑的关系
     * @param    paramList    对象
     * @return                       删除的结果信息
     */
    @PostMapping("/bd-del")
    public AdmResponse delete(@RequestBody JSONArray paramList) throws Exception {
        // 组装上下文条件
        InstanceUrlParam context = AdmContextUtil.toDmpContext();
        for (int i = 0; i < paramList.size(); i++) {
            JSONObject param = paramList.getJSONObject(i);
            String buildingId = param.getString("buildingId");
            String spaceId = param.getString("spaceId");
            String type = param.getString("objectType");
            if(!StrUtil.isAllNotEmpty(buildingId,spaceId)) {
                return AdmResponse.failure("spaceId和buildingId 不可以为空");
            }
            ObjectNode node = JsonNodeFactory.instance.objectNode();
            node.put("graphCode", DEFAULT_GRAPH_CODE);
            node.put("relCode", DEFAULT_REL_CODE);
            node.put("objFrom", buildingId);
            node.put("objTo",spaceId);
            QueryCriteria criteria = new QueryCriteria();
            criteria.setCriteria(node);
            service.doDelete(context, criteria);
        }
        return AdmResponse.success();
    }

    /**
     * 修改业务空间所在的建筑
     * @param    requestParam    对象
     * @return                       删除的结果信息
     */
    @PostMapping("/bd-update")
    public AdmResponse delete(@RequestBody JSONObject requestParam) throws Exception {
        JSONArray content = requestParam.getJSONArray("content");
        for (int i = 0; i < content.size(); i++) {
            JSONObject param = content.getJSONObject(i);
            String buildingId = param.getString("buildingId");
            String spaceId = param.getString("spaceId");
            String type = param.getString("objectType");
            if(!StrUtil.isAllNotEmpty(buildingId,spaceId,type)) {
                return AdmResponse.failure("buildingId、spaceId、objectType 不能为空");
            }
        }
        // 组装上下文条件
        InstanceUrlParam context = AdmContextUtil.toDmpContext();
        for (int i = 0; i < content.size(); i++) {
            JSONObject param = content.getJSONObject(i);
            String buildingId = param.getString("buildingId");
            String spaceId = param.getString("spaceId");
            String type = param.getString("objectType");
            ObjectNode node = JsonNodeFactory.instance.objectNode();
            node.put("graphCode", DEFAULT_GRAPH_CODE);
            node.put("relCode", DEFAULT_REL_CODE);
            node.put("objFrom", buildingId);
            node.put("objTo",spaceId);
            QueryCriteria criteria = new QueryCriteria();
            criteria.setCriteria(node);
            service.doDelete(context, criteria);
            // 创建关系对象
            List<RelationDTO> voList = new ArrayList<>();
            voList.add(new RelationDTO(null, DEFAULT_GRAPH_CODE, DEFAULT_REL_CODE, type, buildingId , spaceId));
            service.doSave(context, voList);
        }
        return AdmResponse.success();
    }
}
