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

    <resultMap type="LiveRewardRecord" id="LiveRewardRecordResult">
        <result property="id"    column="id"    />
        <result property="liveId"    column="live_id"    />
        <result property="userId"    column="user_id"    />
        <result property="incomeType"    column="income_type"    />
        <result property="sourceType"    column="source_type"    />
        <result property="sourceId"    column="source_id"    />
        <result property="rewardType"    column="reward_type"    />
        <result property="num"    column="num"    />
        <result property="beforeNum"    column="before_num"    />
        <result property="afterNum"    column="after_num"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
    </resultMap>

    <sql id="selectLiveRewardRecordVo">
        select id, live_id, user_id, income_type, source_type, source_id, reward_type, num, before_num, after_num, create_time, create_by from live_reward_record
    </sql>

    <select id="selectLiveRewardRecordList" parameterType="LiveRewardRecord" resultMap="LiveRewardRecordResult">
        <include refid="selectLiveRewardRecordVo"/>
        <where>
            <if test="liveId != null "> and live_id = #{liveId}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="incomeType != null "> and income_type = #{incomeType}</if>
            <if test="sourceType != null "> and source_type = #{sourceType}</if>
            <if test="sourceId != null "> and source_id = #{sourceId}</if>
            <if test="rewardType != null "> and reward_type = #{rewardType}</if>
            <if test="num != null "> and num = #{num}</if>
            <if test="beforeNum != null "> and before_num = #{beforeNum}</if>
            <if test="afterNum != null "> and after_num = #{afterNum}</if>
        </where>
    </select>

    <select id="selectLiveRewardRecordById" parameterType="Long" resultMap="LiveRewardRecordResult">
        <include refid="selectLiveRewardRecordVo"/>
        where id = #{id}
    </select>

    <insert id="insertLiveRewardRecord" parameterType="LiveRewardRecord" useGeneratedKeys="true" keyProperty="id">
        insert into live_reward_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="liveId != null">live_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="incomeType != null">income_type,</if>
            <if test="sourceType != null">source_type,</if>
            <if test="sourceId != null">source_id,</if>
            <if test="rewardType != null">reward_type,</if>
            <if test="num != null">num,</if>
            <if test="beforeNum != null">before_num,</if>
            <if test="afterNum != null">after_num,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="liveId != null">#{liveId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="incomeType != null">#{incomeType},</if>
            <if test="sourceType != null">#{sourceType},</if>
            <if test="sourceId != null">#{sourceId},</if>
            <if test="rewardType != null">#{rewardType},</if>
            <if test="num != null">#{num},</if>
            <if test="beforeNum != null">#{beforeNum},</if>
            <if test="afterNum != null">#{afterNum},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
         </trim>
    </insert>

    <update id="updateLiveRewardRecord" parameterType="LiveRewardRecord">
        update live_reward_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="liveId != null">live_id = #{liveId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="incomeType != null">income_type = #{incomeType},</if>
            <if test="sourceType != null">source_type = #{sourceType},</if>
            <if test="sourceId != null">source_id = #{sourceId},</if>
            <if test="rewardType != null">reward_type = #{rewardType},</if>
            <if test="num != null">num = #{num},</if>
            <if test="beforeNum != null">before_num = #{beforeNum},</if>
            <if test="afterNum != null">after_num = #{afterNum},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null and createBy != ''">create_by = #{createBy},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteLiveRewardRecordById" parameterType="Long">
        delete from live_reward_record where id = #{id}
    </delete>

    <delete id="deleteLiveRewardRecordByIds" parameterType="String">
        delete from live_reward_record where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="selectRewardSummary" parameterType="com.fs.live.vo.LiveRewardRecordVo"  resultType="com.fs.live.vo.LiveRewardRecordStatisticsVo">
        SELECT
            CAST(lrr.live_id AS CHAR) AS liveId,
            l.live_name AS liveName,
            DATE_FORMAT(lrr.create_time, '%Y-%m') AS monthString,
            SUM(CASE WHEN lrr.reward_type = 2 THEN num ELSE 0 END) AS integralCount
        FROM
            live_reward_record lrr
                left join live l on lrr.live_id=l.live_id
        WHERE
            1=1
          AND lrr.reward_type = 2
        <if test="companyId != null">
            and lrr.source_id = #{companyId}
        </if>
        <if test="step != null">
            and DATE_FORMAT(lrr.create_time, '%Y-%m') in
            <foreach item="dateStr" collection="step" open="(" separator="," close=")">
                DATE_FORMAT(#{dateStr}, '%Y-%m')
            </foreach>
        </if>
        GROUP BY
            lrr.live_id, l.live_name, DATE_FORMAT(lrr.create_time, '%Y-%m')
    </select>
</mapper>
