<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright (C) 2018-2019
  ~ All rights reserved, Designed By admin
  -->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.starry.weichat.mapper.WxMsgMapper">
    <select id="listWxMsgMapGroup" resultType="com.baomidou.mybatisplus.core.metadata.IPage">
        select a.*,
        b.count_msg
        from
        wx_msg as a
        right join
        (select `wx_user_id`,
        max(`create_time`) as maxtime,
        count(`wx_user_id`) as count_msg
        from wx_msg
        <where>
            <if test="query.type != null and query.type != ''">
                AND `type` = #{query.type}
            </if>
            <if test="query.readFlag != null and query.readFlag != ''">
                AND `read_flag` = #{query.readFlag}
            </if>
            <if test="query.notInRepType != null and query.notInRepType != ''">
                AND `rep_type` NOT IN (#{query.notInRepType})
            </if>
        </where>
        group by `wx_user_id`) as b
        on a.`wx_user_id` = b.`wx_user_id`
        and a.`create_time` = b.maxtime
        order by a.`create_time` desc ;
    </select>
</mapper>
