<?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="org.dromara.member.mapper.AppVersionMapper">

    <resultMap type="org.dromara.member.domain.AppVersion" id="AppVersionResult">
        <id property="versionId" column="version_id"/>
        <result property="versionCode" column="version_code"/>
        <result property="versionName" column="version_name"/>
        <result property="updateContent" column="update_content"/>
        <result property="downloadUrl" column="download_url"/>
        <result property="isForce" column="is_force"/>
        <result property="platform" column="platform"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <resultMap type="org.dromara.member.domain.vo.AppVersionVo" id="AppVersionVoResult">
        <id property="versionId" column="version_id"/>
        <result property="versionCode" column="version_code"/>
        <result property="versionName" column="version_name"/>
        <result property="updateContent" column="update_content"/>
        <result property="downloadUrl" column="download_url"/>
        <result property="isForce" column="is_force"/>
        <result property="platform" column="platform"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <sql id="selectAppVersionVo">
        select version_id,
               version_code,
               version_name,
               update_content,
               download_url,
               is_force,
               platform,
               create_time
        from app_version
    </sql>

    <select id="selectAppVersionList" parameterType="AppVersion" resultMap="AppVersionVoResult">
        <include refid="selectAppVersionVo"/>
        <where>
            <if test="versionName != null and versionName != ''">and version_name like concat('%', #{versionName},
                '%')
            </if>
            <if test="versionCode != null and versionCode != ''">and version_code = #{versionCode}</if>
            <if test="platform != null and platform != ''">and platform = #{platform}</if>
            <if test="isForce != null">and is_force = #{isForce}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectAppVersionByVersionId" parameterType="Long" resultMap="AppVersionVoResult">
        <include refid="selectAppVersionVo"/>
        where version_id = #{versionId}
    </select>

    <select id="selectLatestVersionByPlatform" parameterType="String" resultMap="AppVersionVoResult">
        <include refid="selectAppVersionVo"/>
        where platform = #{platform}
        order by version_code desc
        limit 1
    </select>

    <select id="checkVersionCodeUnique" parameterType="AppVersion" resultType="int">
        select count(1) from app_version
        where version_code = #{versionCode} and platform = #{platform}
        <if test="versionId != null and versionId != 0">and version_id != #{versionId}</if>
    </select>

    <insert id="insertAppVersion" parameterType="AppVersion" useGeneratedKeys="true" keyProperty="versionId">
        insert into app_version
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="versionCode != null and versionCode != ''">version_code,</if>
            <if test="versionName != null and versionName != ''">version_name,</if>
            <if test="updateContent != null">update_content,</if>
            <if test="downloadUrl != null">download_url,</if>
            <if test="isForce != null">is_force,</if>
            <if test="platform != null and platform != ''">platform,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="versionCode != null and versionCode != ''">#{versionCode},</if>
            <if test="versionName != null and versionName != ''">#{versionName},</if>
            <if test="updateContent != null">#{updateContent},</if>
            <if test="downloadUrl != null">#{downloadUrl},</if>
            <if test="isForce != null">#{isForce},</if>
            <if test="platform != null and platform != ''">#{platform},</if>
            sysdate()
        </trim>
    </insert>

    <update id="updateAppVersion" parameterType="AppVersion">
        update app_version
        <trim prefix="SET" suffixOverrides=",">
            <if test="versionCode != null and versionCode != ''">version_code = #{versionCode},</if>
            <if test="versionName != null and versionName != ''">version_name = #{versionName},</if>
            <if test="updateContent != null">update_content = #{updateContent},</if>
            <if test="downloadUrl != null">download_url = #{downloadUrl},</if>
            <if test="isForce != null">is_force = #{isForce},</if>
            <if test="platform != null and platform != ''">platform = #{platform},</if>
        </trim>
        where version_id = #{versionId}
    </update>

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

    <delete id="deleteAppVersionByVersionIds" parameterType="String">
        delete from app_version where version_id in
        <foreach item="versionId" collection="array" open="(" separator="," close=")">
            #{versionId}
        </foreach>
    </delete>

</mapper>