All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kasinf.framework.rest.repository.BaseTreeRepository Maven / Gradle / Ivy

The newest version!
package com.kasinf.framework.rest.repository;

import com.kasinf.framework.rest.eneity.AbstractEntity;
import com.kasinf.framework.rest.eneity.type.TreeEntity;
import jdk.nashorn.internal.runtime.options.Option;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.NoRepositoryBean;

import java.io.Serializable;
import java.util.Optional;

/**
 * 通用树形结构存储库。
 * 1、修改查询节点方法。将其孩子节点也带出来。并且不触发N+1查询问题。
 *
 * @param   必须实现Treeable接口
 * @param  树型结构实体的主键类型
 * @author lkhsh
 */
@NoRepositoryBean
public interface BaseTreeRepository extends BaseRepository {

    /**
     * 查找兄弟节点的weight,默认从1开始,往后每次+1
     *
     * @param id 父亲节点
     * @return weight
     */
    @Query("select case when max(x.weight) is null then 1 else (max(x.weight) + 1) end " +
            "from #{#entityName} x where x.parent = ?1")
    Integer findNextWeight(TreeEntity id);

    /**
     * 查询根节点的下一个序号
     * @return weight
     */
    @Query("select case when max(x.weight) is null then 1 else (max(x.weight) + 1) end " +
            "from #{#entityName} x where x.parent is null")
    Integer findRootNextWeight();

    /**
     * 若本节点更换了上级节点,将本节点的所有孩子节点的路径更新
     *
     * @param nowParentIds 当前路径
     * @param oldParentIds 旧路径
     */
    @Modifying
    @Query("update #{#entityName} x set x.parentIds=concat(?1,substring(parentIds, length(?2)+1)) where x.parentIds like concat(?2,'%')")
    void updateParentIds(String nowParentIds, String oldParentIds);

    /*委托给BaseTreeRepositoryImpl处理*/
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy