package com.sptg.device.biz.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.device.api.entity.MqConf;
import com.sptg.device.api.feigns.MqConfFeign;
import com.sptg.device.api.util.RabbitMqConnectionUtil;
import com.sptg.device.biz.service.MqConfServiceInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * title: MqConfController
 * package: com.sptg.device.biz.controller
 * description: 队列配置-控制器
 *
 *                  
 *                
 */
@RestController
@RequiredArgsConstructor
public class MqConfController implements MqConfFeign {

    private final MqConfServiceInterface mqConfServiceInterface;

    @Override
    @PostMapping(value = "/mq/pagingQuery")
    public Page<MqConf> pagingQuery(@RequestBody Page<MqConf> page) {
        return this.mqConfServiceInterface.selectPage(page);
    }

    @Override
    @PostMapping(value = "/mq/addEntity")
    public ResultBean<Boolean> addEntity(@RequestBody MqConf conf) {
        return new ResultBean<>(ResultEnum.SUCCESS,this.mqConfServiceInterface.insert(conf));
    }

    @Override
    @GetMapping(value = "/mq/get/{id}")
    public MqConf getMqById(@PathVariable(value = "id")Long id) {
        return this.mqConfServiceInterface.selectById(id);
    }

    @Override
    @PostMapping(value = "/mq/editEntity")
    public ResultBean<Boolean> editEntity(@RequestBody MqConf conf) {
        boolean result = this.mqConfServiceInterface.updateById(conf);
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }

    @Override
    @GetMapping(value = "/mq/deleteEntity/{id}")
    public ResultBean<Boolean> deleteEntityById(@PathVariable(value = "id") Long id) {
        MqConf conf = this.mqConfServiceInterface.selectById(id);
        boolean result = true;
        if (null != conf){
            //  判断队列的在线状态是否可删除
            Assert.isTrue(conf.getOnLineStatus() != OperationConstant.ONE,"队列服务器正在使用中");
            conf.setStatus(OperationConstant.MINUS_ONE);
            result = this.mqConfServiceInterface.updateById(conf);
        }
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }

    @Override
    @GetMapping(value = "/mq/startServer/{id}")
    public ResultBean<Boolean> startServer(@PathVariable(value = "id") Long id) {
        MqConf conf = this.mqConfServiceInterface.selectById(id);
        Assert.notNull(conf,"队列服务器Id不存在");
        //  判断队列是否可用
        Assert.isTrue(RabbitMqConnectionUtil.testConnect(conf),"队列服务器拒绝连接");
        conf.setOnLineStatus(OperationConstant.ONE);
        return this.editEntity(conf);
    }

    @Override
    @GetMapping(value = "/mq/stopServer/{id}")
    public ResultBean<Boolean> stopServer(@PathVariable(value = "id")Long id) {
        MqConf conf = this.mqConfServiceInterface.selectById(id);
        Assert.notNull(conf,"队列服务器Id不存在");
        conf.setOnLineStatus(OperationConstant.ZERO);
        boolean result = this.mqConfServiceInterface.updateById(conf);
        //  TODO    通知设备与该队列服务器断开连接

        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }
}
