CustomerMapper.xml 45.8 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 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
<?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.CustomerMapper">
    <resultMap id="BaseResultMap" type="cn.com.glsx.entmicro.entity.Customer">
        <!--
          WARNING - @mbg.generated
        -->
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="customer_name" jdbcType="VARCHAR" property="customerName"/>
        <result column="customer_phone" jdbcType="VARCHAR" property="customerPhone"/>
        <result column="user_id" jdbcType="VARCHAR" property="userid"/>
        <result column="customer_sex" jdbcType="TINYINT" property="customerSex"/>
        <result column="type" jdbcType="TINYINT" property="type"/>
        <result column="position" jdbcType="VARCHAR" property="position"/>
        <result column="corp_name" jdbcType="VARCHAR" property="corpName"/>
        <result column="corp_full_name" jdbcType="VARCHAR" property="corpFullName"/>
        <result column="account_id" jdbcType="INTEGER" property="accountId"/>
        <result column="one_id" jdbcType="VARCHAR" property="oneId"/>
        <result column="avatar" jdbcType="VARCHAR" property="avatar"/>
        <result column="wechat_id" jdbcType="VARCHAR" property="wechatId"/>
        <result column="merchantId" jdbcType="INTEGER" property="merchantid"/>
        <result column="created_by" jdbcType="INTEGER" property="createdBy"/>
        <result column="created_time" jdbcType="TIMESTAMP" property="createdTime"/>
        <result column="delete_time" jdbcType="TIMESTAMP" property="deleteTime"/>
        <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="one_id" jdbcType="VARCHAR" property="oneId"/>
        <result column="union_id" jdbcType="VARCHAR" property="unionId"/>
    </resultMap>
    <resultMap id="resultMap" type="cn.com.glsx.entmicro.model.response.CustomerInfoResponse">
        <!--
          WARNING - @mbg.generated
        -->
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="customer_name" jdbcType="VARCHAR" property="customerName"/>
        <result column="customer_phone" jdbcType="VARCHAR" property="customerPhone"/>
        <result column="user_id" jdbcType="VARCHAR" property="userid"/>
        <result column="customer_sex" jdbcType="TINYINT" property="customerSex"/>
        <result column="type" jdbcType="TINYINT" property="type"/>
        <result column="position" jdbcType="VARCHAR" property="position"/>
        <result column="corp_name" jdbcType="VARCHAR" property="corpName"/>
        <result column="one_id" jdbcType="VARCHAR" property="oneId"/>
        <result column="avatar" jdbcType="VARCHAR" property="avatar"/>
        <result column="wechat_id" jdbcType="VARCHAR" property="wechatId"/>
        <result column="created_time" jdbcType="TIMESTAMP" property="createdTime"/>
        <result column="staffId" jdbcType="TINYINT" property="staffId"/>
        <result column="del_flag" jdbcType="TINYINT" property="delFlag"/>
        <result column="is_join" jdbcType="TINYINT" property="isJoin"/>
        <result column="merchant_id" jdbcType="TINYINT" property="merchantId"/>
        <result column="union_id" jdbcType="VARCHAR" property="unionId"/>
        <result column="add_time" jdbcType="TIMESTAMP" property="addTime"/>
        <result column="add_way" jdbcType="INTEGER" property="addWay"/>
        <result column="cust_add_way" jdbcType="INTEGER" property="custAddWay"/>
        <result column="updated_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="follow_time" jdbcType="TIMESTAMP" property="followTime"/>
        <result column="user_oneid" jdbcType="VARCHAR" property="userOneid"/>
    </resultMap>

    <resultMap id="CustomerBaseMapResponse" type="cn.com.glsx.entmicro.model.response.BelongCustomerBaseResponse">
        <result column="id" property="id"/>
        <result column="chat_id" property="chatId" jdbcType="VARCHAR"/>
        <result column="chat_name" property="groupName" jdbcType="VARCHAR"/>
        <result column="join_time" property="joinTime" jdbcType="TIMESTAMP"/>
        <result column="group_nickname" property="groupNickName" jdbcType="VARCHAR"/>
        <result column="staff_name" property="groupHostName" jdbcType="VARCHAR"/>
    </resultMap>

    <resultMap id="CustomerBaseCountMapResponse" type="cn.com.glsx.entmicro.model.response.CustomerBaseCountResponse">
        <result column="customer_base_id" property="groupId" jdbcType="INTEGER"/>
        <result column="count_num" property="countNum" jdbcType="INTEGER"/>
    </resultMap>

    <resultMap id="CustomerFollowStaffResponse" type="cn.com.glsx.entmicro.model.response.CustomerFollowStaffResponse">
        <result column="staffName" property="staffName" jdbcType="VARCHAR"/>
        <result column="oneId" property="oneId" jdbcType="VARCHAR"/>
        <result column="customerUserId" property="customerUserId" jdbcType="VARCHAR"/>
        <result column="staffUserId" property="staffUserId" jdbcType="VARCHAR"/>
        <result column="staff_id" property="staffId" jdbcType="VARCHAR"/>
        <result column="created_time" property="createdTime" jdbcType="TIMESTAMP"/>
     </resultMap>


    <sql id="Base_Column_List">
        <!--
          WARNING - @mbg.generated
        -->
        id, customer_name, customer_phone, user_id, customer_sex, `type`, `position`, corp_name,
        corp_full_name, created_by, created_time, delete_time, updated_by, updated_time,
        del_flag, account_id, one_id,avatar,wechat_id,merchantId
    </sql>

    <!--员工跟进信息-->
    <select id="getStaffFollowById" resultType="cn.com.glsx.entmicro.model.response.StaffFollowResponse">
        select es.staff_name        staffName,
               es.staff_phone       remarkMobiles,
               edr.department_names departmentName,
               sc.created_time      addTime,
               ecf.add_way          addWay,
               ecf.remark,
               ecf.description,
               (case
                    when ecf.oper_userid = es.user_id then 0
                    when ecf.oper_userid = sc.user_id then 1
                    else 2 end) as  addType,
               es.user_id       as  userid,
               ecf.id           as  id
        from scrm_customer sc
                 left join entmicro_customer_follow ecf on sc.id = ecf.customer_id
                 left join entmicro_staff es on ecf.staff_id = es.id
                 left join (select esdr.staff_id as staff_id, group_concat(ed.department_name) department_names
                            from entmicro_staff_depart_relation esdr
                                     left join entmicro_department ed on esdr.department_id = ed.id
                            where esdr.del_flag = 0
                            group by esdr.staff_id) edr on edr.staff_id = es.id
        where sc.id = #{id}
          and ecf.del_flag = 0
          and es.del_flag = 0
    </select>

    <!--员工跟进-->
    <select id="getStaffFollowByOneId" resultType="cn.com.glsx.entmicro.model.response.StaffFollowResponse">
        select t.*,group_concat(t.departmentName) as departmentName from (select es.staff_name                                      staffName,
               ecf.remark_mobiles                                 remarkMobiles,
               ed.department_name                              departmentName,
               date_format(ecf.created_time, '%Y-%m-%d %H:%i:%s') addTime,
               ecf.add_way                                        addWay,
               ecf.remark,
               ecf.description,
               (case
                    when ecf.oper_userid = es.user_id then 0
                    when ecf.oper_userid = sc.user_id then 1
                    else null end) as                                addType,
               es.user_id       as                                userid,
               ecf.id           as                                id,
               ecf.`staff_id`   AS                               staffId
        from scrm_customer sc
                 left join entmicro_customer_follow ecf on sc.id = ecf.customer_id
                 left join entmicro_staff es on ecf.staff_id = es.id
                 left join entmicro_staff_depart_relation edr on edr.staff_id = es.id and edr.del_flag = 0
                 left join entmicro_department ed on edr.department_id = ed.id
        where sc.one_id = #{oneId}
--           and sc.del_flag = 0
--           and ecf.del_flag = 0
          and es.del_flag = 0
            and edr.relevance_merchant=#{merchantId}
            )t  group by t.staffName
    </select>

    <!--客户所属客户群-->
    <select id="getCustomerBaseByOneId" resultMap="CustomerBaseMapResponse">
        select scb.id,
               scb.chat_id,
               scb.chat_name,
               scbr.join_time,
               (case
                    when scbr.group_nickname = "" or scbr.group_nickname is null
                        then sc.customer_name
                    else scbr.group_nickname end) as group_nickname,
               es.staff_name
        from scrm_customer sc
                 left join scrm_customer_base_relation scbr on sc.id = scbr.customer_id and scbr.type=2
                 left join scrm_customer_base scb on scb.id = scbr.customer_base_id
                 left join entmicro_staff es on scb.lord = es.user_id
                 left join entmicro_staff_depart_relation edr on edr.staff_id = es.id
        where sc.one_id = #{oneId} and edr.relevance_merchant=#{merchantId}
          and scbr.del_flag = 0
          and scb.del_flag = 0
        GROUP BY scb.id
    </select>

    <!--统计客户群客户数-->
    <select id="countCustomerBase" parameterType="java.util.Set"
            resultMap="CustomerBaseCountMapResponse">
        select customer_base_id,count(1) count_num from scrm_customer_base_relation where
        customer_base_id IN
        <foreach collection="ids" index="index" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
        and del_flag = 0
        and type = 2
        GROUP BY customer_base_id
    </select>

    <select id="getCustomerByUserId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from scrm_customer where user_id = #{userid}
    </select>

    <select id="findAllCustomerByMerchantId"
            resultType="cn.com.glsx.entmicro.model.request.CustomerStandardRequest">
        select sc.user_id as externalUserId, es.user_id as userId
        from scrm_customer sc
                 left join entmicro_customer_follow ecf on sc.id = ecf.customer_id
                 left join entmicro_staff es on ecf.staff_id = es.id
                 left join entmicro_staff_depart_relation esdr on esdr.staff_id = es.id
        where esdr.relevance_merchant = #{merchantId} and esdr.del_flag=0 and es.del_flag=0 and sc.del_flag=0
        <if test="userIds != null and  userIds.size>0">
           and es.user_id IN
        <foreach collection="userIds" index="index" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
        </if>
        group by sc.user_id, es.user_id
    </select>
    <select id="getCustomersByUserId" resultMap="BaseResultMap">
        select * from scrm_customer where
        user_id IN
        <foreach collection="ids" index="index" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
    </select>

    <!-- 修改oneid -->
    <update id="updateOneIdByCust">
        <if test="oneId != null and oneId != ''">
            UPDATE scrm_customer
            <set>
                `one_id` = #{oneId}
            </set>
            WHERE `id` = #{custId}
        </if>
    </update>

    <!-- oneid变更 -->
    <update id="changeOneId">
        UPDATE scrm_customer
        <set>
            `one_id` = #{newOneId}
        </set>
        WHERE id in
        <foreach collection="ids" index="index" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
    </update>

    <!-- 删除客户 -->
    <update id="deleteaById">
        UPDATE scrm_customer
        <set>
            del_flag = -1,
            delete_time = now()
        </set>
        WHERE `id` = #{id}
    </update>

    <!--id查客户信息-->
    <select id="getCustomerById" resultType="cn.com.glsx.entmicro.model.response.CustomerInfo">
        select sc.id,
               sc.avatar,
               sc.wechat_id                                      weChatId,
               sc.customer_name                                  customerName,
               ef.remark_mobiles                                 customerPhone,
               sc.customer_sex                                   customerSex,
               sc.`type`,
               sc.`position`,
               sc.account_id                                     accountId,
               sc.user_id                                        userId,
               sc.corp_name                                      corpName,
               sc.corp_full_name                                 corpFullName,
               date_format(sc.created_time, '%Y-%m-%d %H:%i:%s') createdTime,
               sc.created_by                                     createdBy,
               sc.updated_by                                     updatedBy,
               date_format(sc.updated_time, '%Y-%m-%d %H:%i:%s') updatedTime,
               date_format(sc.delete_time, '%Y-%m-%d %H:%i:%s')  deleteTime,
               sc.del_flag                                       delFlag,
               sc.one_id                                         oneId,
               sc.add_way                                        addWay,
               sc.union_Id unionid
        from scrm_customer sc
        left join entmicro_customer_follow ef on sc.id=ef.customer_id
        where sc.id = #{custId} and ef.staff_id=#{staffId}
        group by sc.id
    </select>

    <!--根據oneId查詢員工跟進員-->
    <!--<select id="getCustomerStaffByOneId"
            resultType="cn.com.glsx.entmicro.model.response.CustomerFollowStaffResponse">
        SELECT sc.customer_name as customerName,
        ecf.customer_id as customerId,
        es.staff_name as staffName,
        sc.one_id as oneId
        from scrm_customer sc
        LEFT JOIN entmicro_customer_follow ecf on ecf.customer_id = sc.id
        LEFT JOIN entmicro_staff es on es.id = ecf.staff_id
        WHERE one_id IN
        <foreach collection="oneIds" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
        and ecf.del_flag = 0
        GROUP BY ecf.staff_id
        order by ecf.created_time
    </select>-->
    <select id="getCustomerStaffByOneId"
            resultMap="CustomerFollowStaffResponse">
        SELECT GROUP_CONCAT(distinct es.staff_name) as staffName,
        sc.one_id as oneId
        from scrm_customer sc
        LEFT JOIN entmicro_customer_follow ecf on ecf.customer_id = sc.id
        LEFT JOIN entmicro_staff es on es.id = ecf.staff_id AND es.del_flag != -1
        LEFT JOIN entmicro_staff_depart_relation edr on edr.staff_id = es.id and edr.del_flag=0
        WHERE ecf.del_flag = 0
        and edr.relevance_merchant=#{merchantId}
        and sc.one_id IN
        <foreach collection="oneIds" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
        GROUP BY sc.one_id
--         order by ecf.created_time
    </select>
    <select id="getAllCustomerStaffByOneId"
            resultMap="CustomerFollowStaffResponse">
        SELECT GROUP_CONCAT(distinct es.staff_name) as staffName,
        sc.one_id as oneId
        from scrm_customer sc
        LEFT JOIN entmicro_customer_follow ecf on ecf.customer_id = sc.id
        LEFT JOIN entmicro_staff es on es.id = ecf.staff_id AND es.del_flag != -1
        LEFT JOIN entmicro_staff_depart_relation edr on edr.staff_id = es.id and edr.del_flag=0
        WHERE 1=1
        and edr.relevance_merchant=#{merchantId}
        and sc.one_id IN
        <foreach collection="oneIds" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
        GROUP BY sc.one_id
--         order by ecf.created_time
    </select>

    <select id="getListCustomerStaffByOneId"
            resultMap="CustomerFollowStaffResponse">
        SELECT sc.one_id AS oneId,
            ecf.created_time,
            ecf.staff_id,
            es.user_id AS staffUserId,
            es.staff_name AS staffName
        from scrm_customer sc
        LEFT JOIN entmicro_customer_follow ecf on ecf.customer_id = sc.id
        LEFT JOIN entmicro_staff es on es.id = ecf.staff_id AND es.del_flag != -1
        LEFT JOIN entmicro_staff_depart_relation edr on edr.staff_id = es.id and edr.del_flag=0
        WHERE ecf.del_flag = 0
        and edr.relevance_merchant IN
        <foreach collection="merchantIdList" open="(" close=")" separator="," item="merchantId">
            #{merchantId}
        </foreach>
        <if test="oneIdList !=null and oneIdList.size>0">
            and sc.one_id IN
            <foreach collection="oneIdList" open="(" close=")" separator="," item="oneId">
                #{oneId}
            </foreach>
        </if>
        <if test="staffIdList !=null and staffIdList.size>0">
            and es.id in
            <foreach collection="staffIdList" open="(" close=")" separator="," item="staffId">
                #{staffId}
            </foreach>
        </if>
        GROUP BY ecf.id
    </select>


    <select id="findCustomerByStaffId" resultMap="resultMap">
        select sc.*,
        ecf.staff_id as staffId
        from scrm_customer sc
        LEFT JOIN entmicro_customer_follow ecf on ecf.customer_id = sc.id
        where ecf.staff_id=#{staffId} and ecf.del_flag=0
        <if test="keyword!=null and keyword!=''">
            and (
                sc.customer_name like concat('%',#{keyword},'%') escape '/'
            <if test="isMiniPro != null and isMiniPro">
                or sc.customer_phone like concat('%',#{keyword},'%')
            </if>
            )
        </if>
        <if test="isMiniPro != null and isMiniPro">
            order by ecf.created_time desc
        </if>
    </select>
    <select id="findCustomerBychantIds" resultMap="resultMap">
        select sc.*,es.id as staffId
        from scrm_customer sc
        LEFT JOIN scrm_customer_base_relation base on base.customer_id = sc.id
        LEFT JOIN entmicro_customer_follow ecf on ecf.customer_id = sc.id AND ecf.del_flag=0
        LEFT JOIN entmicro_staff es on es.id = ecf.staff_id
        left join entmicro_staff_depart_relation er on er.staff_id=es.id AND er.del_flag=0
        where er.relevance_merchant=#{custSearchRequest.merchantId}
        <!--<if test="custSearchRequest.chantIds!=null and custSearchRequest.chantIds.size>0">-->
        <!--and sc.id in-->
        <!--<foreach collection="custSearchRequest.chantIds" open="(" close=")" separator="," item="item">-->
        <!--#{item}-->
        <!--</foreach>-->
        <!--</if>-->
        <if test="custSearchRequest.chantIds!=null and custSearchRequest.chantIds.size>0">
            and base.customer_base_id in
            <foreach collection="custSearchRequest.chantIds" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
        <if test="custSearchRequest.staffIds!=null and custSearchRequest.staffIds.size>0">
            and es.id in
            <foreach collection="custSearchRequest.staffIds" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
        <if test="custSearchRequest.startTime != null ">
            and sc.created_time<![CDATA[>=]]>#{custSearchRequest.startTime}
        </if>
        <if test="custSearchRequest.sex != null ">
            and sc.customer_sex=#{custSearchRequest.sex}
        </if>
        <if test="custSearchRequest.endTime != null ">
            and sc.created_time<![CDATA[<=]]>#{custSearchRequest.endTime}
        </if>
        GROUP BY sc.id
    </select>
    <select id="findCustomerByStaffIds" resultMap="resultMap">
        select sc.*
        from scrm_customer sc
        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 er on er.staff_id=es.id
        where er.relevance_merchant=#{custSearchRequest.merchantId}


        <if test="custSearchRequest.startTime != null ">
            and sc.created_time<![CDATA[>=]]>#{custSearchRequest.startTime}
        </if>
        <if test="custSearchRequest.sex != null ">
            and sc.customer_sex=#{custSearchRequest.sex}
        </if>
        <if test="custSearchRequest.endTime != null ">
            and sc.created_time<![CDATA[<=]]>#{custSearchRequest.endTime}
        </if>
        GROUP BY sc.id
    </select>

    <select id="findCustomerByPhones" resultMap="BaseResultMap">
        select t.* from
        (select sc1.*
        from scrm_customer sc1
        where sc1.merchantId = #{merchantId} and sc1.del_flag=0
        and sc1.customer_phone in
        <foreach collection="phones" open="(" close=")" separator="," item="phone">
            #{phone}
        </foreach>
        group by sc1.customer_phone
        union
        select sc2.* from scrm_customer sc2
        LEFT JOIN entmicro_customer_follow ecf on sc2.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.relevance_merchant = #{merchantId} and sc2.del_flag=0 and sc2.customer_phone in
        <foreach collection="phones" open="(" close=")" separator="," item="phone">
            #{phone}
        </foreach>)t
        group by t.customer_phone
    </select>

    <select id="getCustomerInfoByUserId" resultType="cn.com.glsx.entmicro.model.response.CustomerInfo"></select>
    <select id="findNoAddEntmocroCustByMerchant" resultMap="resultMap">
        select *
        from scrm_customer
        where merchantId = #{merchantId}
          and user_id is null
    </select>
    <select id="findNoPhoneCustByMerchant"
            resultMap="resultMap">
          select sc.*,esdr.id as staffId
        from scrm_customer sc
                 LEFT JOIN entmicro_customer_follow ecf on sc.id = ecf.customer_id and ecf.del_flag=0
                 LEFT JOIN entmicro_staff es on es.id = ecf.staff_id and es.del_flag=0
                 left join entmicro_staff_depart_relation esdr on esdr.staff_id = es.id and esdr.del_flag=0
        where esdr.relevance_merchant =  #{merchantId}
          and sc.customer_phone is null and sc.del_flag=0 group by sc.id
    </select>
    <select id="findNoAddEntmocroCustByMerchantCount" resultType="java.lang.Integer">
        select count(1)
        from scrm_customer
        where merchantId = #{merchantId}
          and user_id is null AND del_flag=0
    </select>
    <select id="findNoPhoneCustByMerchantCount" resultType="java.lang.Integer">
        select count(DISTINCT sc.one_id)
        from scrm_customer sc
                 LEFT JOIN entmicro_customer_follow ecf on sc.id = ecf.customer_id AND ecf.del_flag=0
                 LEFT JOIN entmicro_staff es on es.id = ecf.staff_id AND ecf.del_flag=0
                 left join entmicro_staff_depart_relation esdr on esdr.staff_id = es.id
        where esdr.relevance_merchant = #{merchantId}
          and sc.customer_phone is null AND esdr.del_flag=0
    </select>
    <select id="findWaitRenewal" resultMap="resultMap">
        select *
        from scrm_customer t1
                 left join scrm_customer_detail t2 on t1.id = t2.customer_id
        where t2.com_insurance_endtime<![CDATA[ <=]]> DATE_SUB(CURDATE(), INTERVAL 30 DAY)
          and t2.com_insurance_endtime<![CDATA[ >=]]> CURDATE()
          and t1.merchantId = #{merchantId} AND t1.del_flag=0
    </select>
    <select id="findWaitRegularMaintain" resultMap="resultMap">
        select *
        from scrm_customer t1
                 left join scrm_customer_detail t2 on t1.id = t2.customer_id
        where t2.maintenance_date is null
          and t2.buy_date<![CDATA[ >=]]> DATE_SUB(CURDATE(), INTERVAL 7 MONTH)
          and t2.buy_date<![CDATA[ <=]]> DATE_SUB(CURDATE(), INTERVAL 5 MONTH)
          and t1.merchantId = #{merchantId}  AND t1.del_flag=0
    </select>

    <select id="findWaitRenewalCount" resultType="java.lang.Integer">
        select count(DISTINCT t1.one_id)
        from scrm_customer t1
                 left join scrm_customer_detail t2 on t1.id = t2.customer_id
        where t2.com_insurance_endtime<![CDATA[ <=]]> DATE_SUB(CURDATE(), INTERVAL -30 DAY)
            and t2.com_insurance_endtime<![CDATA[ >=]]> CURDATE()
           and t1.merchantId = #{merchantId} AND t1.del_flag=0
    </select>

    <select id="findWaitRegularMaintainCount" resultType="java.lang.Integer">
        select count(1)
        from scrm_customer t1
                 left join scrm_customer_detail t2 on t1.id = t2.customer_id
        where t2.maintenance_date is null
          and t2.buy_date<![CDATA[ >=]]> DATE_SUB(CURDATE(), INTERVAL 7 MONTH)
          and t2.buy_date<![CDATA[ <=]]> DATE_SUB(CURDATE(), INTERVAL 5 MONTH)
           and t1.merchantId = #{merchantId} AND t1.del_flag=0
    </select>
    <select id="findMagicCust" resultMap="resultMap">
        select *
        from scrm_customer
        where merchantId = #{merchantId}
          and add_way = 101
    </select>
    <select id="findMagicCustCount" resultType="java.lang.Integer">
        select count(DISTINCT one_id)
        from scrm_customer_merge
        where merchant_id = #{merchantId}
        <if test="status != null">
            and status = #{status}
        </if>
        and add_way = 101 and one_id is not null
    </select>
    <select id="findPhoneAndMerchantId" resultMap="BaseResultMap">
        select * from scrm_customer where merchantId in
        <foreach collection="merchantid" open="(" close=")" separator="," item="phone">
            #{phone}
        </foreach>
        and customer_phone=#{customerPhone}
    </select>
    <select id="isEntmicroCustomer" resultType="cn.com.glsx.entmicro.entity.Customer">
        select *
        from scrm_customer_merge
        where customer_phone=#{phone}
            and merchant_id = #{merchantId}
            and del_flag=0
    </select>

    <select id="selectAllOneId" resultType="java.lang.String">
        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 scm.del_flag = 0
             )t
        GROUP BY t.oneid ORDER BY t.addTime DESC
    </select>

    <update id="batchUpdateOneId" parameterType="java.util.List">
        <foreach collection="customers" item="item" index="index" open="" close="" separator=";">
            update scrm_customer
            <set>
                <!--客户名称-->
                <if test="item.oneId != null and item.oneId !=''">
                    `one_id` = #{item.oneId}
                </if>
            </set>
            where `id` = ${item.customerId}
        </foreach>
    </update>

    <update id="batchUpdate" parameterType="java.util.List">
        <foreach collection="customers" item="item" index="index" open="" close="" separator=";">
            update scrm_customer
            <trim prefix="set" suffixOverrides=",">
                <!--客户名称-->
                <if test="item.customerName != null and item.customerName !=''">
                    `customer_name` = #{item.customerName},
                </if>
                <!--客户电话-->
                <if test="item.customerPhone != null and item.customerPhone !=''">
                    `customer_phone` = #{item.customerPhone},
                </if>
                <!--性别-->
                <if test="item.customerSex != null">
                    `customer_sex` = #{item.customerSex},
                </if>
                <!--更新人-->
                <if test="item.updatedBy != null">
                    `updated_by` = #{item.updatedBy},
                </if>
                <!--更新时间-->
                <if test="item.updatedTime != null">
                    `updated_time` = #{item.updatedTime},
                </if>
                <if test="item.idCard != null">
                    `id_card` = #{item.idCard}
                </if>
                <if test="item.useridBefEncryption != null">
                    `userid_Bef_Encryption` = #{item.useridBefEncryption}
                </if>
            </trim>
            where `id` = #{item.id}
        </foreach>
    </update>

    <insert id="batchInsert" useGeneratedKeys="true" keyProperty="customers.id">
        insert into scrm_customer
        (`customer_name`,`customer_phone`,`customer_sex`,`created_by`,`created_time`,`del_flag`,`merchantId`,`add_way`)
        values
        <foreach collection="customers" item="item" index="index" open="" close="" separator=",">
            (#{item.customerName}, #{item.customerPhone}, #{item.customerSex}, #{item.createdBy}, #{item.createdTime},
            #{item.delFlag}, #{item.merchantid}, #{item.addWay})
        </foreach>
    </insert>
    <insert id="insertinto">
         ${sql}
    </insert>

    <!--批量oneId查基础表-->
    <select id="findByBatchOneId" resultType="cn.com.glsx.entmicro.model.response.CustomerNickNameRpcResponse">
        SELECT
        sc.one_id oneId,
        sc.customer_name nickName,
        sc.type TYPE,
        sc.corp_name corpName,
        sc.del_flag delFlag,
        sc.user_id as userId
        FROM
        `scrm_customer` sc
        LEFT JOIN `entmicro_customer_follow` ecf ON sc.id = ecf.customer_id
        LEFT JOIN `entmicro_staff` es ON (ecf.staff_id = es.id)
        LEFT JOIN `entmicro_staff_depart_relation` esdr ON (es.id = esdr.staff_id)
        WHERE sc.one_id IN
        <foreach collection="oneIdList" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
        AND esdr.relevance_merchant = #{merchantId}
        GROUP BY sc.id,
        sc.customer_name,
        sc.corp_name
    </select>

    <select id="findCustomerBymergeIds" resultMap="resultMap">
        select t.* from(
        select sm.*,sc.user_id,sc.created_time add_time,sc.add_way cust_add_way from scrm_customer_merge sm
        left join scrm_customer sc on sm.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)
        where 1=1 and sm.del_flag=0
        <if test="mergeIds != null and mergeIds.size>0">
            and sm.id in
            <foreach collection="mergeIds" open="(" close=")" separator="," item="id">
                #{id}
            </foreach>
        </if>
        <if test="phone != null and phone !=''">
            and sm.`customer_phone` = #{phone}
            and sm.merchant_id=#{merchantId}
            and edr.relevance_merchant=#{merchantId}
        </if>
        <if test="merchantId != null and merchantId !=''">
            and sm.merchant_id=#{merchantId}
        </if>
        <if test="customerName != null and customerName !=''">
            and sm.customer_Name like concat('%',#{customerName},'%')
        </if>
        union all
        select sm.*,sc.user_id,sc.created_time add_time,sc.add_way cust_add_way
        FROM
        scrm_customer_merge sm
        LEFT JOIN
        scrm_customer sc ON sm.one_id = sc.one_id
        WHERE 1=1  and sm.del_flag=0
        <if test="mergeIds != null and mergeIds.size>0">
            and sm.id in
            <foreach collection="mergeIds" open="(" close=")" separator="," item="id">
                #{id}
            </foreach>
        </if>
        <if test="phone != null and phone !=''">
            and sm.`customer_phone` = #{phone}
            and sm.merchant_id=#{merchantId}
        </if>
        <if test="merchantId != null and merchantId !=''">
            and sm.merchant_id=#{merchantId}
        </if>
        <if test="customerName != null and customerName !=''">
            and sm.customer_Name like concat('%',#{customerName},'%')
        </if>
        ) t
        group by t.one_id;
    </select>

    <select id="getCustomerByOneId" resultType="java.lang.Integer">
        SELECT count(1)
        from scrm_customer sc
        LEFT JOIN entmicro_customer_follow ecf on ecf.customer_id = sc.id
        LEFT JOIN entmicro_staff es on es.id = ecf.staff_id AND es.del_flag != -1
        LEFT JOIN entmicro_staff_depart_relation edr on edr.staff_id = es.id and edr.del_flag=0
        WHERE ecf.del_flag = 0 and sc.del_flag=0
        and edr.relevance_merchant=#{merchantId}
        and sc.one_id=#{oneId}
    </select>

    <update id="updatePhoneByUserId">
        <foreach collection="customer" item="item" separator=";">
            update scrm_customer
            set customer_phone = #{item.remarkPhone},
            updated_time = now()
            where user_id = #{item.userid}
            and (customer_phone is null
            OR customer_phone = "")
        </foreach>
    </update>
    <update id="updateCustomerMergeSubscribe">
        update scrm_customer_merge scm
        left join scrm_customer sc on sc.one_id = scm.one_id and sc.del_flag = 0
        LEFT JOIN entmicro_customer_follow ecf on ecf.customer_id = sc.id and ecf.del_flag = 0
        LEFT JOIN entmicro_staff es on es.id = ecf.staff_id AND es.del_flag = 0
        LEFT JOIN entmicro_staff_depart_relation edr on edr.staff_id = es.id and edr.del_flag = 0
        left join scrm_wechat_subscriber_mapping swsm on swsm.union_id = sc.union_id and swsm.del_flag = 0
        set scm.subscribe = swsm.subscribe
        where scm.del_flag = 0
        and swsm.id is not null
        and swsm.app_id = #{appId}
        <if test="merchantId != null and merchantId !=''">
            and edr.relevance_merchant = #{merchantId}
        </if>
    </update>

    <select id="selectCustomerNameByPhone" resultType="cn.com.glsx.entmicro.entity.Customer">
        SELECT sc.id,
        sc.one_id oneId,
        sc.user_id userid,
        sc.customer_name customerName,
        sc.customer_phone customerPhone,
        sc.customer_sex customerSex,
        sc.`type`,
        sc.created_time createdTime,
        sc.add_way addWay
        from scrm_customer sc
        left join scrm_customer_merge scm on scm.one_id = sc.one_id
        where scm.merchant_Id = #{merchantId}
        and sc.del_flag = 0
        and sc.add_way in (#{addWay})
        <if test="phoneList != null and phoneList.size() > 0">
            and sc.customer_phone in
            <foreach collection="phoneList" open="(" close=")" item="phone" separator=",">
                #{phone}
            </foreach>
        </if>
        group by sc.customer_phone
        order by case sc.add_way when 101 then 1 when 103 then 2 end, sc.created_time desc
    </select>
    <select id="getNotEntmicroCustomerByOneId" resultType="cn.com.glsx.entmicro.entity.Customer">
        select * from scrm_customer
        where merchantId=#{customerMergeRequest.merchantId} and one_Id=#{customerMergeRequest.oneId}
        <if test="customerMergeRequest.customerPhone != null and customerMergeRequest.customerPhone !=''">
            and customer_phone=#{customerMergeRequest.customerPhone}
        </if>
        limit 1
    </select>
    <select id="filterCustomerChatIdsByStaffIds" resultType="cn.com.glsx.entmicro.model.response.CustGroupResponse">
        SELECT cb.`id` chatId,s.staff_id staffId FROM `scrm_customer_base` cb
        INNER JOIN (SELECT DISTINCT s.`user_id` user_id,s.`id` staff_id FROM `entmicro_staff` s INNER JOIN
        `entmicro_staff_depart_relation` sdr ON sdr.`staff_id`=s.`id`
        WHERE sdr.`staff_id` IN
        <foreach collection="staffIds" open="(" item="staffId" separator="," close=")">
            #{staffId}
        </foreach>
        AND sdr.`del_flag`=0 AND sdr.`relevance_merchant`=#{merchantId}) s ON s.user_id=cb.`lord` WHERE
        cb.`del_flag`=0
    </select>
    <select id="findCusetomerListByMergeIds"
            resultMap="resultMap">
        select
        sc.*,scm.id as mergeId,ecf.created_time,ecf.created_time follow_time
        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 AND es.del_flag != -1
        LEFT JOIN entmicro_staff_depart_relation edr on edr.staff_id = es.id
        WHERE ecf.del_flag = 0 and sc.del_flag=0
        and edr.relevance_merchant=#{merchantId}
        and scm.merchant_Id=#{merchantId}
        and scm.id in
        <foreach collection="mergeIds" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
        <if test="staffId != null">
            and es.id = #{staffId}
        </if>
        <if test="staffId == null">
            union all
            select
            sc.*,scm.id as mergeId
            from scrm_customer_merge scm
            LEFT JOIN scrm_customer sc on scm.one_id=sc.one_id
            where sc.merchantId=#{merchantId} and scm.merchant_Id=#{merchantId}
            and scm.id in
            <foreach collection="mergeIds" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
        group by id
    </select>
    <select id="getMergeIdList" resultType="java.lang.Integer">
        SELECT id FROM `scrm_customer_merge` WHERE one_id IN (SELECT one_id FROM `scrm_customer` WHERE id IN
        <foreach collection="customerIds" item="customerId" open="(" separator="," close=")">
            #{customerId}
        </foreach>
        ) AND merchant_id = #{merchantId} AND del_flag=0
    </select>

    <select id="findCustomerByOneId" resultType="java.lang.Integer">
        select t.id from
        (SELECT sc.id
        from scrm_customer sc
        LEFT JOIN entmicro_customer_follow ecf on ecf.customer_id = sc.id
        LEFT JOIN entmicro_staff es on es.id = ecf.staff_id AND es.del_flag != -1
        LEFT JOIN entmicro_staff_depart_relation edr on edr.staff_id = es.id
        WHERE edr.relevance_merchant=#{merchantId}
        and sc.one_id=#{oldOneId}
        union all
        SELECT sc.id
        from scrm_customer sc
        where
        sc.merchantid=#{merchantId}
        and sc.one_id=#{oldOneId})t group by t.id
    </select>
    <select id="queryOneIdByPhoneNumber" resultType="java.lang.String">
        SELECT one_id FROM `scrm_customer_merge` WHERE merchant_id = #{merchantId} AND customer_phone=#{phone} AND del_flag=0;
    </select>

    <!--查询门店下的企微客户-->
    <select id="selectCustomerByMerchant" resultMap="BaseResultMap">
        SELECT
            sc.id,
            sc.customer_name,
            sc.one_id,
            sc.user_id,
            sc.customer_phone
        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 es.id = esdr.staff_id
        WHERE esdr.relevance_merchant = #{merchantId}
        GROUP BY sc.id
    </select>

    <select id="selectUseridByUnionIdIsNull" resultType="java.lang.String">
        select sc.user_id
        from scrm_customer sc
        left join entmicro_customer_follow ecf on sc.id = ecf.customer_id
        left join entmicro_staff es on ecf.staff_id = es.id and es.del_flag=0
        left join entmicro_staff_depart_relation esdr on esdr.staff_id = es.id and esdr.del_flag=0
        where esdr.relevance_merchant = #{merchantId} and sc.del_flag=0 and sc.union_id is null
        group by sc.user_id
    </select>

    <select id="getCustomerStaffByUserId" resultMap="CustomerFollowStaffResponse">
        SELECT es.user_id staffUserId,
                sc.user_id customerUserId
        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
        WHERE sc.user_id in
        <foreach collection="userIdList" open="(" close=")" separator="," item="item">
                #{item}
        </foreach>
            AND ecf.del_flag = 0
        GROUP BY es.id
    </select>

    <!--  根据客户手机号查询关联员工信息  -->
    <select id="selectFollowStaffByCustomerPhone" resultType="cn.com.glsx.entmicro.model.response.StaffFollowResponse">
        select
        es.staff_name                                       staffName,
        ecf.remark_mobiles                                  remarkMobiles,
        ecf.created_time                                    staffAddTime,
        ecf.add_way                                         addWay,
        ecf.remark,
        ecf.description,
        es.user_id       as                                 staffUserId,
        ecf.`staff_id`   AS                                 staffId
        from entmicro_staff es
        left join entmicro_customer_follow ecf on es.id = ecf.staff_id  and ecf.del_flag = 0
        left join scrm_customer sc on ecf.customer_id = sc.id and sc.del_flag = 0
        left join entmicro_staff_depart_relation esdr on esdr.staff_id = es.id and esdr.del_flag = 0
        where es.del_flag = 0 and esdr.relevance_merchant = #{merchantId}
        <if test="customerPhone != null">
            and sc.customer_phone = #{customerPhone}
        </if>
        <if test="staffIdList != null and staffIdList.size() > 0">
            and es.id in
            <foreach collection="staffIdList" open="(" close=")" item="staffId" separator=",">
                #{staffId}
            </foreach>
        </if>
        <if test="userIdList != null and userIdList.size() > 0">
            and sc.user_id in
            <foreach collection="userIdList" open="(" close=")" item="userId" separator=",">
                #{userId}
            </foreach>
        </if>
        group by es.id
    </select>

    <select id="getStaffByCustomerId" resultType="java.lang.Integer">
        SELECT esdr.relevance_merchant
        FROM `entmicro_customer_follow` ecf
            LEFT JOIN `entmicro_staff_depart_relation` esdr ON ecf.staff_id = esdr.staff_id
        WHERE ecf.customer_id = #{id}
            AND ecf.del_flag = 0
        GROUP BY esdr.relevance_merchant
    </select>

    <select id="selectUseridByUnionId" resultType="java.lang.String">
        select sc.user_id
        from scrm_customer sc
            left join entmicro_customer_follow ecf on sc.id = ecf.customer_id
            left join entmicro_staff es on ecf.staff_id = es.id and es.del_flag=0
            left join entmicro_staff_depart_relation esdr on esdr.staff_id = es.id and esdr.del_flag=0
        where esdr.relevance_merchant = #{merchantId}
            and sc.del_flag=0
            and sc.union_id in
            <foreach collection="unionIdList" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        group by sc.user_id
    </select>

    <select id="getCustomerFollowByOneIdList" resultType="cn.com.glsx.entmicro.model.response.customer.CustomerFollowRpcResponse">
        SELECT
          sc.`user_id` as externalUserId,
          es.`user_id` as staffUserId,
          es.`id` as staffId,
          sc.one_id as oneId,
          esdr.relevance_merchant as merchantId,
            sc.customer_name customerName,
            sc.customer_phone customerPhone
        FROM
          `scrm_customer` sc
          LEFT JOIN `entmicro_customer_follow` ecf
            ON (sc.id = ecf.customer_id AND ecf.`del_flag` = 0)
          LEFT JOIN `entmicro_staff` es
            ON (ecf.staff_id = es.id AND es.`del_flag` = 0)
          LEFT JOIN `entmicro_staff_depart_relation` esdr
            ON (es.id = esdr.staff_id AND esdr.`del_flag` = 0)
        WHERE sc.one_id in
        <foreach collection="oneIdList" open="(" item="item" separator="," close=")">
            #{item}
        </foreach>
          AND esdr.relevance_merchant = #{merchantId}
        GROUP BY ecf.id
    </select>

    <select id="findAddEntmicroCustomerByPhones" resultMap="resultMap">
        select sc.*,es.id as staffId,scm.customer_phone,er.user_oneid
        from scrm_customer sc
        left join scrm_customer_merge scm on sc.one_id=scm.one_id
        LEFT JOIN entmicro_customer_follow ecf on ecf.customer_id = sc.id AND ecf.del_flag=0
        LEFT JOIN entmicro_staff es on es.id = ecf.staff_id
        left join entmicro_staff_depart_relation er on er.staff_id=es.id AND er.del_flag=0
        where er.relevance_merchant=#{merchantId} and scm.is_join=1
        and scm.merchant_id=#{merchantId} and scm.del_flag=0 and es.id is not null
        and scm.customer_phone in
        <foreach collection="phoneList" open="(" item="item" separator="," close=")">
            #{item}
        </foreach>
        GROUP BY scm.one_id
    </select>

    <select id="findCustomerByUserIdOrOneId" resultMap="resultMap">
        select * from scrm_customer
        where user_id = #{id} or one_id = #{id}
        <if test="merchantId != null" >
           and merchantId = #{merchantId}
        </if>
        limit 1
    </select>

    <select id="findUserIdByCustomerId" resultType="java.lang.String">
        select sc.user_id from scrm_customer sc
        where sc.id in
        <foreach collection="customerIds" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </select>
</mapper>