<?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.softwareVersion.mapper.SoftwareVersionMapper">

    <resultMap type="SoftwareVersionVO" id="SoftwareVersionResult" autoMapping="true">
        <result property="id"    column="id"    />
        <result property="version"    column="version"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="content"    column="content"    />
    </resultMap>

    <sql id="selectSoftwareVersionVo">
        select
            id,
            version,
            create_time,
            update_time,
            content
        from bst_software_version
    </sql>

    <sql id="searchCondition">
        <if test="query.version != null  and query.version != ''"> and version like concat('%', #{query.version}, '%')</if>
        <if test="query.content != null  and query.content != ''"> and content = #{query.content}</if>
        ${query.params.dataScope}
    </sql>

    <select id="selectSoftwareVersionList" parameterType="SoftwareVersionQuery" resultMap="SoftwareVersionResult">
        <include refid="selectSoftwareVersionVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectSoftwareVersionById" parameterType="Long" resultMap="SoftwareVersionResult">
        <include refid="selectSoftwareVersionVo"/>
        where id = #{id}
    </select>

    <insert id="insertSoftwareVersion" parameterType="SoftwareVersion" useGeneratedKeys="true" keyProperty="id">
        insert into bst_software_version
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="version != null">version,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="content != null">content,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="version != null">#{version},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="content != null">#{content},</if>
         </trim>
    </insert>

    <insert id="batchInsert" parameterType="SoftwareVersion" useGeneratedKeys="true" keyProperty="id">
        insert into bst_software_version
        <trim prefix="(" suffix=")" suffixOverrides=",">
            version,
            create_time,
            update_time,
            content,
        </trim>
        values
        <foreach collection="list" item="i" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="i.version != null ">#{i.version},</if>
                <if test="i.version == 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.content != null ">#{i.content},</if>
                <if test="i.content == null ">default,</if>
            </trim>
        </foreach>
    </insert>

    <update id="batchUpdate">
        update bst_software_version
        <trim prefix="SET" suffixOverrides=",">
            <foreach open="version = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.version != null ">
                        WHEN #{item.id} THEN #{item.version}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `version`
                    </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="content = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.content != null ">
                        WHEN #{item.id} THEN #{item.content}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `content`
                    </otherwise>
                </choose>
            </foreach>
        </trim>
        where id in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item.id}
        </foreach>
    </update>

    <update id="updateSoftwareVersion" parameterType="SoftwareVersion">
        update bst_software_version
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where id = #{data.id}
    </update>

    <sql id="updateColumns">
        <if test="data.version != null">version = #{data.version},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.updateTime != null">update_time = #{data.updateTime},</if>
        <if test="data.content != null">content = #{data.content},</if>
    </sql>

    <delete id="deleteSoftwareVersionById" parameterType="Long">
        delete from bst_software_version where id = #{id}
    </delete>

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