<?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.ruoyi.frequency.voteoption.mapper.VoteOptionMapper">

    <resultMap type="com.ruoyi.frequency.voteoption.entity.VoteOption" id="VoteOptionResult">
        <result property="id"    column="id"    />
        <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="delFlag"    column="del_flag"    />
        <result property="voteId"    column="vote_id"    />
        <result property="name"    column="name"    />
    </resultMap>

    <sql id="selectVoteOptionVo">
        select id, create_by, create_time, update_by, update_time, del_flag, vote_id, name from t_vote_option
    </sql>

    <select id="selectVoteOptionList" parameterType="com.ruoyi.frequency.voteoption.entity.VoteOption" resultMap="VoteOptionResult">
        <include refid="selectVoteOptionVo"/>
        <where>
            <if test="createTime != null "> and create_time = #{createTime}</if>
            <if test="voteId != null "> and vote_id = #{voteId}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
        </where>
    </select>

    <select id="selectVoteOptionById" parameterType="Long" resultMap="VoteOptionResult">
        <include refid="selectVoteOptionVo"/>
        where id = #{id}
    </select>

    <insert id="insertVoteOption" parameterType="com.ruoyi.frequency.voteoption.entity.VoteOption">
        insert into t_vote_option
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</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="delFlag != null">del_flag,</if>
            <if test="voteId != null">vote_id,</if>
            <if test="name != null">name,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</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="delFlag != null">#{delFlag},</if>
            <if test="voteId != null">#{voteId},</if>
            <if test="name != null">#{name},</if>
         </trim>
    </insert>

    <update id="updateVoteOption" parameterType="com.ruoyi.frequency.voteoption.entity.VoteOption">
        update t_vote_option
        <trim prefix="SET" suffixOverrides=",">
            <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="delFlag != null">del_flag = #{delFlag},</if>
            <if test="voteId != null">vote_id = #{voteId},</if>
            <if test="name != null">name = #{name},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteVoteOptionById" parameterType="Long">
        delete from t_vote_option where id = #{id}
    </delete>

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

    <select id="getList" resultType="com.ruoyi.frequency.voteoption.entity.VoteOption">
        SELECT
            t.id,
            t.create_time,
            t.del_flag,
            t.vote_id,
            t.`name`,
            (
                SELECT
                    COUNT( 1 )
                FROM
                    t_vote_record vr
                WHERE
                    vr.del_flag = 1
                  AND vr.vote_id = t.vote_id
                  AND vr.option_id = t.id
            ) AS num,
            CASE

                WHEN (
                         SELECT
                             COUNT( 1 )
                         FROM
                             t_vote_record vr
                         WHERE
                             vr.del_flag = 1
                           AND vr.option_id = t.id
                           AND vr.user_type = #{userVo.userType}
                           AND vr.user_id = #{userVo.userId}
                     ) > 0 THEN
                    2 ELSE 1
                END isVote
        FROM
            t_vote_option AS t
        WHERE
            t.del_flag = 1
        ORDER BY
            t.sort
    </select>
</mapper>
