<?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.activity.mapper.ActivityMapper">

    <resultMap type="com.ruoyi.frequency.activity.entity.Activity" id="ActivityResult">
        <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="image"    column="image"    />
        <result property="sort"    column="sort"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="browseNum"    column="browse_num"    />
        <result property="content"    column="content"    />
        <result property="likeNum"    column="like_num"    />
    </resultMap>



    <sql id="selectActivityVo">
        select
            t.id ,
            t.create_by ,
            t.create_time ,
            t.update_by ,
            t.update_time ,
            t.del_flag ,
            t.title ,
            t.image ,
            t.sort ,
            t.start_time ,
            t.end_time ,
            t.browse_num,
            t.content,
            t.like_num
        from t_activity AS t

    </sql>

    <select id="selectActivityList" parameterType="com.ruoyi.frequency.activity.entity.Activity" resultMap="ActivityResult">
        <include refid="selectActivityVo"/>
        <where> AND t.del_flag != 2
            <if test="title != null  and title != ''"> and t.title = #{title}</if>
            <if test="image != null  and image != ''"> and t.image = #{image}</if>
            <if test="sort != null "> and t.sort = #{sort}</if>
            <if test="browseNum != null "> and t.browse_num = #{browseNum}</if>
            <if test="delFlag != null and delFlag != ''"> and t.del_flag = #{delFlag}</if>
            <if test="startTime != null ">
                and  t.start_time >= #{startTime}
            </if>
            <if test="endTime != null ">
                and  t.start_time &lt; #{endTime}
            </if>
        </where>
        order by t.sort
    </select>

    <select id="selectActivityById" parameterType="Long" resultMap="ActivityResult">
        <include refid="selectActivityVo"/>
        where t.id = #{id}
    </select>

    <insert id="insertActivity" parameterType="com.ruoyi.frequency.activity.entity.Activity">
        insert into t_activity
        <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="image != null">image,</if>
            <if test="sort != null">sort,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</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="image != null">#{image},</if>
            <if test="sort != null">#{sort},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="browseNum != null">#{browseNum},</if>
        </trim>
    </insert>

    <update id="updateActivity" parameterType="com.ruoyi.frequency.activity.entity.Activity">
        update t_activity
        <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="image != null">image = #{image},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="browseNum != null">browse_num = browse_num + #{browseNum},</if>
            <if test="content != null">content = #{content},</if>
            <if test="likeNum != null">like_num = like_num + #{likeNum},</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>
    </update>

    <select id="activityPage" resultType="com.ruoyi.frequency.activity.entity.Activity">
        select
        t.id ,
        t.create_by ,
        t.create_time ,
        t.update_by ,
        t.update_time ,
        t.del_flag ,
        t.title ,
        t.image ,
        t.sort ,
        t.start_time ,
        t.end_time ,
        t.browse_num,
        t.like_num,
        t.content,
        CASE WHEN fl.id is null THEN 1 ELSE 2 END isLike,
        1 isSelf
        from t_activity AS t
        LEFT JOIN t_like fl on fl.link_id = t.id AND fl.type = 3 AND fl.user_id = #{userVo.userId} AND fl.user_type = #{userVo.userType}
        AND fl.del_flag = 1
        <where> AND t.del_flag = 1
            <if test="req.keyword != null  and req.keyword != ''">
                and t.title like concat('%',#{req.keyword},'%')
            </if>
        </where>
        order by t.sort
    </select>

    <select id="activityDetail" resultType="com.ruoyi.frequency.activity.entity.Activity">
        select
        t.id ,
        t.create_by ,
        t.create_time ,
        t.update_by ,
        t.update_time ,
        t.del_flag ,
        t.title ,
        t.image ,
        t.sort ,
        t.start_time ,
        t.end_time ,
        t.browse_num,
        t.like_num,
        t.content,
        CASE WHEN fl.id is null THEN 1 ELSE 2 END isLike,
        1 isSelf
        from t_activity AS t
        LEFT JOIN t_like fl on fl.link_id = t.id AND fl.type = 3 AND fl.user_id = #{userVo.userId} AND fl.user_type = #{userVo.userType}
        AND fl.del_flag = 1
        where t.id = #{id}
    </select>
    <select id="statistics" resultType="com.ruoyi.frequency.activity.model.result.ActivityCount">
        SELECT
            count(1) num,
            IFNULL(sum(t.browse_num), 0) browseNum,
            IFNULL(sum(t.like_num), 0) likeNum
        FROM
            t_activity t
        where
        t.id in
        <foreach item="id" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>
</mapper>