package com.qunzhixinxi.hnqz.admin.config;

import cn.hutool.core.date.DateUtil;
import com.qunzhixinxi.hnqz.common.data.tenant.TenantContextHolder;
import com.qunzhixinxi.hnqz.common.sequence.builder.DbSeqBuilder;
import com.qunzhixinxi.hnqz.common.sequence.properties.SequenceDbProperties;
import com.qunzhixinxi.hnqz.common.sequence.sequence.Sequence;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @author hnqz
 * @date 2019-05-26
 * <p>
 * 设置发号器生成规则
 */
@Configuration
public class SequenceConfig {

	/**
	 * 订单流水号发号器
	 * @param dataSource
	 * @param properties
	 * @return
	 */
	@Bean
	public Sequence paySequence(DataSource dataSource, SequenceDbProperties properties) {
		return DbSeqBuilder.create()
				.bizName(() -> String.format("payoff_%s_%s", TenantContextHolder.getTenantId(), DateUtil.today()))
				.dataSource(dataSource).step(properties.getStep()).retryTimes(properties.getRetryTimes())
				.tableName(properties.getTableName()).build();
	}
}
