package com.yfd.platform.system.controller;

import com.yfd.platform.config.ResponseResult;
import com.yfd.platform.datasource.DataSource;
import com.yfd.platform.datasource.DataSourceAspect;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author zhengsl
 * @since 2022-09-20
 */
@RestController
@RequestMapping("/system")
@Api(tags = "切换数据库")
public class DataSourceController {

    @Resource
    DataSourceAspect dataSourceAspect;

    /**
     * 切换数据库
     *
     * @DataSource(name="master") 可以通过注解方式切换数据库
     */
    @GetMapping("/changeDataSource")
    @ApiOperation("切换数据库")
    public ResponseResult changeDataSource(Integer type) {
        if (type == null) {
            return ResponseResult.error("参数为空");
        }
        String dataBase = dataSourceAspect.getDataBase(type);
        String mess = "已切换为" + dataBase + "数据库";
        return ResponseResult.success(mess);
    }

}
