package com.sptg.gateway.controller.device;

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.SysLogConstant;
import com.sptg.common.core.util.WebUtils;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.common.security.service.SecurityServiceInterface;
import com.sptg.device.api.constant.DeviceSecurityToken;
import com.sptg.device.api.entity.MqConf;
import com.sptg.device.api.feigns.MqConfFeign;
import com.sptg.permissions.api.entity.Account;
import com.sptg.statistics.api.constant.SysLogMenuConstant;
import com.sptg.statistics.api.entity.SysOperationLog;
import com.sptg.statistics.api.feigns.SysOperationLogFeign;
import lombok.AllArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * title: MqManageController
 * package: com.sptg.gateway.controller.device
 * description: 队列管理-控制器
 *
 *
 *
 */
@RestController
@AllArgsConstructor
public class MqManageController {

    private final MqConfFeign mqConfFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final SysOperationLogFeign sysOperationLogFeign;

    /* -------------------------------------------------------------------------------------------------------------- */

    /**
     * 功能描述: 分页查询队列服务器配置列表
     * @param page 分页条件
     * @return ResultBean<MqConf>
     *
     *
     */
    @GetMapping(value = "/device/mq/pagingQuery")
    @Security(token = DeviceSecurityToken.MQ_SERVER_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Page> pagingQuery(Page<MqConf> page){
        page = this.mqConfFeign.pagingQuery(page);
        return new ResultBean<>(ResultEnum.SUCCESS,page);
    }

    /**
     * 功能描述: 根据Id获取队列配置信息
     * @param id 主键
     * @return ResultBean<MqConf>
     *
     *
     */
    @GetMapping(value = "/device/mq/get/{id}")
    @Security(token = DeviceSecurityToken.MQ_SERVER_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<MqConf> getMqById(@PathVariable(value = "id") Long id){
        MqConf conf = this.mqConfFeign.getMqById(id);
        Assert.notNull(conf,"队列服务器Id不存在");
        return new ResultBean<>(ResultEnum.SUCCESS,conf);
    }

    /**
     * 功能描述: 添加队列服务器配置
     * @param conf 配置信息
     * @return ResultBean<Boolean>
     *
     *
     */
    @PostMapping(value = "/device/mq/add")
    @Security(token = DeviceSecurityToken.MQ_SERVER_ADD,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> addEntity(MqConf conf){
        Assert.notNull(conf,"非法参数");
        Assert.hasText(conf.getName(),"名称不能为空");
        Assert.hasText(conf.getHost(),"主机地址不能为空");
        Assert.notNull(conf.getPort(),"端口号不能为空");
        Assert.hasText(conf.getVirtual(),"数据库名称不能为空");
        Assert.hasText(conf.getUser(),"用户名不能为空");
        Assert.hasText(conf.getPassword(),"密码不能为空");
        Assert.notNull(conf.getMaxConnections(),"最大连接数不能为空");
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        conf.setCreateAccountId(onLineUser.getId());
        ResultBean<Boolean> resultBean=this.mqConfFeign.addEntity(conf);
        if(null!=resultBean.getData()&&resultBean.getData()){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.MQ_SERVER_MANAGE, SysLogConstant.ADD_EVENT,
                    WebUtils.getIp(),conf,onLineUser,conf);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    /**
     * 功能描述: 修改队列服务器配置
     * @param conf 服务器配置
     * @return ResultBean<Boolean>
     *
     *
     */
    @PostMapping(value = "/device/mq/edit")
    @Security(token = DeviceSecurityToken.MQ_SERVER_UPDATE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> editEntity(MqConf conf){
        //  参数校验 [ id,名称,主机地址,端口号,数据库,用户名,密码,连接数 ]
        MqConf oldObj = this.mqConfFeign.getMqById(conf.getId());
        Assert.notNull(conf,"非法参数");
        Assert.notNull(conf.getId(),"Id不能为空");
        Assert.hasText(conf.getName(),"名称不能为空");
        Assert.hasText(conf.getHost(),"主机地址不能为空");
        Assert.notNull(conf.getPort(),"端口号不能为空");
        Assert.hasText(conf.getVirtual(),"数据库名称不能为空");
        Assert.hasText(conf.getUser(),"用户名不能为空");
        Assert.hasText(conf.getPassword(),"密码不能为空");
        Assert.notNull(conf.getMaxConnections(),"最大连接数不能为空");
        ResultBean<Boolean> resultBean=this.mqConfFeign.editEntity(conf);
        if(null!=resultBean.getData()&&resultBean.getData()){
            Account onLineUser = this.securityServiceInterface.getOnLineUser();
            MqConf newObj = this.mqConfFeign.getMqById(conf.getId());
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.MQ_SERVER_MANAGE, SysLogConstant.ALTER_EVENT,
                    WebUtils.getIp(),conf,onLineUser,oldObj,newObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    /**
     * 功能描述: 根据Id删除队列服务器
     * @param id 主键
     * @return Boolean
     *
     *
     */
    @GetMapping(value = "/device/mq/delete/{id}")
    @Security(token = DeviceSecurityToken.MQ_SERVER_DELETE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> deleteEntity(@PathVariable(value = "id") Long id){
        //  根据Id删除队列
        MqConf oldObj = this.mqConfFeign.getMqById(id);
        ResultBean<Boolean> resultBean=this.mqConfFeign.deleteEntityById(id);
        if(null!=resultBean.getData()&&resultBean.getData()){
            Account onLineUser = this.securityServiceInterface.getOnLineUser();
            MqConf conf=new MqConf();
            conf.setId(id);
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.MQ_SERVER_MANAGE, SysLogConstant.DELETE_EVENT,
                    WebUtils.getIp(),conf,onLineUser,oldObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    /**
     * 功能描述: 启用队列服务器
     * @param id 主键
     * @return ResultBean<Boolean>
     *
     *
     */
    @GetMapping(value = "/device/mq/start/{id}")
    @Security(token = DeviceSecurityToken.MQ_SERVER_ENABLE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> startMqServer(@PathVariable(value = "id") Long id){
        ResultBean<Boolean> resultBean=this.mqConfFeign.startServer(id);
        if(null!=resultBean.getData()&&resultBean.getData()){
            Account onLineUser = this.securityServiceInterface.getOnLineUser();
            MqConf conf=new MqConf();
            conf.setId(id);
            MqConf oldObj = this.mqConfFeign.getMqById(id);
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.MQ_SERVER_MANAGE, SysLogConstant.ALTER_EVENT,
                    WebUtils.getIp(),"启用队列服务器",conf,onLineUser,oldObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    @GetMapping(value = "/device/mq/stop/{id}")
    @Security(token = DeviceSecurityToken.MQ_SERVER_ENABLE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> stopMqServer(@PathVariable(value = "id") Long id){
        ResultBean<Boolean> resultBean=this.mqConfFeign.stopServer(id);
        if(null!=resultBean.getData()&&resultBean.getData()){
            Account onLineUser = this.securityServiceInterface.getOnLineUser();
            MqConf conf=new MqConf();
            conf.setId(id);
            MqConf oldObj = this.mqConfFeign.getMqById(id);
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.MQ_SERVER_MANAGE, SysLogConstant.ALTER_EVENT,
                    WebUtils.getIp(),"停止队列服务器",conf,onLineUser,oldObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }
}
