<%_ if (position === 'import') { _%>
import <%= packageName %>.domain.enumeration.ApiPermissionType;
import <%= packageName %>.domain.Authority;
import java.util.Map;
    <%_ if (userIdTypeName === 'UUID') { _%>
import java.util.UUID;
    <%_ } _%>
    <%_ if (ormTool === 'mybatis' && databaseTypeSql) { _%>
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Select;
    <%_ } _%>
<%_ } _%>
<%_ if (position === 'method') { _%>

    <%_ if (ormTool === 'mybatis' && databaseTypeSql) { _%>
    default Optional<ApiPermission> findOneByCode(String groupCode) {
        return Optional.ofNullable(this.selectOne(new QueryWrapper<ApiPermission>().eq("code", groupCode)));
    }
    <%_ } else { _%>
    <%= reactive ? 'Mono' : 'Optional'%><ApiPermission> findOneByCode(String groupCode);
    <%_ } _%>

    <%_ if (ormTool === 'mybatis' && databaseTypeSql) { _%>
    default List<ApiPermission> findAllByType(ApiPermissionType type) {
        return this.selectList(new LambdaQueryWrapper<ApiPermission>().eq(ApiPermission::getType, type));
    }
    <%_ } else { _%>
    <%= reactive ? 'Flux' : 'List'%><ApiPermission> findAllByType(ApiPermissionType type);
    <%_ } _%>

    <%_ if (databaseTypeMongodb) { _%>
    List<ApiPermission> findAllByAuthoritiesIn(List<Authority> authorities);
    <%_ } _%>

    <%_ if (ormTool === 'mybatis' && databaseTypeSql) { _%>
    @Select(
        "select self.* from api_permission self left join rel_jhi_authority__api_permissions aap on self.id = aap.api_permissions_id " +
        "left join rel_jhi_user__authorities jua on jua.authorities_id = aap.authority_id " +
        "where jua.jhi_user_id = #{userId}"
    )
    List<ApiPermission> findAllApiPermissionsByCurrentUser(@Param("userId") <%= userIdTypeName %> userId);
    <%_ } _%>

    <%_ if (ormTool === 'hibernate' && databaseTypeSql) { _%>
        <%_ if (!reactive) { _%>
    @Query("select authority.apiPermissions from User u join u.authorities authority where u.login = ?#{principal.username}")
        <%_ } else { _%>
    @Query("select entity.* from api_permission entity" +
        " join rel_jhi_authority__api_permissions on rel_jhi_authority__api_permissions.api_permissions_id = entity.id" +
        " join rel_jhi_user__authorities on rel_jhi_user__authorities.authorities_id = rel_jhi_authority__api_permissions.api_permissions_id" +
        " join jhi_user on jhi_user.id = rel_jhi_user__authorities.jhi_user_id where jhi_user.login = ?#{principal.username}")
        <%_ } _%>
    <%= reactive ? 'Flux' : 'List'%><ApiPermission> findAllApiPermissionsByCurrentUser();
    <%_ } _%>
<%_ } _%>
