<?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.ImMsgBizEmployeeRecentMapper">
  <resultMap id="imMsgBizEmployeeRecentMap" type="com.tmerclub.cloud.im.model.ImMsgBizEmployeeRecent">
    <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="employee_unread_num" property="employeeUnreadNum"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `id`,`create_time`,`update_time`,`send_type`,`biz_id`,`user_id`,`employee_id`,`employee_unread_num`
  </sql>
  <select id="listBySendTypeAndBizIdAndEmployeeId" resultMap="imMsgBizEmployeeRecentMap">
    select <include refid="Vo_Column_List"/> from im_msg_biz_employee_recent
    where send_type = #{sendType} and biz_id = #{bizId} and employee_id = #{employeeId}
    order by update_time desc
  </select>
  <select id="getById" resultMap="imMsgBizEmployeeRecentMap">
    select <include refid="Vo_Column_List"/> from im_msg_biz_employee_recent where id = #{id}
  </select>
  <select id="getBySendTypeBizIdEmployeeIdUserId" resultMap="imMsgBizEmployeeRecentMap">
    select <include refid="Vo_Column_List"/> from im_msg_biz_employee_recent
    where send_type = #{sendType} and biz_id = #{bizId} and employee_id = #{employeeId} and user_id = #{userId}
  </select>
  <insert id="save" useGeneratedKeys="true" keyProperty="id">
    insert into im_msg_biz_employee_recent (`send_type`,`biz_id`,`user_id`,`employee_id`,`employee_unread_num`)
    values (#{imMsgBizEmployeeRecent.sendType},#{imMsgBizEmployeeRecent.bizId},#{imMsgBizEmployeeRecent.userId},#{imMsgBizEmployeeRecent.employeeId},#{imMsgBizEmployeeRecent.employeeUnreadNum});
  </insert>
  <update id="update">
    update im_msg_biz_employee_recent
    <set>
      <if test="imMsgBizEmployeeRecent.sendType != null">
        `send_type` = #{imMsgBizEmployeeRecent.sendType},
      </if>
      <if test="imMsgBizEmployeeRecent.bizId != null">
        `biz_id` = #{imMsgBizEmployeeRecent.bizId},
      </if>
      <if test="imMsgBizEmployeeRecent.userId != null">
        `user_id` = #{imMsgBizEmployeeRecent.userId},
      </if>
      <if test="imMsgBizEmployeeRecent.employeeId != null">
        `employee_id` = #{imMsgBizEmployeeRecent.employeeId},
      </if>
      <if test="imMsgBizEmployeeRecent.employeeUnreadNum != null">
        `employee_unread_num` = #{imMsgBizEmployeeRecent.employeeUnreadNum},
      </if>
    </set>
    where id = #{imMsgBizEmployeeRecent.id}
  </update>
  <delete id="deleteById">
    delete from im_msg_biz_employee_recent where id = #{id}
  </delete>

  <update id="updateEmployeeUnreadMsgNum">
    UPDATE im_msg_biz_employee_recent SET employee_unread_num = (
      SELECT COUNT(*) FROM im_msg_biz_user_history
      WHERE employee_id = #{employeeId}
      AND employee_unread = 0
      AND who_send = 2
      AND im_msg_biz_user_id = #{id}
    )
    WHERE send_type = #{sendType} and biz_id = #{shopId} and employee_id = #{employeeId} and user_id = #{userId}

  </update>
</mapper>
