<?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="org.jeecg.modules.ctop.mapper.ProjectMemberMapper">

    <select id="selectProjectsByUserId" resultType="java.lang.Long">
        select DISTINCT(project_id)
        from ctop_project_member
        where user_id = #{userId}
    </select>

    <select id="selectProductsByUserId" resultType="java.lang.Long">
        select DISTINCT(product_id)
        from ctop_project
        where product_id is not NULL
          and id in
              (select DISTINCT(project_id)
               from ctop_project_member
               where user_id = #{userId})
    </select>

    <!-- 设计师展示所有自己的角色下制作的视频 -->
    <select id="adminProjects" resultType="org.jeecg.modules.ctop.entity.ProjectMember">
        select
        t1.id as projectId,
        t1.project_name as projectName,
        t1.advertiser_id as advertiserId,
        (
        select name from ctop_advertiser where id = t1.advertiser_id
        )
        as advertiserName,
        t1.media_id as mediaId,
        t1.max_bid as maxBid,
        t1.product_id as productId,
        t1.responsible_name as responsibleName
        from
        ctop_project t1
        where
        media_id in
        <foreach collection="mediaIds" item="item" separator=","
                 open="(" close=")">
            #{item}
        </foreach>
        <if test="companyId !=null and companyId!=''">
            and company_id = #{companyId}
        </if>
        order by create_time desc
    </select>

    <!-- 视频报表设计师leader展示自己所在公司的所有项目 -->
    <select id="designLeaderProjects" resultType="org.jeecg.modules.ctop.entity.ProjectMember">
        select
        distinct b.id as projectId,
        b.project_name as projectName,
        b.advertiser_id as advertiserId,
        b.advertiser_name as advertiserName,
        b.product_id as productId,
        b.media_id as mediaId
        from
        ctop_project_member a
        left join ctop_project b on a.project_id = b.id
        where
        b.company_id = #{companyId}
        and b.media_id in
        <foreach collection="mediaIds" item="item" separator=","
                 open="(" close=")">
            #{item}
        </foreach>
    </select>

    <!-- 设计师展示所有自己的角色下制作的视频 -->
    <select id="designProjects" resultType="org.jeecg.modules.ctop.entity.ProjectMember">
        select
        distinct a.project_id as projectId,
        b.project_name as projectName,
        b.advertiser_id as advertiserId,
        b.advertiser_name as advertiserName,
        b.media_id as mediaId,
        b.product_id as productId
        from
        ctop_project_member a
        left join ctop_project b on a.project_id = b.id
        where a.user_id = #{userId}
        and b.media_id in
        <foreach collection="mediaIds" item="item" separator=","
                 open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="getProductIds" resultType="org.jeecg.modules.ctop.entity.ProjectMember">
        select
        a.project_id as projectId,
        b.project_name as projectName,
        b.advertiser_id as advertiserId,
        b.advertiser_name as advertiserName,
        b.media_id as mediaId,
        b.product_id as productId
        from
        ctop_project_member a
        left join ctop_project b on a.project_id = b.id
        where b.product_id = #{productId}
        <if test="userId !=null and userId!=''">
            and a.user_id = #{userId}
        </if>
        and b.media_id in
        <foreach collection="mediaList" item="item" separator=","
                 open="(" close=")">
            #{item}
        </foreach>
        group by a.project_id
    </select>


    <!-- 运营和运营负责人展示所在项目视频 -->
    <select id="operationProjects" resultType="org.jeecg.modules.ctop.entity.ProjectMember">
        select
        distinct b.id as projectId,
        b.project_name as projectName,
        b.advertiser_id as advertiserId,
        b.advertiser_name as advertiserName,
        b.product_id as productId,
        b.media_id as mediaId
        from
        ctop_user_allocation a
        left join ctop_project b on a.project_id = b.id
        where a.user_id = #{userId}
        and b.media_id in
        <foreach collection="mediaIds" item="item" separator=","
                 open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="getRoleCodeByUserId" resultType="string">
        select role_code
        from sys_user_role a
                 left join sys_role b on a.role_id = b.id
        where a.user_id = #{userId}
    </select>

    <select id="getCompanyIdByUserId" resultType="string">
        select company_id
        from user_company
        where user_id = #{userId}
    </select>

    <select id="queryProjectMemberByUserId" resultType="org.jeecg.modules.ctop.entity.ProjectMember">
        SELECT
        t1.id AS 'projectId',
        project_name AS 'projectName',
        t1.advertiser_id AS 'advertiserId',
        (
        SELECT
        NAME
        FROM
        ctop_advertiser
        WHERE
        id = t1.advertiser_id
        ) AS 'advertiserName',
        t1.media_id as 'mediaId'
        FROM
        ctop_project t1
        WHERE
        t1.id IN (
        SELECT
        project_id
        FROM
        ctop_project_member
        where user_id = #{userId}
        GROUP BY
        project_id
        )
        AND t1.media_id IN
        <foreach collection="mediaIds" item="item" separator=","
                 open="(" close=")">
            #{item}
        </foreach>
        <if test="companyId !=null and companyId!=''">
            and t1.company_id = #{companyId}
        </if>
        order by t1.create_time desc
    </select>

    <select id="getSaleProjects" resultType="org.jeecg.modules.ctop.entity.ProjectMember">
        select
        distinct a.id as projectId,
        a.project_name as projectName,
        a.advertiser_id as advertiserId,
        a.advertiser_name as advertiserName,
        a.media_id as mediaId,
        a.sale_id as saleId,
        b.realname as saleName
        from
        ctop_project a
        left join sys_user b on a.sale_id = b.id
        where a.sale_id = #{userId}
        and a.media_id in
        <foreach collection="mediaIds" item="item" separator=","
                 open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="getProjectByMap" resultType="java.lang.Long">
        select project_id
        from ctop_project_member
        where user_id = #{userId}
        group by project_id
    </select>
    <select id="getProjectByUserId" resultType="cn.com.ctop.common.module.entity.Project">
        select project.*
        from ctop_project_member member
                 left join ctop_project project on project.id = member.project_id
        where member.user_id = #{userId}
    </select>

    <select id="getProjectByAccountIds" resultType="java.lang.Long">
        SELECT
        project_id
        FROM
        ctop_user_allocation
        WHERE
        account_id IN
        <foreach collection="accountIds" item="item" separator=","
                 open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="getProductByAccountIds" resultType="java.lang.Long">
        select DISTINCT(product_id) from ctop_project where product_id is not NULL and id in
        (SELECT
        project_id
        FROM
        ctop_user_allocation
        WHERE
        account_id IN
        <foreach collection="accountIds" item="item" separator=","
                 open="(" close=")">
            #{item}
        </foreach>)
    </select>


    <select id="getProjectByUserIdAndMediaIds" resultType="com.alibaba.fastjson.JSONObject">
        select
        t2.id as 'projectId',
        t2.project_name as 'projectName',
        t2.product_id as 'productId',
        (select name from ctop_advertiser where id = t2.advertiser_id) as 'advertiserName'
        from ctop_project_member t1
        left join ctop_project t2
        on t1.project_id = t2.id
        where 1 = 1
        <if test="userIds != null and userIds.size() > 0">
            and t1.user_id in
            <foreach item="id" index="index" collection="userIds"
                     open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="mediaIds != null and mediaIds.size() != 0 ">
            and t2.media_id in
            <foreach collection="mediaIds" item="item" separator=","
                     open="(" close=")">
                #{item}
            </foreach>
        </if>
        group by t2.id
        order by t2.create_time desc
    </select>
    <select id="getProjectIdsByProductId" resultType="java.lang.Long">
        SELECT
        t1.id
        FROM
        ctop_project t1
        LEFT JOIN ctop_project_member t2 ON t1.id = t2.project_id
        <where>
            <if test="userId != null and userId != ''">
                AND t2.user_id = #{userId}
            </if>
            <if test="mediaId != null and mediaId != ''">
                AND t1.media_id = #{mediaId}
            </if>
            <if test="companyId != null and companyId != ''">
                AND t1.company_id = #{companyId}
            </if>
            <if test="productId != null and productId != ''">
                AND t1.product_id = #{productId}
            </if>
        </where>
        GROUP BY
        t1.id
    </select>
    <select id="getlistByUserId" resultType="org.jeecg.modules.ctop.entity.ProjectMember">
        select cp.id as id, cp.project_name as project_name, cp.media_id as media_id, ca.name as advertiser_name
        from ctop_project_member cpm
        left join ctop_project cp on cpm.project_id = cp.id
        left join ctop_advertiser ca on ca.id = cp.advertiser_id
        where 1=1
        <if test="userId!=null">
            and cpm.user_id = #{userId}
        </if>
        group by cp.id
    </select>


</mapper>
