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.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:020、关系-设备所在竖井
 * @author:lgy
 * @data:2021/9/13 10:00
 */
@RequestMapping("/rel/eq-in-sh")
@RestController
public class RelationEqInShController {
    @Autowired
    private IAdmRelationService service;
    /**
     * 创建设备所在竖井关系，累计添加不会覆盖以前记录
     * @param param
     * @return
     * @throws Exception
     */
    @PostMapping(value = {"/link"})
    public AdmResponse create(@RequestBody JSONObject param) throws Exception {
        String equipId = param.getString("equipId");
        String shaftId = param.getString("shaftId");
        if(!StrUtil.isAllNotEmpty(equipId,shaftId)) {
            return AdmResponse.failure("必填项：EquipId（设备id）、ShaftId（竖井id）");
        }
        // 创建关系对象  EquipinShaft
        List<RelationDTO> voList = new ArrayList<>();
        voList.add(new RelationDTO(null, "MechInArch", "Eq2Sh", null, equipId , shaftId));
        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 equipId = param.getString("equipId");
        String shaftId = param.getString("shaftId");
        if(!StrUtil.isAllNotEmpty(equipId,shaftId)) {
            return AdmResponse.failure("必填项：EquipId（设备id）、shaftId（竖井id）");
        }
        ObjectNode node = JsonNodeFactory.instance.objectNode();
        node.put("graphCode", "MechInArch");
        node.put("relCode", "Eq2Sh");
        node.put("objFrom", equipId);
        node.put("objTo",shaftId);
        criteria.setCriteria(node);
        service.doDelete(context, criteria);
        return AdmResponse.success();
    }
    /**
     * 设备所在竖井，设备一对多，此方法会覆盖以前的记录
     * @param param
     * @return
     * @throws Exception
     */
    @PostMapping("/link-sh")
    public AdmResponse createSh(@RequestBody JSONObject param) throws Exception {
        String equipId = param.getString("equipId");
        JSONArray shaftIdList = param.getJSONArray("shaftIdList");
        if(StrUtil.isBlank(equipId) || CollUtil.isEmpty(shaftIdList)) {
            return AdmResponse.failure("必填项：EquipId（设备id）、ShaftIdList（竖井id列表）,此方法会覆盖以前的记录");
        }
        // 创建关系对象.先删除，后添加
        InstanceUrlParam context = AdmContextUtil.toDmpContext();
        List<RelationDTO> voList = new ArrayList<>();
        for(int i = 0;i < shaftIdList.size();i++) {
            String shaftId = shaftIdList.getString(i);
            if(StrUtil.isBlank(shaftId)) {
                continue;
            }
            QueryCriteria queryRequest = new QueryCriteria();
            ObjectNode node = JsonNodeFactory.instance.objectNode();
            node.put("graphCode", "MechInArch");
            node.put("relCode", "Eq2Sh");
            node.put("objFrom", equipId);
            node.put("objTo", shaftId);
            queryRequest.setCriteria(node);
            service.doDelete(context,queryRequest);
            // 创建关系对象 - 参考(datasyn中DataCenterSync.kt)
            voList.add(new RelationDTO(null, "MechInArch", "Eq2Sh", null, equipId, shaftId));
        }
        // 组装上下文条件
        service.doSave(context, voList);
        return AdmResponse.success();
    }
    /**
     * 所在竖井的设备，竖井一对多，此方法会覆盖以前的记录
     * @param param
     * @return
     * @throws Exception
     */
    @PostMapping(value = {"/link-eq","/link-eq-sh"})
    public AdmResponse createEq(@RequestBody JSONObject param) throws Exception {
        String shaftId = param.getString("shaftId");
        JSONArray equipIdList = param.getJSONArray("equipIdList");
        if(StrUtil.isBlank(shaftId) || CollUtil.isEmpty(equipIdList)) {
            return AdmResponse.failure("必填项：EquipIdList（设备id列表）、ShaftId（竖井id）,此方法会覆盖以前的记录");
        }
        // 创建关系对象.先删除，后添加
        InstanceUrlParam context = AdmContextUtil.toDmpContext();
        List<RelationDTO> voList = new ArrayList<>();
        for(int i = 0;i < equipIdList.size();i++) {
            String equipId = equipIdList.getString(i);
            if(StrUtil.isBlank(shaftId)) {
                continue;
            }
            QueryCriteria queryRequest = new QueryCriteria();
            ObjectNode node = JsonNodeFactory.instance.objectNode();
            node.put("graphCode", "MechInArch");
            node.put("relCode", "Eq2Sh");
            node.put("objFrom", equipId);
            node.put("objTo", shaftId);
            queryRequest.setCriteria(node);
            service.doDelete(context,queryRequest);
            // 创建关系对象 - 参考(datasyn中DataCenterSync.kt)
            voList.add(new RelationDTO(null, "MechInArch", "Eq2Sh", null, equipId, shaftId));
        }
        // 组装上下文条件
        service.doSave(context, voList);
        return AdmResponse.success();
    }
}
