package <%=packageName%>.repository;


import <%=packageName%>.domain.Owner;
import <%=packageName%>.domain.ProcessEntityRelation;

<%_ if (ormTool !== 'mybatis') { _%>
import org.springframework.data.jpa.repository.*;
import com.cosium.spring.data.jpa.entity.graph.domain.EntityGraph;
import com.cosium.spring.data.jpa.entity.graph.repository.EntityGraphJpaRepository;
import com.cosium.spring.data.jpa.entity.graph.repository.EntityGraphJpaSpecificationExecutor;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
<%_ } else { _%>
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.diboot.core.mapper.BaseCrudMapper;
import org.apache.ibatis.annotations.Mapper;
<%_ } _%>
import java.util.Optional;
import java.util.List;

// jhipster-needle-add-import - JHipster will add getters and setters here, do not remove

/**
 * Spring Data  repository for the ProcessEntityRelation entity.
 */
@SuppressWarnings("unused")
<%_ if (ormTool !== 'mybatis' && databaseTypeSql) { _%>
<%= softDelete ? '@SoftDelete' : '@Repository'%>
<%_ } else { _%>
<%= ormTool !== 'mybatis' ? '@Repository' : '@Mapper'%>
<%_ } _%>
public interface ProcessEntityRelationRepository extends <% if (ormTool === 'mybatis') { %>BaseCrudMapper<ProcessEntityRelation><% } else { %>EntityGraphJpaRepository<ProcessEntityRelation, Long>, EntityGraphJpaSpecificationExecutor<ProcessEntityRelation><% } %> {
    <%_ if (ormTool === 'mybatis') { _%>

    default List<ProcessEntityRelation> findAll() {
        return this.selectList(null);
    }

    default Optional<ProcessEntityRelation> findById(Long id) {
        return Optional.ofNullable(this.selectById(id));
    }
    <%_ } _%>
    <%_ if (ormTool !== 'mybatis') { _%>
    @Query("select p.processInstanceId from ProcessEntityRelation p where p.entityId =:entityId and p.entityType=:entityType")
    String getProcessInstanceIdByEntity(@Param("entityId") Long entityId, @Param("entityType") String entityType);
    <%_ } else { _%>
    default String getProcessInstanceIdByEntity(Long entityId, String entityType) {
        return Optional.ofNullable(this.selectOne(new LambdaQueryWrapper<ProcessEntityRelation>()
            .select(ProcessEntityRelation::getProcessInstanceId)
            .eq(ProcessEntityRelation::getEntityId, entityId)
            .eq(ProcessEntityRelation::getEntityType, entityType)
        )).map(ProcessEntityRelation::getProcessInstanceId).orElse(null);
    }
    <%_ } _%>

    // jhipster-needle-repository-add-method - JHipster will add getters and setters here, do not remove
}
