package com.persagy.proxy.relation.controller;

import cn.hutool.core.util.StrUtil;
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.constant.AdmRelationTypeEnum;
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 com.persagy.proxy.relation.model.SpForSyBaseList;
import com.persagy.proxy.relation.model.SyForSpBase;
import com.persagy.proxy.relation.model.SyForSpBaseList;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
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;


/**
 * @author zhangwencheng
 * 系统服务于业务空间
 */


@Slf4j
@RestController
@RequestMapping("/rel/sy-for-sp-base")
public class RelationSyForSpController {



    @Autowired
    private IAdmRelationService service;

/**
     * 创建系统服务于业务空间关系
     *
     * @param rSyForSpBase
     * @return
     * @throws Exception
     */

    @PostMapping("/link")
    public AdmResponse create(@RequestBody SyForSpBase rSyForSpBase) throws Exception {
        log.info("创建系统服务于业务空间关系create方法开始参数projectId为{}");
        //系统Id
        String sysID = rSyForSpBase.getSysId();
        //业务空间Id
        String spaceId = rSyForSpBase.getSpaceId();
        if (StrUtil.isBlank(sysID) || StrUtil.isBlank(spaceId)) {
            return AdmResponse.failure("必填项：sysID（系统id）、SpaceId（业务空间id）");
        }
        AdmRelationTypeEnum sy2spFor = AdmRelationTypeEnum.SY2SP_FOR;
        String graphCode = sy2spFor.getGraphCode();
        String relCode = sy2spFor.getRelCode();
        List<RelationDTO> voList = new ArrayList<>();
        RelationDTO relation = new RelationDTO(null, graphCode, relCode, rSyForSpBase.getObjectType(), sysID, spaceId);
        voList.add(relation);
        service.doSave(AdmContextUtil.toDmpContext(), voList);
        log.info("创建系统服务于业务空间关系create方法结束");
        return AdmResponse.success();
    }


/**
     * 系统服务于业务空间关系，系统一对多
     *
     * @return
     * @throws Exception
     */

    @PostMapping("/link-sp")
    public AdmResponse createSp(@RequestBody SyForSpBaseList syForSpBaseList) throws Exception {
        log.info("创建系统空间关系createSp开始");
        InstanceUrlParam context = AdmContextUtil.toDmpContext();
        List<String> spaceIdList = syForSpBaseList.getSpaceIdList();
        String sysId = syForSpBaseList.getSysId();
        List<RelationDTO> voList = new ArrayList<>();
        AdmRelationTypeEnum sy2spFor = AdmRelationTypeEnum.SY2SP_FOR;
        String graphCode = sy2spFor.getGraphCode();
        String relCode = sy2spFor.getRelCode();
        //校验输入参数
        if (CollectionUtils.isEmpty(spaceIdList) || StringUtils.isEmpty(sysId)) {
            return AdmResponse.failure("必填项：SpaceIdList（业务空间id列表）、SysID(系统id)");
        }
        for (String spaceId : spaceIdList) {
            RelationDTO relation = new RelationDTO(null, graphCode, relCode, "", sysId, spaceId);
            voList.add(relation);
        }
        service.doSave(context, voList);
        log.info("创建系统空间关系createSp结束");
        return AdmResponse.success();
    }


/**
     * 服务于业务空间的系统，业务空间一对多
     *
     * @param spForSyBaseList
     * @return
     * @throws Exception
     */
    @PostMapping("/link-sy")
    public AdmResponse createSy(@RequestBody SpForSyBaseList spForSyBaseList) {
        log.info("创建系统空间关系业务空间一对多link-sy开始");
        InstanceUrlParam context = AdmContextUtil.toDmpContext();
        List<String> sysIdList = spForSyBaseList.getSysIdList();
        String spaceId = spForSyBaseList.getSpaceId();
        List<RelationDTO> voList = new ArrayList<>();
        if (CollectionUtils.isEmpty(sysIdList) || StringUtils.isEmpty(spaceId)) {
            return AdmResponse.failure("必填项：spaceId业务空间id,SysIDList(系统id列表)）");
        }
        AdmRelationTypeEnum sy2spFor = AdmRelationTypeEnum.SY2SP_FOR;
        String graphCode = sy2spFor.getGraphCode();
        String relCode = sy2spFor.getRelCode();
        for (String sysId: sysIdList) {
            RelationDTO relation = new RelationDTO(null, graphCode, relCode, "", sysId, spaceId);
            voList.add(relation);
        }
        service.doSave(context, voList);
        log.info("创建系统空间关系业务空间一对多link-sy结束");
        return AdmResponse.success();
    }



/**
     * 根据对象删除系统服务于业务空间关系，只针对一个对象
     * @param syForSpBase
     * @return
     * @throws Exception
     */
    @PostMapping("/unlink")
    public AdmResponse delete(@RequestBody SyForSpBase syForSpBase) throws Exception {
        log.info("创建系统服务于业务空间关系create方法开始参数projectId为{}");
        //系统Id
        String sysID = syForSpBase.getSysId();
        //业务空间Id
        String spaceId = syForSpBase.getSpaceId();
        if (StrUtil.isBlank(sysID) || StrUtil.isBlank(spaceId)) {
            return AdmResponse.failure("必填项：sysID（系统id）、SpaceId（业务空间id）");
        }
        // 组装上下文条件
        InstanceUrlParam context = AdmContextUtil.toDmpContext();
        QueryCriteria criteria = new QueryCriteria();
        ObjectNode node = JsonNodeFactory.instance.objectNode();
        AdmRelationTypeEnum sy2spFor = AdmRelationTypeEnum.SY2SP_FOR;
        String graphCode = sy2spFor.getGraphCode();
        String relCode = sy2spFor.getRelCode();
        node.put("graphCode", graphCode);
        node.put("relCode", relCode);
//        node.put("relValue", syForSpBase.getObjectType());
        node.put("objFrom", sysID);
        node.put("objTo", spaceId);
        criteria.setCriteria(node);
        service.doDelete(context, criteria);
        log.info("创建系统服务于业务空间关系create方法结束");
        return AdmResponse.success();
    }
}

