<?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.ImMsgBizUserMapper">
  <resultMap id="imMsgBizUserMap" type="com.tmerclub.cloud.im.model.ImMsgBizUser">
    <id column="id" property="id" />
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <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="biz_unread_num" property="bizUnreadNum"/>
    <result column="user_unread_num" property="userUnreadNum"/>
    <result column="last_msg_id" property="lastMsgId"/>
    <result column="last_msg_type" property="lastMsgType"/>
    <result column="last_msg_content" property="lastMsgContent"/>
    <result column="last_msg_who_send" property="lastMsgWhoSend"/>
    <result column="last_msg_time" property="lastMsgTime"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `id`,`create_time`,`update_time`,`send_type`,`biz_id`,`user_id`,`employee_id`,`biz_unread_num`,`user_unread_num`,`last_msg_id`,`last_msg_type`,`last_msg_content`,`last_msg_who_send`,`last_msg_time`
  </sql>
  <select id="getById" resultMap="imMsgBizUserMap">
    select <include refid="Vo_Column_List"/> from im_msg_biz_user where id = #{id}
  </select>
  <select id="getBySendTypeAndBizIdAndUserId" resultMap="imMsgBizUserMap">
    select <include refid="Vo_Column_List"/> from im_msg_biz_user where send_type = #{sendType} and biz_id = #{bizId} and user_id = #{userId}
  </select>
  <select id="listByUserId" resultType="com.tmerclub.cloud.im.model.ImMsgBizUser">
    select <include refid="Vo_Column_List"/> from im_msg_biz_user where user_id = #{userId} order by update_time desc
  </select>
  <insert id="save" useGeneratedKeys="true" keyProperty="id">
    insert into im_msg_biz_user (`send_type`,`biz_id`,`user_id`,`employee_id`,`biz_unread_num`,`user_unread_num`,`last_msg_id`,`last_msg_type`,`last_msg_content`,`last_msg_who_send`,`last_msg_time`)
    values (#{imMsgBizUser.sendType},#{imMsgBizUser.bizId},#{imMsgBizUser.userId},#{imMsgBizUser.employeeId},#{imMsgBizUser.bizUnreadNum},#{imMsgBizUser.userUnreadNum},#{imMsgBizUser.lastMsgId},#{imMsgBizUser.lastMsgType},#{imMsgBizUser.lastMsgContent},#{imMsgBizUser.lastMsgWhoSend},#{imMsgBizUser.lastMsgTime});
  </insert>
  <update id="update">
    update im_msg_biz_user
    <set>
      <if test="imMsgBizUser.sendType != null">
        `send_type` = #{imMsgBizUser.sendType},
      </if>
      <if test="imMsgBizUser.bizId != null">
        `biz_id` = #{imMsgBizUser.bizId},
      </if>
      <if test="imMsgBizUser.userId != null">
        `user_id` = #{imMsgBizUser.userId},
      </if>
      <if test="imMsgBizUser.employeeId != null">
        `employee_id` = #{imMsgBizUser.employeeId},
      </if>
      <if test="imMsgBizUser.bizUnreadNum != null">
        `biz_unread_num` = #{imMsgBizUser.bizUnreadNum},
      </if>
      <if test="imMsgBizUser.userUnreadNum != null">
        `user_unread_num` = #{imMsgBizUser.userUnreadNum},
      </if>
      <if test="imMsgBizUser.lastMsgId != null">
        `last_msg_id` = #{imMsgBizUser.lastMsgId},
      </if>
      <if test="imMsgBizUser.lastMsgType != null">
        `last_msg_type` = #{imMsgBizUser.lastMsgType},
      </if>
      <if test="imMsgBizUser.lastMsgContent != null">
        `last_msg_content` = #{imMsgBizUser.lastMsgContent},
      </if>
      <if test="imMsgBizUser.lastMsgWhoSend != null">
        `last_msg_who_send` = #{imMsgBizUser.lastMsgWhoSend},
      </if>
      <if test="imMsgBizUser.lastMsgTime != null">
        `last_msg_time` = #{imMsgBizUser.lastMsgTime},
      </if>
    </set>
    where id = #{imMsgBizUser.id}
  </update>
  <delete id="deleteById">
    delete from im_msg_biz_user where id = #{id}
  </delete>

  <select id="getImUserUnreadNum" resultType="java.lang.Integer">
    select SUM(user_unread_num) from im_msg_biz_user where user_id = #{userId}
    </select>

  <update id="updateUserUnreadHistor">
    UPDATE im_msg_biz_user SET user_unread_num =
        (select count(*) from im_msg_biz_user_history where im_msg_biz_user_id = #{imMsgBizUserId} and user_unread = #{userUnread} and who_send = #{whoSend})
    WHERE id = #{imMsgBizUserId}
  </update>

  <update id="updateShopUserUnreadMsgNum">
    UPDATE im_msg_biz_user SET user_unread_num =
        (SELECT COUNT(*) FROM im_msg_biz_user_history WHERE im_msg_biz_user_id = #{id} and employee_unread = #{unread} and who_send = #{whoSend})
    WHERE id = #{id}
  </update>
</mapper>
