CustomerBaseMapper.xml 33.3 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 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
<?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.CustomerBaseMapper">
    <resultMap id="BaseResultMap" type="cn.com.glsx.entmicro.entity.CustomerBase">
        <!--
          WARNING - @mbg.generated
        -->
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="chat_id" jdbcType="VARCHAR" property="chatId"/>
        <result column="lord" jdbcType="VARCHAR" property="lord"/>
        <result column="created_by" jdbcType="INTEGER" property="createdBy"/>
        <result column="created_time" jdbcType="TIMESTAMP" property="createdTime"/>
        <result column="updated_by" jdbcType="INTEGER" property="updatedBy"/>
        <result column="updated_time" jdbcType="TIMESTAMP" property="updatedTime"/>
        <result column="del_flag" jdbcType="TINYINT" property="delFlag"/>
        <result column="merchant_Id" jdbcType="INTEGER" property="merchantid"/>
        <result column="chat_name" jdbcType="VARCHAR" property="chatName"/>
        <result column="chat_manage" jdbcType="VARCHAR" property="chatManage"/>
    </resultMap>

    <resultMap id="CustomerGroupListMap" type="cn.com.glsx.entmicro.model.response.CustomerBaseCustListResponse">
        <id column="one_id" property="id"/>
        <id column="customer_id" property="customerId" jdbcType="INTEGER"/>
        <result column="group_nickname" property="groupNickName" jdbcType="VARCHAR"/>
        <result column="customer_name" property="customerName" jdbcType="VARCHAR"/>
        <result column="customer_phone" property="customerPhone" jdbcType="VARCHAR"/>
        <result column="customer_sex" property="customerSex" jdbcType="TINYINT"/>
        <result column="join_scene" property="joinScene" jdbcType="TINYINT"/>
        <result column="join_time" property="joinTime" jdbcType="TIMESTAMP"/>
        <result column="type" property="type"/>
        <result column="invitor" property="invitor" jdbcType="INTEGER"/>
        <result column="invitor_name" property="invitorName" jdbcType="VARCHAR"/>
        <result column="chat_name" property="chatName" jdbcType="VARCHAR"/>
        <result column="lord" property="lord" jdbcType="VARCHAR"/>
        <result column="reationMethod" property="reationMethod" jdbcType="VARCHAR"/>
        <result column="avatar" property="avatar" jdbcType="VARCHAR"/>
        <result column="isMyCustomer" property="isMyCustomer" jdbcType="INTEGER"/>
        <result column="car_number" property="carNumber" jdbcType="VARCHAR"/>
        <result column="user_id" property="userId" jdbcType="VARCHAR"/>
    </resultMap>

    <resultMap id="resultMap" type="cn.com.glsx.entmicro.model.response.CustomerBaseInfoResponse">
        <!--
          WARNING - @mbg.generated
        -->
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="chat_id" jdbcType="VARCHAR" property="chatId"/>
        <result column="chat_name" jdbcType="VARCHAR" property="groupName"/>
        <result column="lord" jdbcType="VARCHAR" property="lord"/>
        <result column="customer_id" property="customerId" jdbcType="INTEGER"/>
        <result column="group_nickname" property="groupNickName" jdbcType="VARCHAR"/>
        <result column="customer_name" property="customerName" jdbcType="VARCHAR"/>
        <result column="customer_phone" property="customerPhone" jdbcType="VARCHAR"/>
        <result column="user_id" property="userId" jdbcType="VARCHAR"/>
    </resultMap>


    <sql id="Base_Column_List">
        <!--
          WARNING - @mbg.generated
        -->
        id, chat_id, lord, created_by, created_time, updated_by, updated_time, del_flag,
        merchant_Id, chat_name, chat_manage, creation_method
    </sql>


    <select id="getCustomerBaseByPage" parameterType="cn.com.glsx.entmicro.model.request.CustomerBaseSearchRequest"
            resultType="cn.com.glsx.entmicro.model.response.CustomerBaseListResponse">
        SELECT scb.id,
        scb.chat_id chatId,
        es.staff_name groupHostName,
        scb.created_time addTime,
        scb.chat_name custGroupName,
        scb.chat_manage groupAdminName,
        scb.merchant_id merchantId,
        scb.creation_method creationMethod,
        scb.del_flag delFlag
        FROM scrm_customer_base scb
        LEFT JOIN entmicro_staff es on scb.lord = es.user_id
        LEFT join scrm_customer_base_relation sbr on (sbr.type=1 and sbr.customer_base_id=scb.id)
        left join entmicro_staff esa on sbr.customer_id=esa.id
        left join entmicro_staff_depart_relation esdr on esdr.staff_id = esa.id and esdr.del_flag = 0
        WHERE
        <if test="datalist != null and  datalist.size>0">
            esdr.relevance_merchant in
            <foreach collection="datalist" index="index" open="(" close=")" separator="," item="id">
                #{id}
            </foreach>
        </if>
        <if test="chatIdList != null">
            and scb.chat_id in
            <foreach collection="chatIdList" index="index" open="(" close=")" separator="," item="id">
                #{id}
            </foreach>
        </if>
        <if test="delFlag != null">
            and scb.del_flag = #{delFlag}
        </if>
        <!--开始时间-->
        <if test="beginTime != null">
            and scb.created_time <![CDATA[>=]]> #{beginTime,jdbcType=DATE}
        </if>
        <!--结束时间-->
        <if test="endTime != null">
            and scb.created_time <![CDATA[<=]]> #{endTime,jdbcType=DATE}
        </if>
        <!--客户群名称-->
        <if test="custGroupName != null and custGroupName !=''">
            AND scb.chat_name like '%${custGroupName}%'
        </if>
        <!--群主-->
        <if test="groupHostName != null and groupHostName !=''">
            AND es.staff_name like '%${groupHostName}%'
        </if>
        <!--管理员-->
        <if test="groupAdminName != null and groupAdminName !=''">
            AND scb.chat_manage like '%${groupAdminName}%'
        </if>
        <if test="userType != null and userType == 2">
            and esdr.user_oneid=#{uuid}
        </if>
        <if test="creationMethod != null">
            and scb.creation_method = #{creationMethod}
        </if>
        group by scb.chat_id
        ORDER BY addTime DESC
    </select>

    <!--入群客户-->
    <select id="getTodayCustomer" parameterType="cn.com.glsx.entmicro.model.request.CustomerBaseSearchRequest"
            resultType="cn.com.glsx.entmicro.model.response.GetCustomerTotalResponse">
        SELECT scb.chat_name chatName,
               scb.merchant_id merchantId,
               (case when scbr.total is null then 0 else scbr.total end) total,
               scb.id customerBaseId,
               scb.chat_id chatId,
               esdr.relevance_merchant
        from scrm_customer_base scb
                 LEFT JOIN (SELECT t.*
                            from (SELECT COUNT(*) total,customer_base_id
                                  from scrm_customer_base_relation
                                  WHERE DATEDIFF(join_time, NOW()) = 0 and del_flag = 0 and type = 2
                                  GROUP BY customer_base_id) t) scbr
                           on scbr.customer_base_id = scb.id
                 LEFT JOIN entmicro_staff es on es.user_id = scb.lord
                 LEFT JOIN entmicro_staff_depart_relation esdr on (esdr.staff_id = es.id and esdr.del_flag = 0)
        WHERE scb.del_flag = 0
          and esdr.relevance_merchant= #{merchantId}
        GROUP BY scb.id
    </select>

    <!--退群客户-->
    <select id="todayRemoveList" parameterType="cn.com.glsx.entmicro.model.request.CustomerBaseSearchRequest"
            resultType="cn.com.glsx.entmicro.model.response.GetCustomerTotalResponse">
        SELECT scb.chat_name chatName,
               scb.merchant_id merchantId,
               (case when scbr.total is null then 0 else scbr.total end) total,
               scb.id customerBaseId,
               scb.chat_id chatId,
               esdr.relevance_merchant
        from scrm_customer_base scb
                 LEFT JOIN (SELECT t.*
                            from (SELECT COUNT(DISTINCT customer_id) total,customer_base_id
                                  from scrm_customer_base_relation
                                  WHERE DATEDIFF(quit_time, NOW()) = 0 and del_flag = -1 and type = 2
                                  GROUP BY customer_base_id) t) scbr
                           on scbr.customer_base_id = scb.id
                 LEFT JOIN entmicro_staff es on es.user_id = scb.lord
                 LEFT JOIN entmicro_staff_depart_relation esdr on (esdr.staff_id = es.id and esdr.del_flag = 0)
        WHERE scb.del_flag = 0
          and esdr.relevance_merchant= #{merchantId}
        GROUP BY scb.id
    </select>

    <select id="getByChatId" resultMap="BaseResultMap">
        select *
        from scrm_customer_base
        where chat_id = #{chatId}
          and del_flag = 0
    </select>


    <select id="getCustomerBaseInfo" resultType="cn.com.glsx.entmicro.model.response.CustomerBaseInfoResponse">
        select es.staff_name groupHostName,
        es.staff_email groupHostEmail,
        es.staff_phone groupHostPhone,
        GROUP_CONCAT(DISTINCT edr.department_name) groupHostDepart,
        (case when scb.chat_name = "" || scb.chat_name is null
        then "群聊(默认)" else scb.chat_name end) groupName,
        scb.notice notice,
        scb.chat_manage groupAdmin,
        scb.created_time createDate
        from scrm_customer_base scb
        left join entmicro_staff es on es.user_id = scb.lord
        left join (select esdr.staff_id,ed.department_name from entmicro_staff_depart_relation esdr
        left join entmicro_department ed on esdr.department_id = ed.id where esdr.del_flag = 0 and
        esdr.relevance_merchant in
        <foreach collection="authorizeMerchants" index="authorizeMerchant" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>)edr on edr.staff_id = es.id
        where scb.id = #{customerBaseId}
        and scb.del_flag = 0
        group by scb.id
    </select>

    <select id="getGroupCustomerByPage" parameterType="cn.com.glsx.entmicro.model.request.CustBaseCustSearchRequest"
            resultMap="CustomerGroupListMap">
        SELECT t.*
        from
        (SELECT
        scbr.id,
        sc.one_id,
        (CASE WHEN scbr.group_nickname = "" OR scbr.group_nickname IS NULL
        THEN sc.customer_name ELSE scbr.group_nickname END) AS group_nickname,
        sc.customer_name,
        sc.customer_phone,
        sc.customer_sex,
        scbr.join_scene,
        scbr.join_time,
        es.staff_name invitor_name,
        sc.type,
        scb.chat_name,
        scb.lord
        FROM scrm_customer sc
        LEFT JOIN scrm_customer_base_relation scbr ON sc.id = scbr.customer_id
        LEFT JOIN `entmicro_staff` es ON es.id = scbr.invitor AND es.del_flag != -1
        LEFT JOIN `entmicro_customer_follow` ecf ON ecf.customer_id = sc.id
        LEFT JOIN `entmicro_staff` es1 ON ecf.staff_id = es1.id
        LEFT JOIN `entmicro_staff_depart_relation` esdr ON es1.id = esdr.staff_id
        LEFT JOIN scrm_customer_base scb ON scb.id = scbr.customer_base_id
        WHERE 1=1
        <if test="id != null">
            and scbr.customer_base_id = #{id}
        </if>
        <if test="datalist != null and  datalist.size>0">
            and esdr.relevance_merchant in
            <foreach collection="datalist" index="index" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
        AND scbr.del_flag = 0
        AND sc.del_flag IN (0,-1)
        AND scbr.type = 2
        <!--开始时间-->
        <if test="beginTime != null">
            and scbr.join_time <![CDATA[>=]]> #{beginTime,jdbcType=DATE}
        </if>
        <!--结束时间-->
        <if test="endTime != null">
            and scbr.join_time <![CDATA[<=]]> #{endTime,jdbcType=DATE}
        </if>
        <!--性别-->
        <if test="customerSex != null">
            and sc.customer_sex = #{customerSex}
        </if>
        <if test="customerPhone != null and customerPhone !=''">
            AND sc.customer_phone like '%${customerPhone}%'
        </if>
        <!--客户名称-->
        <if test="customerName != null and customerName !=''">
            AND sc.customer_name like '%${customerName}%'
        </if>
        GROUP BY scbr.id
        order by scbr.join_time desc, scbr.id desc) t
        <!--本群昵称-->
        <if test="groupNickName != null and groupNickName !=''">
            where t.group_nickname like '%${groupNickName}%'
        </if>
    </select>

    <select id="findAllChant" resultMap="BaseResultMap">
        select * from scrm_customer_base where del_flag = 0
        and merchant_Id=#{merchantId}
    </select>

    <select id="findAllChantByUserIds" resultMap="BaseResultMap">
        select * from scrm_customer_base where del_flag = 0
        and merchant_Id=#{merchantId}
        <if test="userIds!=null and userIds.size>0">
            and lord IN
            <foreach collection="userIds" open="(" close=")" separator="," item="item">
                #{item,jdbcType=VARCHAR}
            </foreach>
        </if>
    </select>

    <select id="getByUserId" resultType="cn.com.glsx.entmicro.entity.MassChatDetail">
        SELECT t.id id, t.lord userId, t.chat_id chatId, t.chat_name chatName,
        s.staff_name userName, d.id deptId, d.department_name deptName
        FROM( SELECT * FROM scrm_customer_base WHERE del_flag = 0 AND lord IN
        <foreach collection="userIdList" open="(" close=")" separator="," item="item">
            #{item,jdbcType=VARCHAR}
        </foreach>
        ) t LEFT JOIN `entmicro_staff` s ON t.lord = s.user_id
        LEFT JOIN `entmicro_department` d ON s.department_id = d.id
    </select>
    <select id="findCustBaseByContion"
            resultMap="resultMap">
        select t1.* from scrm_customer_base t1
        LEFT JOIN scrm_customer_base_relation t2 ON t1.id = t2.customer_base_id
        LEFT JOIN entmicro_staff t3 ON t3.id = t2.customer_id
        LEFT JOIN entmicro_staff_depart_relation t4 ON t3.id = t4.staff_id
        where 1=1
        <if test="custBaseSearchRequest.ids!=null and custBaseSearchRequest.ids.size>0">
            AND t1.id in
            <foreach collection="custBaseSearchRequest.ids" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
        <if test="custBaseSearchRequest.chantIds!=null and custBaseSearchRequest.chantIds.size>0">
            AND t1.chat_id in
            <foreach collection="custBaseSearchRequest.chantIds" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
        <if test="custBaseSearchRequest.customerBaseName!=null and custBaseSearchRequest.customerBaseName">
            AND t1.chat_name like ('%',custBaseSearchRequest.customerBaseName,'%')
        </if>
        AND t4.relevance_merchant=#{custBaseSearchRequest.merchantId}
        AND t1.del_flag = 0
        GROUP BY t1.id
    </select>

    <select id="findCusBaseByMerchantId" resultMap="resultMap">
        SELECT t1.id,
            t1.chat_id,
            t1.lord as lord,
            (case when t1.chat_name = "" || t1.chat_name is null
            then "群聊(默认)" else t1.chat_name end) chat_name
        from scrm_customer_base t1
            LEFT JOIN scrm_customer_base_relation t2 on t1.id = t2.customer_base_id
            LEFT JOIN entmicro_staff t3 on t3.id = t2.customer_id
            LEFT JOIN entmicro_staff_depart_relation t4 on t3.id = t4.staff_id
        where t4.relevance_merchant = #{merchantId}
	        and t1.del_flag = 0
            GROUP BY t1.id
    </select>

    <select id="findCusBaseByStaffIds" resultMap="resultMap">
        SELECT t1.id,
        t1.chat_id,
        (case when t1.chat_name = "" || t1.chat_name is null
        then "群聊(默认)" else t1.chat_name end) chat_name
        from scrm_customer_base t1
        LEFT JOIN scrm_customer_base_relation t2 on t1.id = t2.customer_base_id
        LEFT JOIN entmicro_staff t3 on t3.id = t2.customer_id
        LEFT JOIN entmicro_staff_depart_relation t4 on t3.id = t4.staff_id
        where t4.relevance_merchant = #{merchantId}
        <if test="staffIds != null and staffIds.size() > 0">
            and t3.id in
            <foreach collection="staffIds" open="(" close=")" item="id" separator=",">
                #{id}
            </foreach>
        </if>
        and t1.del_flag = 0
        GROUP BY t1.id
    </select>

    <select id="getMassChatItemEntityInfoByIds"
            resultType="cn.com.glsx.entmicro.entity.mass.MassChatItemEntity">
        SELECT
        b.`id` AS staffId,
        b.`chat_name` AS groupName,
        COUNT(1) AS customerCount
        FROM
        `scrm_customer_base` b
        LEFT JOIN `scrm_customer_base_relation` r
        ON r.`customer_base_id` = b.`id`
        WHERE b.`id` IN
        <foreach collection="ids" open="(" separator="," close=")" item="id">
            #{id}
        </foreach>
        AND r.`type`=2
        AND r.`del_flag` = 0
        GROUP BY b.`id` ;
    </select>

    <!-- 成员总数 -->
    <select id="getAllMembers" parameterType="cn.com.glsx.entmicro.model.request.CustomerBaseSearchRequest"
            resultType="cn.com.glsx.entmicro.model.response.GetCustomerTotalResponse">
        SELECT (case when COUNT(chat_id) is null then 0 else COUNT(chat_id) end) total,
               chat_id chatId
        FROM scrm_customer_base scb
            LEFT JOIN scrm_customer_base_relation scbr
            ON scb.id = scbr.customer_base_id
        where scbr.del_flag = 0 AND scb.del_flag = 0
        <if test="idList != null and idList.size() > 0">
            and scbr.customer_base_id in
            <foreach collection="idList" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
        GROUP BY scb.id
    </select>

    <!-- 群跟进员工数 -->
    <select id="getStaffs" resultType="cn.com.glsx.entmicro.model.response.GetCustomerTotalResponse"
            parameterType="cn.com.glsx.entmicro.model.request.CustomerBaseSearchRequest">
        SELECT (case when COUNT(chat_id) is null then 0 else COUNT(chat_id) end) total,
            chat_id chatId,
            scb.id
        FROM scrm_customer_base scb
                 LEFT JOIN scrm_customer_base_relation scbr ON scb.id = scbr.customer_base_id
                 LEFT JOIN entmicro_staff es ON scbr.customer_id = es.id
        where scbr.del_flag = 0
            AND scb.del_flag = 0
            AND type = 1
            AND es.del_flag = 0
        <if test="idList != null and idList.size() > 0">
            and scb.id in
            <foreach collection="idList" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
        GROUP BY scb.id
    </select>

    <!-- 一客一群的客群 -->
    <select id="getGroupOneChatIdList" resultType="java.lang.String">
        SELECT chat_id chatId FROM scrm_group_one WHERE del_flag = 0
    </select>

    <!-- 客群员工列表 -->
    <select id="getFollowStaffByPage" parameterType="cn.com.glsx.entmicro.model.request.CustBaseCustSearchRequest"
            resultType="cn.com.glsx.entmicro.model.response.CustomerBaseStaffListResponse">
        SELECT es.`id`,
        es.`staff_name` staffName,
        scbr.`group_nickname` groupNickName,
        es.`staff_phone` staffPhone,
        es.`staff_sex` staffSex,
        scbr.`join_scene` joinScene,
        es2.`staff_name` invitorName,
        scbr.`join_time` joinTime,
        1 type
        FROM scrm_customer_base_relation scbr
        LEFT JOIN entmicro_staff es ON scbr.`customer_id` = es.`id`
        LEFT JOIN entmicro_staff es2 ON scbr.`invitor` = es2.`id`
        WHERE scbr.`del_flag` = 0 AND es.`del_flag` = 0 AND scbr.`type` = 1
        AND scbr.`customer_base_id` = #{id}
        <!--开始时间-->
        <if test="beginTime != null">
            and scbr.join_time <![CDATA[>=]]> #{beginTime,jdbcType=DATE}
        </if>
        <!--结束时间-->
        <if test="endTime != null">
            and scbr.join_time <![CDATA[<=]]> #{endTime,jdbcType=DATE}
        </if>
        <!--性别-->
        <if test="customerSex != null">
            and es.`staff_sex` = #{customerSex}
        </if>
        <!--客户名称-->
        <if test="customerName != null and customerName !=''">
            AND es.`staff_name` like '%${customerName}%'
        </if>
        <!--本群昵称-->
        <if test="groupNickName != null and groupNickName !=''">
            and scbr.group_nickname like '%${groupNickName}%'
        </if>
        order by scbr.join_time desc
    </select>

    <!-- 潜在客户列表 -->
    <select id="getPotentialCustomerByPage" parameterType="cn.com.glsx.entmicro.model.request.CustBaseCustSearchRequest"
            resultType="cn.com.glsx.entmicro.model.response.CustomerBaseCustListResponse">
        SELECT scbr.customer_id id,
        scbr.group_nickname groupNickName,
        scbr.invitor,
        scbr2.group_nickname invitorName,
        scbr.join_scene joinScene,
        scbr.join_time joinTime,
        sc.type type
        FROM scrm_customer_base_relation scbr
            LEFT JOIN (SELECT sc.id customer_id ,ecf.id
            FROM `scrm_customer` sc
            LEFT JOIN `entmicro_customer_follow` ecf ON ecf.customer_id = sc.id
            GROUP BY sc.id) ecf
        ON scbr.customer_id = ecf.customer_id
        LEFT JOIN scrm_customer_base_relation scbr2 ON scbr.invitor = scbr2.customer_id
        left join scrm_customer sc on sc.id = scbr.customer_id
        WHERE scbr.del_flag = 0 AND scbr.type = 2 AND ecf.id IS NULL
        AND scbr.customer_base_id = #{id}
        <!--开始时间-->
        <if test="beginTime != null">
            and scbr.join_time <![CDATA[>=]]> #{beginTime,jdbcType=DATE}
        </if>
        <!--结束时间-->
        <if test="endTime != null">
            and scbr.join_time <![CDATA[<=]]> #{endTime,jdbcType=DATE}
        </if>
        <!--性别-->
        <if test="customerSex != null">
            and sc.`customer_sex` = #{customerSex}
        </if>
        <!--本群昵称-->
        <if test="groupNickName != null and groupNickName !=''">
            and scbr.group_nickname like '%${groupNickName}%'
        </if>
        GROUP BY scbr.customer_id
        ORDER BY scbr.join_time DESC
    </select>

    <!-- 退群客户列表 -->
    <select id="getQuitCustomerByPage" parameterType="cn.com.glsx.entmicro.model.request.CustBaseCustSearchRequest"
            resultType="cn.com.glsx.entmicro.model.response.CustomerBaseCustListResponse">
        SELECT scbr.customer_id id,
        sc.one_id oneId,
        sc.customer_name customerName,
        scbr.group_nickname groupNickName,
        sc.customer_phone customerPhone,
        sc.customer_sex customerSex,
        scbr.join_scene joinScene,
        scbr.group_nickname invitorName,
        scbr.join_time joinTime,
        scbr.quit_time quitTime,
        sc.type type
        FROM scrm_customer_base_relation scbr
        LEFT JOIN scrm_customer sc ON scbr.customer_id = sc.id
        LEFT JOIN scrm_customer_base_relation scbr2 ON scbr.invitor = scbr2.customer_id
        LEFT JOIN `entmicro_customer_follow` ecf ON ecf.customer_id = sc.id
        LEFT JOIN `entmicro_staff` es1 ON ecf.staff_id = es1.id
        LEFT JOIN `entmicro_staff_depart_relation` esdr ON es1.id = esdr.staff_id
        WHERE scbr.del_flag = -1
        AND scbr.type = 2 AND sc.del_flag = 0
        AND scbr.customer_base_id = #{id}
        <if test="datalist != null and  datalist.size>0">
            and esdr.relevance_merchant in
            <foreach collection="datalist" index="index" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
        <!--开始时间-->
        <if test="beginTime != null">
            and scbr.join_time <![CDATA[>=]]> #{beginTime,jdbcType=DATE}
        </if>
        <!--结束时间-->
        <if test="endTime != null">
            and scbr.join_time <![CDATA[<=]]> #{endTime,jdbcType=DATE}
        </if>
        <!--性别-->
        <if test="customerSex != null">
            and sc.customer_sex = #{customerSex}
        </if>
        <!--客户名称-->
        <if test="customerName != null and customerName !=''">
            AND sc.customer_name like '%${customerName}%'
        </if>
        <!--本群昵称-->
        <if test="groupNickName != null and groupNickName !=''">
            and scbr.group_nickname like '%${groupNickName}%'
        </if>
        GROUP BY scbr.customer_id
        ORDER BY scbr.quit_time DESC
    </select>

    <!--今天退群列表-->
    <select id="getTodayRemoveList" resultType="cn.com.glsx.entmicro.model.response.StatisticsCustomerBaseInfoResponse">
        SELECT id as id,
               customer_base_id as customerBaseId,
               customer_id as customerId
        FROM scrm_customer_base_relation
        WHERE DATEDIFF(quit_time, NOW()) = 0 AND del_flag = -1 AND TYPE = 2
        <if test="idList != null and idList.size() > 0">
            and customer_base_id in
            <foreach collection="idList" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
    </select>

    <!--今天退群列表-->
    <select id="getAllRemoveList" resultType="cn.com.glsx.entmicro.model.response.StatisticsCustomerBaseInfoResponse">
        SELECT id as id,
               customer_base_id as customerBaseId,
               customer_id as customerId
        FROM scrm_customer_base_relation
        WHERE del_flag = -1 AND TYPE = 2
        <if test="idList != null and idList.size() > 0">
            and customer_base_id in
            <foreach collection="idList" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
    </select>

    <!--今天入群列表-->
    <select id="getTodayAddList" resultType="cn.com.glsx.entmicro.model.response.StatisticsCustomerBaseInfoResponse">
        SELECT id as id,
               customer_base_id as customerBaseId,
               customer_id as customerId
        FROM scrm_customer_base_relation
        WHERE DATEDIFF(join_time, NOW()) = 0 AND del_flag = 0 AND TYPE = 2
        <if test="idList != null and idList.size() > 0">
            and customer_base_id in
            <foreach collection="idList" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
    </select>

    <!--群全部客户列表-->
    <select id="getCustomerByGroup"
            resultMap="CustomerGroupListMap">
        SELECT t.*
        from
        (SELECT
        sc.id customer_id,
        sc.one_id,
        (CASE WHEN scbr.group_nickname = "" OR scbr.group_nickname IS NULL
        THEN sc.customer_name ELSE scbr.group_nickname END) AS group_nickname,
        sc.customer_name,
        sc.customer_phone,
        sc.customer_sex,
        scbr.join_scene,
        scbr.join_time,
        sc.type,
        sc.avatar,
        scb.chat_name,
        scb.lord,
        if(scb.reation_method=1,"手动拉群","一客一群") as reationMethod,
        scd.car_number,
        sc.user_id,
        -1 isMyCustomer
        FROM scrm_customer sc
        LEFT JOIN scrm_customer_base_relation scbr ON sc.id = scbr.customer_id
        LEFT JOIN `entmicro_customer_follow` ecf ON scbr.customer_id = ecf.customer_id
        LEFT JOIN `entmicro_staff` es1 ON es1.id = ecf.staff_id
        LEFT JOIN `entmicro_staff_depart_relation` esdr ON es1.id = esdr.staff_id AND esdr.del_flag = 0
        LEFT JOIN scrm_customer_base scb ON scb.id = scbr.customer_base_id
        LEFT JOIN `scrm_customer_detail` scd ON sc.id = scd.customer_id AND scd.del_flag = 0
        WHERE 1=1
        <if test="id != null">
            AND scbr.customer_base_id = #{id}
        </if>
        AND scbr.del_flag = 0
        AND sc.del_flag IN (0,-1)
        AND scbr.type = 2
        <!--开始时间-->
        <if test="beginTime != null">
            and scbr.join_time <![CDATA[>=]]> #{beginTime,jdbcType=DATE}
        </if>
        <!--结束时间-->
        <if test="endTime != null">
            and scbr.join_time <![CDATA[<=]]> #{endTime,jdbcType=DATE}
        </if>
        <!--性别-->
        <if test="customerSex != null">
            and sc.customer_sex = #{customerSex}
        </if>
        <!--客户名称-->
        <if test="customerName != null and customerName !=''">
            AND sc.customer_name like '%${customerName}%'
        </if>
        <if test="customerPhone != null and customerPhone !=''">
            AND sc.customer_phone like '%${customerPhone}%'
        </if>
        GROUP BY scbr.id
        order by scbr.join_time desc) t
        <where>
            <!--本群昵称-->
            <if test="groupNickName != null and groupNickName !=''">
                t.group_nickname like CONCAT('%',#{groupNickName},'%')
            </if>
            <if test = "keyword != null and keyword != ''">
                AND CONCAT(IFNULL(t.customer_name,''),IFNULL(t.car_number,''),IFNULL(t.customer_phone, ''))
                LIKE CONCAT('%',#{keyword},'%')
            </if>
        </where>
    </select>

    <!-- 客群员工列表 -->
    <select id="getBaseFollowStaffList" parameterType="cn.com.glsx.entmicro.model.request.CustBaseCustSearchRequest"
            resultType="cn.com.glsx.entmicro.model.response.CustomerBaseStaffListResponse">
        SELECT es.`id`,
        es.`staff_name` staffName,
        IF (scbr.`group_nickname` IS NULL OR scbr.`group_nickname` = '',
        es.`staff_name`,scbr.`group_nickname`) AS groupNickName,
        es.thumb_avatar thumbAvatar,
        scbr.`join_time` joinTime,
        es.user_id userId,
        1 type ,
        IF(scb.lord = es.user_id,0,-1) AS isLord
        FROM scrm_customer_base_relation scbr
        LEFT JOIN entmicro_staff es ON scbr.`customer_id` = es.`id`
        LEFT JOIN entmicro_staff es2 ON scbr.`invitor` = es2.`id`
        LEFT JOIN `scrm_customer_base` scb ON scb.id = scbr.customer_base_id
        WHERE scbr.`del_flag` = 0
        AND es.`del_flag` = 0
        AND scbr.`type` = 1
        AND scbr.`customer_base_id` = #{id}
        <!--员工名称-->
        <if test="customerName != null and customerName !=''">
            AND es.`staff_name` like concat('%',#{customerName},'%')
        </if>
        GROUP BY es.id
        order by scbr.join_time desc
    </select>

    <select id="findByChatId" resultMap="resultMap">
        select id,
        chat_id,
        lord,
        chat_name
        from scrm_customer_base
        where chat_id = #{chatId}
        group by chat_id
    </select>

    <!--获取客户群客户人数-->
    <select id="countAllBaseCustomerNum" resultType="cn.com.glsx.entmicro.model.response.GetCustomerTotalResponse">
        SELECT COUNT(id) as total,
            customer_base_id as customerBaseId
        from scrm_customer_base_relation
        where del_flag = 0 and type = 2
        <if test="idList != null and idList.size() > 0">
            and customer_base_id in
            <foreach collection="idList" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
        GROUP BY customer_base_id
    </select>


    <select id="getCustomerByChatIds" resultMap="resultMap">
        SELECT
        scbr.customer_id customer_id,
        sc.customer_name customerName,
        scbr.group_nickname groupNickName,
        sc.customer_phone customerPhone,
        sc.customer_sex customerSex,
        scbr.join_scene joinScene,
        scbr.group_nickname invitorName,
        scbr.join_time joinTime,
        scbr.quit_time quitTime,
        sc.type type,
        scb.chat_id,
        scb.chat_name,
        sc.user_id
        FROM scrm_customer_base scb
        LEFT JOIN scrm_customer_base_relation scbr ON scb.id = scbr.customer_base_id
        LEFT JOIN scrm_customer sc ON scbr.customer_id = sc.id
        WHERE scbr.del_flag =0
        AND scbr.type = 2 AND sc.del_flag = 0
        <if test="chatIds != null and chatIds.size() > 0">
           and scb.chat_id in
            <foreach collection="chatIds" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
    </select>
    <select id="findGroupOneByFirstCustomer" parameterType="cn.com.glsx.entmicro.model.request.CustGroupOneSearchRequest"
            resultType="cn.com.glsx.entmicro.model.response.CustomerBaseListResponse">
        SELECT scb.id,
               scb.chat_id chatId,
               scb.created_time addTime,
               scb.chat_name custGroupName,
               scb.chat_manage groupAdminName,
               scb.merchant_id merchantId,
               scb.creation_method creationMethod,
               scb.del_flag delFlag
        from scrm_customer_base_relation as scbr
        inner join scrm_customer as sc on sc.id = scbr.customer_id
        inner join scrm_customer_base as scb on scb.id = scbr.customer_base_id
        <where>
            sc.del_flag = 0 and scb.del_flag = 0
            and scbr.id in (
                select SUBSTRING_INDEX(GROUP_CONCAT(id ORDER BY `join_time` asc), ',', 1)
                from scrm_customer_base_relation
                where del_flag = 0 and type = 2 and quit_scene is null group by customer_base_id
            )
            <if test="creationMethod != null">
                and scb.creation_method = #{creationMethod}
            </if>
            <if test="customerPhone != null">
                and sc.custome_phone = #{customerPhone}
            </if>
            <if test="userId != null">
                and sc.user_id = #{userId}
            </if>
        </where>
    </select>
</mapper>