<?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="cn.com.ctop.userefficientvideomap.mapper.UserEfficientVideoMapMapper">

    <!-- 获取有效视频以及视频和人的对应关系 -->
    <select id="getUserVideoMap" resultType="cn.com.ctop.userefficientvideomap.entity.EffiVideoDTO">
            select
            a.signature,
            sum(a.charge) as charge,
            d1.id as clip_id,
            d1.realname as clip_name,
            d2.id as shot_id,
            d2.realname as shot_name,
            d3.id as plan_id,
            d3.realname as plan_name,
            d4.id as plane_id,
            d4.realname as plane_name
        from ctop_kuaishou_report_daily_creative_statistic a
        left join ctop_material_info b on a.signature = b.code
        left join ctop_material_ascription c on b.id = c.material_id
        left join sys_user d1 on c.clip_id = d1.id
        left join sys_user d2 on c.shot_id = d2.id
        left join sys_user d3 on c.plan_id = d3.id
        left join sys_user d4 on c.plane_id = d4.id
        where a.signature is not null and b.id is not null
        and DATE_FORMAT(b.create_time,'%Y-%m-%d') &gt;= #{startDate}
        and DATE_FORMAT(b.create_time,'%Y-%m-%d') &lt;= #{endDate}
        and DATE_FORMAT(a.stat_date,'%Y-%m-%d') &gt;= #{startCalcDate}
        and DATE_FORMAT(a.stat_date,'%Y-%m-%d') &lt;= #{endCalcDate}
        group by a.signature
        having sum(a.charge) >= 5000
    </select>

    <!-- 根据md5获取有效视频是否存在 -->
    <select id="getEffiVideoCountBySignature" resultType="java.lang.Integer">
            select
            count(1)
        from ctop_user_efficient_video_map
        where efficient_video_signature = #{signature}
        and app_type = #{appType}
    </select>

    <!-- 根据年、季度、userId获取有效视频数量 -->
    <select id="getEffiVideoCountByUserId" resultType="java.lang.Integer">
        select
        count(1)
        from
        ctop_user_efficient_video_map
        where year = #{year}
        and quarter = #{quarter}
        and user_id = #{userId}
    </select>

    <!-- 按人获取季度总视频数量 -->
    <select id="getQuarterMaterialCountByUserId" resultType="java.lang.Integer">
            select
            count(1)
        from ctop_material_info a
        left join ctop_material_ascription b on a.id = b.material_id
        where date_format(a.create_time,'%Y-%m-%d') &gt;= #{startTime}
        and date_format(a.create_time,'%Y-%m-%d') &lt;= #{endTime}
        and
        <choose>
            <when test=" roleCode == 'plan' ">
                b.plan_id = #{userId}
            </when>
            <when test=" roleCode == 'clip' ">
                b.clip_id = #{userId}
            </when>
            <otherwise>
                b.shot_id = #{userId}
            </otherwise>
        </choose>

    </select>

    <!-- 根据clip_id获取视频总消耗 -->
    <select id="getTotalCostByClipId" resultType="java.math.BigDecimal">
            select
            sum(a.charge) as charge
        from ctop_kuaishou_report_daily_creative_statistic a
        left join ctop_material_info b on a.signature = b.code
        left join ctop_material_ascription c on b.id = c.material_id
        left join sys_user d1 on c.clip_id = d1.id
        left join sys_user d2 on c.shot_id = d2.id
        left join sys_user d3 on c.plan_id = d3.id
        left join sys_user d4 on c.plane_id = d4.id
        where a.signature is not null and b.id is not null
        and DATE_FORMAT(b.create_time,'%Y-%m-%d') &gt;= #{startDate}
        and DATE_FORMAT(b.create_time,'%Y-%m-%d') &lt;= #{endDate}
        and c.clip_id = #{userId}
    </select>

    <!-- 根据clip_id获取视频总消耗 -->
    <select id="getTotalCostByShotId" resultType="java.math.BigDecimal">
            select
            sum(a.charge) as charge
        from ctop_kuaishou_report_daily_creative_statistic a
        left join ctop_material_info b on a.signature = b.code
        left join ctop_material_ascription c on b.id = c.material_id
        left join sys_user d1 on c.clip_id = d1.id
        left join sys_user d2 on c.shot_id = d2.id
        left join sys_user d3 on c.plan_id = d3.id
        left join sys_user d4 on c.plane_id = d4.id
        where a.signature is not null and b.id is not null
        and DATE_FORMAT(b.create_time,'%Y-%m-%d') &gt;= #{startDate}
        and DATE_FORMAT(b.create_time,'%Y-%m-%d') &lt;= #{endDate}
        and c.shot_id = #{userId}
    </select>

    <!-- 根据clip_id获取视频总消耗 -->
    <select id="getTotalCostByPlanId" resultType="java.math.BigDecimal">
            select
            sum(a.charge) as charge
        from ctop_kuaishou_report_daily_creative_statistic a
        left join ctop_material_info b on a.signature = b.code
        left join ctop_material_ascription c on b.id = c.material_id
        left join sys_user d1 on c.clip_id = d1.id
        left join sys_user d2 on c.shot_id = d2.id
        left join sys_user d3 on c.plan_id = d3.id
        left join sys_user d4 on c.plane_id = d4.id
        where a.signature is not null and b.id is not null
        and DATE_FORMAT(b.create_time,'%Y-%m-%d') &gt;= #{startDate}
        and DATE_FORMAT(b.create_time,'%Y-%m-%d') &lt;= #{endDate}
        and c.plan_id = #{userId}
    </select>

    <!-- 根据plane_id获取视频总消耗 -->
    <select id="getTotalCostByPlaneId" resultType="java.math.BigDecimal">
            select
            sum(a.charge) as charge
        from ctop_kuaishou_report_daily_creative_statistic a
        left join ctop_material_info b on a.signature = b.code
        left join ctop_material_ascription c on b.id = c.material_id
        left join sys_user d1 on c.clip_id = d1.id
        left join sys_user d2 on c.shot_id = d2.id
        left join sys_user d3 on c.plan_id = d3.id
        left join sys_user d4 on c.plane_id = d4.id
        where a.signature is not null and b.id is not null
        and DATE_FORMAT(b.create_time,'%Y-%m-%d') &gt;= #{startDate}
        and DATE_FORMAT(b.create_time,'%Y-%m-%d') &lt;= #{endDate}
        where c.plane_id = #{userId}
    </select>

    <select id="getProjectTotalCostByUserId" resultType="java.math.BigDecimal">
        select
        sum(dailiAccount.charge) as charge
        from
        ctop_kuaishou_report_daily_account dailiAccount
        left join
        ctop_user_allocation allocation on dailiAccount.account_id = allocation.account_id
        left join
        ctop_project_member projectMember on projectMember.project_id = allocation.project_id
        where
        projectMember.user_id = #{userId}
        AND
        dailiAccount.stat_date &gt;= #{startDate}
        AND
        dailiAccount.stat_date &lt; #{endDate}
    </select>

    <!-- 获取这个人所在的所有项目 -->
    <select id="getProjectsByUserId" resultType="cn.com.ctop.userefficientvideomap.entity.ProjectDTO">
        select
        project_id as projectId,
        project_name as projectName
        from
        ctop_project_member
        where
        user_id = #{userId}
    </select>


    <!-- 根据项目id获取平面设计师的数量 -->
    <select id="getPlaneCountByProjectId" resultType="java.lang.Integer">
        select
        count(1)
        from ctop_project_member
        where project_id = #{projectId}
        and role_code = 'plane';
    </select>

    <!-- 根据获取的id -->
    <select id="getPlaneCountByUserId" resultType="java.lang.Integer">
        select
        count(1)
        from sys_user as u
        left join sys_user_role ur where u.user_id = ur.user_id
        left join sys_role r on ur.role_id = r.id
        where u.org_code = (select org_code from sys_user u2 where id = #{userId})
        and role_code = 'plane'
    </select>

    <select id="designMaterialInfoList" resultType="cn.com.ctop.userefficientvideomap.entity.DesignMaterialInfo">    <!-- 素材、素材跑量、封面点击数、封面曝光数、行为数 -->
        select
        b.id as id,
        b.url as videoUrl, <!-- 素材url -->
        sum(a.photo_show) as pageExposureCount, <!-- 封面曝光数 -->
        sum(a.photo_click) as pageClickCount, <!-- 封面点击数 -->
        sum(a.aclick) as materialExposureCount, <!-- 素材曝光数 -->
        sum(a.bclick) as behaviorCount,<!-- 行为数 -->
        sum(a.charge) as totalCost <!-- 素材跑量 -->
        from
        ctop_material_info b
        left join ctop_material_ascription c on b.id = c.material_id
        left join ctop_kuaishou_report_daily_creative_statistic a on b.code = a.signature
        where
        <choose>
            <when test=" roleCode == 'plan' ">
                c.plan_id = #{userId}
            </when>
            <when test=" roleCode == 'clip' ">
                c.clip_id = #{userId}
            </when>
            <otherwise>
                c.shot_id = #{userId}
            </otherwise>
        </choose>
         and DATE_FORMAT(b.create_time,'%Y-%m-%d') &gt;= #{startDate}
         and DATE_FORMAT(b.create_time,'%Y-%m-%d') &lt;= #{endDate}
        group by b.id
    </select>

    <!-- 根据material_info的id查询出素材账户、投放账户、跑量 -->
    <select id="getMaterialAccountInfoByMaterialId" resultType="cn.com.ctop.userefficientvideomap.entity.DesignMaterialInfo">
        select
        a.account_id as accountId,
        a.account_name as accountName,
        '快手' as platform,
        sum(charge) as totalCost
        from
        ctop_material_info b
        left join ctop_material_ascription c on b.id = c.material_id
        left join ctop_kuaishou_report_daily_creative_statistic a on b.code = a.signature
        where
        b.id = #{materialId}
        and DATE_FORMAT(b.create_time,'%Y-%m-%d') &gt;= #{startDate}
         and DATE_FORMAT(b.create_time,'%Y-%m-%d') &lt;= #{endDate}
    </select>

    <!-- 获取这个人所在的所有项目 -->
    <select id="getProjects" resultType="cn.com.ctop.userefficientvideomap.entity.ProjectDTO">
        select
        project_id as projectId,
        project_name as projectName
        from
        ctop_project
        where
        media_id = #{mediaId}
    </select>

    <!-- 根据projectId计算项目总消耗 -->
    <select id="getProjectQuarterCostById" resultType="java.math.BigDecimal">
            select
            sum(dailiAccount.charge) as charge
            from
            ctop_kuaishou_report_daily_account dailiAccount
            left join
            ctop_user_allocation allocation on dailiAccount.account_id = allocation.account_id
        where
        dailiAccount.project_id = #{projectId}
        AND
        dailiAccount.stat_date &gt;= #{startDate}
        AND
        dailiAccount.stat_date &lt; #{endDate}
    </select>

    <select id="getPlaneByProjectId" resultType="cn.com.ctop.userefficientvideomap.entity.UserDto2">
        select
        a.user_id as userId,
        a.user_name as userName,
        b.id as roleId,
        b.role_name as roleName
        from
        ctop_project_member a
        left join sys_role b on a.role_code = b.role_code
        where
        a.project_id = #{projectId}
        a.role_code in ('planeLeader','plane')
    </select>

    <!-- 获取快手设计师leader的信息 -->
    <select id="getKuaishouDesignLeaderList" resultType="cn.com.ctop.userefficientvideomap.entity.UserDto2">
            select
            a.user_id as userId,
            c.realname as userName,
            '8d3e2ed58bdedf17716f698ea674da27' as roleId,
            '设计组长' as roleName,
            a.dep_id as depId
        FROM sys_user_depart a
        left join sys_user c on a.user_id = c.id
        left join sys_depart b on a.dep_id = b.id
                    WHERE a.user_id in(
                        select
                        user_id
                        from sys_user_role
                        WHERE role_id = '8d3e2ed58bdedf17716f698ea674da27'   <!-- 设计师 -->
                        )
        and b.parent_id = '4e939124a5f541c88838e49d2a3ea72b'  <!-- 快手设计部 -->
    </select>

    <!-- 根据部门id -->
    <select id="getClip" resultType="string">
        select
        user_id
        from
        sys_user_depart a
        left join sys_user_role b on a.user_id = b.user_id
        where a.dep_id = #{depId}
        and b.role_code = 'clip'
    </select>

    <select id="getClipQuarterCost" resultType="java.math.BigDecimal">
            select
            total_cost
        from
        ctop_performance
        where
        app_type = #{appType}
        and quarter = #{quarter}
        and year = #{year}
        and user_id = #{userId}
    </select>

</mapper>
