<?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.FsUserCourseVideoRedPackageMapper">

    <resultMap type="FsUserCourseVideoRedPackage" id="FsUserCourseVideoRedPackageResult">
        <result property="id"    column="id"    />
        <result property="companyId"    column="company_id"    />
        <result property="videoId"    column="video_id"    />
        <result property="redPacketMoney"    column="red_packet_money"    />
    </resultMap>

    <sql id="selectFsUserCourseVideoRedPackageVo">
        select id, company_id, video_id, red_packet_money from fs_user_course_video_red_package
    </sql>

    <select id="selectFsUserCourseVideoRedPackageList" parameterType="FsUserCourseVideoRedPackage" resultMap="FsUserCourseVideoRedPackageResult">
        <include refid="selectFsUserCourseVideoRedPackageVo"/>
        <where>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="videoId != null "> and video_id = #{videoId}</if>
            <if test="redPacketMoney != null "> and red_packet_money = #{redPacketMoney}</if>
        </where>
    </select>

    <select id="selectFsUserCourseVideoRedPackageById" parameterType="Long" resultMap="FsUserCourseVideoRedPackageResult">
        <include refid="selectFsUserCourseVideoRedPackageVo"/>
        where id = #{id}
    </select>
    <select id="listByCompanyIdAndVideoIds" resultType="com.fs.course.domain.FsUserCourseVideoRedPackage">
        select * from fs_user_course_video_red_package where data_type = 1 and company_id = #{companyId} and video_id in <foreach collection="videoIdList" item="item" open="(" separator="," close=")">#{item}</foreach>
    </select>
    <select id="listByRuleIds" resultType="com.fs.course.domain.FsUserCourseVideoRedPackage">
        select * from fs_user_course_video_red_package where data_type = 3 and rule_id in <foreach collection="ids" item="item" open="(" separator="," close=")">#{item}</foreach>
    </select>

    <insert id="insertFsUserCourseVideoRedPackage" parameterType="FsUserCourseVideoRedPackage" useGeneratedKeys="true" keyProperty="id">
        insert into fs_user_course_video_red_package
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyId != null">company_id,</if>
            <if test="videoId != null">video_id,</if>
            <if test="redPacketMoney != null">red_packet_money,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="companyId != null">#{companyId},</if>
            <if test="videoId != null">#{videoId},</if>
            <if test="redPacketMoney != null">#{redPacketMoney},</if>
        </trim>
    </insert>

    <update id="updateFsUserCourseVideoRedPackage" parameterType="FsUserCourseVideoRedPackage">
        update fs_user_course_video_red_package
        <trim prefix="SET" suffixOverrides=",">
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="videoId != null">video_id = #{videoId},</if>
            <if test="redPacketMoney != null">red_packet_money = #{redPacketMoney},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteFsUserCourseVideoRedPackageById" parameterType="Long">
        delete from fs_user_course_video_red_package where id = #{id}
    </delete>

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

    <insert id="batchSaveFsUserCourseVideoRedPackage" useGeneratedKeys="true" keyProperty="id">
        insert into fs_user_course_video_red_package
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="list[0].companyId != null">company_id,</if>
            <if test="list[0].videoId != null">video_id,</if>
            <if test="list[0].redPacketMoney != null">red_packet_money,</if>
            <if test="list[0].periodId !=null">period_id,</if>
            <if test="list[0].dataType !=null">data_type,</if>
            <if test="list[0].ruleId !=null">rule_id,</if>
        </trim>
        VALUES
        <foreach collection="list" item="item" separator=",">
            <trim  prefix="(" suffix=")" suffixOverrides=",">
                <if test="item.companyId != null">#{item.companyId},</if>
                <if test="item.videoId != null">#{item.videoId},</if>
                <if test="item.redPacketMoney != null">#{item.redPacketMoney},</if>
                <if test="item.periodId !=null">#{item.periodId},</if>
                <if test="item.dataType !=null">#{item.dataType},</if>
                <if test="item.ruleId !=null">#{item.ruleId},</if>
            </trim>
        </foreach>
    </insert>

    <!-- 根据营期id、视频id、公司id删除红包数据   -->
    <delete id="batchDeleteParams" parameterType="String">
        delete from fs_user_course_video_red_package
        WHERE
        <foreach collection="list" item="item" separator=" OR ">
            (data_type = 2 AND period_id = #{item.periodId} AND video_id = #{item.videoId} AND company_id = #{item.companyId})
        </foreach>
    </delete>

    <!-- 批量更新红包数据（暂弃） -->
    <update id="batchUpdateFsUserCourseVideoRedPackage">
        <foreach collection="list" item="item" separator=";">
            update fs_user_course_video_red_package
            set red_packet_money = #{item.redPacketMoney}
            where period_id = #{item.periodId}
            and video_id = #{item.videoId}
            and company_id = #{item.companyId}
            and data_type = 2
        </foreach>
    </update>

    <!-- 批量查询匹配的红包数据 -->
    <select id="selectByParamsList" resultMap="FsUserCourseVideoRedPackageResult">
        select id, company_id, video_id, red_packet_money, period_id, data_type
        from fs_user_course_video_red_package
        where data_type = 2
        and (
        <foreach collection="list" item="item" separator=" OR ">
            (period_id = #{item.periodId} AND video_id = #{item.videoId} AND company_id = #{item.companyId})
        </foreach>
        )
    </select>

    <!-- 按条件更新红包金额 -->
    <update id="updateRedPackageByParams" parameterType="FsUserCourseVideoRedPackage">
        update fs_user_course_video_red_package
        set red_packet_money = #{redPacketMoney}
        where period_id = #{periodId}
          and video_id = #{videoId}
          and company_id = #{companyId}
          and data_type = 2
    </update>

    <select id="selectByRuleIds" resultType="com.fs.course.domain.FsUserCourseVideoRedPackage">
        select * from fs_user_course_video_red_package where data_type = 3 and rule_id in <foreach collection="ruleIds" open="(" separator="," close=")" item="item">#{item}</foreach>
    </select>
</mapper>
