<?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"  resultType="com.fs.live.vo.LiveUserFirstProfit">
        SELECT
            lo.order_id,lo.order_code,lo.user_name,lo.user_phone,lo.total_price,lo.pay_price,lo.is_pay,lo.status,
            cu.user_id AS company_user_id,
            cu.nick_name AS company_user_name,
            c.company_name
        FROM
            live_order lo
                LEFT JOIN
            live_user_first_entry lufe
            ON lo.user_id = lufe.user_id
                AND DATE(lo.create_time) = lufe.entry_date
            LEFT JOIN
            company_user cu
        ON lufe.company_user_id = cu.user_id
            left join company c on cu.company_id = c.company_id
        ORDER BY
            lo.create_time DESC
    </select>
</mapper>
