package com.malk.test;

import com.malk.repository.dao.primary.McTableDao;
import com.malk.repository.entity.slave.McTablePo;
import com.malk.server.common.McR;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 指定运行环境
 **/
@Slf4j
@Profile({"dev", "test"})
@RestController
@RequestMapping("/test/ds")
@ConditionalOnProperty(name = "spel.multiSource", havingValue = "true")
public class DSTestController {

    @Autowired
    @Qualifier("primaryMcTableDao")
    private McTableDao tableDaoPrimary;

    @Autowired
    @Qualifier("slaveMcTableDao")
    private com.malk.repository.dao.slave.McTableDao tableDaoSlave;

    /**
     * 验证多数据源切换
     */
    @GetMapping("dataSource")
    McR dataSource() {

        log.info("primary, {}", tableDaoPrimary.findAll().size());
        log.info("slave, {}", tableDaoSlave.findAll().size());

        com.malk.repository.entity.primary.McTablePo tablePrimary = new com.malk.repository.entity.primary.McTablePo();
        tablePrimary.setTDesc("xxxx");
        tablePrimary.setTName("xxxx");
        tableDaoPrimary.save(tablePrimary);

        McTablePo tableSlave = new McTablePo();
        tableSlave.setTDesc("xxxx");
        tableSlave.setTName("xxxx");
        tableDaoSlave.save(tableSlave);

        return McR.success();
    }
}



