<?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.inspect.metadata.version.mapper.VersionManagementMapper">

    <resultMap type="VersionManagement" id="VersionManagementResult">
        <result property="versionId" column="version_id"/>
        <result property="versionName" column="version_name"/>
        <result property="remark" column="remark"/>
        <result property="systemId" column="system_id"/>
        <result property="systemCode" column="system_code"/>
        <result property="systemName" column="system_name"/>
        <result property="systemType" column="system_type"/>
        <result property="filePath" column="file_path"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="reserve1" column="reserve1"/>
        <result property="reserve2" column="reserve2"/>
        <result property="reserve3" column="reserve3"/>
    </resultMap>

    <sql id="selectVersionManagementVo">
        select version_id,
               version_name,
               remark,
               system_id,
               system_code,
               system_name,
               system_type,
               file_path,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time,
               reserve1,
               reserve2,
               reserve3
        from version_management
    </sql>

    <select id="selectVersionManagementList" parameterType="VersionManagement" resultMap="VersionManagementResult">
        <include refid="selectVersionManagementVo"/>
        <where>
            <if test="versionName != null  and versionName != ''">and version_name like concat('%', #{versionName},
                '%')
            </if>
            <if test="systemName != null  and systemName != ''">and system_name like concat('%', #{systemName}, '%')
            </if>
            <if test="systemType != null  and systemType != ''">and system_type = #{systemType}</if>
        </where>
        ORDER BY create_time DESC
    </select>

    <select id="selectVersionManagementByVersionId" parameterType="Long" resultMap="VersionManagementResult">
        <include refid="selectVersionManagementVo"/>
        where version_id = #{versionId}
    </select>

    <insert id="insertVersionManagement" parameterType="VersionManagement" useGeneratedKeys="true"
            keyProperty="versionId">
        insert into version_management
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="versionName != null">version_name,</if>
            <if test="remark != null">remark,</if>
            <if test="systemId != null">system_id,</if>
            <if test="systemCode != null">system_code,</if>
            <if test="systemName != null">system_name,</if>
            <if test="systemType != null">system_type,</if>
            <if test="filePath != null">file_path,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="reserve1 != null">reserve1,</if>
            <if test="reserve2 != null">reserve2,</if>
            <if test="reserve3 != null">reserve3,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="versionName != null">#{versionName},</if>
            <if test="remark != null">#{remark},</if>
            <if test="systemId != null">#{systemId},</if>
            <if test="systemCode != null">#{systemCode},</if>
            <if test="systemName != null">#{systemName},</if>
            <if test="systemType != null">#{systemType},</if>
            <if test="filePath != null">#{filePath},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="reserve1 != null">#{reserve1},</if>
            <if test="reserve2 != null">#{reserve2},</if>
            <if test="reserve3 != null">#{reserve3},</if>
        </trim>
    </insert>

    <update id="updateVersionManagement" parameterType="VersionManagement">
        update version_management
        <trim prefix="SET" suffixOverrides=",">
            <if test="versionName != null">version_name = #{versionName},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="systemId != null">system_id = #{systemId},</if>
            <if test="systemCode != null">system_code = #{systemCode},</if>
            <if test="systemName != null">system_name = #{systemName},</if>
            <if test="systemType != null">system_type = #{systemType},</if>
            <if test="filePath != null">file_path = #{filePath},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="reserve1 != null">reserve1 = #{reserve1},</if>
            <if test="reserve2 != null">reserve2 = #{reserve2},</if>
            <if test="reserve3 != null">reserve3 = #{reserve3},</if>
        </trim>
        where version_id = #{versionId}
    </update>

    <delete id="deleteVersionManagementByVersionId" parameterType="Long">
        delete
        from version_management
        where version_id = #{versionId}
    </delete>

    <delete id="deleteVersionManagementByVersionIds" parameterType="String">
        delete from version_management where version_id in
        <foreach item="versionId" collection="array" open="(" separator="," close=")">
            #{versionId}
        </foreach>
    </delete>
    <select id="getNewVersionInfo" resultMap="VersionManagementResult">
        SELECT *
        FROM version_management b,
             (SELECT MAX(a.version_id) version_id FROM version_management a GROUP BY a.system_type) c
        WHERE b.version_id = c.version_id
    </select>
</mapper>
