<?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.customer.RankingsMapper">

    <select id="selectBusinessOrderCustomer" resultType="com.storlead.sales.pojo.vo.customer.SalesDataAddCountVO">
        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,
        mt.dept_name as deptName,
        <choose>
            <when test="query.summaryType == 30">
                DATE_FORMAT(mt.get_time, '%Y-%m') as groupType
            </when>
            <when test="query.summaryType == 50">
                DATE_FORMAT(mt.get_time, '%Y-%m-%d') as groupType
            </when>
            <when test="query.summaryType == 70">
                mt.dept_id as groupType
            </when>
            <when test="query.summaryType == 80">
                mt.real_name as groupType
            </when>
        </choose>
        FROM v_business_order_customer as mt
        <where>
            <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 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 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>
            and mt.is_delete = 0
        </where>
        group by groupType
        order by
        <choose>
            <when test="query.targetType == 10">
                customerNum
            </when>
            <when test="query.targetType == 30">
                businessNum
            </when>
            <when test="query.targetType == 11">
                clusNum
            </when>
            <when test="query.targetType == 40">
                orderFormNum
            </when>
        </choose>
        desc
    </select>

    <select id="selectOrderProduct" resultType="com.storlead.sales.pojo.vo.customer.TransactionDataVo">
        SELECT
        o.owner_by As ownerId,
        u.real_name As realName,
        u.dept_id As deptId,
        d.name AS deptName,
        SUM(o.total_order_amt) AS dealAmount,
        SUM(q.total_number) AS dealProductNum,
        COUNT(o.id) AS dealOrder,
        ROUND(SUM(o.total_order_amt) / COUNT(o.id), 2) AS avgCustomerPrice,
        <choose>
            <when test="query.summaryType == 30">
                DATE_FORMAT(o.signing_date, '%Y-%m') as groupType
            </when>
            <when test="query.summaryType == 50">
                DATE_FORMAT(o.signing_date, '%Y-%m-%d') as groupType
            </when>
            <when test="query.summaryType == 70">
                u.dept_id as groupType
            </when>
            <when test="query.summaryType == 80">
                u.real_name as groupType
            </when>
        </choose>
        FROM
        order_form AS o
        LEFT JOIN
        quotation AS q ON o.id = q.order_form_id
        LEFT JOIN
        user AS u ON o.owner_by = u.id
        LEFT JOIN
        dept AS d ON u.dept_id = d.id
        <where>
            <if test="query.ownerByIds != null and query.ownerByIds.size > 0">
                and o.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 >= DATE_FORMAT(#{query.beginCycleTime}, '%Y-%m-%d 00:00:00') and o.signing_date <![CDATA[<=]]> DATE_FORMAT(#{query.endCycleTime}, '%Y-%m-%d 23:59:59')
            </if>
            <if test="query.summaryType == 70">
                and u.dept_id is not null
            </if>
            <if test="query.summaryType == 80">
                and o.owner_by is not null
            </if>
            and o.is_delete = 0
        </where>
        GROUP BY
        groupType
        ORDER BY
        dealAmount DESC;
    </select>

    <select id="selectClueConversionRate" resultType="com.storlead.sales.pojo.vo.customer.ConversionRateListVo">
        SELECT
        c.owner_by As ownerId,
        u.real_name As realName,
        u.dept_id As deptId,
        d.name AS deptName,
        SUM(CASE WHEN c.customer_form = 11 OR (c.customer_form = 10 AND c.has_from_clue = 1) THEN 1 ELSE 0 END) AS convertedNum,
        SUM(CASE WHEN c.customer_form = 10 AND c.has_from_clue = 1 THEN 1 ELSE 0 END) AS newNum,
        CASE
        WHEN SUM(CASE WHEN c.customer_form = 11 OR (c.customer_form = 10 AND c.has_from_clue = 1) THEN 1 ELSE 0 END) = 0 THEN 0
        ELSE ROUND((SUM(CASE WHEN c.customer_form = 10 AND c.has_from_clue = 1 THEN 1 ELSE 0 END) / SUM(CASE WHEN c.customer_form = 11 OR (c.customer_form = 10 AND c.has_from_clue = 1) THEN 1 ELSE 0 END)) * 100, 2)
        END AS conversionRate,
        <choose>
            <when test="query.summaryType == 30">
                DATE_FORMAT(c.create_time, '%Y-%m') as groupType
            </when>
            <when test="query.summaryType == 50">
                DATE_FORMAT(c.create_time, '%Y-%m-%d') as groupType
            </when>
            <when test="query.summaryType == 70">
                u.dept_id as groupType
            </when>
            <when test="query.summaryType == 80">
                u.real_name as groupType
            </when>
        </choose>
        FROM
        customer AS c
        LEFT JOIN
        user AS u ON c.owner_by = u.id
        LEFT JOIN
        dept AS d ON u.dept_id = d.id
        <where>
            <if test="query.ownerByIds != null and query.ownerByIds.size > 0">
                and c.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 c.create_time >= DATE_FORMAT(#{query.beginCycleTime}, '%Y-%m-%d 00:00:00') and c.create_time <![CDATA[<=]]> DATE_FORMAT(#{query.endCycleTime}, '%Y-%m-%d 23:59:59')
            </if>
            <if test="query.summaryType == 70">
                and u.dept_id is not null
            </if>
            <if test="query.summaryType == 80">
                and c.owner_by is not null
            </if>
            and c.is_delete = 0
        </where>
        GROUP BY
        groupType
        HAVING (convertedNum > 0 or newNum > 0)
        ORDER BY
        conversionRate DESC;
    </select>

    <select id="selectCustomerConversionRate" resultType="com.storlead.sales.pojo.vo.customer.ConversionRateListVo">
        SELECT
        c.owner_by AS ownerId,
        u.real_name AS realName,
        u.dept_id AS deptId,
        d.name AS deptName,
        sum(case when b.id is null then 0 ELSE 1 end) newNum,
        COUNT( c.id) AS convertedNum,
        CASE
        WHEN COUNT( c.id) = 0 THEN 0
        ELSE ROUND(COUNT(  b.id) / COUNT( c.id) * 100, 2)
        END AS conversionRate,
        <choose>
            <when test="query.summaryType == 30">
                DATE_FORMAT(c.create_time, '%Y-%m') as groupType
            </when>
            <when test="query.summaryType == 50">
                DATE_FORMAT(c.create_time, '%Y-%m-%d') as groupType
            </when>
            <when test="query.summaryType == 70">
                u.dept_id as groupType
            </when>
            <when test="query.summaryType == 80">
                u.real_name as groupType
            </when>
        </choose>
        FROM
        customer AS c
        LEFT JOIN USER AS u ON c.owner_by = u.id
        LEFT JOIN (select * from business where is_delete = 0 group by customer_id) AS b ON c.id = b.customer_id
        LEFT JOIN dept AS d ON u.dept_id = d.id
        <where>
            <if test="query.ownerByIds != null and query.ownerByIds.size > 0">
                and c.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 c.create_time >= DATE_FORMAT(#{query.beginCycleTime}, '%Y-%m-%d 00:00:00') and c.create_time <![CDATA[<=]]> DATE_FORMAT(#{query.endCycleTime}, '%Y-%m-%d 23:59:59')
            </if>
            <if test="query.summaryType == 70">
                and u.dept_id is not null
            </if>
            <if test="query.summaryType == 80">
                and c.owner_by is not null
            </if>
            and c.customer_form = 10
            and c.owner_by is not null
            AND c.is_delete = 0
        </where>
        GROUP BY
        groupType
        ORDER BY
        conversionRate DESC;
    </select>

    <select id="selectBusinessConversionRate" resultType="com.storlead.sales.pojo.vo.customer.ConversionRateListVo">
        SELECT
        b.owner_by As ownerId,
        u.real_name As realName,
        u.dept_id As deptId,
        d.name AS deptName,
        SUM(CASE WHEN b.stage = 4 THEN 1 ELSE 0 END) AS newNum,
        COUNT(b.id) AS convertedNum,
        CASE
        WHEN COUNT(b.id) = 0 THEN 0
        ELSE ROUND(SUM(CASE WHEN b.stage = 4 THEN 1 ELSE 0 END) / COUNT(b.id) * 100, 2)
        END AS conversionRate,
        <choose>
            <when test="query.summaryType == 30">
                DATE_FORMAT(b.geted_date, '%Y-%m') as groupType
            </when>
            <when test="query.summaryType == 50">
                DATE_FORMAT(b.geted_date, '%Y-%m-%d') as groupType
            </when>
            <when test="query.summaryType == 70">
                u.dept_id as groupType
            </when>
            <when test="query.summaryType == 80">
                u.real_name as groupType
            </when>
        </choose>
        FROM
        business AS b
        LEFT JOIN
        user AS u ON b.owner_by = u.id
        LEFT JOIN
        order_form AS o ON b.id = o.business_id
        LEFT JOIN
        dept AS d ON u.dept_id = d.id
        <where>
            <if test="query.ownerByIds != null and query.ownerByIds.size > 0">
                and b.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  b.geted_date>= DATE_FORMAT(#{query.beginCycleTime}, '%Y-%m-%d 00:00:00') and  b.geted_date <![CDATA[<=]]> DATE_FORMAT(#{query.endCycleTime}, '%Y-%m-%d 23:59:59')
            </if>
            <if test="query.summaryType == 70">
                and u.dept_id is not null
            </if>
            <if test="query.summaryType == 80">
                and b.owner_by is not null
            </if>
            and b.is_delete = 0
        </where>
        GROUP BY
        groupType
        ORDER BY
        conversionRate DESC;
    </select>

    <select id="selectBusinessLossRate" resultType="com.storlead.sales.pojo.vo.customer.ConversionRateListVo">
        SELECT
        b.owner_by As ownerId,
        u.dept_id As deptId,
        d.name AS deptName,
        SUM(CASE WHEN b.has_loss = 1 THEN 1 ELSE 0 END) AS newNum,
        COUNT(b.id) AS convertedNum,
        CASE
        WHEN COUNT(b.id) = 0 THEN 0
        ELSE ROUND(SUM(CASE WHEN b.has_loss = 1 THEN 1 ELSE 0 END) * 1.0 / COUNT(b.id) * 100, 2)
        END AS conversionRate,
        <choose>
            <when test="query.summaryType == 30">
                DATE_FORMAT(b.geted_date, '%Y-%m') as groupType
            </when>
            <when test="query.summaryType == 50">
                DATE_FORMAT(b.geted_date, '%Y-%m-%d') as groupType
            </when>
            <when test="query.summaryType == 70">
                u.dept_id as groupType
            </when>
            <when test="query.summaryType == 80">
                u.real_name as groupType
            </when>
        </choose>
        FROM
        business AS b
        LEFT JOIN
        user AS u ON b.owner_by = u.id
        LEFT JOIN
        order_form AS o ON b.id = o.business_id
        LEFT JOIN
        dept AS d ON u.dept_id = d.id
        <where>
            <if test="query.ownerByIds != null and query.ownerByIds.size > 0">
                and b.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  b.geted_date >= DATE_FORMAT(#{query.beginCycleTime}, '%Y-%m-%d 00:00:00') and  b.geted_date <![CDATA[<=]]> DATE_FORMAT(#{query.endCycleTime}, '%Y-%m-%d 23:59:59')
            </if>
            <if test="query.summaryType == 70">
                and u.dept_id is not null
            </if>
            <if test="query.summaryType == 80">
                and b.owner_by is not null
            </if>
            and b.is_delete = 0
        </where>
        GROUP BY
        groupType
        ORDER BY
        conversionRate DESC;
    </select>

    <select id="selectQuotationByTotalNumber" resultType="com.storlead.sales.pojo.vo.customer.QuotationSalesListVO">
        SELECT
        sd.ssd_sku_code AS productName,
        SUM(sd.order_amt) AS salesAmount,
        SUM(sd.product_count) AS salesNum,
        CASE
        WHEN SUM(sd.product_count) > 0 THEN ROUND(SUM(sd.order_amt) / SUM(sd.product_count), 2)
        ELSE 0
        END AS avgPrice
        FROM
        order_form AS o
        LEFT JOIN
        quotation AS q ON o.quotation_id = q.id
        LEFT JOIN
        sku_detail AS sd ON  sd.quotation_id = q.id
        <where>
            <if test="query.ownerByIds != null and query.ownerByIds.size > 0">
                and sd.owner_by in
                <foreach collection="query.ownerByIds" item="ownerById" open="(" separator="," close=")">
                    #{ownerById}
                </foreach>
            </if>
            <if test="query.productName != null ">
                AND sd.ssd_sku_code = #{query.productName}
            </if>
            <if test="query.beginCycleTime != null and query.endCycleTime != null">
                and  o.signing_date >= DATE_FORMAT(#{query.beginCycleTime}, '%Y-%m-%d 00:00:00') and  o.signing_date <![CDATA[<=]]> DATE_FORMAT(#{query.endCycleTime}, '%Y-%m-%d 23:59:59')
            </if>
            and o.is_delete = 0
            and sd.is_delete = 0
        </where>
        GROUP BY
        productName
    </select>

    <select id="selectQuotationByTime" resultType="com.storlead.sales.pojo.vo.customer.QuotationTimeListVO">
        SELECT
        DATE_FORMAT(o.signing_date, '%Y-%m') AS month,
        SUM(sd.order_amt) AS salesAmount,
        SUM(sd.product_count) AS salesNum
        FROM
        order_form AS o
        LEFT JOIN
        quotation AS q ON o.quotation_id = q.id
        LEFT JOIN
        sku_detail AS sd ON sd.quotation_id = q.id
        <where>
            <if test="query.ownerByIds != null and query.ownerByIds.size > 0">
                and sd.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 >= DATE_FORMAT(#{query.beginCycleTime}, '%Y-%m-%d 00:00:00')
                and o.signing_date <![CDATA[<=]]> DATE_FORMAT(#{query.endCycleTime}, '%Y-%m-%d 23:59:59')
            </if>
            and o.is_delete = 0
            and sd.is_delete = 0
        </where>
        GROUP BY
        month
        ORDER BY
        month;
    </select>

    <select id="selectFollowRecord" resultType="com.storlead.sales.pojo.vo.customer.UserFollowUpModeCountVO">
        SELECT
        u.real_name as userName,
        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(sum(1),0) as followUpNum,
        <choose>
            <when test="query.summaryType == 30 and query.followUpTimeType == '10'">
                DATE_FORMAT(mt.create_time, '%Y-%m') as groupType
            </when>
            <when test="query.summaryType == 50 and query.followUpTimeType == '10'">
                DATE_FORMAT(mt.create_time, '%Y-%m-%d') as groupType
            </when>
            <when test="query.summaryType == 30 and query.followUpTimeType == '20'">
                DATE_FORMAT(mt.follow_up_time, '%Y-%m') as groupType
            </when>
            <when test="query.summaryType == 50 and query.followUpTimeType == '20'">
                DATE_FORMAT(mt.follow_up_time, '%Y-%m-%d') as groupType
            </when>
            <when test="query.summaryType == 70 ">
                u.dept_id as groupType
            </when>
            <when test="query.summaryType == 80 ">
                u.real_name as groupType
            </when>
        </choose>
        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>
            <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>
            <choose>
                <when 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')
                </when>
                <when 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')
                </when>
            </choose>
            and mt.is_delete = 0
        </where>
        GROUP BY
        groupType
        ORDER BY
        <choose>
            <when test="query.targetType == 1">
                videosNum
            </when>
            <when test="query.targetType == 2">
                comeNum
            </when>
            <when test="query.targetType == 3">
                visitNum
            </when>
            <when test="query.targetType == 4">
                emailNum
            </when>
            <when test="query.targetType == 5">
                otherNum
            </when>
        </choose>
        DESC;
    </select>

    <select id="selectClosedBusinessByMonth" resultType="com.storlead.sales.pojo.vo.customer.ClosedBusinessMonthCountVO">
        SELECT
        COUNT(b.id) AS businessNum,
        IFNULL(SUM(o.total_order_amt), 0) AS businessAmount,
        DATE_FORMAT(o.signing_date, '%Y-%m') as groupType
        FROM
        order_form AS o
        left join business as b on o.business_id = b.id
        left join user as u on u.id = o.owner_by
        left join dept as d on u.dept_id = d.id
        <where>
            <if test="query.ownerByIds != null and query.ownerByIds.size > 0">
                and b.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 >= DATE_FORMAT(#{query.beginCycleTime}, '%Y-%m-%d 00:00:00')  and o.signing_date <![CDATA[<=]]> DATE_FORMAT(#{query.endCycleTime}, '%Y-%m-%d 23:59:59')
            </if>
            and o.is_delete = 0
            and b.is_delete = 0
            and b.stage = 4
            and b.has_loss = 0
        </where>
        GROUP BY
        groupType
    </select>

    <select id="selectCloseBusinessAllList" resultType="com.storlead.sales.pojo.vo.customer.CloseBusinessAllListVO">
        SELECT
        b.title AS title,
        c.customer_name AS customer,
        b.stage as statusKey,
        o.signing_date AS closeTime,
        b.sources_of_business_dict_value AS sourceKey,
        u.real_name AS ownerBy
        FROM
        business AS b
        left join
        order_form as o on b.id = o.business_id
        left join
        customer as c on b.customer_id = c.id
        left join
        user as u on u.id = b.owner_by
        <where>
            <if test="query.ownerByIds != null and query.ownerByIds.size > 0">
                and b.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 >= DATE_FORMAT(#{query.beginCycleTime}, '%Y-%m-%d 00:00:00')  and o.signing_date <![CDATA[<=]]> DATE_FORMAT(#{query.endCycleTime}, '%Y-%m-%d 23:59:59')
            </if>
            and b.is_delete = 0
            and b.stage = 4
            and b.has_loss = 0
        </where>
        GROUP BY
        title
    </select>

</mapper>
