<?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.vote.mapper.VoteMapper">

    <resultMap type="com.ruoyi.frequency.vote.entity.Vote" id="VoteResult">
        <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="title"    column="title"    />
        <result property="content"    column="content"    />
        <result property="cutoffDate"    column="cutoff_date"    />
        <result property="initiator"    column="initiator"    />
        <result property="num"    column="num"    />
        <result property="type"    column="type"    />
        <result property="pickNum"    column="pick_num"    />
        <result property="likeNum"    column="like_num"    />
        <result property="commentNum"    column="comment_num"    />
        <result property="browseNum"    column="browse_num"    />
    </resultMap>

    <sql id="selectVoteVo">
        SELECT
            t.id,
            t.create_by,
            t.create_time,
            t.update_by,
            case when t.del_flag = 1 and t.cutoff_date &lt; now() then 4 else t.del_flag end as del_flag,
            t.update_time,
            t.title,
            t.content,
            t.cutoff_date,
            t.initiator,
            (
                SELECT
                    count(1)
                FROM
                    (
                        SELECT DISTINCT
                            user_id,
                            user_type
                        FROM
                            t_vote_record vr
                        WHERE
                            vr.del_flag = 1
                          AND vr.vote_id = t.id
                    ) rec
            ) AS num,
            t.type,
            t.pick_num,
            t.like_num,
            t.comment_num,
            t.browse_num
        FROM
            t_vote AS t
    </sql>

    <select id="selectVoteList" parameterType="com.ruoyi.frequency.vote.entity.Vote" resultMap="VoteResult">
        <include refid="selectVoteVo"/>
        <where>
             and t.del_flag != 2
            <if test="createTime != null "> and t.create_time = #{createTime}</if>
            <if test="title != null  and title != ''"> and t.title like concat('%',#{title}, '%')</if>
            <if test="content != null  and content != ''"> and t.content = #{content}</if>
            <if test="cutoffDate != null "> and t.cutoff_date = #{cutoffDate}</if>
            <if test="initiator != null  and initiator != ''"> and t.initiator = #{initiator}</if>
            <if test="delFlag != null ">
            <choose>
                <when test="delFlag == 4">
                    and t.cutoff_date &lt; now()
                    and t.del_flag = 1
                </when>
                <otherwise>
                    and t.del_flag = #{delFlag}
                </otherwise>
            </choose>
            </if>
            <if test="beginTime != null ">
                and  t.create_time >= #{beginTime}

            </if>
            <if test="endTime != null ">
                and  t.create_time &lt; #{endTime}
            </if>
            <if test="cutoffBeginTime != null ">
                and  t.cutoff_date >= #{cutoffBeginTime}

            </if>
            <if test="cutoffEndTime != null ">
                and  t.cutoff_date &lt;= #{cutoffEndTime}
            </if>
        </where>
        order by t.create_time desc
    </select>


    <select id="statistics" parameterType="com.ruoyi.frequency.vote.entity.Vote" resultType="com.ruoyi.frequency.vote.model.result.VoteCount">
        SELECT
        count(1) AS num,
        sum(
        (
        SELECT
        COUNT(DISTINCT vr.user_id)
        FROM
        t_vote_record vr
        WHERE
        vr.vote_id = t.id
        )
        ) AS accumulativeNumber
        FROM
        t_vote AS t
        where
        t.id in
        <foreach item="id" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="selectVoteById" parameterType="Long" resultMap="VoteResult">
        <include refid="selectVoteVo"/>
        where id = #{id}
    </select>

    <insert id="insertVote" parameterType="com.ruoyi.frequency.vote.entity.Vote">
        insert into t_vote
        <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="title != null">title,</if>
            <if test="content != null">content,</if>
            <if test="cutoffDate != null">cutoff_date,</if>
            <if test="initiator != null">initiator,</if>
            <if test="likeNum != null">like_num,</if>
            <if test="browseNum != null">browse_num,</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="title != null">#{title},</if>
            <if test="content != null">#{content},</if>
            <if test="cutoffDate != null">#{cutoffDate},</if>
            <if test="initiator != null">#{initiator},</if>
            <if test="likeNum != null">#{likeNum},</if>
            <if test="browseNum != null">#{browseNum},</if>
        </trim>
    </insert>

    <update id="updateVote" parameterType="com.ruoyi.frequency.vote.entity.Vote">
        update t_vote
        <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="title != null">title = #{title},</if>
            <if test="content != null">content = #{content},</if>
            <if test="cutoffDate != null">cutoff_date = #{cutoffDate},</if>
            <if test="initiator != null">initiator = #{initiator},</if>
            <if test="pickNum != null">pick_num = #{pickNum},</if>
            <if test="likeNum != null">like_num = like_num + #{likeNum},</if>
            <if test="browseNum != null">browse_num =  browse_num + #{browseNum},</if>
            <if test="commentNum != null">comment_num =  comment_num + #{commentNum},</if>
        </trim>
        where id = #{id}
        <if test="likeNum != null">and like_num + #{likeNum} >= 0</if>
        <if test="browseNum != null">and  browse_num + #{browseNum} >= 0</if>
        <if test="commentNum != null">and  comment_num + #{commentNum} >= 0</if>
    </update>

    <delete id="deleteVoteById" parameterType="Long">
        delete from t_vote where id = #{id}
    </delete>

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

    <select id="votePage" resultType="com.ruoyi.controller.resp.VotePageResp">
        SELECT
        t.id,
        t.create_time,
        t.del_flag,
        t.title,
        t.content,
        t.cutoff_date,
        t.initiator,
        t.type,
        t.pick_num,
        t.like_num,
        t.browse_num,
        t.comment_num,
        CASE WHEN l.id is null THEN 1 ELSE 2 END isLike,
        (
        SELECT
        count(1)
        FROM
        (
        SELECT DISTINCT
        user_id,
        user_type
        FROM
        t_vote_record vr
        WHERE
        vr.del_flag = 1
        AND vr.vote_id = t.id
        ) rec
        ) AS num,
        IF(( SELECT COUNT( 1 ) FROM t_vote_record vr WHERE vr.del_flag = 1 AND vr.vote_id = t.id  AND vr.user_type=#{userVo.userType}
        AND vr.user_id=#{userVo.userId}) > 0,2,1) isVote
        FROM
        t_vote AS t
        LEFT JOIN t_vote_like l on l.object_id = t.id AND l.type = 1 AND l.user_id = #{userVo.userId} AND l.user_type = #{userVo.userType}
        AND l.del_flag = 1
        WHERE
        t.del_flag = 1
        <if test="isSelf">
            AND (
            SELECT COUNT( 1 )
            FROM t_vote_record vr
            WHERE vr.del_flag = 1
            AND vr.vote_id = t.id
            AND vr.user_type=#{userVo.userType}
            AND vr.user_id=#{userVo.userId}
            )> 0
        </if>
        ORDER BY
        t.create_time DESC
    </select>


    <select id="voteDetail" resultType="com.ruoyi.controller.resp.VoteDetailResp">
        SELECT
            t.id,
            t.create_time,
            t.del_flag,
            t.title,
            t.content,
            t.cutoff_date,
            t.initiator,
            t.type,
            t.pick_num,
            t.like_num,
            t.browse_num,
            t.comment_num,
            CASE WHEN l.id is null THEN 1 ELSE 2 END isLike,
            (
                SELECT
                    count(1)
                FROM
                    (
                        SELECT DISTINCT
                            user_id,
                            user_type
                        FROM
                            t_vote_record vr
                        WHERE
                            vr.del_flag = 1
                          AND vr.vote_id = t.id
                    ) rec
            ) AS num,
            IF(( SELECT COUNT( 1 ) FROM t_vote_record vr WHERE vr.del_flag = 1 AND vr.vote_id = t.id  AND vr.user_type=#{userVo.userType}
                                                           AND vr.user_id=#{userVo.userId}) > 0,2,1) isVote
        FROM
            t_vote AS t
                LEFT JOIN t_vote_like l on l.object_id = t.id AND l.type = 1 AND l.user_id = #{userVo.userId} AND l.user_type = #{userVo.userType}
                AND l.del_flag = 1
        WHERE
            t.id = #{voteId}
    </select>
</mapper>
