CustomerMergeMapper.xml 35.5 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
<?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.CustomerMergeMapper">
    <resultMap id="BaseResultMap" type="cn.com.glsx.entmicro.entity.CustomerMerge">
        <!--
          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="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="created_time" jdbcType="TIMESTAMP" property="updatedTime"/>
        <result column="del_flag" jdbcType="TINYINT" property="delFlag"/>
        <result column="status" jdbcType="TINYINT" property="status"/>
        <result column="add_way" jdbcType="TINYINT" property="addWay"/>
        <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="id_card" jdbcType="VARCHAR" property="idCard"/>
        <result column="data_create_time" jdbcType="TIMESTAMP" property="dataCreateTime"/>
        <result column="community" jdbcType="VARCHAR" property="community"/>
        <result column="building" jdbcType="VARCHAR" property="building"/>
        <result column="room" jdbcType="VARCHAR" property="room"/>
        <result column="community_id" jdbcType="INTEGER" property="communityId"/>
        <result column="building_id" jdbcType="INTEGER" property="buildingId"/>
        <result column="room_id" jdbcType="INTEGER" property="roomId"/>
        <result column="homeowner_type" jdbcType="TINYINT" property="homeownerType"/>
    </resultMap>

    <resultMap id="customerListMapResponse" type="cn.com.glsx.entmicro.model.response.CustomerListResponse">
        <id column="id" property="id"/>
        <result column="oneid" property="oneId"/>
        <result column="customer_name" property="customerName"/>
        <result column="customer_phone" property="customerPhone"/>
        <result column="customer_sex" property="customerSex"/>
        <result column="follow_staff" property="followStaff"/>
        <result column="is_join" property="isJoin"/>
        <result column="stype" property="type"/>
        <result column="addTime" property="addTime"/>
        <result column="status" property="status"/>
        <result column="add_way" property="addWay"/>
        <result column="sourceChannel" property="sourceChannel"/>
        <result column="avatar" jdbcType="VARCHAR" property="avatar"/>
        <result column="state" jdbcType="VARCHAR" property="custState"/>
        <result column="group_nickname" property="customerNickName"/>
        <result column="nickName" property="nickName"/>
        <result column="customer_id" property="customerId"/>
        <result column="union_id" jdbcType="VARCHAR" property="unionId"/>
        <result column="data_create_time" jdbcType="TIMESTAMP" property="dataCreateTime"/>
        <result column="subscribe" jdbcType="TINYINT" property="subscribe"/>
        <result column="is_login_applet" jdbcType="TINYINT" property="isLoginApplet"/>
    </resultMap>

    <resultMap id="customerInfoMapResponse" type="cn.com.glsx.entmicro.model.response.CustomerInfoResponse">
        <id column="id" property="id"/>
        <result column="oneId" property="oneId"/>
    </resultMap>

    <sql id="Base_Column_List">
        <!--
          WARNING - @mbg.generated
        -->
        id, customer_name, customer_phone, customer_sex, `type`, `position`, corp_name, created_time,status,
        del_flag,one_id,avatar,wechat_id,add_way,is_join,merchant_id,id_card,data_create_time, community, building, room,
        community_id, building_id, room_id
    </sql>

    <select id="getCustomerByPage" parameterType="cn.com.glsx.entmicro.model.request.CustomerSearchRequest"
            resultMap="customerListMapResponse">
        select
        t.id,
        t.oneid,
        t.customer_name,
        t.customer_phone,
        t.customer_sex,
        t.is_join,
        t.stype,
        t.addTime,
        t.status,
        t.add_way,
        t.cust_state state,
        t.staff_id,
        t.avatar,
        t.nickName,
        t.customer_id,
        t.subscribe,
        t.is_login_applet,
        t.merchant_id merchantId,
        t.community,
        t.building,
        t.room
        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,
        scm.status status,
        scm.add_way,
        sc.cust_state,
        edr.id staff_id,
        sc.avatar,
        GROUP_CONCAT(DISTINCT sc.customer_name) nickName,
        sc.id customer_id,
        scm.subscribe,
        scm.is_login_applet,
        scm.merchant_id,
        scm.community,
        scm.building,
        scm.room
        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)
        WHERE 1=1
        <if test="datalist != null and  datalist.size>0">
            and edr.relevance_merchant in
            <foreach collection="datalist" index="index" open="(" close=")" separator="," item="id">
                #{id}
            </foreach>
            and scm.merchant_id in
            <foreach collection="datalist" index="index" open="(" close=")" separator="," item="id">
                #{id}
            </foreach>
        </if>
        <!--开始时间 合并表时间为准-->
        <if test="beginTime != null">
            and scm.created_time <![CDATA[>=]]> #{beginTime,jdbcType=DATE}
        </if>
        <!--结束时间 合并表时间为准-->
        <if test="endTime != null">
            and scm.created_time <![CDATA[<=]]> #{endTime,jdbcType=DATE}
        </if>
        <!--客户名称-->
        <if test="customerName != null and customerName !=''">
            AND scm.customer_name like concat('%', #{customerName}, '%')
        </if>
        <!--当前状态-->
        <if test="status != null">
            AND scm.status = #{status}
        </if>
        <!--性别-->
        <if test="customerSex != null">
            AND scm.customer_sex = #{customerSex}
        </if>
        <!--手机号-->
        <if test="customerPhone != null and customerPhone !=''">
            AND scm.customer_phone like concat('%', #{customerPhone}, '%')
        </if>
        <!--是否已加企微 1.加入 0.未加入-->
        <if test="isJoin != null">
            AND scm.is_join = #{isJoin}
        </if>
        <!--跟进员工-->
        <if test="followStaff != null and followStaff !=''">
            and es.staff_name like concat('%', #{followStaff}, '%') and ecf.del_flag = 0
        </if>
        <!--来源-->
        <if test="addWay != null">
            <choose>
                <when test="addWay == -1">
                    AND scm.add_way in (0,2,3,4,5,6,7,8,9,109)
                </when>
                <otherwise>
                    AND scm.add_way = #{addWay}
                </otherwise>
            </choose>
        </if>
        <if test="userType != null and userType == 2">
            and edr.user_oneid=#{uuid}
        </if>
        <if test="custState == null || custState ==''">
            and scm.del_flag = 0
        </if>
        <if test="deleteTime != null">
            and DATE_FORMAT(sc.delete_time, '%Y-%m-%d') = #{deleteTime,jdbcType=DATE}
        </if>
        <if test="oneIds != null and oneIds.size>0">
            and scm.one_id in
            <foreach collection="oneIds" index="index" open="(" close=")" separator="," item="id">
                #{id}
            </foreach>
        </if>
        <if test="subscribe != null">
            AND scm.subscribe = #{subscribe}
        </if>
        <if test="isLoginApplet != null">
            AND scm.is_login_applet = #{isLoginApplet}
        </if>
        <if test="unionId != null">
            AND sc.union_id = #{unionId}
        </if>
        GROUP BY scm.one_id
        <if test="userType != null and userType == 1">
            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,
            scm.status status,
            scm.add_way,
            sc.cust_state,
            null staff_id,
            sc.avatar,
            '' nickName,
            sc.id customer_id,
            scm.subscribe,
            scm.is_login_applet,
            scm.merchant_id,
            scm.community,
            scm.building,
            scm.room
            FROM
            scrm_customer_merge scm
            LEFT JOIN
            scrm_customer sc ON scm.one_id = sc.one_id
            WHERE sc.merchantId in
            <if test="datalist != null and  datalist.size>0">
                <foreach collection="datalist" index="index" open="(" close=")" separator="," item="id">
                    #{id}
                </foreach>
                and scm.merchant_id in
                <foreach collection="datalist" index="index" open="(" close=")" separator="," item="id">
                    #{id}
                </foreach>
            </if>
            <if test="custState == null || custState ==''">
                and scm.del_flag = 0
            </if>
            <if test="followStaff != null and followStaff !=''">
                and sc.id is null
            </if>
            <!--开始时间 以合并表时间为准-->
            <if test="beginTime != null">
                and scm.created_time <![CDATA[>=]]> #{beginTime,jdbcType=DATE}
            </if>
            <!--结束时间 以合并表时间为准-->
            <if test="endTime != null">
                and scm.created_time <![CDATA[<=]]> #{endTime,jdbcType=DATE}
            </if>
            <!--客户名称-->
            <if test="customerName != null and customerName !=''">
                AND scm.customer_name like concat('%', #{customerName}, '%')
            </if>
            <!--当前状态-->
            <if test="status != null">
                AND scm.status = #{status}
            </if>
            <!--性别-->
            <if test="customerSex != null">
                AND scm.customer_sex = #{customerSex}
            </if>
            <!--手机号-->
            <if test="customerPhone != null and customerPhone !=''">
                AND scm.customer_phone like concat('%', #{customerPhone}, '%')
            </if>
            <!--是否已加企微 1.加入 0.未加入-->
            <if test="isJoin != null">
                AND scm.is_join = #{isJoin}
            </if>
            <if test="addWay != null">
                <choose>
                    <when test="addWay == -1">
                        AND scm.add_way in (0,2,3,4,5,6,7,8,9)
                    </when>
                    <otherwise>
                        AND scm.add_way = #{addWay}
                    </otherwise>
                </choose>
            </if>
            <if test="deleteTime != null">
                and DATE_FORMAT(sc.delete_time, '%Y-%m-%d') = #{deleteTime,jdbcType=DATE}
            </if>
            <if test="oneIds != null and oneIds.size>0">
                and scm.one_id in
                <foreach collection="oneIds" index="index" open="(" close=")" separator="," item="id">
                    #{id}
                </foreach>
            </if>
            <if test="subscribe != null">
                AND scm.subscribe = #{subscribe}
            </if>
            <if test="isLoginApplet != null">
                AND scm.is_login_applet = #{isLoginApplet}
            </if>
            <if test="unionId != null">
                AND sc.union_id = #{unionId}
            </if>
        </if>
        )t
        <where>
            <trim prefixOverrides="and">
                <if test="custState != null and custState != ''">
                    cust_state = #{custState}
                </if>
                <if test="staffIds != null and staffIds.size() > 0">
                    and staff_id in
                    <foreach collection="staffIds" open="(" close=")" item="staffId" separator=",">
                        #{staffId}
                    </foreach>
                </if>
                <if test="nickName != null and nickName != ''">
                    and nickName like concat('%', #{nickName}, '%')
                </if>
            </trim>
        </where>
        GROUP BY t.oneid
        ORDER by t.addTime DESC
    </select>

    <select id="getCodeCustomerByPage" parameterType="cn.com.glsx.entmicro.model.request.CodeCustomerSearchRpcRequest"
            resultMap="customerListMapResponse">
        select scm.id,
        scm.one_id oneid,
        scm.customer_name,
        scm.customer_sex,
        scm.`type` stype,
        ecf.created_time addTime,
        sc.del_flag status,
        ecf.state,
        edr.id staff_id,
        sc.avatar
        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)
        WHERE 1=1 and edr.del_flag=0
        <if test="datalist != null and  datalist.size>0">
            and edr.relevance_merchant in
            <foreach collection="datalist" index="index" open="(" close=")" separator="," item="id">
                #{id}
            </foreach>
            and scm.merchant_id in
            <foreach collection="datalist" index="index" open="(" close=")" separator="," item="id">
                #{id}
            </foreach>
        </if>
        <!--开始时间-->
        <if test="beginTime != null">
            and ecf.created_time <![CDATA[>=]]> #{beginTime,jdbcType=DATE}
        </if>
        <!--结束时间-->
        <if test="endTime != null">
            and ecf.created_time <![CDATA[<=]]> #{endTime,jdbcType=DATE}
        </if>
        <!--客户名称-->
        <if test="customerName != null and customerName !=''">
            AND scm.customer_name like '%${customerName}%'
        </if>
        <!--当前状态-->
        <if test="status != null">
            AND sc.del_flag = #{status}
        </if>
        <if test="custState != null and custState != ''">
            and ecf.state = #{custState}
        </if>
        <if test="staffIds != null and staffIds.size() > 0">
            and edr.id in
            <foreach collection="staffIds" open="(" close=")" item="staffId" separator=",">
                #{staffId}
            </foreach>
        </if>
        GROUP BY
        scm.one_id
        ORDER by ecf.created_time DESC
    </select>

    <!--客户详情信息-->
    <select id="getCustomerByOneId" resultType="cn.com.glsx.entmicro.model.response.CustomerInfoResponse">
        select scm.id,
               scm.one_id         oneId,
               scm.avatar,
               scm.wechat_id      weChatId,
               scm.customer_name  customerName,
               scm.customer_phone customerPhone,
               scm.customer_sex   customerSex,
               scm.`type`,
               scm.`position`,
               scm.corp_name      corpName,
               scm.created_time   addTime,
               scm.status        status,
               scm.add_way        addWay,
               scm.is_join        isJoin,
               dcc.code_name      sourceChannel,
               sc.id customerId,
               scm.id_card idCard,
               scm.`union_id` unionId,
               case scm.subscribe when '1' then '是' else '否' end as subscribeName,
               case scm.is_login_applet when '1' then '是' else '否' end as isLoginAppletName,
               scm.community,
               scm.building,
               scm.room,
               scm.homeowner_type homeownerType
        from scrm_customer_merge scm
                 LEFT JOIN scrm_customer sc ON sc.one_id = scm.one_id
                 LEFT JOIN (SELECT code_name,code_state FROM drac_channel_code WHERE code_system = 0) dcc ON dcc.code_state = sc.cust_state
        where scm.one_id = #{oneId} and scm.merchant_Id=#{merchantId}
          and scm.del_flag = 0
          group by scm.one_Id
        order by scm.created_time desc
    </select>

    <!--oneids查询客户-->
    <select id="getCustomerByOneIds" resultMap="customerInfoMapResponse">
        select id,
        one_id oneId
        from scrm_customer_merge
        where one_id in
        <foreach collection="oneIds" open="(" close=")" separator="," item="oneId">
            #{oneId}
        </foreach>
        and merchant_Id=#{merchantId}
        and del_flag = 0
        order by created_time desc
    </select>
    <select id="findByOneIdAndMerchant" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from scrm_customer_merge where
        one_id=#{oneId} and merchant_id=#{merchantId} LIMIT 1
    </select>

    <!-- 删除客户 -->
    <update id="logicDelete">
        UPDATE scrm_customer_merge
        <set>
            del_flag = -1,
            updated_time = now()
        </set>
        WHERE `one_id` = #{oldOneId} and merchant_id=#{merchantId}
    </update>

    <update id="batchUpdate" parameterType="java.util.List">
        <foreach collection="customers" item="item" index="index" open="" close="" separator=";">
            update scrm_customer_merge
            <trim prefix="set" suffixOverrides=",">
                <!--客户电话-->
                <if test="item.customerPhone != null and item.customerPhone !=''">
                    `customer_phone` = #{item.customerPhone},
                </if>
                <!--客户名称-->
                <if test="item.customerName != null and item.customerName !=''">
                    `customer_name` = #{item.customerName},
                </if>
                <!--性别-->
                <if test="item.customerSex != null">
                    `customer_sex` = #{item.customerSex},
                </if>
                <!--性别-->
                <if test="item.idCard != null and item.idCard != ''">
                    `id_card` = #{item.idCard},
                </if>
            </trim>
            where `id` = ${item.id}
        </foreach>
    </update>

    <update id="updateCustPhone">
        update scrm_customer_merge
        set customer_phone = #{remarkMobiles}
        where one_id = #{oneId}
        and merchant_id = #{merchantId}
    </update>

    <update id="updatePhone">
        update scrm_customer
        set customer_phone = #{remarkMobiles}
        where id = #{id}
    </update>

    <update id="updatePhoneByUserId">
        UPDATE scrm_customer_merge scm
            LEFT JOIN scrm_customer sc ON scm.one_id = sc.one_id
            SET scm.customer_phone = #{customerPhone}
        WHERE sc.user_id = #{userId}
          AND scm.merchant_id = #{merchantId}
    </update>

    <select id="getCustomerIdByPage" parameterType="cn.com.glsx.entmicro.model.request.CustomerSearchRequest"
            resultType="Integer">
        SELECT t2.id FROM (
        select
        t.id
        from (
        select
        scm.id,
        scm.one_id,
        scm.created_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)
        LEFT JOIN
        entmicro_staff_depart_relation edr on (edr.staff_id = es.id)
        WHERE 1=1
        <if test="datalist != null and  datalist.size>0">
            and edr.relevance_merchant in
            <foreach collection="datalist" index="index" open="(" close=")" separator="," item="id">
                #{id}
            </foreach>
            and scm.merchant_id in
            <foreach collection="datalist" index="index" open="(" close=")" separator="," item="id">
                #{id}
            </foreach>
        </if>
        <!--开始时间-->
        <if test="beginTime != null">
            and sc.created_time <![CDATA[>=]]> #{beginTime,jdbcType=DATE}
        </if>
        <!--结束时间-->
        <if test="endTime != null">
            and sc.created_time <![CDATA[<=]]> #{endTime,jdbcType=DATE}
        </if>
        <!--客户名称-->
        <if test="customerName != null and customerName !=''">
            AND scm.customer_name like '%${customerName}%'
        </if>
        <!--当前状态-->
        <if test="status != null">
            AND scm.status = #{status}
        </if>
        <!--性别-->
        <if test="customerSex != null">
            AND scm.customer_sex = #{customerSex}
        </if>
        <!--手机号-->
        <if test="customerPhone != null and customerPhone !=''">
            AND scm.customer_phone like '%${customerPhone}%'
        </if>
        <!--是否已加企微 1.加入 0.未加入-->
        <if test="isJoin != null">
            AND scm.is_join = #{isJoin}
        </if>
        <!--跟进员工-->
        <if test="followStaff != null and followStaff !=''">
            and es.staff_name like concat('%', #{followStaff}, '%') and ecf.del_flag = 0
        </if>
        <!--来源-->
        <if test="addWay != null">
            AND scm.add_way = #{addWay}
        </if>
        <if test="userType != null and userType == 2">
            and edr.user_oneid=#{uuid}
        </if>
        <if test="custState == null || custState ==''">
            and scm.del_flag = 0
        </if>
        <if test="deleteTime != null">
            and DATE_FORMAT(sc.delete_time, '%Y-%m-%d') = #{deleteTime,jdbcType=DATE}
        </if>
        <if test="oneIds != null and oneIds.size>0">
            and scm.one_id in
            <foreach collection="oneIds" index="index" open="(" close=")" separator="," item="id">
                #{id}
            </foreach>
        </if>
        GROUP BY scm.one_id
        <if test="userType != null and userType == 1">
            union all
            select
            scm.id,
            scm.one_id,
            scm.created_time
            FROM
            scrm_customer_merge scm
            LEFT JOIN
            scrm_customer sc ON scm.one_id = sc.one_id
            WHERE sc.merchantId in
            <if test="datalist != null and  datalist.size>0">
                <foreach collection="datalist" index="index" open="(" close=")" separator="," item="id">
                    #{id}
                </foreach>
                and scm.merchant_id in
                <foreach collection="datalist" index="index" open="(" close=")" separator="," item="id">
                    #{id}
                </foreach>
            </if>
            <if test="custState == null || custState ==''">
                and scm.del_flag = 0
            </if>
            <if test="followStaff != null and followStaff !=''">
                and sc.id is null
            </if>
            <!--开始时间-->
            <if test="beginTime != null">
                and sc.created_time <![CDATA[>=]]> #{beginTime,jdbcType=DATE}
            </if>
            <!--结束时间-->
            <if test="endTime != null">
                and sc.created_time <![CDATA[<=]]> #{endTime,jdbcType=DATE}
            </if>
            <!--客户名称-->
            <if test="customerName != null and customerName !=''">
                AND scm.customer_name like '%${customerName}%'
            </if>
            <!--当前状态-->
            <if test="status != null">
                AND scm.status = #{status}
            </if>
            <!--性别-->
            <if test="customerSex != null">
                AND scm.customer_sex = #{customerSex}
            </if>
            <!--手机号-->
            <if test="customerPhone != null and customerPhone !=''">
                AND scm.customer_phone like '%${customerPhone}%'
            </if>
            <!--是否已加企微 1.加入 0.未加入-->
            <if test="isJoin != null">
                AND scm.is_join = #{isJoin}
            </if>
            <if test="addWay != null">
                AND scm.add_way = #{addWay}
            </if>
            <if test="deleteTime != null">
                and DATE_FORMAT(sc.delete_time, '%Y-%m-%d') = #{deleteTime,jdbcType=DATE}
            </if>
            <if test="oneIds != null and oneIds.size>0">
                and scm.one_id in
                <foreach collection="oneIds" index="index" open="(" close=")" separator="," item="id">
                    #{id}
                </foreach>
            </if>
        </if>
        )t
        <where>
            <trim prefixOverrides="and">
                <if test="custState != null and custState != ''">
                    cust_state = #{custState}
                </if>
                <if test="staffIds != null and staffIds.size() > 0">
                    and staff_id in
                    <foreach collection="staffIds" open="(" close=")" item="staffId" separator=",">
                        #{staffId}
                    </foreach>
                </if>
                <if test="nickName != null and nickName != ''">
                    and nickName like concat('%', #{nickName}, '%')
                </if>
            </trim>
        </where>
        GROUP BY t.one_id
        ORDER by t.created_time DESC
        ) t2
    </select>

    <select id="getCustomerById" resultMap="customerListMapResponse">
        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 addTime1,
        scm.status status1,
        scm.add_way,
        sc.cust_state,
        edr.id staff_id,
        sc.avatar,
        GROUP_CONCAT(DISTINCT sc.customer_name) nickName,
        sc.id customer_id
        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)
        WHERE scm.id IN
        <foreach collection="mergeIdList" open="(" close=")" item="id" separator=",">
            #{id}
        </foreach>
        GROUP BY scm.one_id
        ORDER BY scm.created_time DESC
    </select>

    <select id="selectCustomerExistPhoneByUserId" resultType="java.lang.String">
        SELECT scm.customer_phone FROM
        scrm_customer_merge scm
        LEFT JOIN
        scrm_customer sc ON scm.one_id = sc.one_id AND sc.del_flag = 0
        WHERE sc.user_id IN
        <foreach collection="userIdList" open="(" close=")" item="userId" separator=",">
            #{userId}
        </foreach>
        <if test="merchantId != null">
            AND scm.merchant_id = #{merchantId}
        </if>
        AND scm.customer_phone IS NOT NULL
        and scm.customer_phone != ''
        GROUP BY scm.customer_phone
    </select>

    <select id="selectOneIdByCustomerPhone" resultType="java.lang.String">
        SELECT t.one_id FROM (
            SELECT scm.one_id, scm.customer_phone, scm.created_time FROM scrm_customer_merge scm
            WHERE scm.customer_phone = #{phone}
            AND scm.merchant_id = #{merchantId}
            AND scm.del_flag = 0
            HAVING 1
            ORDER BY scm.created_time DESC
        )t GROUP BY t.customer_phone
    </select>
    <select id="selectAllId" resultType="java.lang.Integer">
        select id from ${tableName}
    </select>
    <select id="selectCustomerMergeByOneIdAndMerchantId"
            resultMap="BaseResultMap">
      select customer_name
            ,customer_phone
            ,customer_sex
            ,`type`
            ,`position`
            ,corp_name
            ,one_id
            ,avatar
            ,wechat_id
            ,created_time
            ,created_time
            ,del_flag
            ,status
            ,add_way
            ,is_join
            ,merchant_id
            ,union_id
            ,id_card
      from ${tableName} where one_id=#{oneId} and merchant_id=#{merchantId}
      limit 1
    </select>
    <select id="findCustomerByOpenCondition" resultMap="customerListMapResponse">
        select
        t.id,
        t.oneid,
        t.customer_name,
        t.customer_phone,
        t.customer_sex,
        t.is_join,
        t.stype,
        t.addTime,
        t.status,
        t.add_way,
        t.cust_state state,
        t.staff_id,
        t.avatar,
        t.nickName,
        t.customer_id
        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,
        scm.status status,
        scm.add_way,
        sc.cust_state,
        edr.id staff_id,
        sc.avatar,
        GROUP_CONCAT(DISTINCT sc.customer_name) nickName,
        sc.id customer_id
        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)
        WHERE 1=1
        and edr.relevance_merchant=#{merchantId}
        and scm.merchant_id=#{merchantId}
        <if test="mergeIds != null and mergeIds.size() > 0">
            and scm.id in
            <foreach collection="mergeIds" open="(" close=")" item="id" separator=",">
                #{id}
            </foreach>
        </if>
        <!--客户名称-->
        <if test="customerName != null and customerName !=''">
            AND scm.customer_name like '%${customerName}%'
        </if>
        <!--手机号-->
        <if test="customerPhone != null and customerPhone !=''">
            AND scm.customer_phone like '%${customerPhone}%'
        </if>
        <!--是否已加企微 1.加入 0.未加入-->
        <if test="status != null">
            AND scm.is_join = #{status}
        </if>
        <if test="staffId != null">
            AND es.id = #{isJoin}
        </if>
        GROUP BY scm.one_id
        <if test="staffId == null">
            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,
            scm.status status,
            scm.add_way,
            sc.cust_state,
            null staff_id,
            sc.avatar,
            '' nickName,
            sc.id customer_id
            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}
            <if test="mergeIds != null and mergeIds.size() > 0">
                and scm.id in
                <foreach collection="mergeIds" open="(" close=")" item="id" separator=",">
                    #{id}
                </foreach>
            </if>
            <!--客户名称-->
            <if test="customerName != null and customerName !=''">
                AND scm.customer_name like '%${customerName}%'
            </if>
            <!--手机号-->
            <if test="customerPhone != null and customerPhone !=''">
                AND scm.customer_phone like '%${customerPhone}%'
            </if>
            <!--是否已加企微 1.加入 0.未加入-->
            <if test="status != null">
                AND scm.is_join = #{status}
            </if>
        </if>
        )t
        GROUP BY t.oneid
    </select>

    <select id="listByBatchPhone" resultMap="BaseResultMap">
        select one_id, is_join, customer_phone
        from scrm_customer_merge
        where del_flag = 0
        and customer_phone in
        <foreach collection="phoneList" item="phone" open="(" separator="," close=")">
            #{phone}
        </foreach>
        and merchant_id = #{merchantId}
    </select>

    <select id="listByBatchUnionId" resultMap="BaseResultMap">
        select one_id, is_join, customer_phone, union_id
        from scrm_customer_merge
        where del_flag = 0
        and union_id in
        <foreach collection="unionIdList" item="unionId" open="(" separator="," close=")">
            #{unionId}
        </foreach>
        and merchant_id = #{merchantId}
    </select>


    <select id="findCustomerByUserId" resultMap="customerInfoMapResponse">
        SELECT scm.* FROM
        scrm_customer_merge scm
        LEFT JOIN
        scrm_customer sc ON scm.one_id = sc.one_id AND sc.del_flag = 0
        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 sc.user_id=#{userId}
            AND scm.merchant_id = #{merchantId} AND edr.relevance_merchant=#{merchantId}
        GROUP BY scm.one_id
    </select>

    <select id="getUserPropertyMessageByOneId" resultType="cn.com.glsx.entmicro.dto.CustomerPropertyInfoDTO">
        select
            one_id as oneId,community, building, room,
            community_id as communityId, building_id as buildingId,
            room_id as roomId,homeowner_type as homeownerType
        from scrm_customer_merge
        where one_id = #{oneId} and del_flag = 0
    </select>

    <select id="findUnionIdsByOneId" resultType="String">
        SELECT
            distinct if(sc.union_id is null, scm.union_id, sc.union_id)
        FROM scrm_entmicro.scrm_customer sc
         left join scrm_entmicro.scrm_customer_merge scm on sc.one_id = scm.one_id
        where sc.del_flag = 0 and scm.del_flag = 0 and sc.one_id  = #{oneId}
    </select>
    <select id="findEntmicroCusByMerchantId"
            resultType="cn.com.glsx.entmicro.model.sop.EntmiroCustomerInfo">
        select
        scm.one_id oneid,
        sc.customer_name as customerName,
        sc.user_id as externalUserId,
        sc.avatar as avatar,
        scm.customer_phone as customerPhone,
        es.user_id as userId,
        es.staff_name as userName,
        scm.is_join as isJoin
        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)
        WHERE 1=1
        and edr.relevance_merchant=#{merchantId} and scm.del_flag=0
        and scm.merchant_id=#{merchantId}
        group by scm.one_id
    </select>
</mapper>