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

    <!-- 通用查询映射结果 -->

    <!--    按照人员统计各个业务的跟进次数-->
    <select id="countBusFollowUpByResourceType" resultType="com.storlead.sales.pojo.vo.report.UserFollowUpCountVO">
        SELECT u.real_name as userName,d.id as deptId,d.name as deptName,mt.owner_by as userId,
                IFNULL(sum(case when mt.resource_type = 10 then 1 else 0 end),0) as customerNum,
                IFNULL(sum(case when mt.resource_type = 11 then 1 else 0 end),0) as clusNum,
                IFNULL(sum(case when mt.resource_type = 30 then 1 else 0 end),0) as businessNum,
                IFNULL(sum(case when mt.resource_type = 40 then 1 else 0 end),0) as orderFormNum,
                IFNULL(sum(case when mt.resource_type = 20 then 1 else 0 end),0) as liaisonNum,
                IFNULL(sum(case when mt.resource_type in (10,11,20,30,40) then 1 else 0 end),0) as followUpNum
        FROM customer_follow_up as mt
        left join user as u on u.id = mt.owner_by
        left join dept as d on u.dept_id = d.id
        <where>
            and mt.is_delete = 0
            <if test="query.ownerByIds != null and query.ownerByIds.size > 0">
                and mt.owner_by in
                <foreach collection="query.ownerByIds" item="ownerById" open="(" separator="," close=")">
                    #{ownerById}
                </foreach>
            </if>
            <if test="query.followUpTimeType != null and query.followUpTimeType == 10">
                and mt.create_time >= DATE_FORMAT(#{query.beginCycleTime}, '%Y-%m-%d 00:00:00')  and mt.create_time <![CDATA[<=]]> DATE_FORMAT(#{query.endCycleTime}, '%Y-%m-%d 23:59:59')
            </if>
            <if test="query.followUpTimeType != null and query.followUpTimeType == 20">
                and mt.follow_up_time >= DATE_FORMAT(#{query.beginCycleTime}, '%Y-%m-%d 00:00:00')  and mt.follow_up_time <![CDATA[<=]]> DATE_FORMAT(#{query.endCycleTime}, '%Y-%m-%d 23:59:59')
            </if>
        </where>
        group by mt.owner_by
    </select>


    <select id="countBusFollowUpByFollowType" resultType="com.storlead.sales.pojo.vo.report.UserFollowUpModeCountVO">
        SELECT u.real_name as userName,d.id as deptId,d.name as deptName,mt.owner_by as userId,
        IFNULL(sum(case when mt.follow_up_type_value = 1 then 1 else 0 end),0) as videosNum,
        IFNULL(sum(case when mt.follow_up_type_value = 2 then 1 else 0 end),0) as comeNum,
        IFNULL(sum(case when mt.follow_up_type_value = 3 then 1 else 0 end),0) as visitNum,
        IFNULL(sum(case when mt.follow_up_type_value = 5 then 1 else 0 end),0) as emailNum,
        IFNULL(sum(case when mt.follow_up_type_value = 4 then 1 else 0 end),0) as otherNum,
        IFNULL(count(1),0) as followUpNum
        FROM customer_follow_up as mt
        left join user as u on u.id = mt.owner_by
        left join dept as d on u.dept_id = d.id
        <where>
            and mt.is_delete = 0
            <if test="query.ownerByIds != null and query.ownerByIds.size > 0">
                and mt.owner_by in
                <foreach collection="query.ownerByIds" item="ownerById" open="(" separator="," close=")">
                    #{ownerById}
                </foreach>
            </if>
            <if test="query.deptIds != null and query.deptIds.size > 0">
                and u.dept_id in
                <foreach collection="query.deptIds" item="deptId" open="(" separator="," close=")">
                    #{deptId}
                </foreach>
            </if>
            <if test="query.followUpTimeType != null and query.followUpTimeType == 10">
                and mt.create_time >= DATE_FORMAT(#{query.beginCycleTime}, '%Y-%m-%d 00:00:00')  and mt.create_time <![CDATA[<=]]> DATE_FORMAT(#{query.endCycleTime}, '%Y-%m-%d 23:59:59')
            </if>
            <if test="query.followUpTimeType != null and query.followUpTimeType == 20">
                and mt.follow_up_time >= DATE_FORMAT(#{query.beginCycleTime}, '%Y-%m-%d 00:00:00')  and mt.follow_up_time <![CDATA[<=]]> DATE_FORMAT(#{query.endCycleTime}, '%Y-%m-%d 23:59:59')
            </if>
        </where>
        group by mt.owner_by
    </select>

    <select id="selectBusinessOrderCustomerWhere" resultType="com.storlead.sales.pojo.vo.report.SalesDataGrowCountVO">
        SELECT
        IFNULL(sum(case when mt.data_type = 10 then 1 else 0 end),0) as customerNum,
        IFNULL(sum(case when mt.data_type = 30 then 1 else 0 end),0) as businessNum,
        IFNULL(sum(case when mt.data_type = 11 then 1 else 0 end),0) as clusNum,
        IFNULL(sum(case when mt.data_type = 40 then 1 else 0 end),0) as orderFormNum,
        <if test="query.summaryType == 80">
            mt.create_dept_name as deptName
        </if>
        <if test="query.summaryType != 80">
            mt.dept_name as deptName
        </if>
        <if test="query.summaryType == 70">
            ,mt.dept_name as groupType
        </if>
        <if test="query.summaryType == 80">
            ,mt.create_by_name as groupType
        </if>
        <if test="query.summaryType == 30 || query.summaryType == 50">
            <if test="query.queryTimeType == 10">
                ,DATE_FORMAT(mt.create_time, #{query.dataFormat}) as groupType
            </if>
            <if test="query.queryTimeType == 20">
                ,DATE_FORMAT(mt.get_time, #{query.dataFormat}) as groupType
            </if>
        </if>
        FROM v_business_order_customer as mt
        <where>
            and mt.is_delete = 0 and mt.is_delete = 0 and mt.real_name is not null
            <if test="query.beginCycleTime != null and query.endCycleTime != null">
                <if test="query.queryTimeType == 10">
                    and mt.create_time >= DATE_FORMAT(#{query.beginCycleTime}, '%Y-%m-%d 00:00:00')  and mt.create_time <![CDATA[<=]]> DATE_FORMAT(#{query.endCycleTime}, '%Y-%m-%d 23:59:59')
                </if>
                <if test="query.queryTimeType == 20">
                    and mt.get_time >= DATE_FORMAT(#{query.beginCycleTime}, '%Y-%m-%d 00:00:00')  and mt.get_time <![CDATA[<=]]> DATE_FORMAT(#{query.endCycleTime}, '%Y-%m-%d 23:59:59')
                </if>
            </if>
            <if test="query.summaryType == 70">
                and mt.dept_id is not null
            </if>
            <if test="query.summaryType == 80">
                and mt.owner_by is not null
            </if>
            <if test="query.deptIds != null and query.deptIds.size > 0">
                and mt.dept_id in
                <foreach collection="query.deptIds" item="deptId" open="(" separator="," close=")">
                    #{deptId}
                </foreach>
            </if>
            <if test="query.ownerByIds != null and query.ownerByIds.size > 0">
                and mt.owner_by in
                <foreach collection="query.ownerByIds" item="ownerById" open="(" separator="," close=")">
                    #{ownerById}
                </foreach>
            </if>
            <if test="query.createByIds != null and query.createByIds.size > 0">
                and mt.create_by in
                <foreach collection="query.createByIds" item="createById" open="(" separator="," close=")">
                    #{createById}
                </foreach>
            </if>
        </where>
        group by groupType
    </select>


    <select id="countBusFollowUpByResourceTypeVo" resultType="com.storlead.sales.pojo.vo.report.UserFollowUpCountVO">
        SELECT u.real_name as userName,d.id as deptId,d.name as deptName,mt.owner_by as userId,
        IFNULL(sum(case when mt.resource_type = 10 then 1 else 0 end),0) as customerNum,
        IFNULL(sum(case when mt.resource_type = 11 then 1 else 0 end),0) as clusNum,
        IFNULL(sum(case when mt.resource_type = 30 then 1 else 0 end),0) as businessNum,
        IFNULL(sum(case when mt.resource_type = 40 then 1 else 0 end),0) as orderFormNum,
        IFNULL(sum(case when mt.resource_type = 20 then 1 else 0 end),0) as liaisonNum,
        IFNULL(sum(case when mt.resource_type in (10,11,20,30,40) then 1 else 0 end),0) as followUpNum
        FROM customer_follow_up as mt
        left join user as u on u.id = mt.owner_by
        left join dept as d on u.dept_id = d.id
        <where>
            and mt.is_delete = 0
            <if test="query.ownerByIds != null and query.ownerByIds.size > 0">
                and mt.owner_by in
                <foreach collection="query.ownerByIds" item="ownerById" open="(" separator="," close=")">
                    #{ownerById}
                </foreach>
            </if>
            <if test="query.deptIds != null and query.deptIds.size > 0">
                and u.dept_id in
                <foreach collection="query.deptIds" item="deptId" open="(" separator="," close=")">
                    #{deptId}
                </foreach>
            </if>
            <if test="query.followUpTimeType != null and query.followUpTimeType == 10">
                and mt.create_time >= DATE_FORMAT(#{query.beginCycleTime}, '%Y-%m-%d 00:00:00')  and mt.create_time <![CDATA[<=]]> DATE_FORMAT(#{query.endCycleTime}, '%Y-%m-%d 23:59:59')
            </if>
            <if test="query.followUpTimeType != null and query.followUpTimeType == 20">
                and mt.follow_up_time >= DATE_FORMAT(#{query.beginCycleTime}, '%Y-%m-%d 00:00:00')  and mt.follow_up_time <![CDATA[<=]]> DATE_FORMAT(#{query.endCycleTime}, '%Y-%m-%d 23:59:59')
            </if>
        </where>
    </select>


    <select id="countBusFollowUpByFollowTypeVo" resultType="com.storlead.sales.pojo.vo.report.UserFollowUpModeCountVO">
        SELECT u.real_name as userName,d.id as deptId,d.name as deptName,mt.owner_by as userId,
        IFNULL(sum(case when mt.follow_up_type_value = 1 then 1 else 0 end),0) as videosNum,
        IFNULL(sum(case when mt.follow_up_type_value = 2 then 1 else 0 end),0) as comeNum,
        IFNULL(sum(case when mt.follow_up_type_value = 3 then 1 else 0 end),0) as visitNum,
        IFNULL(sum(case when mt.follow_up_type_value = 5 then 1 else 0 end),0) as emailNum,
        IFNULL(sum(case when mt.follow_up_type_value = 4 then 1 else 0 end),0) as otherNum,
        IFNULL(count(1),0) as followUpNum
        FROM customer_follow_up as mt
        left join user as u on u.id = mt.owner_by
        left join dept as d on u.dept_id = d.id
        <where>
            and mt.is_delete = 0
            <if test="query.ownerByIds != null and query.ownerByIds.size > 0">
                and mt.owner_by in
                <foreach collection="query.ownerByIds" item="ownerById" open="(" separator="," close=")">
                    #{ownerById}
                </foreach>
            </if>
            <if test="query.deptIds != null and query.deptIds.size > 0">
                and u.dept_id in
                <foreach collection="query.deptIds" item="deptId" open="(" separator="," close=")">
                    #{deptId}
                </foreach>
            </if>
            <if test="query.followUpTimeType != null and query.followUpTimeType == 10">
                and mt.create_time >= DATE_FORMAT(#{query.beginCycleTime}, '%Y-%m-%d 00:00:00')  and mt.create_time <![CDATA[<=]]> DATE_FORMAT(#{query.endCycleTime}, '%Y-%m-%d 23:59:59')
            </if>
            <if test="query.followUpTimeType != null and query.followUpTimeType == 20">
                and mt.follow_up_time >= DATE_FORMAT(#{query.beginCycleTime}, '%Y-%m-%d 00:00:00')  and mt.follow_up_time <![CDATA[<=]]> DATE_FORMAT(#{query.endCycleTime}, '%Y-%m-%d 23:59:59')
            </if>
        </where>
    </select>

<!--    跟进次数-->
<!--    跟进客户数-->
<!--    成交订单-->
<!--    成交金额-->
<!--    成交产品总数-->
    <select id="countBusinessSummaryTotal" resultType="com.storlead.sales.pojo.vo.report.SalesDataGrowCountVO">
        select
                <if test="query.summaryType == 30">
                    DATE_FORMAT(mt.create_time, '%Y-%m') as groupType
                </if>
                <if test="query.summaryType == 50">
                    DATE_FORMAT(mt.create_time, '%Y-%m-%d') as groupType
                </if>
                <if test="query.targetType == 1">
                    ,count(1) AS dataCount
                </if>
                <if test="query.targetType == 10">
                  ,count(DISTINCT  customer_id  ) AS dataCount
                </if>
                <if test="query.targetType == 30">
                    ,count(1) AS dataCount
                </if>
                <if test="query.targetType == 40">
                    ,sum(total_order_amt) as dataCount
                </if>
                <if test="query.targetType == 50">
                   , sum(q.total_number) as dataCount
                </if>
        FROM
        <if test="query.targetType == 1">
            customer_follow_up
        </if>
        <if test="query.targetType == 10">
            (select create_time,owner_by,is_delete,customer_id,resource_type from customer_follow_up where
                resource_type = 10 and is_delete = 0
                <if test="query.ownerByIds != null and query.ownerByIds.size > 0">
                    and owner_by in
                    <foreach collection="query.ownerByIds" item="ownerById" open="(" separator="," close=")">
                        #{ownerById}
                    </foreach>
                </if>
                group by DATE_FORMAT(create_time, '%Y-%m'),owner_by,customer_id)
        </if>
        <if test="query.targetType == 30 || query.targetType == 40 || query.targetType == 50">
            order_form
        </if>
         as mt
        left join user as u on u.id = mt.owner_by
        left join dept as d on u.dept_id = d.id
        <if test="query.targetType == 50">
            left join quotation as q on mt.id = q.order_form_id
        </if>
        <where>
            <if test="query.targetType == 10">
                and mt.resource_type = 10
            </if>
            <if test="query.targetType != 10">
                and mt.is_delete = 0
            </if>
            <if test="query.ownerByIds != null and query.ownerByIds.size > 0">
                and mt.owner_by in
                <foreach collection="query.ownerByIds" item="ownerById" open="(" separator="," close=")">
                    #{ownerById}
                </foreach>
            </if>
            <if test="query.beginCycleTime != null and query.endCycleTime != null">
                <if test="query.targetType == 30 || query.targetType == 40 || query.targetType == 50">
                    and mt.signing_date >= DATE_FORMAT(#{query.beginCycleTime}, '%Y-%m-%d 00:00:00')  and mt.signing_date <![CDATA[<=]]> DATE_FORMAT(#{query.endCycleTime}, '%Y-%m-%d 23:59:59')
                </if>
                <if test="query.targetType == 1 || query.targetType == 10">
                    and mt.create_time >= DATE_FORMAT(#{query.beginCycleTime}, '%Y-%m-%d 00:00:00')  and mt.create_time <![CDATA[<=]]> DATE_FORMAT(#{query.endCycleTime}, '%Y-%m-%d 23:59:59')
                </if>
            </if>
        </where>
        group by groupType
    </select>


<!--    新增客户-->
<!--    新增商机-->
<!--    新增线索-->
<!--    新增订单-->
<!--    成交订单-->
<!--    成交总额-->
    <select id="calculateBusinessGrowthStatistics" resultType="com.storlead.sales.pojo.vo.report.SalesBusinessGrowCountVO">
        select
        <if test="query.increaseType == 1 || query.increaseType == 3">
              DATE_FORMAT(mt.create_time, '%Y-%m') as groupType,
        </if>
        <if test="query.increaseType == 2">
              DATE_FORMAT(mt.geted_date, '%Y-%m') as groupType,
        </if>
        <if test="query.increaseType == 4 || query.increaseType == 5 || query.increaseType == 6">
              DATE_FORMAT(mt.signing_date, '%Y-%m') as groupType,
        </if>
        <if test="query.increaseType == 1 || query.increaseType == 2 || query.increaseType == 3 || query.increaseType == 4 || query.increaseType == 5">
            count(1) as dataCount
        </if>
        <if test="query.increaseType == 6">
            IFNULL(sum(q.total_order_amt),0) as dataCount
        </if>
        FROM
        <if test="query.increaseType == 1 || query.increaseType == 3">
            customer as mt
        </if>
        <if test="query.increaseType == 2">
            business as mt
        </if>
        <if test="query.increaseType == 4 || query.increaseType == 5 || query.increaseType == 6">
            order_form as mt
        </if>
        left join user as u on u.id = mt.owner_by
        left join dept as d on u.dept_id = d.id
        <if test="query.increaseType == 6">
            left join quotation as q on mt.id = q.order_form_id
        </if>
        <where>
            and mt.is_delete = 0
            <if test="query.increaseType == 1">
               AND  mt.customer_form = 10
                AND mt.owner_by IS NOT NULL
            </if>
            <if test="query.increaseType == 3">
                AND  mt.customer_form = 11
                AND mt.owner_by IS NOT NULL
            </if>
            <if test="query.ownerByIds != null and query.ownerByIds.size > 0">
                and mt.owner_by in
                <foreach collection="query.ownerByIds" item="ownerById" open="(" separator="," close=")">
                    #{ownerById}
                </foreach>
            </if>
            <if test="query.increaseType == 4">
                AND mt.business_id IN (select id from business WHERE is_delete =0 AND has_loss=0 AND stage=4)
            </if>
            <if test="query.years != null and query.years.size > 0">
                <if test="query.increaseType == 1 || query.increaseType == 3">
                    and YEAR(mt.create_time) in
                </if>
                <if test="query.increaseType == 2">
                    and YEAR(mt.geted_date) in
                </if>
                <if test="query.increaseType == 4 || query.increaseType == 5 || query.increaseType == 6">
                    and YEAR(mt.signing_date) in
                </if>
                <foreach collection="query.years" item="year" open="(" separator="," close=")">
                    #{year}
                </foreach>
            </if>
        </where>
        group by groupType
    </select>

    <!-- 客户成交TOP10 -->
    <select id="selectSalesDealRankTop" resultType="com.storlead.sales.pojo.vo.report.SalesDealRankCountlVO">
        select mt.customer_name as customerName
             ,sum(o.total_order_amt) as orderDealAmount
             ,count(o.id) as orderDealNum
        from customer as mt
        right join (select * from order_form where is_delete = 0) as o
                   on mt.id = o.customer_id
        left join user as u on u.id = mt.owner_by
        left join dept as d on u.dept_id = d.id
        <where>
            and mt.is_delete = 0 and mt.customer_form = 10
            and mt.has_public_customer = 0 and mt.has_garbage_customer = 0
            <if test="query.ownerByIds != null and query.ownerByIds.size > 0">
                and mt.owner_by in
                <foreach collection="query.ownerByIds" item="ownerById" open="(" separator="," close=")">
                    #{ownerById}
                </foreach>
            </if>
            <if test="query.beginCycleTime != null and query.endCycleTime != null">
                and o.signing_date >= #{query.beginCycleTime}  and o.signing_date <![CDATA[<=]]> #{query.endCycleTime}
            </if>
        </where>
        group by mt.id order by orderDealAmount desc limit 10
    </select>

    <select id="selectSalesAreaSpreadSummary" resultType="com.storlead.sales.pojo.vo.report.SalesAreaSpreadCountVO">
        select count(DISTINCT mt.id) as customerNum
             ,IFNULL(sum( CASE WHEN o.id is null THEN 0 ELSE 1 END ), 0 ) AS orderDealNum
             ,sum(IFNULL(total_order_amt,0)) as orderDealAmount
             <if test="query.areaType == 1">
                 ,mt.continent as areaName
             </if>
             <if test="query.areaType == 2">
                 ,mt.country as areaName
             </if>
             from customer as mt
             left join (select * from order_form where is_delete = 0) as o
             on o.customer_id = mt.id
            <where>
                and mt.is_delete = 0 and mt.customer_form = 10
                    and mt.has_public_customer = 0 and mt.has_garbage_customer = 0
                <if test="query.ownerByIds != null and query.ownerByIds.size > 0">
                    and mt.owner_by in
                    <foreach collection="query.ownerByIds" item="ownerById" open="(" separator="," close=")">
                        #{ownerById}
                    </foreach>
                </if>
                <if test="query.beginCycleTime != null and query.endCycleTime != null">
                    and o.signing_date >= #{query.beginCycleTime}  and o.signing_date <![CDATA[<=]]> #{query.endCycleTime}
                </if>
            </where>
            <if test="query.areaType == 1">
                group by  mt.continent
            </if>
            <if test="query.areaType == 2">
                group by  mt.country
            </if>

    </select>

    <select id="selectSalesAreaSpreadTotal" resultType="com.storlead.sales.pojo.vo.report.SalesAreaSpreadTotalVO">
        select count(DISTINCT mt.id) as customerTotalNum
          ,IFNULL(sum( CASE WHEN o.id is null THEN 0 ELSE 1 END ), 0 ) AS orderTotalNum
        ,sum(IFNULL(total_order_amt,0)) as orderTotalAmount
        ,mt.country from customer as mt
        left join (select * from order_form where is_delete = 0) as o
        on o.customer_id = mt.id
        <where>
            and mt.is_delete = 0 and mt.customer_form = 10
            and mt.has_public_customer = 0 and mt.has_garbage_customer = 0
            <if test="query.ownerByIds != null and query.ownerByIds.size > 0">
                and mt.owner_by in
                <foreach collection="query.ownerByIds" item="ownerById" open="(" separator="," close=")">
                    #{ownerById}
                </foreach>
            </if>
            <if test="query.beginCycleTime != null and query.endCycleTime != null">
                and o.signing_date >= #{query.beginCycleTime}  and o.signing_date <![CDATA[<=]]> #{query.endCycleTime}
            </if>
        </where>
    </select>

</mapper>
