<?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.course.mapper.FsCourseRedPacketLogMapper">

    <resultMap type="FsCourseRedPacketLog" id="FsCourseRedPacketLogResult">
        <result property="logId"    column="log_id"    />
        <result property="courseId"    column="course_id"    />
        <result property="userId"    column="user_id"    />
        <result property="videoId"    column="video_id"    />
        <result property="companyUserId"    column="company_user_id"    />
        <result property="companyId"    column="company_id"    />
        <result property="amount"    column="amount"    />
        <result property="createTime"    column="create_time"    />
        <result property="qwUserId"    column="qw_user_id"    />
        <result property="outBatchNo"    column="out_batch_no"    />
        <result property="status"    column="status"    />
        <result property="updateTime"    column="update_time"    />
        <result property="watchLogId"    column="watch_log_id"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectFsCourseRedPacketLogVo">
        select log_id,watch_log_id, remark,out_batch_no,status,update_time,course_id, user_id, video_id, company_user_id, company_id, amount, create_time, qw_user_id from fs_course_red_packet_log
    </sql>

    <select id="selectFsCourseRedPacketLogList" parameterType="FsCourseRedPacketLog" resultMap="FsCourseRedPacketLogResult">
        <include refid="selectFsCourseRedPacketLogVo"/>
        <where>
            <if test="courseId != null "> and course_id = #{courseId}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="videoId != null "> and video_id = #{videoId}</if>
            <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="amount != null "> and amount = #{amount}</if>
            <if test="qwUserId != null  and qwUserId != ''"> and qw_user_id = #{qwUserId}</if>
        </where>
    </select>

    <select id="selectFsCourseRedPacketLogByLogId" parameterType="Long" resultMap="FsCourseRedPacketLogResult">
        <include refid="selectFsCourseRedPacketLogVo"/>
        where log_id = #{logId}
    </select>
    <select id="selectFsCourseRedPacketLogListVONew" resultType="com.fs.course.vo.FsCourseRedPacketLogListPVO">
        select
        watch.log_id,
        watch.user_id,
        uc.course_name,
        v.title as video_name,
        watch.log_type,
        watch.qw_external_contact_id,
        watch.qw_user_id,
        watch.course_id,
        watch.send_type,
        watch.video_id,
        l.create_time,
        SEC_TO_TIME(watch.duration) duration,
        l.amount,
        l.status,
        l.out_batch_no,
        v.title,u.nick_name as fsNickName,u.avatar as fsAvatar,u.phone,cu.nick_name company_user_name,c.company_name,qu.qw_user_name
        from fs_course_watch_log watch
        left join fs_course_red_packet_log l on watch.user_id = l.user_id and watch.video_id = l.video_id
        left join fs_user_course_video v on v.video_id = watch.video_id
        left join fs_user_course uc on uc.course_id = l.course_id
        left join fs_user u on u.user_id = watch.user_id
        left join company_user cu on cu.user_id=watch.company_user_id
        left join company c on c.company_id=watch.company_id
        LEFT JOIN qw_user qu on qu.id= watch.qw_user_id
        <where>
            <if test = ' userId !=null '> and watch.user_id = #{userId} </if>
            <if test = ' companyId !=null '> and watch.company_id = #{companyId} </if>
            <if test = ' companyUserName !=null '> and cu.nick_name  like concat('%', #{companyUserName}, '%') </if>
            <if test = ' nickName !=null '> and u.nick_name  like concat('%', #{nickName}, '%') </if>
            <if test = ' courseId !=null '> and watch.course_id = #{courseId} </if>
            <if test = ' videoId !=null '> and watch.video_id = #{videoId} </if>
            <if test = ' status !=null '> and l.status = #{status} </if>
            <if test = ' logType !=null '> and watch.log_type = #{logType} </if>
            <if test = ' phone !=null '> and u.phone = #{phone} </if>
            <if test = ' qwUserId !=null '> and watch.qw_user_id = #{qwUserId} </if>
            <if test="sTime != null">  and DATE(watch.create_time) &gt;= DATE(#{sTime})</if>
            <if test="eTime != null">  and DATE(watch.create_time) &lt;= DATE(#{eTime})</if>
        </where>
         order by watch.log_id desc
    </select>

    <!-- 查询新用户红包金额总和 -->
    <select id="getNewVipRedPackAmountByCompanyUserIdId" resultType="java.math.BigDecimal">
        select
            ifnull(sum(fcrpl.amount), 0)
        from fs_course_red_packet_log fcrpl
        inner join fs_user fu on fu.user_id = fcrpl.user_id and date(fu.create_time) = CURDATE()
        where fcrpl.company_user_id = #{companyUserId}
    </select>

    <insert id="insertFsCourseRedPacketLog" parameterType="FsCourseRedPacketLog" useGeneratedKeys="true" keyProperty="logId">
        insert into fs_course_red_packet_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="courseId != null">course_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="videoId != null">video_id,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="companyId != null">company_id,</if>
            <if test="amount != null">amount,</if>
            <if test="createTime != null">create_time,</if>
            <if test="qwUserId != null">qw_user_id,</if>
            <if test="outBatchNo != null">out_batch_no,</if>
            <if test="status != null">status,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="watchLogId != null">watch_log_id,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="courseId != null">#{courseId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="videoId != null">#{videoId},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="amount != null">#{amount},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="qwUserId != null">#{qwUserId},</if>
            <if test="outBatchNo != null">#{outBatchNo},</if>
            <if test="status != null">#{status},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="watchLogId != null">#{watchLogId},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateFsCourseRedPacketLog" parameterType="FsCourseRedPacketLog">
        update fs_course_red_packet_log
        <trim prefix="SET" suffixOverrides=",">
            <if test="courseId != null">course_id = #{courseId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="videoId != null">video_id = #{videoId},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="qwUserId != null">qw_user_id = #{qwUserId},</if>
            <if test="outBatchNo != null">out_batch_no = #{outBatchNo},</if>
            <if test="status != null">status = #{status},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="watchLogId != null">watch_log_id = #{watchLogId},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where log_id = #{logId}
    </update>

    <delete id="deleteFsCourseRedPacketLogByLogId" parameterType="Long">
        delete from fs_course_red_packet_log where log_id = #{logId}
    </delete>

    <delete id="deleteFsCourseRedPacketLogByLogIds" parameterType="String">
        delete from fs_course_red_packet_log where log_id in
        <foreach item="logId" collection="array" open="(" separator="," close=")">
            #{logId}
        </foreach>
    </delete>
</mapper>
