<?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.live.mapper.LiveUserFirstEntryMapper">

    <resultMap type="LiveUserFirstEntry" id="LiveUserFirstEntryResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="liveId"    column="live_id"    />
        <result property="entryDate"    column="entry_date"    />
        <result property="firstEntryTime"    column="first_entry_time"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyUserId"    column="company_user_id"    />
    </resultMap>

    <sql id="selectLiveUserFirstEntryVo">
        select id, user_id, live_id, entry_date, first_entry_time,company_id,company_user_id, create_time, update_time from live_user_first_entry
    </sql>

    <select id="selectLiveUserFirstEntryList" parameterType="LiveUserFirstEntry" resultMap="LiveUserFirstEntryResult">
        <include refid="selectLiveUserFirstEntryVo"/>
        <where>
            <if test="userId != null  and userId != ''"> and user_id = #{userId}</if>
            <if test="liveId != null  and liveId != ''"> and live_id = #{liveId}</if>
            <if test="entryDate != null "> and entry_date = #{entryDate}</if>
            <if test="firstEntryTime != null "> and first_entry_time = #{firstEntryTime}</if>
            <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
        </where>
    </select>

    <select id="selectLiveUserFirstEntryById" parameterType="Long" resultMap="LiveUserFirstEntryResult">
        <include refid="selectLiveUserFirstEntryVo"/>
        where id = #{id}
    </select>

    <insert id="insertLiveUserFirstEntry" parameterType="LiveUserFirstEntry" useGeneratedKeys="true" keyProperty="id">
        insert into live_user_first_entry
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null and userId != ''">user_id,</if>
            <if test="liveId != null and liveId != ''">live_id,</if>
            <if test="entryDate != null">entry_date,</if>
            <if test="firstEntryTime != null">first_entry_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="companyId != null">company_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null and userId != ''">#{userId},</if>
            <if test="liveId != null and liveId != ''">#{liveId},</if>
            <if test="entryDate != null">#{entryDate},</if>
            <if test="firstEntryTime != null">#{firstEntryTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="companyId != null">#{companyId},</if>
         </trim>
    </insert>

    <update id="updateLiveUserFirstEntry" parameterType="LiveUserFirstEntry">
        update live_user_first_entry
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null and userId != ''">user_id = #{userId},</if>
            <if test="liveId != null and liveId != ''">live_id = #{liveId},</if>
            <if test="entryDate != null">entry_date = #{entryDate},</if>
            <if test="firstEntryTime != null">first_entry_time = #{firstEntryTime},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteLiveUserFirstEntryById" parameterType="Long">
        delete from live_user_first_entry where id = #{id}
    </delete>

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

    <select id="selectLiveProfitList" parameterType="com.fs.live.vo.LiveUserFirstProfit" resultType="com.fs.live.vo.LiveUserFirstProfit">

        select lv.live_id,lv.live_name,count(1) as liveNum,c.company_name,cu.user_id as companyUserId,cu.nick_name as companyUserName from live_user_first_entry lufe left join live lv on lv.live_id = lufe.live_id
        left join company_user cu on cu.user_id = lufe.company_user_id
        left join company c on c.company_id = cu.company_id
        where lufe.company_id > 0 and lufe.company_user_id > 0 and  lufe.company_id  is not null
        <if test="companyId != null"> and lufe.company_id = #{companyId}</if>
        <if test="companyUserId != null"> and lufe.company_user_id = #{companyUserId}</if>
        <if test="liveName != null and liveName != '' "> and lv.live_name like concat('%',#{liveName},'%')</if>
        group by lufe.live_id,lufe.company_id,lufe.company_user_id

        ORDER BY
        lv.create_time DESC

    </select>

    <select id="selectLiveUserEntryDetailList" resultType="com.fs.live.vo.LiveUserEntryDetail">
        select
            lv.live_id,
            lv.live_name,
            cu.user_id as companyUserId,
            cu.nick_name as companyUserName,
            lufe.user_id as userId,
            fu.nickname as userName,
            lufe.first_entry_time,
            lufe.entry_date
        from live_user_first_entry lufe
        left join live lv on lv.live_id = lufe.live_id
        left join company_user cu on cu.user_id = lufe.company_user_id
        left join fs_user fu on fu.user_id = lufe.user_id
        where 1=1
        <if test="liveId != null"> and lufe.live_id = #{liveId}</if>
        <if test="companyUserId != null"> and lufe.company_user_id = #{companyUserId}</if>
        order by lufe.first_entry_time desc
    </select>

<!--    select lv.live_id,lv.live_name,c.company_name,c.company_id,lufe.user_id,lufe.live_id,lufe.entry_date,lufe.first_entry_time,lufe.company_user_id,lufe.* from live_user_first_entry lufe left join live lv on lv.live_id = lufe.live_id-->
<!--    left join company_user cu on cu.company_user_id = lufe.company_user_id-->
<!--    left join company c on c.company_id = cu.company_id-->

<!--    where 1=1-->
<!--    <if test="companyId != null"> and lufe.company_id = #{companyId}</if>-->
<!--    <if test="companyUserId != null"> and lufe.company_user_id = #{companyUserId}</if>-->

<!--    <if test="liveName != null and liveName != '' "> and lv.liveName like concat('%',#{liveName},'%')</if>-->

<!--    group by lv.live_id-->

<!--    ORDER BY-->
<!--    lv.create_time DESC-->
</mapper>
