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

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

    <select id="getListByStatus" resultType="com.storlead.sales.pojo.vo.report.CustomerStatusTotalVO">
        SELECT
            customer_status_dict_value,
            GROUP_CONCAT(id ORDER BY id ASC SEPARATOR ',') AS customer_ids
        FROM
            customer
        WHERE
            is_delete = 0
            AND customer_form = 10
            AND has_public_customer = 0
            AND has_garbage_customer = 0
            AND customer_status_dict_value IN
            <foreach collection="csa.statusList" item="status" open="(" separator="," close=")">
                #{status}
            </foreach>
            <if test="csa.beginTime != null">
                AND create_time > #{csa.beginTime}
            </if>
            <if test="csa.endTime != null">
                AND #{csa.endTime} > create_time
            </if>

            <if test="csa.ownerByIds != null and csa.ownerByIds.size > 0">
                AND owner_by IN
                <foreach collection="csa.ownerByIds" item="ownerById" open="(" separator="," close=")">
                    #{ownerById}
                </foreach>
            </if>
        GROUP BY customer_status_dict_value

    </select>

    <select id="getListBySource" resultType="com.storlead.sales.pojo.vo.report.CustomerStatusTotalVO">
        SELECT
        customer_source_dict_value,
        GROUP_CONCAT(id ORDER BY id ASC SEPARATOR ',') AS customer_ids
        FROM
        customer
        WHERE
        is_delete = 0
        AND customer_form = 10
        AND has_public_customer = 0
        AND has_garbage_customer = 0
        AND customer_source_dict_value IN
        <foreach collection="csa.sourceList" item="source" open="(" separator="," close=")">
            #{source}
        </foreach>
        <if test="csa.beginTime != null">
            AND create_time > #{csa.beginTime}
        </if>
        <if test="csa.endTime != null">
            AND #{csa.endTime} > create_time
        </if>

        <if test="csa.ownerByIds != null and csa.ownerByIds.size > 0">
            AND owner_by IN
            <foreach collection="csa.ownerByIds" item="ownerById" open="(" separator="," close=")">
                #{ownerById}
            </foreach>
        </if>
        GROUP BY customer_source_dict_value

    </select>

    <select id="getListByLevel" resultType="com.storlead.sales.pojo.vo.report.CustomerStatusTotalVO">
        SELECT
        customer_level_dict_value,
        GROUP_CONCAT(id ORDER BY id ASC SEPARATOR ',') AS customer_ids
        FROM
        customer
        WHERE
        is_delete = 0
        AND customer_form = 10
        AND has_public_customer = 0
        AND has_garbage_customer = 0
        AND customer_level_dict_value IN
        <foreach collection="csa.levelList" item="level" open="(" separator="," close=")">
            #{level}
        </foreach>
        <if test="csa.beginTime != null">
            AND create_time > #{csa.beginTime}
        </if>
        <if test="csa.endTime != null">
            AND #{csa.endTime} > create_time
        </if>

        <if test="csa.ownerByIds != null and csa.ownerByIds.size > 0">
            AND owner_by IN
            <foreach collection="csa.ownerByIds" item="ownerById" open="(" separator="," close=")">
                #{ownerById}
            </foreach>
        </if>
        GROUP BY customer_level_dict_value

    </select>

    <select id="getListByCountry" resultType="com.storlead.sales.pojo.vo.report.CustomerStatusTotalVO">
        SELECT
        country,
        GROUP_CONCAT(id ORDER BY id ASC SEPARATOR ',') AS customer_ids
        FROM
        customer
        WHERE
        is_delete = 0
        AND customer_form = 10
        AND has_public_customer = 0
        AND has_garbage_customer = 0
        <if test="csa.beginTime != null">
            AND create_time > #{csa.beginTime}
        </if>
        <if test="csa.endTime != null">
            AND #{csa.endTime} > create_time
        </if>

        <if test="csa.ownerByIds != null and csa.ownerByIds.size > 0">
            AND owner_by IN
            <foreach collection="csa.ownerByIds" item="ownerById" open="(" separator="," close=")">
                #{ownerById}
            </foreach>
        </if>
        GROUP BY country

    </select>
</mapper>
