package com.persagy.proxy.relation.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.util.CollectionUtils;
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.relation.model.ShInSyList;
import com.persagy.proxy.relation.model.SyInSh;
import com.persagy.proxy.relation.model.SyInShList;
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 lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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;

/**
 * @author zhangwencheng
 * 系统所在楼层相关接口-032
 */
@Slf4j
@RestController
@RequestMapping("/rel/sy-in-sh")
public class RelationSyInShController {

    @Autowired
    private IAdmRelationService service;

    /**
     * 创建系统所在竖井关系
     *
     * @param syInSh 系统和竖井的关系对象
     * @return project 创建的结果信息
     */
    @PostMapping(value = "/link")
    public AdmResponse create(@RequestBody SyInSh syInSh) {
        //系统Id
        String sysID = syInSh.getSysId();
        //楼层Id
        String shaftId = syInSh.getShaftId();
        //
        String type = syInSh.getType();
        if (StrUtil.isBlank(sysID) || StrUtil.isBlank(shaftId)) {
            return AdmResponse.failure("必填项：SysId（系统id）、shaftId（竖井id）");
        }
        List<RelationDTO> voList = new ArrayList<>();
        AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.SY2SH;
        String graphCode = typeEnum.getGraphCode();
        String relCode = typeEnum.getRelCode();
        RelationDTO relation = new RelationDTO(null, graphCode, relCode, type, sysID, shaftId);
        voList.add(relation);
        service.doSave(AdmContextUtil.toDmpContext(), voList);
        return AdmResponse.success();
    }

    /**
     * 根据对象删除
     *
     * @param syInSh
     * @param projectId
     * @return
     */
    @PostMapping("/unlink")
    public AdmResponse delete(@RequestBody SyInSh syInSh) {
        String sysID = syInSh.getSysId();
        //楼层Id
        String shaftId = syInSh.getShaftId();
        //
//        String type = syInSh.getType();
        if (StrUtil.isBlank(sysID) || StrUtil.isBlank(shaftId)) {
            return AdmResponse.failure("必填项：SysId（系统id）、shaftId（竖井id）");
        }
        AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.SY2SH;
        String graphCode = typeEnum.getGraphCode();
        String relCode = typeEnum.getRelCode();
        ObjectNode node = JsonNodeFactory.instance.objectNode();
        node.put("graphCode", graphCode);
        node.put("relCode", relCode);
//        node.put("relValue", type);
        node.put("objFrom", sysID);
        node.put("objTo", shaftId);
        QueryCriteria criteria = new QueryCriteria();
        criteria.setCriteria(node);
        service.doDelete(AdmContextUtil.toDmpContext(), criteria);
        return AdmResponse.success();
    }


    /**
     * 创建系统所在竖井关系
     *
     * @param syInShList 系统和竖井的关系对象
     * @return 创建的结果信息
     */
    @PostMapping(value = "/link-sh")
    public AdmResponse createSh(@RequestBody SyInShList syInShList) {
        String sysId = syInShList.getSysId();
        List<String> shaftIdList = syInShList.getShaftIdList();
        if (StringUtils.isBlank(sysId) || CollectionUtils.isEmpty(shaftIdList)) {
            return AdmResponse.failure("必填项：sysId（系统sysId不能为空）、shaftIdList（竖井Id不能为空）");
        }
        AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.SY2SH;
        InstanceUrlParam context = AdmContextUtil.toDmpContext();
        deleteBySysId(typeEnum,sysId,context);
        String graphCode = typeEnum.getGraphCode();
        String relCode = typeEnum.getRelCode();
        List<RelationDTO> voList = new ArrayList<>();
        for (String shaftId : shaftIdList) {
            RelationDTO relation = new RelationDTO(null, graphCode, relCode, null, sysId, shaftId);
            voList.add(relation);
        }
        service.doSave(context, voList);
        return AdmResponse.success();
    }

    /**
     * 根据系统Id删除关系
     * @param typeEnum
     * @param sysId
     * @param context
     */
    private void deleteBySysId(AdmRelationTypeEnum typeEnum, String sysId, InstanceUrlParam context) {
        ObjectNode node = JsonNodeFactory.instance.objectNode();
        node.put("graphCode", typeEnum.getGraphCode());
        node.put("relCode", typeEnum.getRelCode());
        node.put("objFrom", sysId);
        QueryCriteria criteria = new QueryCriteria();
        criteria.setCriteria(node);
        service.doDelete(context, criteria);
    }


    /**
     * 创建所在竖井的系统关系 先删除以前的竖井在增加新的关系
     *
     * @param     shInSyList    系统和竖井的关系对象
     * @return               创建的结果信息
     */
    @PostMapping(value = "/link-sy")
    public AdmResponse createSy(@RequestBody ShInSyList shInSyList){
        List<String> sysIdList = shInSyList.getSysIdList();
        String shaftId = shInSyList.getShaftId();
        if (StringUtils.isBlank(shaftId) || CollectionUtils.isEmpty(sysIdList)) {
            return AdmResponse.failure("必填项：shaftId（竖井shaftId不能为空）、sysIdList（系统Id不能为空）");
        }
        //先删除以前竖井
        AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.SY2SH;
        InstanceUrlParam context = AdmContextUtil.toDmpContext();
        deleteByShaftId(shaftId,typeEnum,context);
        String graphCode = typeEnum.getGraphCode();
        String relCode = typeEnum.getRelCode();
        List<RelationDTO> voList = new ArrayList<>();
        for (String sysId : sysIdList) {
            RelationDTO relation = new RelationDTO(null, graphCode, relCode, null, sysId, shaftId);
            voList.add(relation);
        }
        service.doSave(context, voList);
        return  AdmResponse.success();
    }

    /**
     * 根据竖井Id删除竖井关系
     * @param shaftId
     */
    private void deleteByShaftId(String shaftId,AdmRelationTypeEnum typeEnum,InstanceUrlParam context) {
        ObjectNode node = JsonNodeFactory.instance.objectNode();
        node.put("graphCode", typeEnum.getGraphCode());
        node.put("relCode", typeEnum.getRelCode());
        node.put("objTo", shaftId);
        QueryCriteria criteria = new QueryCriteria();
        criteria.setCriteria(node);
        service.doDelete(context, criteria);
    }

    /**
     * 创建所在竖井的系统关系
     *
     * @param     shInSyList    系统和竖井的关系对象
     * @return               创建的结果信息
     */
    @PostMapping("/link-sy-sh")
    public AdmResponse createSySh(@RequestBody ShInSyList shInSyList){
        String shaftId = shInSyList.getShaftId();
        List<String> sysIdList = shInSyList.getSysIdList();
        if (StringUtils.isBlank(shaftId) || CollectionUtils.isEmpty(sysIdList)) {
            return AdmResponse.failure("必填项：shaftId（竖井shaftId不能为空）、sysIdList（系统Id不能为空）");
        }
        AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.SY2SH;
        String graphCode = typeEnum.getGraphCode();
        String relCode = typeEnum.getRelCode();
        List<RelationDTO> voList = new ArrayList<>();
        for (String sysId : sysIdList) {
            RelationDTO relation = new RelationDTO(null, graphCode, relCode, null, sysId, shaftId);
            voList.add(relation);
        }
        service.doSave(AdmContextUtil.toDmpContext(), voList);
        return AdmResponse.success();
    }

    /**
     * 系统所在竖井，系统一对多，不覆盖
     *
     * @param    syInShList    系统和竖井的关系对象
     * @return              创建的结果信息
     */
    @PostMapping("/link-sy-shs")
    public AdmResponse createSyShs(@RequestBody  SyInShList syInShList){
        String sysId = syInShList.getSysId();
        List<String> shaftIdList = syInShList.getShaftIdList();
        if (StringUtils.isBlank(sysId) || CollectionUtils.isEmpty(shaftIdList)) {
            return AdmResponse.failure("必填项：shaftId（竖井shaftId不能为空）、sysId（系统Id不能为空）");
        }
        AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.SY2SH;
        String graphCode = typeEnum.getGraphCode();
        String relCode = typeEnum.getRelCode();
        List<RelationDTO> voList = new ArrayList<>();
        for (String shaftId : shaftIdList) {
            RelationDTO relation = new RelationDTO(null, graphCode, relCode, null, sysId, shaftId);
            voList.add(relation);
        }
        service.doSave(AdmContextUtil.toDmpContext(), voList);
        return AdmResponse.success();
    }
}
