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

    <resultMap type="FsCourseTrafficLog" id="FsCourseTrafficLogResult">
        <result property="logId"    column="log_id"    />
        <result property="userId"    column="user_id"    />
        <result property="videoId"    column="video_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="qwExternalContactId"    column="qw_external_contact_id"    />
        <result property="internetTraffic"    column="internet_traffic"    />
        <result property="qwUserId"    column="qw_user_id"    />
        <result property="companyUserId"    column="company_user_id"    />
        <result property="companyId"    column="company_id"    />
        <result property="courseId"    column="course_id"    />
        <result property="uuId"    column="uu_id"    />
    </resultMap>

    <sql id="selectFsCourseTrafficLogVo">
        select log_id, uu_id,user_id, video_id, create_time, qw_external_contact_id, internet_traffic, qw_user_id, company_user_id, company_id, course_id, project from fs_course_traffic_log
    </sql>

    <select id="selectFsCourseTrafficLogList" parameterType="FsCourseTrafficLog" resultMap="FsCourseTrafficLogResult">
        <include refid="selectFsCourseTrafficLogVo"/>
        <where>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="videoId != null "> and video_id = #{videoId}</if>
            <if test="qwExternalContactId != null "> and qw_external_contact_id = #{qwExternalContactId}</if>
            <if test="internetTraffic != null "> and internet_traffic = #{internetTraffic}</if>
            <if test="qwUserId != null  and qwUserId != ''"> and qw_user_id = #{qwUserId}</if>
            <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="courseId != null "> and course_id = #{courseId}</if>
        </where>
    </select>

    <select id="selectFsCourseTrafficLogByLogId" parameterType="Long" resultMap="FsCourseTrafficLogResult">
        <include refid="selectFsCourseTrafficLogVo"/>
        where log_id = #{logId}
    </select>
    <select id="selectTrafficByCompany" resultType="com.fs.course.vo.FsCourseTrafficLogListVO">
        select c.company_name, SUM(l.internet_traffic) AS total_internet_traffic
        ,DATE_FORMAT(l.create_time, '%Y-%m') AS `month`  from fs_course_traffic_log l
        left join company c on c.company_id = l.company_id
        where 1 = 1
        <if test= 'maps.year != null '>
            and YEAR(l.create_time) = #{maps.year}
            </if>
        <if test= 'maps.month != null '>
            and MONTH(l.create_time) = #{maps.month}
            </if>
        <if test = ' maps.companyId !=null '>
            and l.company_id = #{maps.companyId}
            </if>
        group by l.company_id,`month`

    </select>
    <select id="selectTrafficNew" resultType="com.fs.course.vo.FsCourseTrafficLogListVO">
        select company_id,project,course_id,SUM(internet_traffic) AS total_internet_traffic
        ,DATE_FORMAT(create_time, '%Y-%m') AS `month`  from fs_course_traffic_log
        <where>
            <if test="startDate != null and endDate != null">
                and create_time between #{startDate} AND #{endDate}
            </if>
            <if test='companyId !=null'>
                and company_id = #{companyId}
            </if>
            <if test="courseId != null">
                and course_id = ${courseId}
            </if>
            <if test="project">
                and project = ${project}
            </if>
        </where>
        group by company_id,`month`,project,course_id
    </select>

    <insert id="insertFsCourseTrafficLog" parameterType="FsCourseTrafficLog" useGeneratedKeys="true" keyProperty="logId">
        insert into fs_course_traffic_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="videoId != null">video_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="qwExternalContactId != null">qw_external_contact_id,</if>
            <if test="internetTraffic != null">internet_traffic,</if>
            <if test="qwUserId != null">qw_user_id,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="companyId != null">company_id,</if>
            <if test="courseId != null">course_id,</if>
            <if test="uuId != null">uu_id,</if>
            <if test="project != null">project,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="videoId != null">#{videoId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="qwExternalContactId != null">#{qwExternalContactId},</if>
            <if test="internetTraffic != null">#{internetTraffic},</if>
            <if test="qwUserId != null">#{qwUserId},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="courseId != null">#{courseId},</if>
            <if test="uuId != null">#{uuId},</if>
            <if test="project != null">#{project},</if>
         </trim>
    </insert>

    <insert id="insertOrUpdateTrafficLog" parameterType="FsCourseTrafficLog" useGeneratedKeys="true" keyProperty="logId">
        insert into fs_course_traffic_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="videoId != null">video_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="qwExternalContactId != null">qw_external_contact_id,</if>
            <if test="internetTraffic != null">internet_traffic,</if>
            <if test="qwUserId != null">qw_user_id,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="companyId != null">company_id,</if>
            <if test="courseId != null">course_id,</if>
            <if test="uuId != null">uu_id,</if>
            <if test="project != null">project,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="videoId != null">#{videoId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="qwExternalContactId != null">#{qwExternalContactId},</if>
            <if test="internetTraffic != null">#{internetTraffic},</if>
            <if test="qwUserId != null">#{qwUserId},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="courseId != null">#{courseId},</if>
            <if test="uuId != null">#{uuId},</if>
            <if test="project != null">#{project},</if>
        </trim>
        on duplicate key update
        <trim suffixOverrides=",">
            <if test="internetTraffic != null">internet_traffic = #{internetTraffic},</if>
        </trim>
    </insert>

    <update id="updateFsCourseTrafficLog" parameterType="FsCourseTrafficLog">
        update fs_course_traffic_log
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="videoId != null">video_id = #{videoId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="qwExternalContactId != null">qw_external_contact_id = #{qwExternalContactId},</if>
            <if test="internetTraffic != null">internet_traffic = #{internetTraffic},</if>
            <if test="qwUserId != null">qw_user_id = #{qwUserId},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="courseId != null">course_id = #{courseId},</if>
            <if test="uuId != null">uu_id = #{uuId},</if>
            <if test="project != null">project = #{project},</if>
        </trim>
        where log_id = #{logId}
    </update>

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

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