<?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.fs.store.mapper.FsUserOnlineStateMapper">

    <resultMap type="FsUserOnlineState" id="FsUserOnlineStateResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="nickname"    column="nickname"    />
        <result property="avatar"    column="avatar"    />
        <result property="phone"    column="phone"    />
        <result property="createTime"    column="create_time"    />
        <result property="maOpenId"    column="ma_open_id"    />
        <result property="mpOpenId"    column="mp_open_id"    />
        <result property="unionId"    column="union_id"    />
        <result property="status"    column="status"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyUserId"    column="company_user_id"    />
        <result property="companyName"    column="company_name"    />
        <result property="companyUserName"    column="company_user_name"    />
        <result property="onlineStatus"    column="online_status"    />
        <result property="onlineTime"    column="online_time"    />
        <result property="watchCourseCount"    column="watch_course_count"    />
        <result property="partCourseCount"    column="part_course_count"    />
        <result property="lastWatchDate"    column="last_watch_date"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectFsUserOnlineStateVo">
        select id, user_id, nickname, avatar, phone, create_time, ma_open_id, mp_open_id, union_id, status, company_id, company_user_id, company_name, company_user_name, online_status, online_time, watch_course_count, part_course_count, last_watch_date, update_time from fs_user_online_state
    </sql>

    <select id="selectFsUserOnlineStateList" parameterType="FsUserOnlineState" resultMap="FsUserOnlineStateResult">
        <include refid="selectFsUserOnlineStateVo"/>
        <where>
            <if test="nickname != null  and nickname != ''"> and nickname like concat('%', #{nickname}, '%')</if>
            <if test="avatar != null  and avatar != ''"> and avatar = #{avatar}</if>
            <if test="phone != null  and phone != ''"> and phone like concat ('%', #{phone}, '%')</if>
            <if test="maOpenId != null  and maOpenId != ''"> and ma_open_id = #{maOpenId}</if>
            <if test="mpOpenId != null  and mpOpenId != ''"> and mp_open_id = #{mpOpenId}</if>
            <if test="unionId != null  and unionId != ''"> and union_id = #{unionId}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
            <if test="companyName != null and companyName !='' "> and company_name like concat('%', #{companyName}, '%')</if>
            <if test="companyUserName != null and companyUserName !='' "> and company_user_name like concat('%', #{companyUserName}, '%')</if>
            <if test="onlineStatus != null "> and online_status = #{onlineStatus}</if>
            <if test="onlineTime != null "> and online_time = #{onlineTime}</if>
            <if test="watchCourseCount != null "> and watch_course_count = #{watchCourseCount}</if>
            <if test="partCourseCount != null "> and part_course_count = #{partCourseCount}</if>
            <if test="lastWatchDate != null "> and last_watch_date = #{lastWatchDate}</if>
            <if test="updateTime != null "> and update_time = #{updateTime}</if>
        </where>
    </select>

    <select id="selectFsUserOnlineStateById" parameterType="Long" resultMap="FsUserOnlineStateResult">
        <include refid="selectFsUserOnlineStateVo"/>
        where id = #{id}
    </select>

    <insert id="insertFsUserOnlineState" parameterType="FsUserOnlineState" useGeneratedKeys="true" keyProperty="id">
        insert into fs_user_online_state
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="nickname != null">nickname,</if>
            <if test="avatar != null">avatar,</if>
            <if test="phone != null">phone,</if>
            <if test="createTime != null">create_time,</if>
            <if test="maOpenId != null">ma_open_id,</if>
            <if test="mpOpenId != null">mp_open_id,</if>
            <if test="unionId != null">union_id,</if>
            <if test="status != null">status,</if>
            <if test="companyId != null">company_id,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="companyName != null">company_name,</if>
            <if test="companyUserName != null">company_user_name,</if>
            <if test="onlineStatus != null">online_status,</if>
            <if test="onlineTime != null">online_time,</if>
            <if test="watchCourseCount != null">watch_course_count,</if>
            <if test="partCourseCount != null">part_course_count,</if>
            <if test="lastWatchDate != null">last_watch_date,</if>
            <if test="updateTime != null ">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="nickname != null">#{nickname},</if>
            <if test="avatar != null">#{avatar},</if>
            <if test="phone != null">#{phone},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="maOpenId != null">#{maOpenId},</if>
            <if test="mpOpenId != null">#{mpOpenId},</if>
            <if test="unionId != null">#{unionId},</if>
            <if test="status != null">#{status},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="companyName != null">#{companyName},</if>
            <if test="companyUserName != null">#{companyUserName},</if>
            <if test="onlineStatus != null">#{onlineStatus},</if>
            <if test="onlineTime != null">#{onlineTime},</if>
            <if test="watchCourseCount != null">#{watchCourseCount},</if>
            <if test="partCourseCount != null">#{partCourseCount},</if>
            <if test="lastWatchDate != null">#{lastWatchDate},</if>
            <if test="updateTime != null ">#{updateTime},</if>
         </trim>
        on duplicate key update
        <trim suffixOverrides=",">
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="companyName != null">company_Name = #{companyName},</if>
            <if test="companyUserName != null">company_user_name = #{companyUserName},</if>
            <if test="updateTime != null ">update_time = #{updateTime}</if>
        </trim>
    </insert>

    <update id="updateFsUserOnlineState" parameterType="FsUserOnlineState">
        update fs_user_online_state
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="nickname != null">nickname = #{nickname},</if>
            <if test="avatar != null">avatar = #{avatar},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="maOpenId != null">ma_open_id = #{maOpenId},</if>
            <if test="mpOpenId != null">mp_open_id = #{mpOpenId},</if>
            <if test="unionId != null">union_id = #{unionId},</if>
            <if test="status != null">status = #{status},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="companyName != null">company_Name = #{companyName},</if>
            <if test="companyUserName != null">company_user_name = #{companyUserName},</if>
            <if test="onlineStatus != null">online_status = #{onlineStatus},</if>
            <if test="onlineTime != null">online_time = #{onlineTime},</if>
            <if test="watchCourseCount != null">watch_course_count = #{watchCourseCount},</if>
            <if test="partCourseCount != null">part_course_count = #{partCourseCount},</if>
            <if test="lastWatchDate != null">last_watch_date = #{lastWatchDate},</if>
            <if test="updateTime != null ">update_time = #{updateTime}</if>
        </trim>
        where user_id = #{userId}
    </update>

    <delete id="deleteFsUserOnlineStateById" parameterType="Long">
        delete from fs_user_online_state where user_id = #{userId}
    </delete>

    <delete id="deleteFsUserOnlineStateByIds" parameterType="String">
        delete from fs_user_online_state where user_id in
        <foreach item="userId" collection="array" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </delete>

    <select id="selectUserNotOnline" resultType="FsUserOnlineState">
        SELECT
            a.*,
            company_user.nick_name as companyUserName,
            company.company_name
        FROM
            (
                SELECT
                    user_id,
                    nickname,
                    avatar,
                    phone,
                    create_time,
                    ma_open_id,
                    mp_open_id,
                    union_id,
                    STATUS,
                    company_id,
                    company_user_id,
                    2 AS onlineStatus,
                    0 AS watchCourseCount,
                    0 AS partCourseCount,
                    NOW() AS updateTime
                FROM
                    fs_user
                WHERE
                    is_del = 0
                  AND user_id NOT IN ( SELECT DISTINCT user_id FROM fs_course_watch_log WHERE send_type = 1 )
            ) a
                LEFT JOIN company_user ON company_user.user_id = a.company_user_id
                LEFT JOIN company ON company.company_id = a.company_id
    </select>

    <select id="selectExistWatchLogUser" resultType="FsUser">
        SELECT
            count( 1 ),
            fs_course_watch_log.user_id,
            create_time
        FROM
            fs_course_watch_log
        WHERE
            send_type = 1
        GROUP BY
            fs_course_watch_log.user_id
        HAVING
            count( 1 ) = 1
           AND create_time >= DATE_SUB( NOW(), INTERVAL 15 MINUTE )
    </select>

</mapper>
