/*
* Copyright (c) 京北方信息技术股份有限公司 Corporation 2021 . All rights reserved.
*
*/
package net.northking.cctp.flowable.db.jpa;

import net.northking.cctp.common.jpa.BasicJpaRepository;
import net.northking.cctp.flowable.db.entity.SpDefState;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * 状态定义
 *
 * <p>文件内容由代码生成器产生，请不要手动修改！ <br>
 * createdate:  2021-11-23 20:58:03  <br>
 * @author:  maven-cctp-plugin  <br>
 * @since:  1.0 <br>
 */
public interface SpDefStateRepository extends BasicJpaRepository<SpDefState, String>
{
    @Query(value = "select state.label " +
            "from sp_def_state state " +
            "left join sp_def_event event on state.id = event.target " +
            "left join sp_def_event_auth eventAuth on event.id = eventAuth.event_id " +
            "where event.source = :source " +
            "and eventAuth.authority in :roleList " +
            "and state.tenant_id = :tenantId " +
            "group by event.target", nativeQuery = true)
    List<String> queryAvailableStates(@Param("source") String source,
                                      @Param("roleList") List<String> roleList,
                                      @Param("tenantId") String tenantId);

    @Query(value = "select authority " +
            "from sp_def_state state " +
            "left join sp_def_event event on state.id = event.source " +
            "left join sp_def_event_auth eventAuth on event.id = eventAuth.event_id " +
            "where state.label = :state " +
            "and state.sp_id = :processId " +
            "group by eventAuth.authority", nativeQuery = true)
    List<String> queryAvailableRoles(@Param("processId") String processId,
                                     @Param("state") String  state);
}
