CustomerReportMapper.xml 27.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
<?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="cn.com.glsx.entmicro.mapper.CustomerReportMapper">
    <resultMap id="BaseResultMap" type="cn.com.glsx.entmicro.model.request.DracDataMerchantQyWeekDO">
        <!--
          WARNING - @mbg.generated
        -->
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="merchant_id" jdbcType="INTEGER" property="merchantId"/>
        <result column="week_name" jdbcType="VARCHAR" property="weekName"/>
        <result column="qy_total_count" jdbcType="INTEGER" property="qyTotalCount"/>
        <result column="qy_customer_ratio" jdbcType="DOUBLE" property="qyCustomerRatio"/>
        <result column="apply_count" jdbcType="INTEGER" property="applyCount"/>
        <result column="apply_degree" jdbcType="DOUBLE" property="applyDegree"/>
        <result column="newly_qy_count" jdbcType="INTEGER" property="newlyQyCount"/>
        <result column="newly_qy_degree" jdbcType="DOUBLE" property="newlyQyDegree"/>
        <result column="flow_qy_count" jdbcType="INTEGER" property="flowQyCount"/>
        <result column="flow_qy_degree" jdbcType="DOUBLE" property="flowQyDegree"/>
        <result column="total_count" jdbcType="INTEGER" property="totalCount"/>
        <result column="newly_total_count" jdbcType="INTEGER" property="newlyTotalCount"/>
        <result column="newly_total_degree" jdbcType="DOUBLE" property="newlyTotalDegree"/>
        <result column="created_time" jdbcType="TIMESTAMP" property="createdTime"/>
    </resultMap>

    <resultMap id="StaffDOMap" type="cn.com.glsx.entmicro.model.request.DracDataMerchantQyWeekStaffDO">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="week_id" jdbcType="INTEGER" property="weekId"/>
        <result column="staff_id" jdbcType="INTEGER" property="staffId"/>
        <result column="staff_name" jdbcType="VARCHAR" property="staffName"/>
        <result column="newly_qy_count" jdbcType="INTEGER" property="newlyQyCount"/>
        <result column="newly_qy_ratio" jdbcType="DOUBLE" property="newlyQyRatio"/>
        <result column="merchant_id" jdbcType="INTEGER" property="merchantId"/>
        <result column="created_time" jdbcType="TIMESTAMP" property="createdTime"/>
    </resultMap>

    <resultMap id="GroupReportMap" type="cn.com.glsx.entmicro.model.response.GroupCustomerReportVO">
        <result column="merchant_id" jdbcType="INTEGER" property="groupMerchantId"/>
        <result column="qy_total_count" jdbcType="INTEGER" property="qyTotalCount"/>
        <result column="newly_qy_count" jdbcType="INTEGER" property="newlyQyCount"/>
        <result column="newly_total_count" jdbcType="VARCHAR" property="newlyTotalCount"/>
        <result column="newly_qy_count" jdbcType="INTEGER" property="newlyQyCount"/>
    </resultMap>

    <!--定时统计门店周报-->
    <insert id="insertWeekly" useGeneratedKeys="true" keyProperty="id" keyColumn="id"
            parameterType="cn.com.glsx.entmicro.model.request.DracDataMerchantQyWeekDO">
        INSERT INTO
        `drac_data_merchant_qy_week` (`merchant_id`, `week_name`, `qy_total_count`, `qy_customer_ratio`, `apply_count`, `apply_degree`,
                                      `newly_qy_count`, `newly_qy_degree`, `flow_qy_count`, `flow_qy_degree`, `total_count`, `newly_total_count`, newly_total_degree,
                                      `del_flag`, `created_by`, `created_time`, `updated_by`, `updated_time`)
        VALUES
            (
            #{merchantId},
            #{weekName},
            #{qyTotalCount},
            #{qyCustomerRatio},
            #{applyCount},
            #{applyDegree},
            #{newlyQyCount},
            #{newlyQyDegree},
            #{flowQyCount},
            #{flowQyDegree},
            #{totalCount},
            #{newlyTotalCount},
            #{newlyTotalDegree},
            #{delFlag},
            #{createdBy},
            #{createdTime},
            NULL,
            NULL
            )
    </insert>

    <!--批量插入周报-->
    <insert id="insertBatchWeeklyReport" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO
        `drac_data_merchant_qy_week` (`id`, `merchant_id`, `week_name`, `qy_total_count`, `qy_customer_ratio`, `apply_count`, `apply_degree`,
        `newly_qy_count`, `newly_qy_degree`, `flow_qy_count`, `flow_qy_degree`, `total_count`, `newly_total_count`, newly_total_degree,
        `del_flag`, `created_by`, `created_time`, `updated_by`, `updated_time`)
        VALUES
        <foreach collection="list" item="item" index="index" open="" close="" separator=",">
        (
        NULL,
        #{item.merchantId},
        #{item.weekName},
        #{item.qyTotalCount},
        #{item.qyCustomerRatio},
        #{item.applyCount},
        #{item.applyDegree},
        #{item.newlyQyCount},
        #{item.newlyQyDegree},
        #{item.flowQyCount},
        #{item.flowQyDegree},
        #{item.totalCount},
        #{item.newlyTotalCount},
        #{item.newlyTotalDegree},
        #{item.delFlag},
        #{item.createdBy},
        #{item.createdTime},
        NULL,
        NULL
        )
        </foreach>
    </insert>

    <!--批量插入月报-->
    <insert id="insertBatchMonthlyReport" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO
        `drac_data_merchant_qy_month` (`id`, `merchant_id`, `week_name`, `qy_total_count`, `qy_customer_ratio`, `apply_count`, `apply_degree`,
        `newly_qy_count`, `newly_qy_degree`, `flow_qy_count`, `flow_qy_degree`, `total_count`, `newly_total_count`, newly_total_degree,
        `del_flag`, `created_by`, `created_time`, `updated_by`, `updated_time`)
        VALUES
        <foreach collection="list" item="item" index="index" open="" close="" separator=",">
            (
            NULL,
            #{item.merchantId},
            #{item.weekName},
            #{item.qyTotalCount},
            #{item.qyCustomerRatio},
            #{item.applyCount},
            #{item.applyDegree},
            #{item.newlyQyCount},
            #{item.newlyQyDegree},
            #{item.flowQyCount},
            #{item.flowQyDegree},
            #{item.totalCount},
            #{item.newlyTotalCount},
            #{item.newlyTotalDegree},
            #{item.delFlag},
            #{item.createdBy},
            #{item.createdTime},
            NULL,
            NULL
            )
        </foreach>
    </insert>

    <!--批量添加周报员工分布-->
    <insert id="insertBatchStaffDistributionByWeek" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO
        `drac_data_merchant_qy_week_staff`
        (`week_id`, `staff_id`, `staff_name`, `newly_qy_count`, `newly_qy_ratio`, `del_flag`, `created_by`, `created_time`)
        VALUES
        <foreach collection="list" item="item" index="index" open="" close="" separator=",">
            (
                #{item.weekId},
                #{item.staffId},
                #{item.staffName},
                #{item.newlyQyCount},
                #{item.newlyQyRatio},
                #{item.delFlag},
                #{item.createdBy},
                #{item.createdTime}
            )
        </foreach>
    </insert>

    <!--批量添加月报员工分布-->
    <insert id="insertBatchStaffDistributionByMonth" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO
        `drac_data_merchant_qy_month_staff`
        (`week_id`, `staff_id`, `staff_name`, `newly_qy_count`, `newly_qy_ratio`, `del_flag`, `created_by`, `created_time`)
        VALUES
        <foreach collection="list" item="item" index="index" open="" close="" separator=",">
            (
            #{item.weekId},
            #{item.staffId},
            #{item.staffName},
            #{item.newlyQyCount},
            #{item.newlyQyRatio},
            #{item.delFlag},
            #{item.createdBy},
            #{item.createdTime}
            )
        </foreach>
    </insert>

    <!--查询门店当前日期客户报告-->
    <select id="selectCurrentDateReport" resultMap="BaseResultMap">
        select id,merchant_id,week_name,qy_total_count,qy_customer_ratio,apply_count,apply_degree,newly_qy_count,newly_qy_degree,
               flow_qy_count,flow_qy_degree,total_count,newly_total_count,newly_total_degree
        from drac_data_merchant_qy_week where del_flag = 0 AND week_name = #{reportName}
        <if test="merchantIdList != null and merchantIdList.size() > 0">
            AND merchant_id in
            <foreach collection="merchantIdList" item="merchantId" separator="," open="(" close=")">
                #{merchantId}
            </foreach>
        </if>
        GROUP BY merchant_id
        UNION
        SELECT id,merchant_id,week_name,qy_total_count,qy_customer_ratio,apply_count,apply_degree,newly_qy_count,newly_qy_degree,
               flow_qy_count,flow_qy_degree,total_count,newly_total_count,newly_total_degree
        FROM drac_data_merchant_qy_month WHERE del_flag = 0 AND week_name = #{reportName}
        <if test="merchantIdList != null and merchantIdList.size() > 0">
            AND merchant_id in
            <foreach collection="merchantIdList" item="merchantId" separator="," open="(" close=")">
                #{merchantId}
            </foreach>
        </if>
        GROUP BY merchant_id
    </select>

    <!--查询已存在日期报告的商户号-->
    <select id="selectMerchantIdListByReportName" resultType="java.lang.Integer">
        SELECT merchant_id merchantId
        FROM drac_data_merchant_qy_week w
        WHERE del_flag = 0 AND w.week_name = #{reportName}
        UNION
        SELECT merchant_id merchantId
        FROM drac_data_merchant_qy_month m
        WHERE del_flag = 0 AND m.week_name = #{reportName}
    </select>

    <!--查询周报员工分布-->
    <select id="selectStaffDistributionByWeek" resultMap="StaffDOMap">
        SELECT id,week_id,staff_id,staff_name,newly_qy_count,newly_qy_ratio,created_time
        FROM drac_data_merchant_qy_week_staff
        WHERE del_flag = 0 AND week_id = #{weekId}
        ORDER BY newly_qy_count DESC
    </select>

    <!--查询月报员工分布-->
    <select id="selectStaffDistributionByMonth" resultMap="StaffDOMap">
        SELECT id,week_id week_id,staff_id,staff_name,newly_qy_count,newly_qy_ratio,created_time
        FROM drac_data_merchant_qy_month_staff
        WHERE del_flag = 0 AND week_id = #{monthId}
        ORDER BY newly_qy_count DESC
    </select>

    <!--查询集团当前日期新增企微客户数 & 新增客户数-->
    <select id="selectCurrentDateGroupReport" resultType="java.util.Map">
        SELECT SUM(newly_qy_count) newlyQyCount, SUM(newly_total_count) newlyTotalCount
        FROM drac_data_merchant_qy_week w
        WHERE w.del_flag = 0 AND w.week_name = #{reportName}
        <if test="merchantIdList != null and merchantIdList.size() > 0">
            AND merchant_id in
            <foreach collection="merchantIdList" item="merchantId" separator="," open="(" close=")">
                #{merchantId}
            </foreach>
        </if>
    </select>

    <!--查询所有商户号-->
    <select id="selectAllMerchantId" resultType="java.lang.Integer">
        SELECT merchant_id FROM entmicro_authorize
        group by merchant_id
    </select>

    <!--最早客户创建时间-->
    <select id="selectEarliestCreatedCustomerDate" resultType="cn.com.glsx.entmicro.model.CustomerStatisticModel">
        SELECT MIN(created_time) createdTime,merchant_id merchantId FROM (
            SELECT sc.created_time,esdr.relevance_merchant merchant_id FROM (scrm_customer sc
                LEFT JOIN entmicro_customer_follow ecf ON sc.id = ecf.customer_id
                LEFT JOIN entmicro_staff es ON es.id = ecf.staff_id
                LEFT JOIN entmicro_staff_depart_relation esdr ON esdr.staff_id = es.id)
        <if test="merchantIdList != null and merchantIdList.size() > 0">
            WHERE esdr.relevance_merchant IN
            <foreach collection="merchantIdList" item="merchantId" separator="," open="(" close=")">
                #{merchantId}
            </foreach>
        </if>
            UNION all
            SELECT sc.created_time,sc.merchantId merchant_id FROM scrm_customer sc
        <if test="merchantIdList != null and merchantIdList.size() > 0">
            WHERE sc.merchantId IN
            <foreach collection="merchantIdList" item="merchantId" separator="," open="(" close=")">
                #{merchantId}
            </foreach>
        </if>
        )t
        GROUP BY t.merchant_id
    </select>

    <!--查询集团旗下所有门店当前日期的企微客户总数 & 新增企微客户数 & 新增客户数-->
    <select id="selectGroupSum" resultType="java.util.Map">
        SELECT SUM(qy_total_count) sumQyTotalCount,SUM(newly_qy_count) sumNewlyQyCount,SUM(total_count) sumTotalCount,sum(newly_total_count) sumNewlyTotalCount
        FROM
            drac_data_merchant_qy_week
        WHERE del_flag = 0 AND week_name = #{reportName}
        <if test="merchantIdList != null and merchantIdList.size() > 0">
            AND merchant_id in
            <foreach collection="merchantIdList" item="merchantId" separator="," open="(" close=")">
                #{merchantId}
            </foreach>
        </if>
        GROUP BY week_name
        UNION
        SELECT SUM(qy_total_count) sumQyCotalCount,SUM(newly_qy_count) sumNewlyQyCount,SUM(total_count) sumTotalCount,sum(newly_total_count) sumNewlyTotalCount
        FROM
            drac_data_merchant_qy_month
        WHERE del_flag = 0 AND week_name = #{reportName}
        <if test="merchantIdList != null and merchantIdList.size() > 0">
            AND merchant_id in
            <foreach collection="merchantIdList" item="merchantId" separator="," open="(" close=")">
                #{merchantId}
            </foreach>
        </if>
        GROUP BY week_name
    </select>

    <!--查询集团企微客户占比最高的门店-->
    <select id="selectNewlyQyHighestRatioMerchantId" resultType="java.lang.Integer">
        SELECT merchant_id
        FROM drac_data_merchant_qy_week
        WHERE del_flag = 0 AND week_name = #{reportName}
        <if test="merchantIdList != null and merchantIdList.size() > 0">
            AND merchant_id in
            <foreach collection="merchantIdList" item="merchantId" separator="," open="(" close=")">
                #{merchantId}
            </foreach>
        </if>
        AND qy_customer_ratio =
            (SELECT MAX(qy_customer_ratio) max_qy_customer_ratio
            FROM drac_data_merchant_qy_week
            WHERE del_flag = 0 AND week_name = #{reportName}
            <if test="merchantIdList != null and merchantIdList.size() > 0">
                AND merchant_id in
                <foreach collection="merchantIdList" item="merchantId" separator="," open="(" close=")">
                    #{merchantId}
                </foreach>
            </if>
            GROUP BY week_name)
        GROUP BY week_name
        UNION
        SELECT merchant_id
        FROM drac_data_merchant_qy_month
        WHERE del_flag = 0 AND week_name = #{reportName}
        <if test="merchantIdList != null and merchantIdList.size() > 0">
            AND merchant_id in
            <foreach collection="merchantIdList" item="merchantId" separator="," open="(" close=")">
                #{merchantId}
            </foreach>
        </if>
        AND qy_customer_ratio =
            (SELECT MAX(qy_customer_ratio) max_qy_customer_ratio
            FROM drac_data_merchant_qy_month
            WHERE del_flag = 0 AND week_name = #{reportName}
            <if test="merchantIdList != null and merchantIdList.size() > 0">
                AND merchant_id in
                <foreach collection="merchantIdList" item="merchantId" separator="," open="(" close=")">
                    #{merchantId}
                </foreach>
            </if>
            GROUP BY week_name)
        GROUP BY week_name
    </select>

    <!--查询集团企微客户增长速度最快的门店-->
    <select id="selectNewlyQyFastestGrowthMerchantId" resultType="java.lang.Integer">
        SELECT merchant_id
        FROM drac_data_merchant_qy_week
        WHERE del_flag = 0 AND week_name = #{reportName}
        <if test="merchantIdList != null and merchantIdList.size() > 0">
            AND merchant_id in
            <foreach collection="merchantIdList" item="merchantId" separator="," open="(" close=")">
                #{merchantId}
            </foreach>
        </if>
          AND newly_qy_degree =
              (SELECT MAX(newly_qy_degree) max_newly_qy_degree
               FROM drac_data_merchant_qy_week
               WHERE del_flag = 0 AND week_name = #{reportName}
                <if test="merchantIdList != null and merchantIdList.size() > 0">
                    AND merchant_id in
                    <foreach collection="merchantIdList" item="merchantId" separator="," open="(" close=")">
                        #{merchantId}
                    </foreach>
                </if>
               GROUP BY week_name)
        GROUP BY week_name
        UNION
        SELECT merchant_id
        FROM drac_data_merchant_qy_month
        WHERE del_flag = 0 AND week_name = #{reportName}
        <if test="merchantIdList != null and merchantIdList.size() > 0">
            AND merchant_id in
            <foreach collection="merchantIdList" item="merchantId" separator="," open="(" close=")">
                #{merchantId}
            </foreach>
        </if>
          AND newly_qy_degree =
              (SELECT MAX(newly_qy_degree) max_newly_qy_degree
               FROM drac_data_merchant_qy_month
               WHERE del_flag = 0 AND week_name = #{reportName}
                <if test="merchantIdList != null and merchantIdList.size() > 0">
                    AND merchant_id in
                    <foreach collection="merchantIdList" item="merchantId" separator="," open="(" close=")">
                        #{merchantId}
                    </foreach>
                </if>
               GROUP BY week_name)
        GROUP BY week_name
    </select>

    <!--查询集团企微客户数最多的门店-->
    <select id="selectQyCustomerMostMerchantId" resultType="java.lang.Integer">
        SELECT merchant_id
        FROM drac_data_merchant_qy_week
        WHERE del_flag = 0 AND week_name = #{reportName}
        <if test="merchantIdList != null and merchantIdList.size() > 0">
            AND merchant_id in
            <foreach collection="merchantIdList" item="merchantId" separator="," open="(" close=")">
                #{merchantId}
            </foreach>
        </if>
        AND qy_total_count =
                (SELECT MAX(qy_total_count) max_qy_total_count
                FROM drac_data_merchant_qy_week
                WHERE del_flag = 0 AND week_name = #{reportName}
                <if test="merchantIdList != null and merchantIdList.size() > 0">
                    AND merchant_id in
                    <foreach collection="merchantIdList" item="merchantId" separator="," open="(" close=")">
                        #{merchantId}
                    </foreach>
                </if>
                GROUP BY week_name)
        GROUP BY week_name
        UNION
        SELECT merchant_id
        FROM drac_data_merchant_qy_month
        WHERE del_flag = 0 AND week_name = #{reportName}
        <if test="merchantIdList != null and merchantIdList.size() > 0">
            AND merchant_id in
            <foreach collection="merchantIdList" item="merchantId" separator="," open="(" close=")">
                #{merchantId}
            </foreach>
        </if>
        AND qy_total_count =
                (SELECT MAX(qy_total_count) max_qy_total_count
                FROM drac_data_merchant_qy_month
                WHERE del_flag = 0 AND week_name = #{reportName}
                <if test="merchantIdList != null and merchantIdList.size() > 0">
                    AND merchant_id in
                    <foreach collection="merchantIdList" item="merchantId" separator="," open="(" close=")">
                        #{merchantId}
                    </foreach>
                </if>
                GROUP BY week_name)
        GROUP BY week_name
    </select>

    <!--  查询门店企微客户占比排行  -->
    <select id="selectMerchantTopByQyCustomerRatio" resultType="cn.com.glsx.entmicro.model.response.MerchantTopResponse">
        SELECT id,merchant_id merchantId,qy_customer_ratio qyCustomerRatio,newly_qy_count newlyQyCount,newly_qy_degree newlyQyDegree FROM `drac_data_merchant_qy_week`
        WHERE del_flag = 0 AND week_name = #{reportName}
        <if test="merchantIdList != null and merchantIdList.size() > 0">
            AND merchant_id in
            <foreach collection="merchantIdList" item="merchantId" separator="," open="(" close=")">
                #{merchantId}
            </foreach>
        </if>
        GROUP BY merchant_id
        UNION
        SELECT id,merchant_id merchantId,qy_customer_ratio qyCustomerRatio,newly_qy_count newlyQyCount,newly_qy_degree newlyQyDegree FROM `drac_data_merchant_qy_month`
        WHERE del_flag = 0 AND week_name = #{reportName}
        <if test="merchantIdList != null and merchantIdList.size() > 0">
            AND merchant_id in
            <foreach collection="merchantIdList" item="merchantId" separator="," open="(" close=")">
                #{merchantId}
            </foreach>
        </if>
        GROUP BY merchant_id
        ORDER BY qyCustomerRatio DESC
    </select>

    <!--  查询门店新增企微客户数排行  -->
    <select id="selectMerchantTopByNewlyQyCount" resultType="cn.com.glsx.entmicro.model.response.MerchantTopResponse">
        SELECT id,merchant_id merchantId,newly_qy_count newlyQyCount,newly_qy_degree newlyQyDegree FROM `drac_data_merchant_qy_week`
        WHERE del_flag = 0 AND week_name = #{reportName}
        <if test="merchantIdList != null and merchantIdList.size() > 0">
            AND merchant_id in
            <foreach collection="merchantIdList" item="merchantId" separator="," open="(" close=")">
                #{merchantId}
            </foreach>
        </if>
        GROUP BY merchant_id
        UNION
        SELECT id,merchant_id merchantId,newly_qy_count newlyQyCount,newly_qy_degree newlyQyDegree FROM `drac_data_merchant_qy_month`
        WHERE del_flag = 0 AND week_name = #{reportName}
        <if test="merchantIdList != null and merchantIdList.size() > 0">
            AND merchant_id in
            <foreach collection="merchantIdList" item="merchantId" separator="," open="(" close=")">
                #{merchantId}
            </foreach>
        </if>
        GROUP BY merchant_id
        ORDER BY newlyQyCount DESC
    </select>

    <!--  流失客户数  -->
    <select id="selectFlowQyCustomerByMerchantId" resultMap="BaseResultMap">
        select esdr.relevance_merchant merchant_id,
            COUNT(DISTINCT sc.one_id) flow_qy_count
        from scrm_customer sc
        LEFT JOIN entmicro_customer_follow ecf on sc.id = ecf.customer_id
        LEFT JOIN entmicro_staff es on es.id = ecf.staff_id
        left join entmicro_staff_depart_relation esdr on esdr.staff_id = es.id
        LEFT JOIN scrm_customer_merge scm ON sc.one_id = scm.one_id
        where esdr.del_flag = 0
        <if test="merchantIdList != null and merchantIdList.size() > 0">
            AND esdr.relevance_merchant in
            <foreach collection="merchantIdList" item="merchantId" separator="," open="(" close=")">
                #{merchantId}
            </foreach>
        </if>
        <if test="startTime != null and endTime != null">
            and sc.delete_time between #{startTime} and #{endTime}
        </if>
        and sc.del_flag = -1
        AND scm.status = -1
        GROUP BY
        esdr.relevance_merchant
    </select>

    <!-- 测试用 - 查询商户下的所有oneId  -->
    <select id="selectMerchantOneIdToDataHub" resultType="java.lang.Long">
        SELECT t.oneid
        FROM (
                 SELECT scm.id, scm.one_id oneid, scm.customer_name, scm.customer_phone, scm.customer_sex, scm.is_join, scm.`type` stype, scm.created_time ADDTIME, sc.del_flag STATUS,
                        scm.add_way, sc.cust_state, dcc.code_name sourceChannel, edr.id staff_id, sc.avatar, scbr.group_nickname
                 FROM scrm_customer_merge scm
                          LEFT JOIN scrm_customer sc ON scm.one_id = sc.one_id
                          LEFT JOIN entmicro_customer_follow ecf ON ecf.customer_id = sc.id
                          LEFT JOIN entmicro_staff es ON (es.id = ecf.staff_id)
                          LEFT JOIN entmicro_staff_depart_relation edr ON (edr.staff_id = es.id)
                          LEFT JOIN scrm_customer_base_relation scbr ON scbr.customer_id = sc.id
                          LEFT JOIN (SELECT code_name,code_state FROM drac_channel_code WHERE code_system = 0) dcc ON sc.cust_state = dcc.code_state
                 WHERE 1=1 AND edr.del_flag=0
                   AND edr.relevance_merchant IN ( #{merchantId} )
                   AND scm.merchant_id IN ( #{merchantId} )
                   AND scm.del_flag = 0
                 UNION ALL
                 SELECT scm.id, scm.one_id oneid, scm.customer_name, scm.customer_phone, scm.customer_sex, scm.is_join, scm.`type` stype, scm.created_time ADDTIME, sc.del_flag STATUS,
                        scm.add_way, sc.cust_state, dcc.code_name sourceChannel, NULL staff_id, sc.avatar, scbr.group_nickname FROM scrm_customer_merge scm
                            LEFT JOIN scrm_customer sc ON scm.one_id = sc.one_id
                            LEFT JOIN scrm_customer_base_relation scbr ON scbr.customer_id = sc.id
                            LEFT JOIN (SELECT code_name,code_state FROM drac_channel_code WHERE code_system = 0) dcc ON sc.cust_state = dcc.code_state
                 WHERE sc.merchantId IN ( #{merchantId} )
                   AND scm.merchant_id IN ( #{merchantId} )
                   AND scm.del_flag = 0
             )t
        GROUP BY t.oneid ORDER BY t.addTime DESC
    </select>

    <!-- 测试用 - 查询所有商户号   -->
    <select id="selectAllMerchantIdToDataHub" resultType="java.lang.Long">
        SELECT t.merchant_id FROM (
              SELECT relevance_merchant merchant_id FROM entmicro_staff_depart_relation
              GROUP BY relevance_merchant
              UNION
              SELECT merchantId merchant_id FROM scrm_customer
              GROUP BY merchantId
          )t GROUP BY t.merchant_id
    </select>

    <!-- 客户数截止时间新增 -->
    <select id="getCustomerDateTimeCount" resultType="java.lang.Integer">
        select sum(comment.cou) from
        (select count(DISTINCT sc.one_id) cou from (scrm_customer sc
        LEFT JOIN entmicro_customer_follow ecf on sc.id = ecf.customer_id
        LEFT JOIN entmicro_staff es on es.id = ecf.staff_id
        left join entmicro_staff_depart_relation esdr on esdr.staff_id = es.id)
        where esdr.del_flag = 0 and esdr.relevance_merchant in
        <foreach collection="merchantIds" item="merchantId" separator="," open="(" close=")">
            #{merchantId}
        </foreach>
        and DATE_FORMAT(sc.created_time,'%Y/%m/%d') = #{dateTime}
        UNION
        select count(DISTINCT sc.one_id) cou from scrm_customer sc
        where
        sc.user_id is null and
        sc.merchantId in
        <foreach collection="merchantIds" item="merchantId" separator="," open="(" close=")">
            #{merchantId}
        </foreach>
        and DATE_FORMAT(sc.created_time,'%Y/%m/%d') <![CDATA[<=]]> #{dateTime})comment
    </select>
</mapper>