<?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.ycwl.basic.mapper.TemplateMapper">
    <insert id="add">
        insert into template(id, scenic_id, `name`, pid, is_placeholder, source_url, effects, luts, overlays, audios, cover_url, frame_rate, speed, price, slash_price, sort, crop_enable, zoom_cut, only_if, resolution, create_time)
        values (#{id}, #{scenicId}, #{name}, #{pid}, #{isPlaceholder}, #{sourceUrl}, #{effects}, #{luts}, #{overlays}, #{audios}, #{coverUrl}, #{frameRate}, #{speed}, #{price}, #{slashPrice}, #{sort}, #{cropEnable}, #{zoomCut}, #{onlyIf}, #{resolution}, now())
    </insert>
    <insert id="addConfig">
        insert into template_config(id, template_id, duplicate_enable, create_time)
        values (#{id}, #{templateId}, #{duplicateEnable}, now())
    </insert>
    <update id="update">
        update template
        <set>
            update_time = now(),
            <if test="name!= null">`name` = #{name}, </if>
            <if test="scenicId!= null">`scenic_id` = #{scenicId}, </if>
            <if test="pid!= null">pid = #{pid}, </if>
            <if test="isPlaceholder!= null">is_placeholder = #{isPlaceholder}, </if>
            <if test="sourceUrl!= null">source_url = #{sourceUrl}, </if>
            <if test="effects!= null">effects = #{effects}, </if>
            <if test="luts!= null">luts = #{luts}, </if>
            <if test="overlays!= null">overlays = #{overlays}, </if>
            <if test="audios!= null">audios = #{audios}, </if>
            <if test="frameRate!= null">frame_rate = #{frameRate}, </if>
            <if test="coverUrl!= null">cover_url = #{coverUrl}, </if>
            <if test="price!= null">price = #{price}, </if>
            <if test="slashPrice!= null">slash_price = #{slashPrice}, </if>
            <if test="speed!= null">speed = #{speed}, </if>
            <if test="sort!= null">sort = #{sort}, </if>
            <if test="cropEnable!= null">crop_enable = #{cropEnable}, </if>
            <if test="zoomCut!= null">zoom_cut = #{zoomCut}, </if>
            <if test="onlyIf!= null">only_if = #{onlyIf}, </if>
            <if test="resolution!= null">resolution = #{resolution}, </if>
        </set>
        where id = #{id}
    </update>
    <update id="updateStatus">
        update template
        set status =
                (CASE
                    status
                     WHEN 1 THEN
                         0
                     WHEN 0 THEN
                         1
                    END)
        where id = #{id}
    </update>
    <update id="updateConfigById">
        update template_config
        <set>
            <if test="isDefault!= null">is_default = #{isDefault}, </if>
            minimal_placeholder_fill = #{minimalPlaceholderFill},
            automatic_placeholder_fill = #{automaticPlaceholderFill},
            duplicate_enable = #{duplicateEnable}
        </set>
        where id = #{id}
    </update>
    <update id="updateSort">
        update template
        set sort = #{sort}
        where id = #{templateId}
    </update>
    <delete id="deleteById">
        delete from template where id = #{id}
    </delete>
    <delete id="deleteByPid">
        delete from template where pid = #{id}
    </delete>
    <delete id="deleteByScenicId">
        delete from template where scenic_id = #{id}
    </delete>
    <delete id="deleteConfigByTemplateId">
        delete from template_config where template_id = #{id}
    </delete>
    <delete id="deleteConfigById">
        delete from template_config where id = #{id}
    </delete>
    <select id="list" resultType="com.ycwl.basic.model.pc.template.resp.TemplateRespVO">
        select t.*
        from template t
        <where>
            pid = 0
            <if test="scenicId!=null" >
                and scenic_id = #{scenicId}
            </if>
            <if test="name!= null">and locate(#{name},t.`name`) > 0 </if>
            <if test="scenicId!= null">and t.scenic_id = #{scenicId} </if>
            <if test="isPlaceholder!= null">and is_placeholder = #{isPlaceholder} </if>
            <if test="status!= null">and t.`status` = #{status} </if>
            <if test="startTime!= null">and t.create_time &gt;= #{startTime} </if>
            <if test="endTime!= null">and t.create_time &lt;= #{endTime} </if>
        </where>
        order by scenic_id, sort
    </select>
    <select id="getById" resultType="com.ycwl.basic.model.pc.template.resp.TemplateRespVO">
        select t.*
        from template t
        where t.id = #{id}
    </select>
    <select id="getByPid" resultType="com.ycwl.basic.model.pc.template.resp.TemplateRespVO">
        select t.*
        from template t
        where pid = #{id}
        order by sort
    </select>
    <select id="getConfig" resultType="com.ycwl.basic.model.pc.template.entity.TemplateConfigEntity">
        select * from template_config where template_id = #{templateId}
    </select>
    <select id="listEnabledByScenicId" resultType="com.ycwl.basic.model.pc.template.entity.TemplateEntity">
        select *
        from template
        where scenic_id = #{scenicId} and pid = 0 and status = 1
        order by sort
    </select>
    <select id="listFor" resultType="com.ycwl.basic.model.mobile.scenic.content.ContentPageVO">
        select t.id templateId, t.scenic_id, t.`group`, t.`name`, pid, t.cover_url templateCoverUrl,
               0 as sourceType, sort,
               t.create_time, t.price
        from template t
        where t.scenic_id = #{scenicId} and pid = 0 and t.status = 1
        order by sort
    </select>
    <select id="listEnabledTemplateIdByScenicId" resultType="java.lang.Long">
        select t.id
        from template t
        where t.scenic_id = #{scenicId} and t.pid = 0 and t.status = 1
        order by sort
    </select>
    <select id="get" resultType="com.ycwl.basic.model.pc.template.entity.TemplateEntity">
        select *
        from template
        where id = #{id}
    </select>
    <select id="listEnabled" resultType="com.ycwl.basic.model.pc.template.entity.TemplateEntity">
        select *
        from template
        where status = 1 and pid = 0
        order by sort
    </select>
    <select id="listAllTemplateIdByScenicId" resultType="java.lang.Long">
        select t.id
        from template t
        where t.scenic_id = #{scenicId} and t.pid = 0
        order by sort
    </select>
</mapper>
