<?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.qmth.distributed.print.business.mapper.BasicTemplateOrgMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.qmth.distributed.print.business.entity.BasicTemplateOrg">
        <result column="id" property="id"/>
        <result column="type" property="type"/>
        <result column="rule_id" property="ruleId"/>
        <result column="org_id" property="orgId"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id
        ,
        type, rule_id, org_id
    </sql>

    <select id="listByTypeAndTemplateId" resultType="com.qmth.teachcloud.common.entity.SysOrg">
        SELECT a.id,
               a.code,
               a.name
        FROM sys_org a
                 JOIN
             basic_template_org b ON a.id = b.org_id AND b.type = #{type}
        WHERE b.rule_id = #{templateId}
    </select>

    <select id="templateListByOrgIds"
            resultType="com.qmth.distributed.print.business.bean.result.TemplatePrintInfoResult">
        SELECT
        DISTINCT (tmp.id) as templateId,
        tmp.school_id as schoolId,
        tmp.name as templateName,
        tmp.classify as templateClassify,
        tmp.attachment_id as attachmentId,
        tmp.enable as enable,
        tmp.remark as remark
        FROM
        basic_template_org org
        INNER JOIN
        basic_template tmp ON org.rule_id = tmp.id
        <where>
            and tmp.enable = true
            <if test="orgIds.size() == 0">
                AND org.org_id IN (null)
            </if>
            <if test="orgIds != null and orgIds != '' and orgIds.size() > 0">
                AND org.org_id IN
                <foreach collection="orgIds" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

</mapper>