<?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="cn.com.ty.lift.ud.chat.mapper.ChatMsgMapper" >
  <resultMap id="BaseResultMap" type="cn.com.ty.lift.ud.chat.mapper.entity.ChatMsgEntity" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Apr 28 14:25:10 CST 2020.
    -->
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="sessionid" property="sessionid" jdbcType="VARCHAR" />
    <result column="from_user" property="fromUser" jdbcType="VARCHAR" />
    <result column="to_user" property="toUser" jdbcType="VARCHAR" />
    <result column="type" property="type" jdbcType="INTEGER" />
    <result column="statuz" property="statuz" jdbcType="INTEGER" />
    <result column="create_by" property="createBy" jdbcType="VARCHAR" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_by" property="updateBy" jdbcType="VARCHAR" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
  </resultMap>
  <resultMap id="ResultMapWithBLOBs" type="cn.com.ty.lift.ud.chat.mapper.entity.ChatMsgEntity" extends="BaseResultMap" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Apr 28 14:25:10 CST 2020.
    -->
    <result column="msg" property="msg" jdbcType="LONGVARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Apr 28 14:25:10 CST 2020.
    -->
    id, sessionid, from_user, to_user, type, statuz, create_by, create_time, update_by, 
    update_time
  </sql>
  <sql id="Blob_Column_List" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Apr 28 14:25:10 CST 2020.
    -->
    msg
  </sql>
  <select id="selectByPrimaryKey" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Apr 28 14:25:10 CST 2020.
    -->
    select 
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    from chat_msg
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Apr 28 14:25:10 CST 2020.
    -->
    delete from chat_msg
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insertSelective" parameterType="cn.com.ty.lift.ud.chat.mapper.entity.ChatMsgEntity" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Apr 28 14:25:10 CST 2020.
    -->
    insert into chat_msg
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="sessionid != null" >
        sessionid,
      </if>
      <if test="fromUser != null" >
        from_user,
      </if>
      <if test="toUser != null" >
        to_user,
      </if>
      <if test="type != null" >
        type,
      </if>
      <if test="statuz != null" >
        statuz,
      </if>
      <if test="createBy != null" >
        create_by,
      </if>
      <if test="createTime != null" >
        create_time,
      </if>
      <if test="updateBy != null" >
        update_by,
      </if>
      <if test="updateTime != null" >
        update_time,
      </if>
      <if test="msg != null" >
        msg,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="sessionid != null" >
        #{sessionid,jdbcType=VARCHAR},
      </if>
      <if test="fromUser != null" >
        #{fromUser,jdbcType=INTEGER},
      </if>
      <if test="toUser != null" >
        #{toUser,jdbcType=INTEGER},
      </if>
      <if test="type != null" >
        #{type,jdbcType=INTEGER},
      </if>
      <if test="statuz != null" >
        #{statuz,jdbcType=INTEGER},
      </if>
      <if test="createBy != null" >
        #{createBy,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null" >
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null" >
        #{updateBy,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null" >
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="msg != null" >
        #{msg,jdbcType=LONGVARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="cn.com.ty.lift.ud.chat.mapper.entity.ChatMsgEntity" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Apr 28 14:25:10 CST 2020.
    -->
    update chat_msg
    <set >
      <if test="sessionid != null" >
        sessionid = #{sessionid,jdbcType=VARCHAR},
      </if>
      <if test="fromUser != null" >
        from_user = #{fromUser,jdbcType=INTEGER},
      </if>
      <if test="toUser != null" >
        to_user = #{toUser,jdbcType=INTEGER},
      </if>
      <if test="type != null" >
        type = #{type,jdbcType=INTEGER},
      </if>
      <if test="statuz != null" >
        statuz = #{statuz,jdbcType=INTEGER},
      </if>
      <if test="createBy != null" >
        create_by = #{createBy,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null" >
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null" >
        update_by = #{updateBy,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null" >
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="msg != null" >
        msg = #{msg,jdbcType=LONGVARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKeyWithBLOBs" parameterType="cn.com.ty.lift.ud.chat.mapper.entity.ChatMsgEntity" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Apr 28 14:25:10 CST 2020.
    -->
    update chat_msg
    set sessionid = #{sessionid,jdbcType=VARCHAR},
      from_user = #{fromUser,jdbcType=INTEGER},
      to_user = #{toUser,jdbcType=INTEGER},
      type = #{type,jdbcType=INTEGER},
      statuz = #{statuz,jdbcType=INTEGER},
      create_by = #{createBy,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_by = #{updateBy,jdbcType=VARCHAR},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      msg = #{msg,jdbcType=LONGVARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="cn.com.ty.lift.ud.chat.mapper.entity.ChatMsgEntity" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Apr 28 14:25:10 CST 2020.
    -->
    update chat_msg
    set sessionid = #{sessionid,jdbcType=VARCHAR},
      from_user = #{fromUser,jdbcType=INTEGER},
      to_user = #{toUser,jdbcType=INTEGER},
      type = #{type,jdbcType=INTEGER},
      statuz = #{statuz,jdbcType=INTEGER},
      create_by = #{createBy,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_by = #{updateBy,jdbcType=VARCHAR},
      update_time = #{updateTime,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=INTEGER}
  </update>
  
   
  <select id="selectByQuery" resultType="cn.com.ty.lift.ud.chat.mapper.entity.ChatMsgEntity" >
    SELECT t.*,users.`name`,users.avatar_url as avatarUrl,users.gender,users.signature,users.age,users.type as userLevel from chat_msg as t  
    LEFT JOIN user_info as users on t.from_user = users.user_id
    <where>
        ${ew.sqlSegment}
    </where>
    limit #{page.pageIndex},#{page.pageSize}
  </select>
  <select id="countByQuery" resultType="java.lang.Integer">
     SELECT count(0)from chat_msg as t  
    LEFT JOIN user_info as users on t.from_user = users.user_id
    <where>
        ${ew.sqlSegment}
    </where>
  </select>
  
  <select id="msgHistoryListQuery" resultType="cn.com.ty.lift.ud.neety.protocol.IMMessage">
  SELECT 
  'CHAT' cmd,
  users.`name` `name`,
  users.avatar_url AS avatarUrl,
  t.type `type`,
  t.`sessionid`,
  t.`to_user` receiver,
  t.`from_user` userId,
  t.`from_user` fromUser,
  cs.data_table dataTable,
  t.`msg` 
FROM
  chat_msg AS t 
  LEFT JOIN user_info AS users 
    ON t.from_user = users.user_id 
  LEFT JOIN chat_session cs 
    ON cs.`sessionid` = t.sessionid 
WHERE t.`sessionid` = #{page.sessionid}
limit #{page.pageIndex},#{page.pageSize}
  </select>
  
  <select id="countMsgHistoryList" resultType="java.lang.Integer">
   SELECT 
  count(0) 
FROM
  chat_msg AS t 
  LEFT JOIN user_info AS users 
    ON t.from_user = users.user_id 
  LEFT JOIN chat_session cs 
    ON cs.`sessionid` = t.sessionid 
WHERE t.`sessionid` = #{page.sessionid}
  </select>
</mapper>