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.EqForFl;
import com.persagy.proxy.relation.model.EqForFlList;
import com.persagy.proxy.relation.model.FlForEqList;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
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;

/**
 * 037 -设备服务于楼层
 *
 * @author zhangwencheng
 */

@Slf4j
@RestController
@RequestMapping("/rel/eq-for-fl")
public class RelationEqForFlController {

    @Autowired
    private IAdmRelationService service;

    /**
     * 创建设备服务于楼层关系
     * 必填项：EquipId（设备id）、FloorId（楼层id）
     *
     * @param eqForFl 设备和楼层的关系对象
     * @return 创建的结果信息
     */
    @PostMapping("/link")
    public AdmResponse create(@RequestBody EqForFl eqForFl) {
        String equipId = eqForFl.getEquipId();
        String floorId = eqForFl.getFloorId();
        //检验输入参数
        if (StrUtil.isEmpty(equipId) || StrUtil.isEmpty(floorId)) {
            return AdmResponse.failure("必填项：equipId（设备id）、buildingId（建筑id）");
        }
        AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.EQ2FL_FOR;
        String graphCode = typeEnum.getGraphCode();
        String relCode = typeEnum.getRelCode();
        List<RelationDTO> voList = new ArrayList<>();
        RelationDTO relation = new RelationDTO(null, graphCode, relCode, "", equipId, floorId);
        voList.add(relation);
        service.doSave(AdmContextUtil.toDmpContext(), voList);
        return AdmResponse.success();
    }


    /**
     * 创建服务于楼层的设备关系
     *
     * @param flForEqList 设备和楼层关系对象
     * @return 创建的结果信息
     */
    @PostMapping("link-eq")
    public AdmResponse createEq(@RequestBody FlForEqList flForEqList) {
        List<String> equipIdList = flForEqList.getEquipIdList();
        String floorId = flForEqList.getFloorId();
        //检验输入参数
        if (CollectionUtils.isEmpty(equipIdList) || StrUtil.isEmpty(floorId)) {
            return AdmResponse.failure("必填项：equipId（设备id）、equipIdList（设备Id列表）");
        }
        List<RelationDTO> voList = new ArrayList<>();
        InstanceUrlParam context = AdmContextUtil.toDmpContext();
        AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.EQ2FL_FOR;
        String graphCode = typeEnum.getGraphCode();
        String relCode = typeEnum.getRelCode();
        //根据floorId删除信息
        deleteByObjId(null, floorId, typeEnum, context);
        for (String equipId : equipIdList) {
            RelationDTO relation = new RelationDTO(null, graphCode, relCode, "", equipId, floorId);
            voList.add(relation);
        }
        service.doSave(AdmContextUtil.toDmpContext(), voList);
        return AdmResponse.success();
    }

    /**
     * 设备服务于楼层，设备一对多
     * 必填项：FloorIdList（楼层id列表）、EquipId（设备id）,测方法会覆盖以前的记录
     *
     * @return 创建的结果信息
     */
    @PostMapping("link-fl")
    public AdmResponse createFl(@RequestBody EqForFlList eqForFlList) {
        String equipId = eqForFlList.getEquipId();
        List<String> floorIdList = eqForFlList.getFloorIdList();
        //检验输入参数
        if (CollectionUtils.isEmpty(floorIdList) || StrUtil.isEmpty(equipId)) {
            return AdmResponse.failure("必填项：equipId（设备id）、floorIdList（楼层Id列表）");
        }
        List<RelationDTO> voList = new ArrayList<>();
        InstanceUrlParam context = AdmContextUtil.toDmpContext();
        AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.EQ2FL_FOR;
        String graphCode = typeEnum.getGraphCode();
        String relCode = typeEnum.getRelCode();
        deleteByObjId(equipId, null, typeEnum, context);
        for (String floorId : floorIdList) {
            RelationDTO relation = new RelationDTO(null, graphCode, relCode, "", equipId, floorId);
            voList.add(relation);
        }
        service.doSave(context, voList);
        return AdmResponse.success();
    }

    /**
     * 根据对象删除设备服务于楼层的关系，只针对一个对象
     * 必填项：EquipId（设备id）、FloorId(楼层id)
     * @param   eqForFl      对象
     * @return                删除的结果信息
     */
    @PostMapping(value = "/unlink")
    public AdmResponse delete(@RequestBody EqForFl eqForFl){
        String floorId = eqForFl.getFloorId();
        String equipId = eqForFl.getEquipId();
        if (StrUtil.isEmpty(floorId) || StrUtil.isEmpty(equipId)) {
            return AdmResponse.failure("必填项：equipId（设备id）、floorId（楼层floorId）");
        }
        InstanceUrlParam context = AdmContextUtil.toDmpContext();
        AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.EQ2FL_FOR;
        deleteByObjId(equipId, floorId, typeEnum, context);
        return  AdmResponse.success();
    }
    /**
     * 根据楼层Id删除
     *
     * @param typeEnum
     * @param context
     */
    private void deleteByObjId(String equipId, String floorId, 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("objFrom", equipId);
        node.put("objTo", floorId);
        criteria.setCriteria(node);
        service.doDelete(context, criteria);
    }

}
