<?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="ink.wgink.login.app.dao.appversion.IAppVersionDao">
    
    <cache flushInterval="3600000"/>

    <resultMap id="appVersionDTO" type="ink.wgink.login.app.pojo.dtos.appversion.AppVersionDTO">
        <id property="appVersionId" column="app_version_id"/>
        <result property="appName" column="app_name"/>
        <result property="appSummary" column="app_summary"/>
        <result property="appVersion" column="app_version"/>
        <result property="appDownloadCount" column="app_download_count"/>
        <result property="appFile" column="app_file"/>
        <result property="appApiLock" column="app_api_lock"/>
        <result property="isNecessary" column="is_necessary"/>
        <result property="isRelease" column="is_release"/>
    </resultMap>
    
    <resultMap id="appVersionPO" type="ink.wgink.login.app.pojo.pos.appversion.AppVersionPO">
        <id property="appVersionId" column="app_version_id"/>
        <result property="appName" column="app_name"/>
        <result property="appSummary" column="app_summary"/>
        <result property="appVersion" column="app_version"/>
        <result property="appDownloadCount" column="app_download_count"/>
        <result property="appFile" column="app_file"/>
        <result property="appApiLock" column="app_api_lock"/>
        <result property="isNecessary" column="is_necessary"/>
        <result property="isRelease" column="is_release"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `app_version` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
            `app_version_id` char(36) NOT NULL COMMENT '主键',
            `app_name` varchar(255) NOT NULL COMMENT 'APP名称',
            `app_summary` varchar(255) NOT NULL COMMENT 'APP说明',
            `app_version` int(11) DEFAULT '0' COMMENT 'APP版本号',
            `app_download_count` int(11) DEFAULT '0' COMMENT '下载次数',
            `app_file` char(36) NOT NULL COMMENT 'APP文件',
            `app_api_lock` int(1) DEFAULT '0' COMMENT '接口版本锁定',
            `is_necessary` int(1) DEFAULT '0' COMMENT '强制更新',
            `is_release` int(1) DEFAULT '0' COMMENT '是否发布',
            `creator` char(36) DEFAULT NULL,
            `gmt_create` datetime DEFAULT NULL,
            `modifier` char(36) DEFAULT NULL,
            `gmt_modified` datetime DEFAULT NULL,
            `is_delete` int(1) DEFAULT '0',
            PRIMARY KEY (`id`,`app_version_id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    </update>

    <!-- 新增app版本 -->
    <insert id="save" parameterType="map" flushCache="true">
        INSERT INTO app_version(
            app_version_id,
            app_name,
            app_summary,
            app_version,
            app_download_count,
            app_file,
            is_necessary,
            is_release,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{appVersionId},
            #{appName},
            #{appSummary},
            #{appVersion},
            #{appDownloadCount},
            #{appFile},
            #{isNecessary},
            #{isRelease},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除app版本 -->
    <update id="remove" parameterType="map" flushCache="true">
        UPDATE
            app_version
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            app_version_id IN
        <foreach collection="appVersionIds" index="index" open="(" separator="," close=")">
            #{appVersionIds[${index}]}
        </foreach>
    </update>

    <!-- 修改app版本 -->
    <update id="update" parameterType="map" flushCache="true">
        UPDATE
            app_version
        SET
        <if test="appSummary != null and appSummary != ''">
            app_summary = #{appSummary},
        </if>
            app_file = #{appFile},
            is_necessary = #{isNecessary},
            is_release = #{isRelease},
            app_version = app_version + 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            app_version_id = #{appVersionId}
    </update>

    <!-- 更新APP下载次数 -->
    <update id="updateDownloadCount" parameterType="String" flushCache="true">
        UPDATE
            app_version
        SET
            app_download_count = app_download_count + 1
        WHERE
            app_version_id = #{_parameter}
    </update>

    <!-- 更新APP的锁定状态 -->
    <update id="updateApiLock" parameterType="map" flushCache="true">
        UPDATE
            app_version
        SET
            app_api_lock = #{appApiLock},
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            is_delete = 0
          AND
            app_version_id = #{appVersionId}
    </update>

    <!-- 更新APP的发布状态 -->
    <update id="updateRelease" parameterType="map" flushCache="true">
        UPDATE
            app_version
        SET
            is_release = #{isRelease},
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            is_delete = 0
        AND
            app_version_id = #{appVersionId}
    </update>


    <!-- app版本列表 -->
    <select id="list" parameterType="map" resultMap="appVersionDTO" useCache="true">
        SELECT
            t1.*
        FROM
            app_version t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND
            t1.app_name LIKE CONCAT('%', #{keywords}, '%')
        </if>
        <if test="startTime != null and startTime != ''">
            AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="appVersionIds != null and appVersionIds.size > 0">
            AND
            t1.app_version_id IN
            <foreach collection="appVersionIds" index="index" open="(" separator="," close=")">
                #{appVersionIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- app版本详情 -->
    <select id="get" parameterType="map" resultMap="appVersionDTO" useCache="true">
        SELECT
        t1.*
        FROM
        app_version t1
        WHERE
        t1.is_delete = 0
        <if test="appVersionId != null and appVersionId != ''">
            AND
            t1.app_version_id = #{appVersionId}
        </if>
        <if test="isRelease != null">
            AND
            t1.is_release = #{isRelease}
        </if>
    </select>

    <!-- app版本列表 -->
    <select id="listPO" parameterType="map" resultMap="appVersionPO" useCache="true">
        SELECT
            t1.*
        FROM
            app_version t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND
            t1.app_name LIKE CONCAT('%', #{keywords}, '%')
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="appVersionIds != null and appVersionIds.size > 0">
            AND
            t1.app_version_id IN
            <foreach collection="appVersionIds" index="index" open="(" separator="," close=")">
                #{appVersionIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- app版本详情 -->
    <select id="getPO" parameterType="map" resultMap="appVersionPO" useCache="true">
        SELECT
            t1.*
        FROM
            app_version t1
        WHERE
            t1.is_delete = 0
        <if test="appVersionId != null and appVersionId != ''">
        AND
            t1.app_version_id = #{appVersionId}
        </if>
        <if test="isRelease != null">
        AND
            t1.is_release = #{isRelease}
        </if>
    </select>
    
</mapper>