package com.blueland.config;

import cn.hutool.core.date.DateUtil;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;
import java.util.Date;

/**
 * @author leijie.gao
 * @since 2020-05-27
 */
public class Default2PreciseShardingAlgorithm implements PreciseShardingAlgorithm<String> {

    /**
     * @param targetTableNames sharding 逻辑表 分表规则
     * @param shardingVal      PreciseShardingValue(logicTableName=真实表, columnName=company_id, value=company_id_value)
     * @return {{@link String}}
     */
    @Override
    public String doSharding(Collection<String> targetTableNames, PreciseShardingValue<String> shardingVal) {
        /**
         * tableNames 对应分片库中所有分片表的集合
         * shardingValue 为分片属性，其中 logicTableName 为逻辑表，columnName 分片健（字段），value 为从 SQL 中解析出的分片健的值
         */
        /**
         * 取模算法，分片健 % 表数量
         */
        Integer month = DateUtil.month(new Date()) + 1;

        return "t_pf_sys_pvlog_" + month;
    }
}