<?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.ruoyi.system.mapper.DailySourceMapper">
    
    <resultMap type="DailySource" id="DailySourceResult">
        <result property="dailySourceId"    column="daily_source_id"    />
        <result property="sourceId"    column="source_id"    />
        <result property="uMrTime"    column="u_mr_time"    />
        <result property="bought"    column="bought"    />
        <result property="interactions"    column="interactions"    />
        <result property="shares"    column="shares"    />
        <result property="date"    column="date"    />
    </resultMap>

    <resultMap id="DailySourceDtoResult" type="DailySourceDto">
        <result property="uMrTime"    column="u_mr_time"    />
        <result property="bought"    column="bought"    />
        <result property="interactions"    column="interactions"    />
        <result property="shares"    column="shares"    />
    </resultMap>


    <!--这不和上面的一样吗, income 也不能直接算啊, 暂定这样用吧 -->
    <resultMap id="DailyScenicDtoResult" type="DailyScenicDto">
        <result property="uMrTime"    column="u_mr_time"    />
        <result property="bought"    column="bought"    />
        <result property="interactions"    column="interactions"    />
        <result property="shares"    column="shares"    />
        <result property="income" column="income"/>
    </resultMap>

    <sql id="selectDailySourceVo">
        select daily_source_id, source_id, u_mr_time, bought, interactions, shares, date from daily_source
    </sql>

    <select id="selectDailySourceList" parameterType="DailySource" resultMap="DailySourceResult">
        <include refid="selectDailySourceVo"/>
        <where>  
            <if test="sourceId != null "> and source_id = #{sourceId}</if>
            <if test="uMrTime != null "> and u_mr_time = #{uMrTime}</if>
            <if test="bought != null "> and bought = #{bought}</if>
            <if test="interactions != null "> and interactions = #{interactions}</if>
            <if test="shares != null "> and shares = #{shares}</if>
            <if test="date != null "> and date = #{date}</if>
        </where>
    </select>
    
    <select id="selectDailySourceByDailySourceId" parameterType="Long" resultMap="DailySourceResult">
        <include refid="selectDailySourceVo"/>
        where daily_source_id = #{dailySourceId}
    </select>

    <select id="getStDailySource" resultMap="DailySourceDtoResult">
        select sum(u_mr_time) u_mr_time, sum(bought) bought, sum(interactions) interactions, sum(shares) shares
        from daily_source
        where date = #{date}
    </select>
    <select id="getDailyScenicDto" resultMap="DailyScenicDtoResult">
        select sum(d.u_mr_time) u_mr_time, sum(d.bought) bought, sum(d.interactions) interactions, sum(d.shares) shares
        from daily_source d
        left join sys_source s on d.source_id = s.source_id
        where date = #{date} and s.scenic_id = #{scenicId}

    </select>
    <select id="getDailySourceBySourceIdAndDate" resultMap="DailySourceResult">
        select * from daily_source
        where source_id = #{sourceId} and date = #{date}
    </select>

    <insert id="insertDailySource" parameterType="DailySource" useGeneratedKeys="true" keyProperty="dailySourceId">
        insert into daily_source
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sourceId != null">source_id,</if>
            <if test="uMrTime != null">u_mr_time,</if>
            <if test="bought != null">bought,</if>
            <if test="interactions != null">interactions,</if>
            <if test="shares != null">shares,</if>
            <if test="date != null">date,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="sourceId != null">#{sourceId},</if>
            <if test="uMrTime != null">#{uMrTime},</if>
            <if test="bought != null">#{bought},</if>
            <if test="interactions != null">#{interactions},</if>
            <if test="shares != null">#{shares},</if>
            <if test="date != null">#{date},</if>
         </trim>
    </insert>

    <update id="updateDailySource" parameterType="DailySource">
        update daily_source
        <trim prefix="SET" suffixOverrides=",">
            <if test="sourceId != null">source_id = #{sourceId},</if>
            <if test="uMrTime != null">u_mr_time = #{uMrTime},</if>
            <if test="bought != null">bought = #{bought},</if>
            <if test="interactions != null">interactions = #{interactions},</if>
            <if test="shares != null">shares = #{shares},</if>
            <if test="date != null">date = #{date},</if>
        </trim>
        where daily_source_id = #{dailySourceId}
    </update>

    <delete id="deleteDailySourceByDailySourceId" parameterType="Long">
        delete from daily_source where daily_source_id = #{dailySourceId}
    </delete>

    <delete id="deleteDailySourceByDailySourceIds" parameterType="String">
        delete from daily_source where daily_source_id in 
        <foreach item="dailySourceId" collection="array" open="(" separator="," close=")">
            #{dailySourceId}
        </foreach>
    </delete>
</mapper>