<?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.voterecord.mapper.VoteRecordMapper">
    
    <resultMap type="com.ruoyi.frequency.voterecord.entity.VoteRecord" id="VoteRecordResult">
        <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="userId"    column="user_id"    />
        <result property="userType"    column="user_type"    />
        <result property="optionId"    column="option_id"    />
        <result property="optionName"    column="optionName"    />
        <result property="title"    column="title"    />
        <result property="content"    column="content"    />
        <result property="cutoffDate"    column="cutoff_date"    />
    </resultMap>

    <sql id="selectVoteRecordVo">
        SELECT
            t.id,
            t.create_by,
            t.create_time,
            t.update_by,
            t.update_time,
            t.del_flag,
            t.vote_id,
            t.user_id,
            t.user_type,
            t.option_id,
            t2.`name` optionName,
            t3.title,
            t3.content,
            t3.cutoff_date
        FROM
            t_vote_record AS t
                LEFT JOIN t_vote_option AS t2 ON t.option_id = t2.id
                LEFT JOIN t_vote AS t3 ON t2.vote_id = t3.id
    </sql>

    <select id="selectVoteRecordList" parameterType="com.ruoyi.frequency.voterecord.entity.VoteRecord" resultMap="VoteRecordResult">
        <include refid="selectVoteRecordVo"/>
        <where>
            and t.del_flag = 1
            <if test="createTime != null "> and t.create_time = #{createTime}</if>
            <if test="voteId != null  and voteId != ''"> and t.vote_id = #{voteId}</if>
            <if test="userId != null "> and t.user_id = #{userId}</if>
            <if test="userType != null "> and t.user_type = #{userType}</if>
            <if test="optionId != null "> and t.option_id = #{optionId}</if>
        </where>
        order by t.create_time desc
    </select>
    
    <select id="selectVoteRecordById" parameterType="Long" resultMap="VoteRecordResult">
        <include refid="selectVoteRecordVo"/>
        where t.id = #{id}
    </select>
        
    <insert id="insertVoteRecord" parameterType="com.ruoyi.frequency.voterecord.entity.VoteRecord">
        insert into t_vote_record
        <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="userId != null">user_id,</if>
            <if test="userType != null">user_type,</if>
            <if test="optionId != null">option_id,</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="userId != null">#{userId},</if>
            <if test="userType != null">#{userType},</if>
            <if test="optionId != null">#{optionId},</if>
         </trim>
    </insert>

    <update id="updateVoteRecord" parameterType="com.ruoyi.frequency.voterecord.entity.VoteRecord">
        update t_vote_record
        <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="userId != null">user_id = #{userId},</if>
            <if test="userType != null">user_type = #{userType},</if>
            <if test="optionId != null">option_id = #{optionId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteVoteRecordById" parameterType="Long">
        delete from t_vote_record where id = #{id}
    </delete>

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

    <update id="addVoteRecord" parameterType="com.ruoyi.frequency.voterecord.entity.VoteRecord">
        insert into t_vote_record
        (id, vote_id, create_time,update_time,user_id, user_type, option_id)
        values
            (#{req.id}, #{req.voteId},#{req.createTime},#{req.updateTime}, #{userVo.userId}, #{userVo.userType}, #{req.optionId})
        ON DUPLICATE  KEY UPDATE
                              del_flag = IF(option_id = #{req.optionId} ,
                                  IF( del_flag = 1 and option_id = #{req.optionId} , 3, 1 ),
                                  1
                                  ),
                              option_id = #{req.optionId}

    </update>
</mapper>