<?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.storlead.sales.mapper.BroadcastDataMapper">

    <!-- 查询所有未删除的分公司，用于 CEO/董事长分公司层级报表 -->
    <select id="subCompanyList" resultType="com.storlead.user.pojo.entity.SubCompanyEntity">
        select
            id,
            company_id,
            name,
            `desc`,
            leader_id,
            route_code,
            data_source,
            leader_name
        from sub_company
        where is_delete = 0
    </select>



    <!-- 分公司维度：客户数，按部门聚合 -->
    <select id="customerListBySubCompany" resultType="com.storlead.sales.pojo.vo.report.BroadcastDataVO">
        select
            count(distinct c.id) as totalNum,
            d.name as organization
        from customer c
        left join user u on c.owner_by = u.id
        left join dept d on u.dept_id = d.id
        where c.is_delete = 0
          and c.customer_form = 10
          and d.sub_company_id = #{subCompanyId}
          and d.id IS NOT NULL
          <if test="startDate!= null and endDate!= null">
              and c.create_time between #{startDate} and #{endDate}
          </if>
        group by d.name
    </select>

    <!-- 分公司维度：商机数，按部门聚合 -->
    <select id="businessListBySubCompany" resultType="com.storlead.sales.pojo.vo.report.BroadcastDataVO">
        select
            count(distinct b.id) as totalNum,
            d.name as organization
        from business b
        left join user u on b.owner_by = u.id
        left join dept d on u.dept_id = d.id
        where b.is_delete = 0
          and d.sub_company_id = #{subCompanyId}
          and d.id IS NOT NULL
          <if test="startDate!= null and endDate!= null">
              and b.create_time between #{startDate} and #{endDate}
          </if>
        group by d.name
    </select>

    <!-- 分公司维度：订单数，按部门聚合 -->
    <select id="orderListBySubCompany" resultType="com.storlead.sales.pojo.vo.report.BroadcastDataVO">
        select
            count(distinct o.id) as totalNum,
            d.name as organization
        from order_form o
        left join user u on o.owner_by = u.id
        left join dept d on u.dept_id = d.id
        where o.is_delete = 0
          and d.sub_company_id = #{subCompanyId}
          and d.id IS NOT NULL
          <if test="startDate!= null and endDate!= null">
              and o.create_time between #{startDate} and #{endDate}
          </if>
        group by d.name
    </select>

    <!-- 分公司维度：订单金额，按部门聚合 -->
    <select id="orderAmountListBySubCompany" resultType="com.storlead.sales.pojo.vo.report.BroadcastDataVO">
        select
            sum(o.total_order_amt) as totalNum,
            d.name as organization
        from order_form o
        left join user u on o.owner_by = u.id
        left join dept d on u.dept_id = d.id
        where o.is_delete = 0
          and d.sub_company_id = #{subCompanyId}
          and d.id IS NOT NULL
          <if test="startDate!= null and endDate!= null">
              and o.create_time between #{startDate} and #{endDate}
          </if>
        group by d.name
    </select>

    <!-- 分公司级：线索按部门聚合 -->
    <select id="leadListBySubCompany" resultType="com.storlead.sales.pojo.vo.report.BroadcastDataVO">
        select
            count(distinct c.id) as totalNum,
            d.name as organization
        from customer c
        left join user u on c.owner_by = u.id
        left join dept d on u.dept_id = d.id
        where c.is_delete = 0
          and c.customer_form = 11
          and d.sub_company_id = #{subCompanyId}
          and d.id IS NOT NULL
          <if test="startDate!= null and endDate!= null">
              and c.create_time between #{startDate} and #{endDate}
          </if>
        group by d.name
    </select>

    <!-- 分公司级：跟进按部门聚合 -->
    <select id="followListBySubCompany" resultType="com.storlead.sales.pojo.vo.report.BroadcastDataVO">
        select
            count(distinct f.id) as totalNum,
            d.name as organization
        from customer_follow_up f
        left join user u on f.follow_up_member_id = u.id
        left join dept d on u.dept_id = d.id
        where f.is_delete = 0
          and d.sub_company_id = #{subCompanyId}
          and d.id IS NOT NULL
          <if test="startDate!= null and endDate!= null">
              and f.create_time between #{startDate} and #{endDate}
          </if>
        group by d.name
    </select>

    <!-- 分公司级：邮件按部门聚合 -->
    <select id="mailListBySubCompany" resultType="com.storlead.sales.pojo.vo.report.BroadcastDataVO">
        select
            count(distinct e.id) as totalNum,
            d.name as organization
        from emails e
        left join user u on e.owner_by = u.id
        left join dept d on u.dept_id = d.id
        where e.is_delete = 0
          and e.folder = 'SENT'
          and e.is_trash = 0
          and e.from <![CDATA[<>]]> 'system@storlead.com'
          and d.sub_company_id = #{subCompanyId}
          and d.id IS NOT NULL
          <if test="startDate!= null and endDate!= null">
              and e.create_time between #{startDate} and #{endDate}
          </if>
        group by d.name
    </select>

    <select id="customerList" resultType="com.storlead.sales.pojo.vo.report.BroadcastDataVO">
        select
            count(distinct c.id) as totalNum,
            s.name as organization
        FROM
            customer as c
        LEFT JOIN
            user as u on c.owner_by = u.id
        LEFT JOIN
            sub_company as s on u.sub_company_id = s.id
        WHERE
            c.is_delete = 0
            AND c.customer_form = 10
            AND s.id IS NOT NULL
            <if test="startDate!= null and endDate!= null">
            AND c.create_time BETWEEN #{startDate} AND #{endDate}
            </if>
        GROUP BY
            organization
    </select>

    <select id="busssniessList" resultType="com.storlead.sales.pojo.vo.report.BroadcastDataVO">
        select
            count(distinct b.id) as totalNum,
            s.name as organization
        FROM
            business as b
        LEFT JOIN
            user as u on b.owner_by = u.id
        LEFT JOIN
            sub_company as s on u.sub_company_id = s.id
        WHERE
            b.is_delete = 0
            AND s.id IS NOT NULL
            <if test="startDate!= null and endDate!= null">
            AND b.create_time BETWEEN #{startDate} AND #{endDate}
            </if>
        GROUP BY
            organization
    </select>

    <select id="orderList" resultType="com.storlead.sales.pojo.vo.report.BroadcastDataVO">
        select
            count(distinct o.id) as totalNum,
            s.name as organization
        FROM
            order_form as o
        LEFT JOIN
            user as u on o.owner_by = u.id
        LEFT JOIN
            sub_company as s on u.sub_company_id = s.id
        WHERE
            o.is_delete = 0
            AND s.id IS NOT NULL
            <if test="startDate!= null and endDate!= null">
            AND o.create_time BETWEEN #{startDate} AND #{endDate}
            </if>
        GROUP BY
            organization
    </select>

    <select id="orderAmountList" resultType="com.storlead.sales.pojo.vo.report.BroadcastDataVO">
        select
            sum(o.total_order_amt) as totalNum,
            s.name as organization
        FROM
            order_form as o
        LEFT JOIN
            user as u on o.owner_by = u.id
        LEFT JOIN
            sub_company as s on u.sub_company_id = s.id
        WHERE
            o.is_delete = 0
            AND s.id IS NOT NULL
            <if test="startDate!= null and endDate!= null">
            AND o.create_time BETWEEN #{startDate} AND #{endDate}
            </if>
        GROUP BY
            organization
    </select>

    <!-- 集团级：线索按分公司聚合 -->
    <select id="leadList" resultType="com.storlead.sales.pojo.vo.report.BroadcastDataVO">
        select
            count(distinct c.id) as totalNum,
            s.name as organization
        from customer c
        left join user u on c.owner_by = u.id
        left join sub_company s on u.sub_company_id = s.id
        where c.is_delete = 0
          and c.customer_form = 11
          and s.id IS NOT NULL
          <if test="startDate!= null and endDate!= null">
            and c.create_time between #{startDate} and #{endDate}
          </if>
        group by s.name
    </select>

    <!-- 集团级：跟进按分公司聚合 -->
    <select id="followList" resultType="com.storlead.sales.pojo.vo.report.BroadcastDataVO">
        select
            count(distinct f.id) as totalNum,
            s.name as organization
        from customer_follow_up f
        left join user u on f.follow_up_member_id = u.id
        left join sub_company s on u.sub_company_id = s.id
        where f.is_delete = 0
          and s.id IS NOT NULL
          <if test="startDate!= null and endDate!= null">
            and f.create_time between #{startDate} and #{endDate}
          </if>
        group by s.name
    </select>

    <!-- 集团级：邮件按分公司聚合 -->
    <select id="mailList" resultType="com.storlead.sales.pojo.vo.report.BroadcastDataVO">
        select
            count(distinct e.id) as totalNum,
            s.name as organization
        from emails e
        left join user u on e.owner_by = u.id
        left join sub_company s on u.sub_company_id = s.id
        where e.is_delete = 0
          and e.folder = 'SENT'
          and e.is_trash = 0
          and e.from <![CDATA[<>]]> 'system@storlead.com'
          and s.id IS NOT NULL
          <if test="startDate!= null and endDate!= null">
            and e.create_time between #{startDate} and #{endDate}
          </if>
        group by s.name
    </select>

    <!-- 周期内有任何新增数据的用户集合（客户、线索、商机、订单、跟进、邮件） -->
    <select id="personUserIds" resultType="java.lang.Long">
        select distinct owner_by from (
            -- 客户和线索
            select c.owner_by
            from customer c
            where c.is_delete = 0
              and c.owner_by is not null
              and c.create_time between #{startDate} and #{endDate}
            union all
            -- 商机
            select b.owner_by
            from business b
            where b.is_delete = 0
              and b.owner_by is not null
              and b.create_time between #{startDate} and #{endDate}
            union all
            -- 订单
            select o.owner_by
            from order_form o
            where o.is_delete = 0
              and o.owner_by is not null
              and o.create_time between #{startDate} and #{endDate}
            union all
            -- 跟进
            select f.follow_up_member_id as owner_by
            from customer_follow_up f
            where f.is_delete = 0
              and f.follow_up_member_id is not null
              and f.create_time between #{startDate} and #{endDate}
            union all
            -- 邮件
            select e.owner_by
            from emails e
            where e.is_delete = 0
              and e.is_trash = 0
              and e.from <![CDATA[<>]]> 'system@storlead.com'
              and e.folder = 'SENT'
              and e.owner_by is not null
              and e.create_time between #{startDate} and #{endDate}
        ) as all_users
        where owner_by is not null
    </select>

    <!-- 个人维度：客户/线索数量 -->
    <select id="personCustomerCount" resultType="java.lang.Long">
        select count(distinct c.id)
        from customer c
        where c.is_delete = 0
          and c.customer_form = #{customerForm}
          <if test="param1 != null and param1.size() > 0">
              and c.owner_by in
              <foreach collection="param1" item="id" open="(" separator="," close=")">
                  #{id}
              </foreach>
          </if>
          and c.create_time between #{startDate} and #{endDate}
    </select>

    <!-- 个人维度：商机数量 -->
    <select id="personBusinessCount" resultType="java.lang.Long">
        select count(distinct b.id)
        from business b
        where b.is_delete = 0
          <if test="param1 != null and param1.size() > 0">
              and b.owner_by in
              <foreach collection="param1" item="id" open="(" separator="," close=")">
                  #{id}
              </foreach>
          </if>
          and b.create_time between #{startDate} and #{endDate}
    </select>

    <!-- 个人维度：订单数量 -->
    <select id="personOrderCount" resultType="java.lang.Long">
        select count(distinct o.id)
        from order_form o
        where o.is_delete = 0
          <if test="param1 != null and param1.size() > 0">
              and o.owner_by in
              <foreach collection="param1" item="id" open="(" separator="," close=")">
                  #{id}
              </foreach>
          </if>
          and o.create_time between #{startDate} and #{endDate}
    </select>

    <!-- 个人维度：订单金额 -->
    <select id="personOrderAmount" resultType="java.math.BigDecimal">
        select coalesce(sum(o.total_order_amt),0)
        from order_form o
        where o.is_delete = 0
          <if test="param1 != null and param1.size() > 0">
              and o.owner_by in
              <foreach collection="param1" item="id" open="(" separator="," close=")">
                  #{id}
              </foreach>
          </if>
          and o.create_time between #{startDate} and #{endDate}
    </select>

    <!-- 个人维度：跟进数量 -->
    <select id="personFollowCount" resultType="java.lang.Long">
        select count(distinct f.id)
        from customer_follow_up f
        where f.is_delete = 0
          <if test="param1 != null and param1.size() > 0">
              and f.follow_up_member_id in
              <foreach collection="param1" item="id" open="(" separator="," close=")">
                  #{id}
              </foreach>
          </if>
          and f.create_time between #{startDate} and #{endDate}
    </select>

    <!-- 个人维度：邮件数量（folder='SENT'） -->
    <select id="personEmailCount" resultType="java.lang.Long">
        select count(distinct e.id)
        from emails e
        where e.is_delete = 0
          and e.folder = 'SENT'
          and e.is_trash = 0
          and e.from <![CDATA[<>]]> 'system@storlead.com'
          <if test="param1 != null and param1.size() > 0">
              and e.owner_by in
              <foreach collection="param1" item="id" open="(" separator="," close=")">
                  #{id}
              </foreach>
          </if>
          and e.create_time between #{startDate} and #{endDate}
    </select>


    <!-- 分公司级：线索总数 -->
    <select id="leadCountBySubCompany" resultType="java.lang.Long">
        select count(distinct c.id)
        from customer c
        left join user u on c.owner_by = u.id
        where c.is_delete = 0
          and c.customer_form = 11
          and u.sub_company_id = #{subCompanyId}
          <if test="startDate!= null and endDate!= null">
            and c.create_time between #{startDate} and #{endDate}
          </if>
    </select>

    <!-- 分公司级：跟进总数 -->
    <select id="followCountBySubCompany" resultType="java.lang.Long">
        select count(distinct f.id)
        from customer_follow_up f
        left join user u on f.follow_up_member_id = u.id
        where f.is_delete = 0
          and u.sub_company_id = #{subCompanyId}
          <if test="startDate!= null and endDate!= null">
            and f.create_time between #{startDate} and #{endDate}
          </if>
    </select>

    <!-- 分公司级：邮件总数 -->
    <select id="mailCountBySubCompany" resultType="java.lang.Long">
        select count(distinct e.id)
        from emails e
        left join user u on e.owner_by = u.id
        where e.is_delete = 0
          and e.folder = 'SENT'
          and e.is_trash = 0
          and e.from <![CDATA[<>]]> 'system@storlead.com'
          and u.sub_company_id = #{subCompanyId}
          <if test="startDate!= null and endDate!= null">
            and e.create_time between #{startDate} and #{endDate}
          </if>
    </select>

    <!-- 部门维度：获取单个部门领导对应成员（含领导本人）ID 集合 -->
    <select id="deptMemberIdsByManager" resultType="java.lang.Long">
        select distinct u.id
        from user u
        where u.is_delete = 0
          and (u.manager_id = #{managerId} or u.id = #{managerId})
    </select>

    <!-- 部门维度：根据部门ID获取部门所有成员ID集合 -->
    <select id="deptMemberIdsByDeptId" resultType="java.lang.Long">
        select u.id
        from user u
        where u.is_delete = 0
          and u.dept_id = #{deptId}
    </select>

    <!-- 部门级：客户按人员分组的记录列表 -->
    <select id="customerListByDeptMembers" resultType="com.storlead.sales.pojo.vo.report.BroadcastDataVO">
        select
            count(distinct c.id) as totalNum,
            u.real_name as organization
        from customer c
        left join user u on c.owner_by = u.id
        where c.is_delete = 0
          and c.customer_form = 10
          <if test="startDate!= null and endDate!= null">
            and c.create_time between #{startDate} and #{endDate}
          </if>
          <if test="userIds != null and userIds.size() > 0">
              and c.owner_by in
              <foreach collection="userIds" item="id" open="(" separator="," close=")">
                  #{id}
              </foreach>
          </if>
        group by u.real_name
    </select>

    <!-- 部门级：商机按人员分组的记录列表 -->
    <select id="businessListByDeptMembers" resultType="com.storlead.sales.pojo.vo.report.BroadcastDataVO">
        select
            count(distinct b.id) as totalNum,
            u.real_name as organization
        from business b
        left join user u on b.owner_by = u.id
        where b.is_delete = 0
          <if test="startDate!= null and endDate!= null">
            and b.create_time between #{startDate} and #{endDate}
          </if>
          <if test="userIds != null and userIds.size() > 0">
              and b.owner_by in
              <foreach collection="userIds" item="id" open="(" separator="," close=")">
                  #{id}
              </foreach>
          </if>
        group by u.real_name
    </select>

    <!-- 部门级：订单数量按人员分组的记录列表 -->
    <select id="orderListByDeptMembers" resultType="com.storlead.sales.pojo.vo.report.BroadcastDataVO">
        select
            count(distinct o.id) as totalNum,
            u.real_name as organization
        from order_form o
        left join user u on o.owner_by = u.id
        where o.is_delete = 0
          <if test="startDate!= null and endDate!= null">
            and o.create_time between #{startDate} and #{endDate}
          </if>
          <if test="userIds != null and userIds.size() > 0">
              and o.owner_by in
              <foreach collection="userIds" item="id" open="(" separator="," close=")">
                  #{id}
              </foreach>
          </if>
        group by u.real_name
    </select>

    <!-- 部门级：订单金额按人员分组的记录列表 -->
    <select id="orderAmountListByDeptMembers" resultType="com.storlead.sales.pojo.vo.report.BroadcastDataVO">
        select
            sum(o.total_order_amt) as totalNum,
            u.real_name as organization
        from order_form o
        left join user u on o.owner_by = u.id
        where o.is_delete = 0
          <if test="startDate!= null and endDate!= null">
            and o.create_time between #{startDate} and #{endDate}
          </if>
          <if test="userIds != null and userIds.size() > 0">
              and o.owner_by in
              <foreach collection="userIds" item="id" open="(" separator="," close=")">
                  #{id}
              </foreach>
          </if>
        group by u.real_name
    </select>

    <!-- 部门级：线索按人员分组的记录列表 -->
    <select id="leadListByDeptMembers" resultType="com.storlead.sales.pojo.vo.report.BroadcastDataVO">
        select
            count(distinct c.id) as totalNum,
            u.real_name as organization
        from customer c
        left join user u on c.owner_by = u.id
        where c.is_delete = 0
          and c.customer_form = 11
          <if test="startDate!= null and endDate!= null">
            and c.create_time between #{startDate} and #{endDate}
          </if>
          <if test="userIds != null and userIds.size() > 0">
              and c.owner_by in
              <foreach collection="userIds" item="id" open="(" separator="," close=")">
                  #{id}
              </foreach>
          </if>
        group by u.real_name
    </select>

    <!-- 部门级：跟进按人员分组的记录列表 -->
    <select id="followListByDeptMembers" resultType="com.storlead.sales.pojo.vo.report.BroadcastDataVO">
        select
            count(distinct f.id) as totalNum,
            u.real_name as organization
        from customer_follow_up f
        left join user u on f.follow_up_member_id = u.id
        where f.is_delete = 0
          <if test="startDate!= null and endDate!= null">
            and f.create_time between #{startDate} and #{endDate}
          </if>
          <if test="userIds != null and userIds.size() > 0">
              and f.follow_up_member_id in
              <foreach collection="userIds" item="id" open="(" separator="," close=")">
                  #{id}
              </foreach>
          </if>
        group by u.real_name
    </select>

    <!-- 部门级：邮件按人员分组的记录列表 -->
    <select id="mailListByDeptMembers" resultType="com.storlead.sales.pojo.vo.report.BroadcastDataVO">
        select
            count(distinct e.id) as totalNum,
            u.real_name as organization
        from emails e
        left join user u on e.owner_by = u.id
        where e.is_delete = 0
          and e.is_trash = 0
          and e.folder = 'SENT'
          and e.from <![CDATA[<>]]> 'system@storlead.com'
          <if test="startDate!= null and endDate!= null">
            and e.create_time between #{startDate} and #{endDate}
          </if>
          <if test="userIds != null and userIds.size() > 0">
              and e.owner_by in
              <foreach collection="userIds" item="id" open="(" separator="," close=")">
                  #{id}
              </foreach>
          </if>
        group by u.real_name
    </select>


</mapper>