<?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.yonge.cooleshow.biz.dal.dao.AppVersionInfoDao">

	<resultMap type="com.yonge.cooleshow.biz.dal.entity.AppVersionInfo" id="AppVersionInfo">
		<result column="id_" property="id" />
		<result column="platform_" property="platform" />
		<result column="version_" property="version" />
		<result column="status_" property="status" />
		<result column="is_force_update_" property="isForceUpdate" />
		<result column="description_" property="description" />
		<result column="download_url_" property="downloadUrl" />
		<result column="operator_id_" property="operatorId" />
		<result column="update_time_" property="updateTime" />
		<result column="create_time_" property="createTime" />
	</resultMap>

	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="AppVersionInfo">
		SELECT * FROM
		app_version_info WHERE id_ = #{id}
	</select>
	
	<select id="queryNewestByPlatform" resultMap="AppVersionInfo">
		SELECT * FROM app_version_info WHERE platform_ = #{platform} and status_ = 'newest'
	</select>

	<!-- 全查询 -->
	<select id="findAll" resultMap="AppVersionInfo">
		SELECT * FROM app_version_info
		<where>
			<if test="search != null and search != ''">
				platform_ LIKE CONCAT('%',#{search},'%')
			</if>
		</where>
		ORDER BY status_ DESC
	</select>

	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.yonge.cooleshow.biz.dal.entity.AppVersionInfo"
		useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		<!-- <selectKey resultClass="int" keyProperty="id" > SELECT SEQ_WSDEFINITION_ID.nextval 
			AS ID FROM DUAL </selectKey> -->
		INSERT INTO app_version_info
		(id_,platform_,version_,status_,is_force_update_,description_,download_url_,operator_id_,update_time_,create_time_)
		VALUES(#{id},#{platform},#{version},#{status},#{isForceUpdate},#{description},#{downloadUrl},#{operatorId},NOW(),NOW())
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.yonge.cooleshow.biz.dal.entity.AppVersionInfo">
		UPDATE app_version_info
		<set>
			<if test="operatorId != null">
				operator_id_ = #{operatorId},
			</if>
			<if test="status != null">
				status_ = #{status},
			</if>
			<if test="downloadUrl != null">
				download_url_ = #{downloadUrl},
			</if>
			<if test="platform != null">
				platform_ = #{platform},
			</if>
			<if test="updateTime != null">
				update_time_ = #{updateTime},
			</if>
			<if test="version != null">
				version_ = #{version},
			</if>
			<if test="description != null">
				description_ = #{description},
			</if>
			<if test="isForceUpdate != null">
				is_force_update_ = #{isForceUpdate},
			</if>
		</set>
		WHERE id_ = #{id}
	</update>
	<update id="batchUpdateStatus">
		UPDATE app_version_info SET status_ = 'history',update_time_ = NOW() WHERE platform_ = #{platform} AND status_ = 'newest'
	</update>

	<!-- 根据主键删除一条记录 -->
	<delete id="delete">
		DELETE FROM app_version_info WHERE id_ = #{id}
	</delete>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="AppVersionInfo" parameterType="map">
		SELECT * FROM app_version_info
		<where>
			<if test="keyword != null and keyword.trim() != ''">
				and (id_ like concat('%',#{keyword},'%') or
				version_ like concat('%',#{keyword},'%') or
				description_ like concat('%',#{keyword},'%')
				)
			</if>
			<if test="version != null and version != ''">
				and version_ = #{version}
			</if>
			<if test="platform != null and platform != ''">
				and platform_ = #{platform}
			</if>
			<if test="status != null and status != ''">
				and status_ = #{status}
			</if>
		</where>
		order by version_ desc ,status_='newest' desc , id_ desc
		<include refid="global.limit" />
	</select>

	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM app_version_info
		<where>
			<if test="version != null and version != ''">
				and version_ = #{version}
			</if>
			<if test="platform != null and platform != ''">
				and platform_ = #{platform}
			</if>
			<if test="status != null and status != ''">
				and status_ = #{status}
			</if>
		</where>
	</select>

	<select id="selectAppVersionInfo" resultMap="AppVersionInfo">

		select avi.status_ from app_version_info avi where avi.platform_ = #{platform} and avi.version_ = #{version}

	</select>

	<select id="queryLatestByPlatform" resultMap="AppVersionInfo">
		SELECT *
		FROM app_version_info
		WHERE platform_ = #{platform}
		  and find_in_set(status_, #{status})
		order by version_ desc, status_='newest' desc, id_ desc
		limit 1
	</select>
</mapper>
