<?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.appmessage.mapper.AppMessageMapper">

    <resultMap type="com.ruoyi.frequency.appmessage.entity.AppMessage" id="AppMessageResult">
        <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="userType"    column="user_type"    />
        <result property="title"    column="title"    />
        <result property="content"    column="content"    />
        <result property="messageType"    column="message_type"    />
        <result property="userIds"    column="user_ids"    />
        <result property="hasRelease"    column="has_release"    />
        <result property="releaseTime"    column="release_time"    />
    </resultMap>

    <sql id="selectAppMessageVo">
        select id, create_by, create_time, update_by, update_time, del_flag, user_type, title, content, message_type, user_ids, has_release, release_time from t_app_message
    </sql>

    <select id="selectAppMessageList" parameterType="com.ruoyi.frequency.appmessage.entity.AppMessage" resultMap="AppMessageResult">
        <include refid="selectAppMessageVo"/>
        <where> and del_flag != 2 and is_system = 1
            <if test="createBy != null "> and create_by = #{createBy}</if>
            <if test="updateBy != null "> and update_by = #{updateBy}</if>
            <if test="userType != null "> and user_type = #{userType}</if>
            <if test="title != null  and title != ''"> and title like concat('%', #{title}, '%')</if>
            <if test="content != null  and content != ''"> and content = #{content}</if>
            <if test="messageType != null "> and message_type = #{messageType}</if>
            <if test="userIds != null "> and user_ids = #{userIds}</if>
            <if test="hasRelease != null "> and has_release = #{hasRelease}</if>
            <if test="releaseTime != null "> and release_time = #{releaseTime}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectAppMessageById" parameterType="Long" resultMap="AppMessageResult">
        <include refid="selectAppMessageVo"/>
        where id = #{id}
    </select>

    <insert id="insertAppMessage" parameterType="com.ruoyi.frequency.appmessage.entity.AppMessage">
        insert into t_app_message
        <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="userType != null">user_type,</if>
            <if test="title != null">title,</if>
            <if test="content != null">content,</if>
            <if test="messageType != null">message_type,</if>
            <if test="userIds != null">user_ids,</if>
            <if test="hasRelease != null">has_release,</if>
            <if test="releaseTime != null">release_time,</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="userType != null">#{userType},</if>
            <if test="title != null">#{title},</if>
            <if test="content != null">#{content},</if>
            <if test="messageType != null">#{messageType},</if>
            <if test="userIds != null">#{userIds},</if>
            <if test="hasRelease != null">#{hasRelease},</if>
            <if test="releaseTime != null">#{releaseTime},</if>
        </trim>
    </insert>

    <update id="updateAppMessage" parameterType="com.ruoyi.frequency.appmessage.entity.AppMessage">
        update t_app_message
        <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="userType != null">user_type = #{userType},</if>
            <if test="title != null">title = #{title},</if>
            <if test="content != null">content = #{content},</if>
            <if test="messageType != null">message_type = #{messageType},</if>
            <if test="userIds != null">user_ids = #{userIds},</if>
            <if test="hasRelease != null">has_release = #{hasRelease},</if>
            <if test="releaseTime != null">release_time = #{releaseTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAppMessageById" parameterType="Long">
        delete from t_app_message where id = #{id}
    </delete>

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


    <select id="messagePage" resultType="com.ruoyi.frequency.appmessage.entity.AppMessage">
        SELECT
        t.*, CASE
        WHEN t2.id IS NULL THEN
        1
        ELSE
        t2.is_read
        END isRead,
        t2.id userAppMessageId
        FROM
        t_app_message t
        LEFT JOIN t_user_app_message t2 ON t.id = t2.message_id
        AND t2.user_id = #{userVo.userId}
        AND t2.user_type = #{userVo.userType}
        WHERE
        t.del_flag = 1
        AND (t2.id is null or ( t2.id is not null AND t2.is_show = 1))
        AND t.has_release = 2
        AND (
        (
        t.user_type = #{userVo.userType}
        AND (
        t.message_type = 1
        OR (
        t.message_type = 2
        AND find_in_set(#{userVo.userId}, t.user_ids)
        )
        )
        )
        OR t.user_type = -1
        )
        AND t.release_time >=
        <if test="userVo.userType==1">
            (select create_time from t_customer where id = #{userVo.userId})
        </if>
        <if test="userVo.userType==2">
            (select create_time from t_store where id = #{userVo.userId})
        </if>
        <if test="userVo.userType==3">
            (select create_time from t_sale where id = #{userVo.userId})
        </if>
        <if test="userVo.userType==4">
            (select create_time from t_enterprise where id = #{userVo.userId})
        </if>
        <if test="list!=null and list.size()>0">
            AND t.id in
            <foreach collection="list" index="index" item="i" open="("
                     separator="," close=")">
                #{i}
            </foreach>
        </if>
        ORDER BY
        t.release_time DESC
    </select>

    <select id="messageCenter" resultType="int">
        SELECT
        count(t.id)
        FROM
        t_app_message t
        LEFT JOIN t_user_app_message t2 ON t.id = t2.message_id
        AND t2.user_id = #{userVo.userId}
        AND t2.user_type = #{userVo.userType}
        WHERE
        t.del_flag = 1
        AND (t2.id is null or ( t2.id is not null AND t2.is_read = 1 ))
        AND (
        (
        t.user_type = #{userVo.userType}
        AND (
        t.message_type = 1
        OR (
        t.message_type = 2
        AND find_in_set(#{userVo.userId}, t.user_ids)
        )
        )
        )
        OR t.user_type = -1
        )
        AND t.has_release = 2
        AND t.release_time >=
        <if test="userVo.userType==1">
            (select create_time from t_customer where id = #{userVo.userId})
        </if>
        <if test="userVo.userType==2">
            (select create_time from t_store where id = #{userVo.userId})
        </if>
        <if test="userVo.userType==3">
            (select create_time from t_sale where id = #{userVo.userId})
        </if>
        <if test="userVo.userType==4">
            (select create_time from t_enterprise where id = #{userVo.userId})
        </if>
    </select>

    <select id="systemMessageNew" resultType="com.ruoyi.frequency.appmessage.entity.AppMessage">
        SELECT
        t.*
        FROM
        t_app_message t
        LEFT JOIN t_user_app_message t2 ON t.id = t2.message_id
        AND t2.user_id = #{userVo.userId}
        AND t2.user_type = #{userVo.userType}
        WHERE
        t.del_flag = 1
        AND (t2.id is null or ( t2.id is not null AND t2.is_read = 1 ))
        AND (
        (
        t.user_type = #{userVo.userType}
        AND (
        t.message_type = 1
        OR (
        t.message_type = 2
        AND find_in_set(#{userVo.userId}, t.user_ids)
        )
        )
        )
        OR t.user_type = -1
        )
        AND t.has_release = 2
        AND t.release_time >=
        <if test="userVo.userType==1">
            (select create_time from t_customer where id = #{userVo.userId})
        </if>
        <if test="userVo.userType==2">
            (select create_time from t_store where id = #{userVo.userId})
        </if>
        <if test="userVo.userType==3">
            (select create_time from t_sale where id = #{userVo.userId})
        </if>
        <if test="userVo.userType==4">
            (select create_time from t_enterprise where id = #{userVo.userId})
        </if>
        order by t.release_time desc
        limit 1
    </select>

    <select id="messageDetail" resultType="com.ruoyi.frequency.appmessage.entity.AppMessage">
        SELECT
        t.*, CASE
        WHEN t2.id IS NULL THEN
        1
        ELSE
        t2.is_read
        END isRead,
        t2.id userAppMessageId
        FROM
        t_app_message t
        LEFT JOIN t_user_app_message t2 ON t.id = t2.message_id
        AND t2.user_id = #{userVo.userId}
        AND t2.user_type = #{userVo.userType}
        WHERE
        t.id = #{id}
    </select>
</mapper>
