<?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.ruoyi.bst.help.mapper.HelpMapper">

    <resultMap type="HelpVO" id="HelpResult" autoMapping="true">
        <result property="id"    column="id"    />
        <result property="title"    column="title"    />
        <result property="status"    column="status"    />
        <result property="videoUrl"    column="video_url"    />
        <result property="coverUrl"    column="cover_url"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="code"    column="code"    />
    </resultMap>

    <sql id="selectHelpVo">
        select
            bh.id,
            bh.title,
            bh.status,
            bh.video_url,
            bh.cover_url,
            bh.create_time,
            bh.update_time,
            bh.remark,
            bh.code
        from bst_help bh
    </sql>

    <sql id="searchCondition">
        <if test="query.id != null "> and bh.id = #{query.id}</if>
        <if test="query.title != null  and query.title != ''"> and bh.title like concat('%', #{query.title}, '%')</if>
        <if test="query.status != null  and query.status != ''"> and bh.status = #{query.status}</if>
        <if test="query.videoUrl != null  and query.videoUrl != ''"> and bh.video_url = #{query.videoUrl}</if>
        <if test="query.coverUrl != null  and query.coverUrl != ''"> and bh.cover_url = #{query.coverUrl}</if>
        <if test="query.code != null  and query.code != ''">and bh.code like concat('%', #{query.code}, '%')</if>
        <if test="query.eqCode != null  and query.eqCode != ''"> and bh.code = #{query.eqCode}</if>
        ${query.params.dataScope}
    </sql>

    <select id="selectHelpList" parameterType="HelpQuery" resultMap="HelpResult">
        <include refid="selectHelpVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectHelpById" parameterType="Long" resultMap="HelpResult">
        <include refid="selectHelpVo"/>
        where id = #{id}
    </select>

    <insert id="insertHelp" parameterType="Help" useGeneratedKeys="true" keyProperty="id">
        insert into bst_help
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="title != null and title != ''">title,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="videoUrl != null and videoUrl != ''">video_url,</if>
            <if test="coverUrl != null">cover_url,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="code != null">code,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="title != null and title != ''">#{title},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="videoUrl != null and videoUrl != ''">#{videoUrl},</if>
            <if test="coverUrl != null">#{coverUrl},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="code != null">#{code},</if>
         </trim>
    </insert>

    <insert id="batchInsert" parameterType="Help" useGeneratedKeys="true" keyProperty="id">
        insert into bst_help
        <trim prefix="(" suffix=")" suffixOverrides=",">
            title,
            status,
            video_url,
            cover_url,
            create_time,
            update_time,
            remark,
            code,
        </trim>
        values
        <foreach collection="list" item="i" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="i.title != null  and i.title != ''">#{i.title},</if>
                <if test="i.title == null  or i.title == ''">default,</if>
                <if test="i.status != null  and i.status != ''">#{i.status},</if>
                <if test="i.status == null  or i.status == ''">default,</if>
                <if test="i.videoUrl != null  and i.videoUrl != ''">#{i.videoUrl},</if>
                <if test="i.videoUrl == null  or i.videoUrl == ''">default,</if>
                <if test="i.coverUrl != null ">#{i.coverUrl},</if>
                <if test="i.coverUrl == null ">default,</if>
                <if test="i.createTime != null ">#{i.createTime},</if>
                <if test="i.createTime == null ">default,</if>
                <if test="i.updateTime != null ">#{i.updateTime},</if>
                <if test="i.updateTime == null ">default,</if>
                <if test="i.remark != null ">#{i.remark},</if>
                <if test="i.remark == null ">default,</if>
                <if test="i.code != null ">#{i.code},</if>
                <if test="i.code == null ">default,</if>
            </trim>
        </foreach>
    </insert>

    <update id="batchUpdate">
        update bst_help
        <trim prefix="SET" suffixOverrides=",">
            <foreach open="title = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.title != null  and item.title != ''">
                        WHEN #{item.id} THEN #{item.title}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `title`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="status = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.status != null  and item.status != ''">
                        WHEN #{item.id} THEN #{item.status}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `status`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="video_url = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.videoUrl != null  and item.videoUrl != ''">
                        WHEN #{item.id} THEN #{item.videoUrl}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `video_url`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="cover_url = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.coverUrl != null ">
                        WHEN #{item.id} THEN #{item.coverUrl}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `cover_url`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="create_time = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.createTime != null ">
                        WHEN #{item.id} THEN #{item.createTime}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `create_time`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="update_time = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.updateTime != null ">
                        WHEN #{item.id} THEN #{item.updateTime}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `update_time`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="remark = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.remark != null ">
                        WHEN #{item.id} THEN #{item.remark}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `remark`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="code = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.code != null ">
                        WHEN #{item.id} THEN #{item.code}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `code`
                    </otherwise>
                </choose>
            </foreach>
        </trim>
        where id in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item.id}
        </foreach>
    </update>

    <update id="updateHelp" parameterType="Help">
        update bst_help
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where id = #{data.id}
    </update>

    <sql id="updateColumns">
        <if test="data.title != null and data.title != ''">title = #{data.title},</if>
        <if test="data.status != null and data.status != ''">status = #{data.status},</if>
        <if test="data.videoUrl != null and data.videoUrl != ''">video_url = #{data.videoUrl},</if>
        <if test="data.coverUrl != null">cover_url = #{data.coverUrl},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.updateTime != null">update_time = #{data.updateTime},</if>
        <if test="data.remark != null">remark = #{data.remark},</if>
        <if test="data.code != null">code = #{data.code},</if>
    </sql>

    <delete id="deleteHelpById" parameterType="Long">
        delete from bst_help where id = #{id}
    </delete>

    <delete id="deleteHelpByIds" parameterType="String">
        delete from bst_help where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
