<?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.sooka.mybatis.mapper.TCmsVoteMapper">

    <resultMap id="BaseResultMap" type="com.sooka.mybatis.model.TCmsVote">
        <id column="vote_id" jdbcType="INTEGER" property="vote_id" />
        <result column="content" jdbcType="VARCHAR" property="content" />
        <result column="flag" jdbcType="INTEGER" property="flag" />
        <result column="number" jdbcType="VARCHAR" property="number" />
        <result column="createtime" jdbcType="TIMESTAMP" property="createtime" />
        <result column="publisher" jdbcType="VARCHAR" property="publisher" />
        <result column="xh" jdbcType="INTEGER" property="xh" />
    </resultMap>

    <insert id="saveTopic" useGeneratedKeys="true" keyProperty="vote_id" keyColumn="vote_id" parameterType="com.sooka.mybatis.model.TCmsVote">
        insert into t_cms_vote (content,flag,number,createtime,publisher,xh)
        values (#{content,jdbcType=VARCHAR},#{flag,jdbcType=INTEGER},#{number,jdbcType=VARCHAR},
                #{createtime,jdbcType=TIMESTAMP},#{publisher,jdbcType=VARCHAR},#{xh,jdbcType=INTEGER})
    </insert>

    <insert id="saveOption" parameterType="java.util.List">
        insert into t_cms_vote (content,flag,number,createtime,publisher,xh)
        values
        <foreach collection="list" item="vote"  separator="," >
            (#{vote.content},#{vote.flag},#{vote.number},#{vote.createtime},#{vote.publisher},#{vote.xh})
        </foreach>
    </insert>

    <delete id="deleteTopic" parameterType="java.lang.Integer">
        delete from t_cms_vote
        where vote_id = #{vote_id,jdbcType=INTEGER}
    </delete>

    <delete id="deleteOption" parameterType="java.lang.Integer">
        delete from t_cms_vote
        where flag = #{vote_id,jdbcType=INTEGER}
    </delete>

    <select id="findTopicByOpId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select * from t_cms_vote where vote_id = (select DISTINCT flag from t_cms_vote where vote_id = #{vote_id,jdbcType=INTEGER})
    </select>

    <select id="findOption" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select * from t_cms_vote
        where flag = #{vote_id,jdbcType=INTEGER}
    </select>

    <select id="findTopicOption" parameterType="java.lang.Integer">
        select * from t_cms_vote where vote_id = #{vote_id,jdbcType=INTEGER}
        union all
        select * from t_cms_vote where flag = #{vote_id,jdbcType=INTEGER}
    </select>

    <update id="vote" parameterType="java.lang.Integer">
        update t_cms_vote set number = number+1 where vote_id = #{vote_id,jdbcType=INTEGER}
    </update>

    <select id="findTO" resultMap="BaseResultMap">
        SELECT *  FROM t_cms_vote WHERE flag =(
        SELECT vote_id FROM	t_cms_vote WHERE flag = 0 ORDER BY xh DESC LIMIT 1)
        union all
        (SELECT * FROM t_cms_vote WHERE	flag = 0 ORDER BY xh DESC LIMIT 1)
    </select>
</mapper>