package com.tofly.common.oauth.handler;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.keygen.KeyGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Administrator
 * @date 2019/10/10  11:40
 * 自定义主键生成
 */
@Slf4j
@Component
public class MybatisKeyGenerator implements IKeyGenerator {

    @Autowired
    private DruidDataSource druidDataSource;

    @Override
    public String executeSql(String incrementerName) {
        log.info("incrementerName:{}",incrementerName);
        KeyGenerator keyGenerator;
        String dbtype=druidDataSource.getDbType();
        String sql;
        switch (dbtype){
            case "oracle":
                sql= "select nvl(max(id),0)+1 from "+incrementerName;
                break;
            case "mysql":
                sql="select IFNULL(max(id),0)+1 from  "+incrementerName;
                break;
            default:
                sql = "";
        }

        return sql;
    }
}
