
package com.tmerclub.cloud.common.leaf.segment.mapper;


import com.tmerclub.cloud.common.leaf.segment.model.LeafAlloc;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author leaf
 */
public interface IDAllocMapper {

    /**
     * 获取所有
     * @return
     */
    @Select("SELECT biz_tag, max_id, step, update_time FROM leaf_alloc")
    @Results({
            @Result(column = "biz_tag", property = "key"),
            @Result(column = "max_id", property = "maxId"),
            @Result(column = "step", property = "step"),
            @Result(column = "update_time", property = "updateTime")
    })
    List<LeafAlloc> getAllLeafAllocs();

    /**
     * 根据tag获取
     * @param tag
     * @return
     */
    @Select("SELECT biz_tag, max_id, step FROM leaf_alloc WHERE biz_tag = #{tag}")
    @Results({
            @Result(column = "biz_tag", property = "key"),
            @Result(column = "max_id", property = "maxId"),
            @Result(column = "step", property = "step")
    })
    LeafAlloc getLeafAlloc(@Param("tag") String tag);

    /**
     * 更新max
     * @param tag
     */
    @Update("UPDATE leaf_alloc SET max_id = max_id + step WHERE biz_tag = #{tag}")
    void updateMaxId(@Param("tag") String tag);

    /**
     * 更新max
     * @param leafAlloc
     */
    @Update("UPDATE leaf_alloc SET max_id = max_id + #{leafAlloc.step} WHERE biz_tag = #{leafAlloc.key}")
    void updateMaxIdByCustomStep(@Param("leafAlloc") LeafAlloc leafAlloc);

    /**
     * 获取所有
     * @return
     */
    @Select("SELECT biz_tag FROM leaf_alloc")
    List<String> getAllTags();

    /**
     * 更新max
     * @param tag
     */
    @Update("UPDATE leaf_alloc SET max_id = 1 WHERE biz_tag = #{tag}")
    void updateMaxIdToStep(@Param("tag") String tag);
}
