<?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.sckw.message.dao.KwmMessageUserMapper">

    <insert id="insertBatch">
        insert into kwm_message_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            id,
            msg_id,
            ent_id,
            user_id,
            remark,
            status,
            create_by,
            create_time,
            update_by,
            update_time,
            del_flag
        </trim>
        values
        <foreach collection="list" item="item" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                #{item.id,jdbcType=BIGINT},
                #{item.msgId,jdbcType=BIGINT},
                #{item.entId,jdbcType=BIGINT},
                #{item.userId,jdbcType=VARCHAR},
                #{item.remark,jdbcType=VARCHAR},
                #{item.status,jdbcType=INTEGER},
                #{item.createBy,jdbcType=BIGINT},
                #{item.createTime,jdbcType=TIMESTAMP},
                #{item.updateBy,jdbcType=BIGINT},
                #{item.updateTime,jdbcType=TIMESTAMP},
                #{item.delFlag,jdbcType=INTEGER}
            </trim>
        </foreach>
    </insert>
    <select id="userMsgCenter" resultType="com.sckw.message.model.vo.res.UserCenterMsgVO">
        SELECT
        mu.status as status,
        m.type as type,
        count(*) as num
        FROM
        kwm_message_user mu
        LEFT JOIN kwm_message m ON mu.msg_id = m.id
        <where>
            m.del_flag = 0 and mu.del_flag = 0
            <if test="item.entId != null">
                and mu.ent_id = #{item.entId}
            </if>
            <if test="item.userId != null">
                and mu.user_id = #{item.userId}
            </if>
        </where>
        GROUP BY m.type, mu.status
        ORDER BY mu.status ASC
        LIMIT 5;
    </select>
    <select id="getLatestMsg" resultType="com.sckw.message.model.dto.UserMsgCenterLatestMsgDTO">
        SELECT
        mu.id, mu.status, mu.create_time as createTime,
        m.category, m.type, m.title, m.content, m.url
        FROM
        kwm_message_user mu
        LEFT JOIN kwm_message m ON mu.msg_id = m.id
        <where>
            m.del_flag = 0 and mu.del_flag = 0
            <if test="item.status != null">
                and mu.status = #{item.status}
            </if>
            <if test="item.type != null and item.type != ''">
                and m.type = #{item.type}
            </if>
            <if test="item.entId != null">
                and mu.ent_id = #{item.entId}
            </if>
            <if test="item.userId != null">
                and mu.user_id = #{item.userId}
            </if>
        </where>
        order by mu.create_time desc limit 1;

    </select>
</mapper>