<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ai.da.mapper.primary.LibraryMapper">
    <select id="selectCommonLib" resultType="com.ai.da.mapper.primary.entity.Library">
        SELECT l.*
        FROM `t_enterprise_library` e
        LEFT JOIN t_library l on e.library_id = l.id
        WHERE e.enterprise_id = #{organizationId}
        ORDER BY id ${sort == 'DESC' ? 'DESC' : 'ASC'}
        LIMIT ${limit} OFFSET ${offset}
    </select>

    <select id="selectCommonLibCount" resultType="java.lang.Long">
        SELECT COUNT(l.id)
        FROM `t_enterprise_library` e
        LEFT JOIN t_library l on e.library_id = l.id
        WHERE e.enterprise_id = #{organizationId}
    </select>

    <select id="selectAllSubAccLib" resultType="com.ai.da.mapper.primary.entity.Library">
        SELECT *
        FROM t_library
        WHERE account_id IN (
        SELECT id
        FROM t_account
        WHERE organization_id = #{organizationId}
        )
        ORDER BY id ${sort == 'DESC' ? 'DESC' : 'ASC'}
        LIMIT ${limit} OFFSET ${offset}
    </select>

    <select id="selectAllSubAccLibCount" resultType="java.lang.Long">
        SELECT COUNT(*)
        FROM t_library
        WHERE account_id IN (
        SELECT id
        FROM t_account
        WHERE organization_id = #{organizationId}
        )
    </select>

</mapper>
