<?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.tmerclub.cloud.im.mapper.ImMsgBizUserHistoryMapper">
  <resultMap id="imMsgBizUserHistoryMap" type="com.tmerclub.cloud.im.model.ImMsgBizUserHistory">
    <id column="id" property="id" />
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="im_msg_biz_user_id" property="imMsgBizUserId"/>
    <result column="send_type" property="sendType"/>
    <result column="biz_id" property="bizId"/>
    <result column="user_id" property="userId"/>
    <result column="employee_id" property="employeeId"/>
    <result column="type" property="type"/>
    <result column="content" property="content"/>
    <result column="who_send" property="whoSend"/>
    <result column="user_unread" property="userUnread"/>
    <result column="employee_unread" property="employeeUnread"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `id`,`create_time`,`update_time`,`im_msg_biz_user_id`,`send_type`,`biz_id`,`user_id`,`employee_id`,`type`,`content`,`who_send`,`user_unread`,`employee_unread`
  </sql>
  <select id="listBySendTypeBizIdUserId" resultMap="imMsgBizUserHistoryMap">
    select <include refid="Vo_Column_List"/> from im_msg_biz_user_history where send_type = #{sendType} and biz_id = #{bizId} and user_id = #{userId}
<!--    <choose>-->
<!--      <when test="employeeId != null">-->
<!--        and `employee_id` = #{employeeId}-->
<!--      </when>-->
<!--      <otherwise>-->
<!--        and `read_range` = 0-->
<!--      </otherwise>-->
<!--    </choose>-->
    order by id desc
  </select>
  <select id="getById" resultMap="imMsgBizUserHistoryMap">
    select <include refid="Vo_Column_List"/> from im_msg_biz_user_history where id = #{id}
  </select>
  <insert id="save" useGeneratedKeys="true" keyProperty="id">
    insert into im_msg_biz_user_history (`im_msg_biz_user_id`,`send_type`,`biz_id`,`user_id`,`employee_id`,`type`,`content`,`who_send`,`user_unread`,`employee_unread`,`read_range`)
    values (#{imMsgBizUserHistory.imMsgBizUserId},#{imMsgBizUserHistory.sendType},#{imMsgBizUserHistory.bizId},#{imMsgBizUserHistory.userId},#{imMsgBizUserHistory.employeeId},#{imMsgBizUserHistory.type},#{imMsgBizUserHistory.content},#{imMsgBizUserHistory.whoSend},#{imMsgBizUserHistory.userUnread},#{imMsgBizUserHistory.employeeUnread},#{imMsgBizUserHistory.readRange});
  </insert>
  <update id="update">
    update im_msg_biz_user_history
    <set>
      <if test="imMsgBizUserHistory.imMsgBizUserId != null">
        `im_msg_biz_user_id` = #{imMsgBizUserHistory.imMsgBizUserId},
      </if>
      <if test="imMsgBizUserHistory.sendType != null">
        `send_type` = #{imMsgBizUserHistory.sendType},
      </if>
      <if test="imMsgBizUserHistory.bizId != null">
        `biz_id` = #{imMsgBizUserHistory.bizId},
      </if>
      <if test="imMsgBizUserHistory.userId != null">
        `user_id` = #{imMsgBizUserHistory.userId},
      </if>
      <if test="imMsgBizUserHistory.employeeId != null">
        `employee_id` = #{imMsgBizUserHistory.employeeId},
      </if>
      <if test="imMsgBizUserHistory.type != null">
        `type` = #{imMsgBizUserHistory.type},
      </if>
      <if test="imMsgBizUserHistory.content != null">
        `content` = #{imMsgBizUserHistory.content},
      </if>
      <if test="imMsgBizUserHistory.whoSend != null">
        `who_send` = #{imMsgBizUserHistory.whoSend},
      </if>
      <if test="imMsgBizUserHistory.userUnread != null">
        `user_unread` = #{imMsgBizUserHistory.userUnread},
      </if>
      <if test="imMsgBizUserHistory.employeeUnread != null">
        `employee_unread` = #{imMsgBizUserHistory.employeeUnread},
      </if>
    </set>
    where id = #{imMsgBizUserHistory.id}
  </update>
  <delete id="deleteById">
    delete from im_msg_biz_user_history where id = #{id}
  </delete>
  <select id="getHistoryByUserId" resultMap="imMsgBizUserHistoryMap">
    SELECT
    *
    FROM
    `im_msg_biz_user_history` imh
    LEFT JOIN ( SELECT id aid FROM `im_msg_biz_user_history` WHERE send_type = #{sendType}  AND user_id = #{userId}
        AND biz_id = #{bizId} AND `read_range` = 0 AND content NOT LIKE ( '%系统：转接成功：%' ) ORDER BY id )
    ims ON imh.id = ims.aid
    LEFT JOIN ( SELECT id bid FROM `im_msg_biz_user_history` WHERE send_type = #{sendType}  AND user_id = #{userId}
        AND biz_id = #{bizId} AND `read_range` = 0 AND content LIKE ( '%系统：转接成功：%' ) ORDER BY id DESC LIMIT 1 )
    ime ON imh.id = ime.bid
    WHERE send_type = #{sendType}  AND user_id = #{userId} AND biz_id = #{bizId} AND `read_range` = 0
    AND ( id IN ( aid ) OR id = bid )
    ORDER BY id DESC
  </select>

  <select id="getByParam" resultMap="imMsgBizUserHistoryMap">
    select <include refid="Vo_Column_List"/> from im_msg_biz_user_history
    where `send_type` = #{imMsgBizUserHistory.sendType}
          AND `biz_id` = #{imMsgBizUserHistory.bizId}
          AND `user_id` = #{imMsgBizUserHistory.userId}
          AND `employee_id` = #{imMsgBizUserHistory.employeeId}
          ORDER BY id DESC LIMIT 1
  </select>

  <select id="getUnreadHistoryByUserId" resultMap="imMsgBizUserHistoryMap">
    update im_msg_biz_user_history set user_unread = 1 where im_msg_biz_user_id = #{imMsgBizUserId} and user_unread = 0 and user_id = #{userId}
  </select>

  <update id="updateUnreadHistoryByUserId">
    update im_msg_biz_user_history set user_unread = 1 where im_msg_biz_user_id = #{imMsgBizUserId} and user_unread = 0 and user_id = #{userId}
  </update>

  <update id="updateUnreadHistoryByShopId">
    update im_msg_biz_user_history set employee_unread = 1 where im_msg_biz_user_id = #{id} and employee_unread = 0 and user_id = #{userId}
  </update>
</mapper>
