<?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.cmeeting.mapper.primary.MeetingRecordTemplateMapper">


    <sql id="templateColumnSql">
        meet_type.id as meeting_type_id,
        meet_type.`name` AS meeting_type_name,
        meet_type.personal_permission AS meeting_type_personal_permission,
        meet_type.auth_type AS meeting_type_auth_type,
        meet_type.rel_id AS meeting_type_rel_id,
        meet_type.content AS meeting_type_content,
        meet_type.create_user as meeting_type_create_user,
        meet_type.create_time as meeting_type_create_time,

        cmt_meeting_record_template.id as template_id,
        cmt_meeting_record_template.name as template_name,
        cmt_meeting_record_template.`type` as template_type_type,
        cmt_meeting_record_template.type_detail as template_type_detail,
        cmt_meeting_record_template.create_user AS template_create_user,
        cmt_meeting_record_template.create_time AS template_create_time,
        cmt_meeting_record_template.prompt as template_prompt,
        cmt_meeting_record_template.refer_template_id AS template_refer_template_id,
        IF(meet_template_enable.id IS NULL, 0, 1) AS template_enabled
    </sql>

    <resultMap id="RecordTemplateDtoResultMap" type="com.cmeeting.dto.RecordTemplateDto">
        <id property="id" column="template_id" />
        <result property="name" column="template_name" />
        <result property="type" column="template_type_type" />
        <result property="typeDetail" column="template_type_detail" />
        <result property="createUser" column="template_create_user" />
        <result property="createUserName" column="template_create_user_name" />
        <result property="createTime" column="template_create_time" />
        <result property="enable" column="template_enabled" />
        <result property="prompt" column="template_prompt" />
        <result property="referTemplateId" column="template_refer_template_id" />
        <result property="meetingTypeName" column="meeting_type_name" />
    </resultMap>

    <resultMap id="MeetTypeDtoResultMap" type="com.cmeeting.dto.MeetTypeDto">
        <id property="id" column="meeting_type_id" />
        <result property="name" column="meeting_type_name" />
        <result property="personalPermission" column="meeting_type_personal_permission" />
        <result property="authType" column="meeting_type_auth_type" />
        <result property="relId" column="meeting_type_rel_id" />
        <result property="content" column="meeting_type_content" />
        <result property="createUser" column="meeting_type_create_user" />
        <result property="createTime" column="meeting_type_create_time" />
        <collection property="recordTemplateDTOList" javaType="List" resultMap="RecordTemplateDtoResultMap" />
    </resultMap>


    <!-- 查询授权的模板-会议类型维度 -->
    <select id="selectPermissionTmplInMeetType" resultMap="MeetTypeDtoResultMap">
        select
            <include refid="templateColumnSql" />
        from
            meet_type
                left join cmt_meeting_record_template on meet_type.id = cmt_meeting_record_template.meeting_type and cmt_meeting_record_template.is_del = 0
                    and cmt_meeting_record_template.type = 'custom'
                LEFT JOIN meet_template_enable ON cmt_meeting_record_template.id = meet_template_enable.rel_id
                    AND meet_template_enable.user_id = #{userId},
            module_permission
        where
            meet_type.is_del = 0
            and meet_type.auth_type = 2
            and module_permission.type = 0
            and module_permission.purpose = 1
            and module_permission.target_id = meet_type.id
            and module_permission.rel_id in
            <foreach collection="deptIdList" item="deptId" open="(" separator="," close=")" index="index">
                #{deptId}
            </foreach>

        union

        select
            <include refid="templateColumnSql" />
        from
            meet_type
                left join cmt_meeting_record_template on meet_type.id = cmt_meeting_record_template.meeting_type and cmt_meeting_record_template.is_del = 0
                    and cmt_meeting_record_template.type = 'custom'
                LEFT JOIN meet_template_enable ON cmt_meeting_record_template.id = meet_template_enable.rel_id
                    AND meet_template_enable.user_id = #{userId},
            module_permission
        where
            meet_type.is_del = 0
            and meet_type.auth_type = 2
            and module_permission.type = 1
            and module_permission.purpose = 1
            and module_permission.target_id = meet_type.id
            and module_permission.rel_id = #{userId}

        union

        select
            <include refid="templateColumnSql" />
        from
            meet_type
                left join cmt_meeting_record_template on meet_type.id = cmt_meeting_record_template.meeting_type and cmt_meeting_record_template.is_del = 0
                    and cmt_meeting_record_template.type = 'custom' and cmt_meeting_record_template.create_user = #{userId}
                LEFT JOIN meet_template_enable ON cmt_meeting_record_template.id = meet_template_enable.rel_id
                    AND meet_template_enable.user_id = #{userId}
        where
            meet_type.is_del = 0
            and meet_type.auth_type = 1
    </select>

    <!-- 获取指定授权的模板-纪要模板维度 -->
    <select id="selectPermissionTmpl" resultMap="MeetTypeDtoResultMap">
        select
            <include refid="templateColumnSql" />
        from
            meet_type
                left join cmt_meeting_record_template on meet_type.id = cmt_meeting_record_template.meeting_type and cmt_meeting_record_template.is_del = 0
                    and cmt_meeting_record_template.type = 'custom'
                left join module_permission on module_permission.target_id = cmt_meeting_record_template.id and module_permission.type = 0
                    and module_permission.purpose = 2 and module_permission.rel_id in
                    <foreach collection="deptIdList" item="deptId" open="(" separator="," close=")" index="index">
                        #{deptId}
                    </foreach>
                LEFT JOIN meet_template_enable ON cmt_meeting_record_template.id = meet_template_enable.rel_id
                    AND meet_template_enable.user_id = #{userId}
        where
            meet_type.is_del = 0
            and meet_type.auth_type = 2

        union

        select
            <include refid="templateColumnSql" />
        from
            meet_type
                left join cmt_meeting_record_template on meet_type.id = cmt_meeting_record_template.meeting_type and cmt_meeting_record_template.is_del = 0
                    and cmt_meeting_record_template.type = 'custom'
                left join module_permission on module_permission.target_id = cmt_meeting_record_template.id and module_permission.type = 1
                    and module_permission.purpose = 2 and module_permission.rel_id = #{userId}
                LEFT JOIN meet_template_enable ON cmt_meeting_record_template.id = meet_template_enable.rel_id
                    AND meet_template_enable.user_id = #{userId}
        where
            meet_type.is_del = 0
            and meet_type.auth_type = 2

        union

        select
            <include refid="templateColumnSql" />
        from
            meet_type
                left join cmt_meeting_record_template on meet_type.id = cmt_meeting_record_template.meeting_type and cmt_meeting_record_template.is_del = 0
                    and cmt_meeting_record_template.type = 'custom'
                left join module_permission on module_permission.target_id = cmt_meeting_record_template.id and module_permission.type = 1
                    and module_permission.purpose = 2 and module_permission.rel_id = 'all'
                LEFT JOIN meet_template_enable ON cmt_meeting_record_template.id = meet_template_enable.rel_id
                    AND meet_template_enable.user_id = #{userId}
        where
            meet_type.is_del = 0
            and meet_type.auth_type = 2

    </select>

    <!-- 获取非授权的模板，包括系统通用的和自己创建的 -->
    <select id="selectAllTmplWithoutPermission" resultMap="MeetTypeDtoResultMap">
        select
            <include refid="templateColumnSql" />
        from
            meet_type,
            cmt_meeting_record_template
                LEFT JOIN meet_template_enable ON cmt_meeting_record_template.id = meet_template_enable.rel_id
                    AND meet_template_enable.user_id = #{userId}
        where
            meet_type.is_del = 0
            and meet_type.id = #{meetTypeId}
            and meet_type.id = cmt_meeting_record_template.meeting_type
            and cmt_meeting_record_template.is_del = 0
            and cmt_meeting_record_template.type = 'custom'
            and cmt_meeting_record_template.create_user = #{userId}
    </select>


    <select id="selectRecordTemplateDtoList" resultMap="RecordTemplateDtoResultMap">
        select
            cmt_meeting_record_template.id as template_id,
            cmt_meeting_record_template.name as template_name,
            cmt_meeting_record_template.type as template_type_type,
            cmt_meeting_record_template.type_detail as template_type_detail,
            cmt_meeting_record_template.prompt as template_prompt,
            cmt_meeting_record_template.refer_template_id as template_refer_template_id,
            cmt_meeting_record_template.create_user as template_create_user,
            cmt_meeting_record_template.create_user_name as template_create_user_name,
            cmt_meeting_record_template.create_time as template_create_time,
            meet_type.name as meeting_type_name
        from
            cmt_meeting_record_template,
            meet_type
        <where>
            cmt_meeting_record_template.is_del = 0
            and cmt_meeting_record_template.meeting_type = meet_type.id
            <if test="name != null and name != ''">and cmt_meeting_record_template.name like concat('%', #{name}, '%')</if>
            <if test="type != null and type != ''">and cmt_meeting_record_template.type = #{type}</if>
            <if test="meetingType != null and meetingType != ''">and meet_type.name like concat('%', #{meetingType}, '%')</if>
            <if test="createUser != null and createUser != ''">and (cmt_meeting_record_template.create_user like concat('%', #{createUser}, '%') or cmt_meeting_record_template.create_user_name like concat('%', #{createUser}, '%') )</if>
            <if test="startTime != null">and date_format(cmt_meeting_record_template.update_time, '%Y-%m-%d') &gt;= date_format(#{startTime}, '%Y-%m-%d')</if>
            <if test="endTime != null">and date_format(cmt_meeting_record_template.update_time, '%Y-%m-%d') &lt;= date_format(#{endTime}, '%Y-%m-%d')</if>
        </where>
    </select>



    <select id="selectEnabledTemplate" resultType="com.cmeeting.pojo.MeetingRecordTemplate">
        select t2.* from
        meet_template_enable t1
        inner join cmt_meeting_record_template t2 on t2.is_del = 0 and t2.id = t1.rel_id
        where t1.user_id = #{userId} and t1.meet_type_id = #{meetTypeId};
    </select>


    <select id="selectEnabledSystemTemplate" resultMap="MeetTypeDtoResultMap">
        select
            <include refid="templateColumnSql" />
        from
            meet_type,
            cmt_meeting_record_template,
            meet_template_enable
        where
            meet_type.id = 1
            and meet_type.is_del = 0
            and meet_type.id = cmt_meeting_record_template.meeting_type
            and cmt_meeting_record_template.is_del = 0
            and cmt_meeting_record_template.id = meet_template_enable.rel_id
    </select>

    <resultMap id="recordTemplateMap" type="com.cmeeting.dto.RecordTemplateDto">
        <id property="id" column="temp_id"/>
        <result property="name" column="template_name"/>
        <result property="type" column="type"/>
        <result property="typeDetail" column="type_detail"/>
        <result property="prompt" column="prompt"/>
        <result property="template" column="template"/>
        <result property="referTemplateId" column="refer_template_id"/>
        <result property="enable" column="enable"/>
        <result property="createTime" column="template_create_time"/>
        <result property="createUser" column="template_create_user"/>
    </resultMap>
    <resultMap id="meetTypeWithRecordsMap" type="com.cmeeting.dto.MeetTypeDto">
        <id property="id" column="type_id"/>
        <result property="name" column="type_name"/>
        <result property="personalPermission" column="personal_permission"/>
        <result property="authType" column="auth_type"/>
        <result property="relId" column="rel_type_id"/>
        <result property="content" column="type_content"/>
        <result property="regex" column="regex"/>
        <result property="createTime" column="type_create_time"/>
        <result property="createUser" column="type_create_user"/>
        <collection property="recordTemplateDTOList" ofType="com.cmeeting.dto.RecordTemplateDto" resultMap="recordTemplateMap"
                    javaType="java.util.List" column="id"/>
    </resultMap>


    <select id="selectTemplate" resultMap="meetTypeWithRecordsMap">
        select t1.id as type_id, t1.name as type_name, t1.personal_permission, t1.auth_type, t1.rel_id as rel_type_id,
               t1.content as type_content, t1.regex,
               t1.create_time as type_create_time, t1.create_user as type_create_user,
               t2.id as temp_id, t2.name as template_name, t2.type, t2.type_detail, t2.prompt, t2.refer_template_id, t2.enable,
               t2.create_user as template_create_user, t2.create_time as template_create_time
        from meet_type t1
        left join cmt_meeting_record_template t2 on t2.meeting_type = t1.id and t2.is_del = 0 and t2.type = #{type}
        where t1.is_del = 0
    </select>

    <select id="selectCustomTemplate" resultMap="meetTypeWithRecordsMap">
        select t1.id          as type_id,
               t1.name        as type_name,
               t1.personal_permission,
               t1.auth_type,
               t1.rel_id      as rel_type_id,
               t1.content     as type_content,
               t1.create_time as type_create_time,
               t1.create_user as type_create_user,
               t1.regex,
               t2.id          as temp_id,
               t2.name        as template_name,
               t2.type,
               t2.type_detail,
               t2.prompt,
               t2.refer_template_id,
               t2.create_user as template_create_user,
               t2.create_time as template_create_time,
               t2.template as template,
               CASE
                   WHEN t2.type = 'system' THEN
                       CASE WHEN t3.id IS NULL AND t1.id != 1 THEN 0 ELSE t2.enable END
                   WHEN t2.type = 'custom' THEN
                       CASE WHEN t3.id IS NULL THEN 0 ELSE 1 END
                   ELSE NULL
                   END AS `enable`
        from meet_type t1
                 left join cmt_meeting_record_template t2 on t2.meeting_type = t1.id and t2.is_del = 0 and t2.enable = 1
                 and ((t2.type = 'system' and t2.enable = 1) or (t2.type = 'custom' and t2.create_user = #{userId}))
                 left join meet_template_enable t3 on t3.rel_id = t2.id and t3.user_id = #{userId}
        where t1.is_del = 0
        order by t1.id, CASE WHEN t2.type = 'system' THEN 1 WHEN t2.type = 'custom' THEN 2 ELSE 3 END,
                 t1.create_time,t2.create_time
    </select>

    <update id="disableSystemExceptId">
        update cmt_meeting_record_template set enable = 0 where id != #{id} and meeting_type = 1;
    </update>


</mapper>
