<?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.UsersSessionMapper">
    <resultMap id="BaseResultMap" type="com.izouma.awesomeadmin.model.UsersSession">
        <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="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="type" property="type" jdbcType="INTEGER"/>
        <result column="project_id" property="projectId" jdbcType="INTEGER"/>
        <result column="del_flag" property="delFlag" jdbcType="CHAR"/>
        <result column="contact_id" property="contactId" jdbcType="INTEGER"/>
        <result column="store_id" property="storeId" jdbcType="INTEGER"/>
    </resultMap>
    <sql id="Base_Column_List">
        id, user_id, to_user_id, create_time, update_time, type, project_id, del_flag, contact_id, store_id
    </sql>
    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer">
        select
        <include refid="Base_Column_List"/>
        from users_session
        where id = #{id,jdbcType=INTEGER}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
        DELETE FROM users_session
        WHERE id = #{id,jdbcType=INTEGER}
    </delete>
    <insert id="insert" parameterType="com.izouma.awesomeadmin.model.UsersSession" useGeneratedKeys="true"
            keyProperty="id">
        INSERT INTO users_session (id, user_id, to_user_id,
                                   create_time, update_time, type,
                                   project_id, del_flag, contact_id, store_id)
        VALUES (#{id,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, #{toUserId,jdbcType=INTEGER},
                #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, #{type,jdbcType=INTEGER},
                #{projectId,jdbcType=INTEGER}, #{delFlag,jdbcType=CHAR}, #{contactId,jdbcType=INTEGER}, #{storeId,jdbcType=INTEGER})
    </insert>
    <insert id="insertSelective" parameterType="com.izouma.awesomeadmin.model.UsersSession" useGeneratedKeys="true"
            keyProperty="id">
        insert into users_session
        <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="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="type != null">
                type,
            </if>
            <if test="projectId != null">
                project_id,
            </if>
            <if test="delFlag != null">
                del_flag,
            </if>
            <if test="contactId != null">
                contact_id,
            </if>
            <if test="storeId != null">
                store_id,
            </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="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="type != null">
                #{type,jdbcType=INTEGER},
            </if>
            <if test="projectId != null">
                #{projectId,jdbcType=INTEGER},
            </if>
            <if test="delFlag != null">
                #{delFlag,jdbcType=CHAR},
            </if>
            <if test="contactId != null">
                #{contactId,jdbcType=INTEGER},
            </if>
            <if test="storeId != null">
                #{storeId,jdbcType=INTEGER},
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.izouma.awesomeadmin.model.UsersSession">
        update users_session
        <set>
            <if test="userId != null">
                user_id = #{userId,jdbcType=INTEGER},
            </if>
            <if test="toUserId != null">
                to_user_id = #{toUserId,jdbcType=INTEGER},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="type != null">
                type = #{type,jdbcType=INTEGER},
            </if>
            <if test="projectId != null">
                project_id = #{projectId,jdbcType=INTEGER},
            </if>
            <if test="delFlag != null">
                del_flag = #{delFlag,jdbcType=CHAR},
            </if>
            <if test="storeId != null">
                store_id = #{storeId,jdbcType=INTEGER},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.izouma.awesomeadmin.model.UsersSession">
        UPDATE users_session
        SET user_id     = #{userId,jdbcType=INTEGER},
            to_user_id  = #{toUserId,jdbcType=INTEGER},
            create_time = #{createTime,jdbcType=TIMESTAMP},
            update_time = #{updateTime,jdbcType=TIMESTAMP},
            type        = #{type,jdbcType=INTEGER},
            project_id  = #{projectId,jdbcType=INTEGER},
            del_flag    = #{delFlag,jdbcType=CHAR}
        WHERE id = #{id,jdbcType=INTEGER}
    </update>

    <update id="updateByChatInfo" parameterType="com.izouma.awesomeadmin.model.ChatInfo">
        UPDATE users_session
        SET update_time = NOW()
        WHERE
            del_flag = 'N'
        AND user_id = #{userId}
        AND store_id = #{storeId}
    </update>


    <select id="queryUsersSessionsByPage" parameterType="java.util.Map"
            resultType="com.izouma.awesomeadmin.model.UsersSession">
        select
        <include refid="Base_Column_List"/>
        from users_session
        <where>
            and del_flag = 'N'

            <if test="usersSession.id != null and !&quot;&quot;.equals(usersSession.id)">
                and id=#{UsersSession.id}
            </if>
            <if test="usersSession.userId != null and !&quot;&quot;.equals(usersSession.userId)">
                and user_id=#{usersSession.userId}
            </if>
            <if test="usersSession.toUserId != null and !&quot;&quot;.equals(usersSession.toUserId)">
                and to_user_id=#{usersSession.toUserId}
            </if>
            <if test="usersSession.type != null and !&quot;&quot;.equals(usersSession.type)">
                and type=#{usersSession.type}
            </if>
            <if test="usersSession.projectId != null and !&quot;&quot;.equals(usersSession.projectId)">
                and project_id=#{usersSession.projectId}
            </if>
            <if test="usersSession.storeId != null and !&quot;&quot;.equals(usersSession.storeId)">
                and store_id=#{usersSession.storeId}
            </if>
        </where>
        order by update_time desc
    </select>

    <select id="queryUsersSessionList" parameterType="java.util.Map"
            resultType="com.izouma.awesomeadmin.model.UsersSession">
        select
        <include refid="Base_Column_List"/>
        from users_session
        <where>
            and del_flag = 'N'

            <if test="userId != null and !&quot;&quot;.equals(userId)">
                and user_id=#{userId}
            </if>
            <if test="type != null and !&quot;&quot;.equals(type)">
                and type=#{type}
            </if>
            <if test="storeId != null and !&quot;&quot;.equals(storeId)">
                and store_id=#{storeId}
            </if>
        </where>
        order by update_time desc
    </select>


    <select id="queryAllUsersSession" parameterType="java.util.Map" resultMap="UsersSessionResultMap">
        SELECT DISTINCT
        us.id,
        us.user_id,
        us.type,
        us.store_id,
        MAX(us.update_time) update_time,
        COUNT(chat.id) AS amount
        FROM
        users_session us
        LEFT JOIN chat_info chat ON chat.user_id = us.user_id
        AND chat.store_id = us.store_id
        AND chat.type_flag = us.type
        AND chat.state = 0
        WHERE
        us.user_id != 1

        AND us.store_id IN (
        SELECT
        id
        FROM
        store_info
        WHERE
        del_flag = 'N'
        )
        AND us.del_flag = 'N'

        <if test="userId != null and !&quot;&quot;.equals(userId)">
            AND us.user_id = #{userId}
        </if>
        <if test="storeId != null and !&quot;&quot;.equals(storeId)">
            AND us.store_id = #{storeId}
        </if>
        <if test="type != null and !&quot;&quot;.equals(type)">
            AND us.type = #{type}
        </if>
        <if test="beginTime != null and !&quot;&quot;.equals(beginTime)">
            <![CDATA[ and  us.update_time >= #{beginTime}]]>
        </if>

        <if test="endTime != null and !&quot;&quot;.equals(endTime)">
            <![CDATA[ and   us.update_time <= #{endTime}]]>
        </if>

        GROUP BY
        us.id,
        us.user_id,
        us.type,
        us.store_id
        ORDER BY
        update_time DESC
    </select>


    <select id="queryAllUsersSessionByPage" parameterType="java.util.Map" resultMap="UsersSessionResultMap">

        SELECT DISTINCT
        us.id,
        us.user_id,
        us.type,
        us.store_id,
        MAX(us.update_time) update_time,
        COUNT(chat.id) AS amount
        FROM
        users_session us
        LEFT JOIN chat_info chat ON chat.user_id = us.user_id
        AND chat.store_id = us.store_id
        AND chat.type_flag = us.type
        AND chat.state = 0
        WHERE
        us.user_id != 1

        AND us.store_id IN (
        SELECT
        id
        FROM
        store_info
        WHERE
        del_flag = 'N'
        )
        AND us.del_flag = 'N'

        <if test="userId != null and !&quot;&quot;.equals(userId)">
            AND us.user_id = #{userId}
        </if>
        <if test="storeId != null and !&quot;&quot;.equals(storeId)">
            AND us.store_id = #{storeId}
        </if>
        <if test="type != null and !&quot;&quot;.equals(type)">
            AND us.type = #{type}
        </if>
        <if test="beginTime != null and !&quot;&quot;.equals(beginTime)">
            <![CDATA[ and  us.update_time >= #{beginTime}]]>
        </if>

        <if test="endTime != null and !&quot;&quot;.equals(endTime)">
            <![CDATA[ and   us.update_time <= #{endTime}]]>
        </if>

        GROUP BY
        us.id,
        us.user_id,
        us.type,
        us.store_id
        ORDER BY
        update_time DESC

    </select>

    <select id="queryUsersSession" parameterType="com.izouma.awesomeadmin.model.UsersSession"
            resultType="com.izouma.awesomeadmin.model.UsersSession">
        select
        <include refid="Base_Column_List"/>
        from users_session
        <where>
            and del_flag = 'N'

            <if test="id != null and !&quot;&quot;.equals(id)">
                and id=#{id}
            </if>
            <if test="userId != null and !&quot;&quot;.equals(userId)">
                and user_id=#{userId}
            </if>
            <if test="toUserId != null and !&quot;&quot;.equals(toUserId)">
                and to_user_id=#{toUserId}
            </if>
            <if test="type != null and !&quot;&quot;.equals(type)">
                and type=#{type}
            </if>
            <if test="projectId != null and !&quot;&quot;.equals(projectId)">
                and project_id=#{projectId}
            </if>
            <if test="contactId != null and !&quot;&quot;.equals(contactId)">
                and contact_id=#{contactId}
            </if>
            <if test="storeId != null and !&quot;&quot;.equals(storeId)">
                and store_id=#{storeId}
            </if>
        </where>
        LIMIT 1
    </select>


    <update id="delete">
        UPDATE users_session SET del_flag = 'Y'
        <where>
            AND id = #{id}
        </where>
    </update>


    <select id="getChat" parameterType="java.util.Map" resultType="com.izouma.awesomeadmin.model.ChatInfo">
        SELECT *
        FROM
        chat_info
        WHERE
        user_id = #{userId}
        AND state != 2
        <if test="storeId != null and !&quot;&quot;.equals(storeId)">
            AND store_id = #{storeId}
        </if>
        ORDER BY
        time DESC
        LIMIT 1
    </select>

    <resultMap id="UsersSessionResultMap" type="com.izouma.awesomeadmin.model.UsersSession">

        <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="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="type" property="type" jdbcType="INTEGER"/>
        <result column="project_id" property="projectId" jdbcType="INTEGER"/>
        <result column="store_id" property="storeId" jdbcType="INTEGER"/>
        <result column="chattime" property="chattime"/>
        <association
                property="chatInfo" javaType="com.izouma.awesomeadmin.model.ChatInfo"
                select="getChat"
                column="{ userId = user_id , storeId = store_id}"/>
        <association property="storeInfo"
                     javaType="com.izouma.awesomeadmin.model.StoreInfo"
                     select="com.izouma.awesomeadmin.dao.StoreInfoMapper.queryStoreInfo"
                     column="{ id = store_id }"/>
        <association property="userInfo"
                     javaType="com.izouma.awesomeadmin.model.UserInfo"
                     select="com.izouma.awesomeadmin.dao.UserInfoMapper.querySingleUserInfo"
                     column="{ id = user_id }"/>

    </resultMap>


</mapper>