<?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.slodon.b2b2c.dao.read.helpdesk.HelpdeskMsgReadMapper">
  <resultMap id="resultMap" type="com.slodon.b2b2c.helpdesk.pojo.HelpdeskMsg">
    <id column="msg_id" property="msgId" />
    <result column="member_id" property="memberId" />
    <result column="member_name" property="memberName" />
    <result column="member_avatar" property="memberAvatar" />
    <result column="vendor_id" property="vendorId" />
    <result column="vendor_name" property="vendorName" />
    <result column="vendor_avatar" property="vendorAvatar" />
    <result column="store_id" property="storeId" />
    <result column="store_name" property="storeName" />
    <result column="msg_content" property="msgContent" />
    <result column="msg_state" property="msgState" />
    <result column="msg_type" property="msgType" />
    <result column="user_type" property="userType" />
    <result column="add_time" property="addTime" />
    <result column="is_delete" property="isDelete" />
    <result column="wx_avatar_img" property="wxAvatarImg" />
  </resultMap>
  <!--按照主键值进行操作-->
  <sql id="pkWhere">
    WHERE `msg_id` = #{primaryKey}
  </sql>
  <!--操作条件-->
  <sql id="whereCondition">
    <if test="example != null">
      <trim prefix="WHERE" prefixOverrides="AND|OR">
        <if test="example.msgIdNotEquals != null">
          AND `msg_id` != #{example.msgIdNotEquals}
        </if>
        <if test="example.msgIdIn != null">
          AND `msg_id` in (${example.msgIdIn})
        </if>
        <if test="example.msgIdLt != null">
          AND `msg_id` <![CDATA[ < ]]> #{example.msgIdLt}
        </if>
        <if test="example.memberId != null">
          AND `member_id` = #{example.memberId}
        </if>
        <if test="example.memberName != null">
          AND `member_name` = #{example.memberName}
        </if>
        <if test="example.memberNameLike != null">
          AND `member_name` like concat('%',#{example.memberNameLike},'%')
        </if>
        <if test="example.memberAvatar != null">
          AND `member_avatar` = #{example.memberAvatar}
        </if>
        <if test="example.vendorId != null">
          AND `vendor_id` = #{example.vendorId}
        </if>
        <if test="example.vendorName != null">
          AND `vendor_name` = #{example.vendorName}
        </if>
        <if test="example.vendorNameLike != null">
          AND `vendor_name` like concat('%',#{example.vendorNameLike},'%')
        </if>
        <if test="example.vendorAvatar != null">
          AND `vendor_avatar` = #{example.vendorAvatar}
        </if>
        <if test="example.storeId != null">
          AND `store_id` = #{example.storeId}
        </if>
        <if test="example.storeName != null">
          AND `store_name` = #{example.storeName}
        </if>
        <if test="example.storeNameLike != null">
          AND `store_name` like concat('%',#{example.storeNameLike},'%')
        </if>
        <if test="example.msgContent != null">
          AND `msg_content` = #{example.msgContent}
        </if>
        <if test="example.msgContentLike != null">
          AND `msg_content` like concat('%',#{example.msgContentLike},'%')
        </if>
        <if test="example.msgState != null">
          AND `msg_state` = #{example.msgState}
        </if>
        <if test="example.msgType != null">
          AND `msg_type` = #{example.msgType}
        </if>
        <if test="example.userType != null">
          AND `user_type` = #{example.userType}
        </if>
        <if test="example.addTimeAfter != null">
          AND `add_time` <![CDATA[ >= ]]> #{example.addTimeAfter}
        </if>
        <if test="example.addTimeBefore != null">
          AND `add_time` <![CDATA[ <= ]]> #{example.addTimeBefore}
        </if>
        <if test="example.isDelete != null">
          AND `is_delete` = #{example.isDelete}
        </if>
        <if test="example.wxAvatarImg != null">
          AND `wx_avatar_img` = #{example.wxAvatarImg}
        </if>
      </trim>
    </if>
  </sql>
  <!--排序条件-->
  <sql id="orderBy">
    ORDER BY `msg_id` DESC
  </sql>
  <sql id="orderByOther">
    order by ${example.orderBy}
  </sql>
  <!--分组条件-->
  <sql id="groupBy">
    group by ${example.groupBy}
  </sql>
  <!--分页条件-->
  <sql id="limit">
    <if test="size != null and size &gt; 0">
      limit #{startRow},#{size}
    </if>
  </sql>
  <!--查询符合条件的记录数-->
  <select id="countByExample" parameterType="com.slodon.b2b2c.helpdesk.example.HelpdeskMsgExample" resultType="java.lang.Integer">
    SELECT
      COUNT(*)
    FROM `helpdesk_msg`
    <include refid="whereCondition" />
  </select>
  <!--根据主键查询记录-->
  <select id="getByPrimaryKey" resultMap="resultMap">
    SELECT
      *
    FROM `helpdesk_msg`
    <include refid="pkWhere" />
  </select>
  <!--查询符合条件的记录(所有字段)-->
  <select id="listByExample" resultMap="resultMap">
    SELECT
      *
    FROM `helpdesk_msg`
    <include refid="whereCondition" />
    <if test="example.groupBy != null">
      <include refid="groupBy" />
    </if>
    <choose>
      <when test="example.orderBy != null">
        <include refid="orderByOther" />
      </when>
      <otherwise>
        <include refid="orderBy" />
      </otherwise>
    </choose>
  </select>
  <!--分页查询符合条件的记录(所有字段)-->
  <select id="listPageByExample" resultMap="resultMap">
    SELECT
      *
    FROM `helpdesk_msg`
    <include refid="whereCondition" />
    <if test="example.groupBy != null">
      <include refid="groupBy" />
    </if>
    <choose>
      <when test="example.orderBy != null">
        <include refid="orderByOther" />
      </when>
      <otherwise>
        <include refid="orderBy" />
      </otherwise>
    </choose>
    <include refid="limit" />
  </select>
  <!--查询符合条件的记录(指定字段)-->
  <select id="listFieldsByExample" resultMap="resultMap">
    SELECT
      ${fields}
    FROM `helpdesk_msg`
    <include refid="whereCondition" />
    <if test="example.groupBy != null">
      <include refid="groupBy" />
    </if>
    <choose>
      <when test="example.orderBy != null">
        <include refid="orderByOther" />
      </when>
      <otherwise>
        <include refid="orderBy" />
      </otherwise>
    </choose>
  </select>
  <!--分页查询符合条件的记录(指定字段)-->
  <select id="listFieldsPageByExample" resultMap="resultMap">
    SELECT
      ${fields}
    FROM `helpdesk_msg`
    <include refid="whereCondition" />
    <if test="example.groupBy != null">
      <include refid="groupBy" />
    </if>
    <choose>
      <when test="example.orderBy != null">
        <include refid="orderByOther" />
      </when>
      <otherwise>
        <include refid="orderBy" />
      </otherwise>
    </choose>
    <include refid="limit" />
  </select>

  <select id="getChatList" resultMap="resultMap">
    SELECT
    A.*
    FROM `helpdesk_msg` A
    JOIN (SELECT store_id, MAX(msg_id) msg_id FROM helpdesk_msg GROUP BY store_id) B ON A.store_id =
    B.store_id
    AND A.msg_id = B.msg_id
    <if test="example.memberId != null">
      AND A.`member_id` = #{example.memberId}
    </if>
    <if test="example.memberName != null">
      AND A.`member_name` = #{example.memberName}
    </if>
    <if test="example.vendorId != null">
      AND A.`vendor_id` = #{example.vendorId}
    </if>
    <if test="example.vendorName != null">
      AND A.`vendor_name` = #{example.vendorName}
    </if>
    AND A.is_delete != 1
    ORDER BY A.msg_id DESC

    <choose>
      <when test="example.firstContact != null">
        LIMIT 1
      </when>
      <otherwise>
        <if test="size != null and size &gt; 0">limit #{start},#{size}</if>
      </otherwise>
    </choose>
  </select>

  <select id="getChatCount" resultType="java.lang.Integer">
    SELECT
    COUNT(A.msg_id)
    FROM `helpdesk_msg` A
    JOIN (SELECT store_id, MAX(msg_id) msg_id FROM helpdesk_msg GROUP BY store_id) B ON A.store_id =
    B.store_id
    AND A.msg_id = B.msg_id
    <if test="example.memberId != null and example.memberId !=''">
      AND A.`member_id` = #{example.memberId}
    </if>
    <if test="example.memberName != null and example.memberName !=''">
      AND A.`member_name` = #{example.memberName}
    </if>
    <if test="example.vendorId != null and example.vendorId !=''">
      AND A.`vendor_id` = #{example.vendorId}
    </if>
    <if test="example.vendorName != null and example.vendorName !=''">
      AND A.`vendor_name` = #{example.vendorName}
    </if>
    AND A.is_delete != 1
    ORDER BY A.msg_id DESC
  </select>
</mapper>