<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"https://hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <query name="findUsersNotRootByInStatus">
    <![CDATA[
        SELECT U
        FROM User U
        WHERE 
            U.userStatusType in :inUserStatusType AND
            U.root = false AND
            U.id not in :id AND
            U.location = :userLocation 
        ORDER BY userName ASC
    ]]>
    </query>
    
    <query name="availableUserName">
        <![CDATA[
            SELECT COUNT(id)
            FROM User
            WHERE 
                userStatusType = :inUserStatusType AND
                userName = :userName
        ]]>
    </query>
    
    <sql-query name="findAllUsersByPermissionHowWillReciveNotification">
        <![CDATA[
            SELECT DISTINCT(usr.id) 
            FROM CAR_DRIVER_USER usr
            INNER JOIN USER_HAS_PERMISSION uhp ON usr.id = uhp.id_user
            INNER JOIN PERMISSION p ON uhp.id_permission = p.id
            WHERE 
                p.name IN (:permissionlst) AND
                uhp.id_user != :id AND
                uhp.id_user NOT IN (
                    SELECT id_user 
                    FROM STOP_NOTIFICATION 
                    WHERE stop_notification_type = :stopNotificationType
                )
        ]]>
    </sql-query>
    
    <query name="findNotificationUsers">
        <![CDATA[
            SELECT u
            FROM User u
            INNER JOIN StopNotification sn ON u.id = sn.user
            WHERE u.userStatusType = 'ENABLED'
        ]]>
    </query>
    
    <query name="findUsersWithoutNotification">
        <![CDATA[
            SELECT u
            FROM User u
            LEFT JOIN StopNotification sn ON u.id = sn.user
            WHERE 
                sn.user IS NULL AND
                u.userStatusType = 'ENABLED'
        ]]>
    </query>
</hibernate-mapping>