<?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.ProjectMapper">
    <insert id="add">
        insert into project(scenic_id, `name`, min_play_time, max_play_time, status, template_id, create_at, update_at) 
        values (#{scenicId}, #{name}, #{minPlayTime}, #{maxPlayTime}, #{status}, #{templateId}, now(), now())
    </insert>
    
    <update id="update">
        update project set 
            `name` = #{name}, 
            min_play_time = #{minPlayTime}, 
            max_play_time = #{maxPlayTime}, 
            status = #{status}, 
            template_id = #{templateId}, 
            update_at = now() 
        where id = #{id}
    </update>
    
    <update id="updateStatus">
        update project
        set status = (CASE
            status
                          WHEN 1 THEN
                              0
                          WHEN 0 THEN
                              1
                          ELSE 1
            END),
            update_at = now()
        where id = #{id}
    </update>
    
    <delete id="deleteById">
        delete from project where id = #{id}
    </delete>
    
    <select id="list" resultType="com.ycwl.basic.model.pc.project.resp.ProjectRespVO">
        select p.id, p.scenic_id, p.`name`, p.min_play_time, p.max_play_time, p.status, p.template_id, p.create_at, p.update_at
        from project p
        <where>
            <if test="scenicId != null">
                and p.scenic_id = #{scenicId}
            </if>
            <if test="name != null and name != ''">
                and p.`name` like concat('%', #{name}, '%')
            </if>
            <if test="status != null">
                and p.`status` = #{status}
            </if>
            <if test="templateId != null">
                and p.template_id = #{templateId}
            </if>
        </where>
        order by p.create_at desc
    </select>
    
    <select id="getById" resultType="com.ycwl.basic.model.pc.project.resp.ProjectRespVO">
        select p.id, p.scenic_id, p.`name`, p.min_play_time, p.max_play_time, p.status, p.template_id, p.create_at, p.update_at
        from project p
        where p.id = #{id}
    </select>
</mapper>