<?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.KwmMessageMapper">

  <select id="findPage" resultType="com.sckw.message.model.dto.MessageListDTO" >
      select
      mu.id, mu.ent_id entId, mu.user_id userId, mu.status, mu.remark, mu.create_by createBy, mu.create_time createTime,
      mu.update_by updateBy, mu.update_time updateTime, m.id as msgId, m.category, m.type, m.title, m.content, m.url,
      m.params, m.client_type clientType, m.remark as msgRemark
      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>
          <if test="item.status != null">
              and mu.status = #{item.status}
          </if>
          <if test="item.category != null and item.category != ''">
              and m.category = #{item.category}
          </if>
          <if test="item.startCreateTime != null">
              and mu.create_time &gt;= #{item.startCreateTime,jdbcType=TIMESTAMP}
          </if>
          <if test="item.endCreateTime != null">
              and mu.create_time &lt;= #{item.endCreateTime,jdbcType=TIMESTAMP}
          </if>
      </where>
      ORDER BY
      <choose>
          <when test="item.sortType != null and item.sortType == 1">
              mu.status ASC, mu.create_time DESC
          </when>
          <otherwise>
              mu.create_time DESC
          </otherwise>
      </choose>
  </select>
    <select id="statistics" resultType="com.sckw.core.model.vo.TableTop">
        select
        mu.status as value,count(*) as total
        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>
            <if test="item.category != null and item.category != ''">
                and m.category = #{item.category}
            </if>
            <if test="item.startCreateTime != null">
                and mu.create_time &gt;= #{item.startCreateTime,jdbcType=TIMESTAMP}
            </if>
            <if test="item.endCreateTime != null">
                and mu.create_time &lt;= #{item.endCreateTime,jdbcType=TIMESTAMP}
            </if>
        </where>
        group by mu.status

    </select>
</mapper>