<?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.izouma.awesomeadmin.dao.ChatInfoMapper">
    <resultMap id="BaseResultMap" type="com.izouma.awesomeadmin.model.ChatInfo">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="user_id" property="userId" jdbcType="INTEGER"/>
        <result column="to_user_id" property="toUserId" jdbcType="INTEGER"/>
        <result column="content" property="content" jdbcType="VARCHAR"/>
        <result column="time" property="time" jdbcType="TIMESTAMP"/>
        <result column="content_type" property="contentType" jdbcType="INTEGER"/>
        <result column="state" property="state" jdbcType="INTEGER"/>
        <result column="project_id" property="projectId" jdbcType="INTEGER"/>
        <result column="store_id" property="storeId" jdbcType="INTEGER"/>
        <result column="type_flag" property="typeFlag" jdbcType="INTEGER"/>
        <result column="service_name" property="serviceName" jdbcType="VARCHAR"/>
    </resultMap>
    <sql id="Base_Column_List">
        id, user_id, to_user_id, content, time, content_type, state, project_id, store_id, type_flag, service_name
    </sql>
    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer">
        select
        <include refid="Base_Column_List"/>
        from chat_info
        where id = #{id,jdbcType=INTEGER}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
        DELETE FROM chat_info
        WHERE id = #{id,jdbcType=INTEGER}
    </delete>
    <insert id="insert" parameterType="com.izouma.awesomeadmin.model.ChatInfo" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO chat_info (id, user_id, to_user_id,
                               content, time, content_type,
                               state,project_id, store_id,type_flag)
        VALUES (#{id,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, #{toUserId,jdbcType=INTEGER},
                #{content,jdbcType=VARCHAR}, #{time,jdbcType=TIMESTAMP}, #{contentType,jdbcType=INTEGER},
                #{state,jdbcType=INTEGER},#{projectId,jdbcType=INTEGER},#{storeId,jdbcType=INTEGER},#{typeFlag,jdbcType=INTEGER})
    </insert>
    <insert id="insertSelective" parameterType="com.izouma.awesomeadmin.model.ChatInfo" useGeneratedKeys="true"
            keyProperty="id">
        insert into chat_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="userId != null">
                user_id,
            </if>
            <if test="toUserId != null">
                to_user_id,
            </if>
            <if test="content != null">
                content,
            </if>
            <if test="time != null">
                time,
            </if>
            <if test="contentType != null">
                content_type,
            </if>
            <if test="state != null">
                state,
            </if>
            <if test="projectId != null">
                project_id,
            </if>
            <if test="storeId != null">
                store_id,
            </if>
            <if test="typeFlag != null">
                type_flag,
            </if>
            <if test="serviceName != null">
                service_name,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=INTEGER},
            </if>
            <if test="userId != null">
                #{userId,jdbcType=INTEGER},
            </if>
            <if test="toUserId != null">
                #{toUserId,jdbcType=INTEGER},
            </if>
            <if test="content != null">
                #{content,jdbcType=VARCHAR},
            </if>
            <if test="time != null">
                #{time,jdbcType=TIMESTAMP},
            </if>
            <if test="contentType != null">
                #{contentType,jdbcType=INTEGER},
            </if>
            <if test="state != null">
                #{state,jdbcType=INTEGER},
            </if>
            <if test="projectId != null">
                #{projectId,jdbcType=INTEGER},
            </if>
            <if test="storeId != null">
                #{storeId,jdbcType=INTEGER},
            </if>
            <if test="typeFlag != null">
                #{typeFlag,jdbcType=INTEGER},
            </if>
            <if test="serviceName != null">
                #{serviceName,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.izouma.awesomeadmin.model.ChatInfo">
        update chat_info
        <set>
            <if test="userId != null">
                user_id = #{userId,jdbcType=INTEGER},
            </if>
            <if test="toUserId != null">
                to_user_id = #{toUserId,jdbcType=INTEGER},
            </if>
            <if test="content != null">
                content = #{content,jdbcType=VARCHAR},
            </if>
            <if test="time != null">
                time = #{time,jdbcType=TIMESTAMP},
            </if>
            <if test="contentType != null">
                content_type = #{contentType,jdbcType=INTEGER},
            </if>
            <if test="state != null">
                state = #{state,jdbcType=INTEGER},
            </if>
            <if test="projectId != null">
                project_id = #{projectId,jdbcType=INTEGER},
            </if>
            <if test="storeId != null">
                store_id = #{storeId,jdbcType=INTEGER},
            </if>
            <if test="typeFlag != null">
                type_flag = #{typeFlag,jdbcType=INTEGER},
            </if>
            <if test="serviceName != null">
                service_name = #{serviceName,jdbcType=VARCHAR},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.izouma.awesomeadmin.model.ChatInfo">
        UPDATE chat_info
        SET user_id      = #{userId,jdbcType=INTEGER},
            to_user_id   = #{toUserId,jdbcType=INTEGER},
            content      = #{content,jdbcType=VARCHAR},
            time         = #{time,jdbcType=TIMESTAMP},
            content_type = #{contentType,jdbcType=INTEGER},
            state        = #{state,jdbcType=INTEGER},
            project_id        = #{projectId,jdbcType=INTEGER}
            store_id        = #{storeId,jdbcType=INTEGER}
        WHERE id = #{id,jdbcType=INTEGER}
    </update>


    <select id="queryChatInfosByPage" parameterType="java.util.Map" resultType="com.izouma.awesomeadmin.model.ChatInfo">
        SELECT
        chat_info.*, uInfo.nickname as nick_name,
        uInfo.icon,
        serviceInfo.icon as service_icon,
        serviceInfo.service_name as  service_nick
        FROM
        chat_info
        LEFT JOIN sys_user uInfo ON chat_info.user_id = uInfo.id
        LEFT JOIN customer_service serviceInfo ON chat_info.service_name = serviceInfo.username
        <where>
            and state != 2

            <if test="chatInfo.id != null and !&quot;&quot;.equals(chatInfo.id)">
                and chat_info.id=#{chatInfo.id}
            </if>
            <if test="chatInfo.userId != null and !&quot;&quot;.equals(chatInfo.userId)">
                and chat_info.user_id=#{chatInfo.userId}

            </if>
            <if test="chatInfo.contentType != null and !&quot;&quot;.equals(chatInfo.contentType)">
                and content_type=#{chatInfo.contentType}
            </if>
            <if test="chatInfo.projectId != null and !&quot;&quot;.equals(chatInfo.projectId)">
                and chat_info.project_id=#{chatInfo.projectId}
            </if>
            <if test="chatInfo.storeId != null and !&quot;&quot;.equals(chatInfo.storeId)">
                and chat_info.store_id=#{chatInfo.storeId}
            </if>
            <if test="chatInfo.typeFlag != null and !&quot;&quot;.equals(chatInfo.typeFlag)">
                and chat_info.type_flag=#{chatInfo.typeFlag}
            </if>
            <if test="chatInfo.serviceName != null and !&quot;&quot;.equals(chatInfo.serviceName)">
                and chat_info.service_name=#{chatInfo.serviceName}
            </if>
        </where>
        order by time desc
    </select>

    <select id="queryLastChatInfo" parameterType="com.izouma.awesomeadmin.model.ChatInfo"
            resultType="com.izouma.awesomeadmin.model.ChatInfo">
        select
        <include refid="Base_Column_List"/>
        from chat_info
        <where>
            user_id = #{userId} and service_name = #{serviceName} and store_id=#{storeId}
        </where>
        order by time desc
        limit 1
    </select>

    <select id="queryAllChatInfo" parameterType="java.util.Map" resultType="com.izouma.awesomeadmin.model.ChatInfo">
        select
        <include refid="Base_Column_List"/>
        from chat_info
        <where>
            and state != 2
        </where>
        order by time desc
    </select>

    <select id="queryChatInfo" parameterType="java.util.Map" resultType="com.izouma.awesomeadmin.model.ChatInfo">
        select
        <include refid="Base_Column_List"/>
        from chat_info
        <where>
            and state != 2

            <if test="chatInfo.id != null and !&quot;&quot;.equals(chatInfo.id)">
                and id=#{chatInfo.id}
            </if>
            <if test="chatInfo.userId != null and !&quot;&quot;.equals(chatInfo.userId)">
                and user_id=#{chatInfo.userId}
            </if>
            <if test="chatInfo.toUserId != null and !&quot;&quot;.equals(chatInfo.toUserId)">
                and to_user_id=#{chatInfo.toUserId}
            </if>
            <if test="chatInfo.contentType != null and !&quot;&quot;.equals(chatInfo.contentType)">
                and content_type=#{chatInfo.contentType}
            </if>
        </where>
        LIMIT 1
    </select>


    <update id="delete">
        UPDATE chat_info SET state = 2
        <where>
            AND id = #{id}
        </where>
    </update>
    <update id="readAll">
        UPDATE chat_info
        SET state = 1
        WHERE
        user_id = #{userId}
        AND store_id = #{storeId}
        <if test="serviceName != null and !&quot;&quot;.equals(serviceName)">
            AND service_name = #{serviceName}
        </if>
        <if test="typeFlag != null and !&quot;&quot;.equals(typeFlag)">
            AND type_flag = #{typeFlag}
        </if>
    </update>

</mapper>