<?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.BasicTemplateMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.qmth.distributed.print.business.entity.BasicTemplate">
        <result column="id" property="id"/>
        <result column="school_id" property="schoolId"/>
        <result column="name" property="name"/>
        <result column="type" property="type"/>
        <result column="classify" property="classify"/>
        <result column="attachment_id" property="attachmentId"/>
        <result column="enable" property="enable"/>
        <result column="remark" property="remark"/>
        <result column="create_id" property="createId"/>
        <result column="create_time" property="createTime"/>
        <result column="update_id" property="updateId"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id
        ,
        school_id, name, type, classify, attachment_id, enable, remark, create_id, create_time, update_id, update_time
    </sql>

    <select id="listPage" resultType="com.qmth.distributed.print.business.bean.dto.TemplateDto">
        SELECT
        id,
        school_id schoolId,
        name,
        classify,
        attachment_id attachmentId,
        enable,
        remark,
        create_id createId,
        create_time createTime
        FROM
        basic_template
        <where>
            <if test="schoolId != null and schoolId != ''">
                and school_id = #{schoolId}
            </if>
            <if test="name != null and name != ''">
                and name like concat('%',#{name}, '%')
            </if>
            <if test="enable != null and enable != '' or enable == 0">
                and enable = #{enable}
            </if>
            <if test="startTime != null and startTime != ''">
                and create_time &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                and create_time &lt;= #{endTime}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND create_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
        order by create_time desc
    </select>


    <select id="findTemplateInfoByOrgIds" 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 tmp
        <where>
            and tmp.enable = true
            <if test="orgIds != null and orgIds != '' and orgIds.size() > 0">
                AND tmp.org_id IN
                <foreach collection="orgIds" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
</mapper>
