package com.persagy.proxy.relation.controller;

import cn.hutool.core.collection.CollUtil;
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.entity.InstanceUrlParam;
import com.persagy.proxy.common.entity.RelationDTO;
import org.springframework.beans.factory.annotation.Autowired;
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:022、关系-竖井的贯通关系
 * @author:lgy
 * @data:2021/9/13 13:06
 */
@RestController
@RequestMapping("/rel/sh-through-sh")
public class RelationShaftThroughShaftController {
    @Autowired
    private IAdmRelationService service;
    /**
     * 创建竖井的贯通关系
     * @param param
     * @return
     * @throws Exception
     */
    @PostMapping(value = {"/link"})
    public AdmResponse create(@RequestBody JSONObject param) throws Exception {
        String shaftId = param.getString("shaftId");
        String shaftOtherId = param.getString("shaftOtherId");
        if(!StrUtil.isAllNotEmpty(shaftId,shaftOtherId)) {
            return AdmResponse.failure("必填项：ShaftId（竖井id）、ShaftOtherId（其他竖井id）");
        }
        // 创建关系对象
        List<RelationDTO> voList = new ArrayList<>();
        voList.add(new RelationDTO(null, "ThroughRelationship", "Sh2Sh", null, shaftId , shaftOtherId));
        service.doSave(AdmContextUtil.toDmpContext(), voList);
        return AdmResponse.success();
    }
    /**
     * 根据对象删除关系
     * 根据对象删除竖井的贯通关系，只针对一个对象
     * @param   param      对象
     * @return             删除的结果信息
     */

    @PostMapping("/unlink")
    public AdmResponse delete(@RequestBody JSONObject param) throws Exception {
        if(CollUtil.isEmpty(param)) {
            return AdmResponse.success();
        }
        // 组装上下文条件
        InstanceUrlParam context = AdmContextUtil.toDmpContext();
        // 组装条件
        QueryCriteria criteria = new QueryCriteria();
        String shaftId = param.getString("shaftId");
        String shaftOtherId = param.getString("shaftOtherId");
        if(!StrUtil.isAllNotEmpty(shaftId,shaftOtherId)) {
            return AdmResponse.failure("必填项：ShaftId（竖井id）、ShaftOtherId(其他数据竖井id)");
        }
        ObjectNode node = JsonNodeFactory.instance.objectNode();
        node.put("graphCode", "ThroughRelationship");
        node.put("relCode", "Sh2Sh");
        node.put("objFrom", shaftId);
        node.put("objTo",shaftOtherId);
        criteria.setCriteria(node);
        service.doDelete(context, criteria);
        return AdmResponse.success();
    }
    /**
     * 创建竖井的贯通关系
     * 竖井的贯通，竖井一对多，此方法会覆盖以前的记录
     * @return
     * @throws Exception
     */
    @PostMapping("/link-or")
    public AdmResponse createFloorOtherIdList(@RequestBody JSONObject param) throws Exception {
        String shaftId = param.getString("shaftId");
        JSONArray shaftOtherIdList = param.getJSONArray("shaftOtherIdList");
        if(StrUtil.isBlank(shaftId) || CollUtil.isEmpty(shaftOtherIdList)) {
            return AdmResponse.failure("必填项：ShaftId（竖井id）、ShaftOtherIdList（其他竖井id列表）,此方法会覆盖以前的记录");
        }
        // 创建关系对象.先删除，后添加
        InstanceUrlParam context = AdmContextUtil.toDmpContext();
        List<RelationDTO> voList = new ArrayList<>();
        for(int i = 0;i < shaftOtherIdList.size();i++) {
            String shaftOtherId = shaftOtherIdList.getString(i);
            if(StrUtil.isBlank(shaftOtherId)) {
                continue;
            }
            QueryCriteria queryRequest = new QueryCriteria();
            ObjectNode node = JsonNodeFactory.instance.objectNode();
            node.put("graphCode", "ThroughRelationship");
            node.put("relCode", "Sh2Sh");
            node.put("objFrom", shaftId);
            node.put("objTo", shaftOtherId);
            queryRequest.setCriteria(node);
            service.doDelete(context,queryRequest);
            // 创建关系对象 - 参考(datasyn中DataCenterSync.kt)
            voList.add(new RelationDTO(null, "ThroughRelationship", "Sh2Sh", null, shaftId, shaftOtherId));
        }
        // 组装上下文条件
        service.doSave(context, voList);
        return AdmResponse.success();
    }

    /**
     * 创建竖井的贯通关系
     * 竖井的贯通，竖井一对多，此方法会覆盖以前的记录
     * @param param 竖井和其他竖井列表关系对象r
     * @return
     * @throws Exception
     */
    @PostMapping("/link-shaft")
    public AdmResponse createFloorIdList(@RequestBody JSONObject param) throws Exception {
        String shaftId = param.getString("shaftId");
        JSONArray shaftOtherIdList = param.getJSONArray("shaftOtherIdList");
        if(StrUtil.isBlank(shaftId) || CollUtil.isEmpty(shaftOtherIdList)) {
            return AdmResponse.failure("必填项：ShaftId（竖井id）、ShaftOtherIdList（其他竖井id列表）");
        }
        // 创建关系对象.先删除，后添加
        InstanceUrlParam context = AdmContextUtil.toDmpContext();
        List<RelationDTO> voList = new ArrayList<>();
        for(int i = 0;i < shaftOtherIdList.size();i++) {
            String shaftOtherId = shaftOtherIdList.getString(i);
            if(StrUtil.isBlank(shaftOtherId)) {
                continue;
            }
            QueryCriteria queryRequest = new QueryCriteria();
            ObjectNode node = JsonNodeFactory.instance.objectNode();
            node.put("graphCode", "ThroughRelationship");
            node.put("relCode", "Sh2Sh");
            node.put("objFrom", shaftId);
            node.put("objTo", shaftOtherId);
            queryRequest.setCriteria(node);
            service.doDelete(context,queryRequest);
            // 创建关系对象 - 参考(datasyn中DataCenterSync.kt)
            voList.add(new RelationDTO(null, "ThroughRelationship", "Sh2Sh", null, shaftId, shaftOtherId));
        }
        // 组装上下文条件
        service.doSave(context, voList);
        return AdmResponse.success();
    }
}
