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.relation.model.EqForSh;
import com.persagy.proxy.relation.model.EqForShList;
import com.persagy.proxy.relation.model.ShForEqList;
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.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author zhangwencheng
 * 设备服务于竖井
 */
@RestController
@RequestMapping("/rel/eq-for-sh")
public class RelationEqForShController {

    @Autowired
    private IAdmRelationService service;

    /**
     * 创建设备服务于竖井关系
     * 必填项：EquipId（设备id）、ShaftId（竖井id）
     *
     * @return 创建的结果信息
     */
    @PostMapping("/link")
    public AdmResponse create(@RequestBody EqForSh eqForSh) {
        String equipId = eqForSh.getEquipId();
        String shaftId = eqForSh.getShaftId();
        String type = eqForSh.getType();
        //检验输入参数
        if (StrUtil.isEmpty(equipId) || StrUtil.isEmpty(shaftId)) {
            return AdmResponse.failure("必填项：equipId（设备id）、shaftId（竖井Id）");
        }
        AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.EQ2SH_FOR;
        String graphCode = typeEnum.getGraphCode();
        String relCode = typeEnum.getRelCode();
        List<RelationDTO> voList = new ArrayList<>();
        RelationDTO relation = new RelationDTO(null, graphCode, relCode, type, equipId, shaftId);
        voList.add(relation);
        service.doSave(AdmContextUtil.toDmpContext(), voList);
        return AdmResponse.success();
    }

    /**
     * 创建设备服务于竖井关系
     * 必填项：ShaftIdList（竖井id列表）、EquipId（设备id）,测方法会覆盖以前的记录
     *
     * @param
     * @return 创建的结果信息
     */
    @PostMapping("/link-sh")
    public AdmResponse createSh(@RequestBody EqForShList eqForShList) {
        String equipId = eqForShList.getEquipId();
        List<String> shaftIdList = eqForShList.getShaftIdList();
        //检验输入参数
        if (StrUtil.isEmpty(equipId) || CollectionUtils.isEmpty(shaftIdList)) {
            return AdmResponse.failure("必填项：equipId（设备id）、shaftIdList（竖井Id列表）");
        }
        AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.EQ2SH_FOR;
        String graphCode = typeEnum.getGraphCode();
        String relCode = typeEnum.getRelCode();
        InstanceUrlParam context = AdmContextUtil.toDmpContext();
        deleteByObjId(equipId, null, null, typeEnum, context);
        List<RelationDTO> voList = new ArrayList<>();
        for (String shaftId : shaftIdList) {
            RelationDTO relation = new RelationDTO(null, graphCode, relCode, "", equipId, shaftId);
            voList.add(relation);
        }
        service.doSave(context, voList);
        return AdmResponse.success();
    }

    /**
     * 服务于竖井的设备，竖井一对多
     * 必填项：ShaftId（竖井id）、EquipIdList（设备id列表）,测方法会覆盖以前的记录
     *
     * @param shForEqList 设备和竖井的关系对象
     * @return 创建的结果信息
     */
    @PostMapping("/link-eq")
    public AdmResponse createEq(@RequestBody ShForEqList shForEqList) {
        List<String> equipIdList = shForEqList.getEquipIdList();
        String shaftId = shForEqList.getShaftId();
        //检验输入参数
        if (StrUtil.isEmpty(shaftId) || CollectionUtils.isEmpty(equipIdList)) {
            return AdmResponse.failure("必填项：equipId（设备id）、shaftIdList（竖井Id列表）");
        }
        InstanceUrlParam context = AdmContextUtil.toDmpContext();
        AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.EQ2SH_FOR;
        String graphCode = typeEnum.getGraphCode();
        String relCode = typeEnum.getRelCode();
        deleteByObjId(null, shaftId, null, typeEnum, context);
        List<RelationDTO> voList = new ArrayList<>();
        for (String equipId : equipIdList) {
            RelationDTO relation = new RelationDTO(null, graphCode, relCode, "", equipId, shaftId);
            voList.add(relation);
        }
        service.doSave(context, voList);
        return AdmResponse.success();
    }

    /**
     * 根据对象删除设备服务于竖井关系，只针对一个对象
     * 必填项：EquipId（设备id）、shaftId（竖井id）
     *
     * @param eqForSh 对象
     * @return 删除的结果信息
     */
    @PostMapping("/unlink")
    public AdmResponse delete(@RequestBody EqForSh eqForSh) {
        String shaftId = eqForSh.getShaftId();
        String equipId = eqForSh.getEquipId();
        //检验输入参数
        if (StrUtil.isEmpty(equipId) || StrUtil.isEmpty(shaftId)) {
            return AdmResponse.failure("必填项：equipId（设备id）、shaftId（竖井Id）");
        }
        String type = eqForSh.getType();
        AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.EQ2SH_FOR;
        deleteByObjId(equipId, shaftId, type, typeEnum, AdmContextUtil.toDmpContext());
        return AdmResponse.success();
    }


    private void deleteByObjId(String objFrom, String objTo, String type, AdmRelationTypeEnum typeEnum, InstanceUrlParam context) {
        QueryCriteria criteria = new QueryCriteria();
        ObjectNode node = JsonNodeFactory.instance.objectNode();
        node.put("graphCode", typeEnum.getGraphCode());
        node.put("relCode", typeEnum.getRelCode());
        node.put("relValue", type);
        node.put("objFrom", objFrom);
        node.put("objTo", objTo);
        criteria.setCriteria(node);
        service.doDelete(context, criteria);
    }
}
