张延森 5 jaren geleden
bovenliggende
commit
d68407c650

+ 11
- 0
src/main/java/com/huiju/estateagents/mapper/TaRecommendCustomerMapper.java Bestand weergeven

@@ -199,4 +199,15 @@ public interface TaRecommendCustomerMapper extends BaseMapper<TaRecommendCustome
199 199
                                                            @Param("startDate") LocalDateTime startDate,
200 200
                                                            @Param("endDate") LocalDateTime endDate,
201 201
                                                            @Param("personId") String personId);
202
+
203
+    /**
204
+     * 查询 person 对应 楼盘 的客户
205
+     * 如果对应的楼盘没有客户, 则返回无楼盘的, 小程序级别的客户信息
206
+     * 如果没有无小程序级别客户, 则返回最早的一条
207
+     * @param orgId
208
+     * @param personId
209
+     * @param buildingId
210
+     * @return
211
+     */
212
+    TaRecommendCustomer getCustomerByProjectOrOrg(@Param("orgId") Integer orgId, @Param("personId") String personId, @Param("buildingId") String buildingId);
202 213
 }

+ 114
- 121
src/main/java/com/huiju/estateagents/service/impl/TaPersonServiceImpl.java Bestand weergeven

@@ -21,6 +21,7 @@ import com.huiju.estateagents.service.*;
21 21
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
22 22
 import lombok.extern.slf4j.Slf4j;
23 23
 import org.apache.commons.collections.CollectionUtils;
24
+import org.checkerframework.checker.units.qual.C;
24 25
 import org.springframework.beans.factory.annotation.Autowired;
25 26
 import org.springframework.context.ApplicationContext;
26 27
 import org.springframework.stereotype.Service;
@@ -872,14 +873,12 @@ public class TaPersonServiceImpl extends ServiceImpl<TaPersonMapper, TaPerson> i
872 873
         //
873 874
         LocalDateTime now = LocalDateTime.now();
874 875
 
875
-        // 1. 更新当前人员
876
+        // 更新人员手机号
876 877
         List<TaPerson> persons = getPersonsByOpenId(openid);
877 878
         if (null == persons || persons.size() != 1) {
878 879
             throw new Exception("当前人员 openid 异常");
879 880
         }
880 881
         TaPerson person = persons.get(0);
881
-
882
-        // 更新人员手机号
883 882
         person.setPhone(phone);
884 883
         person.setAuthPhoneTime(now);
885 884
         if (StringUtils.isEmpty(person.getBuildingId())) {
@@ -889,153 +888,147 @@ public class TaPersonServiceImpl extends ServiceImpl<TaPersonMapper, TaPerson> i
889 888
             person.setPersonType(CommConstant.PERSON_ESTATE_CUSTOMER);
890 889
         }
891 890
         updateById(person);
892
-        fixCustomerPersonId(person);
893
-
894
-        // 2. 发放积分
895
-        applicationContext.publishEvent(new EventBus(person, EventBus.EventType.SignUpAgent, orgId));
896 891
 
897
-        // 3. 处理推广人
898
-        TaPerson recPerson = taPersonMapper.getById(promoter);
899
-        if (recPerson != null && CommConstant.STATUS_DELETE.equals(recPerson.getStatus())) {
900
-            recPerson = null;
901
-        }
892
+        // 只要授权手机号, 就是客户
893
+        // 或者 依据手机号反更新推荐客户中 person_id
894
+        // 默认场景, 普通人推荐
895
+        TaRecommendCustomer customer = newOrFixCustomerBy(person, promoter, buildingId);
902 896
 
903
-        TaUser taUser = null;
904
-        List<String> withBuildings = null;  // 推广人授权的楼盘
905
-        boolean isAssociated = false;       // 传入的楼盘与推广人是否有关联关系
906
-        boolean isConsultant = false;       // 是否置业顾问
907
-        if (!StringUtils.isEmpty(promoter)) {
908
-            taUser = taUserMapper.getUserByPerson(orgId, promoter);
909
-            if (null != taUser) {
910
-                isConsultant = taUser != null && taUser.getIsConsultant() != null && taUser.getIsConsultant();
911
-                List<TaPerson> userPerons = new ArrayList<>();
912
-                userPerons.add(recPerson);
913
-                taUser.setPersonIds(userPerons);
914
-
915
-                withBuildings = taUserMapper.getBuildingIdsOf(taUser.getUserId(), null);
916
-
917
-                if (null != taUser.getIsAdmin() && taUser.getIsAdmin()) {
918
-                    isAssociated = true;
919
-                } else {
920
-                    if (null != withBuildings && withBuildings.size() > 0 && !StringUtils.isEmpty(buildingId)) {
921
-                        isAssociated = withBuildings.contains(buildingId);
922
-                    }
923
-                }
924
-            }
925
-        }
926
-
927
-        // 楼盘
928
-        TaBuilding taBuilding = null;
929
-        if (!StringUtils.isEmpty(buildingId)) {
930
-            taBuilding = taBuildingMapper.selectById(buildingId);
931
-            if (taBuilding.getStatus().equals(CommConstant.STATUS_DELETE)) {
932
-                taBuilding = null;
933
-            }
934
-        }
935
-
936
-        // 没有置业, 也没楼盘
937
-        if ((recPerson == null || !isConsultant) && taBuilding == null) {
938
-            TaCustomerFrom customerFrom = new TaCustomerFrom();
939
-            customerFrom.setPersonId(person.getPersonId());
940
-            customerFrom.setPersonName(StringUtils.ifNull(person.getName(), person.getNickname()));
941
-            customerFrom.setTargetType(targetType);
942
-            customerFrom.setTargetId(targetId);
943
-            customerFrom.setTargetName(targetName);
944
-            customerFrom.setOrgId(orgId);
945
-            customerFrom.setSceneId(sceneId);
946
-            customerFrom.setIsOrgFirst(true);   // 授权手机号, 肯定是第一次
947
-            customerFrom.setIsProjectFirst(false);
948
-            customerFrom.setCreateDate(now);
949
-            if (null != recPerson) {
950
-                customerFrom.setSharePersonId(recPerson.getPersonId());
951
-                customerFrom.setSharePersonName(StringUtils.ifNull(recPerson.getName(), recPerson.getNickname()));
952
-            }
953
-
954
-            taCustomerFromMapper.insert(customerFrom);
955
-
956
-
957
-            TaRecommendCustomer customer = copyByPerosn(person, new TaRecommendCustomer());
958
-            customer.setVerifyStatus(CommConstant.VERIFY_READY);
959
-            customer.setStatus(CommConstant.CUSTOMER_REPORT);
960
-            customer.setEntryType(CommConstant.ENTRY_INPUT);
961
-            customer.setCreateDate(now);
962
-            customer.setReportDate(now);
963
-            customer.setRecommendPerson(promoter);
964
-            taRecommendCustomerMapper.insert(customer);
965
-
966
-            return ;
967
-        }
968
-
969
-        // 3. 修正客户信息
970
-        List<TaBuilding> withBuildingList = null;
971
-        if (null != withBuildings && withBuildings.size() > 0) {
972
-            QueryWrapper<TaBuilding> queryBuilding = new QueryWrapper<>();
973
-            queryBuilding.in("building_id", withBuildings);
974
-            withBuildingList = taBuildingMapper.selectList(queryBuilding);
975
-        }
976
-        List<TaRecommendCustomer> customers = mergeCustomerByPhone(person, taUser, taBuilding, withBuildingList, isAssociated, now);
977
-        if (null == customers || customers.size() == 0) {
978
-            throw new Exception("生成客户信息异常, 请联系技术人员");
979
-        }
897
+        // 发放积分
898
+        applicationContext.publishEvent(new EventBus(person, EventBus.EventType.SignUpAgent, orgId));
980 899
 
981
-        // 4. 户来源, 注意不是用户来源
900
+        // 客户来源
982 901
         TaCustomerFrom customerFrom = new TaCustomerFrom();
983 902
         customerFrom.setPersonId(person.getPersonId());
984 903
         customerFrom.setPersonName(StringUtils.ifNull(person.getName(), person.getNickname()));
904
+        customerFrom.setCustomerId(customer.getCustomerId());
985 905
         customerFrom.setTargetType(targetType);
986 906
         customerFrom.setTargetId(targetId);
987 907
         customerFrom.setTargetName(targetName);
988 908
         customerFrom.setOrgId(orgId);
989 909
         customerFrom.setSceneId(sceneId);
990 910
         customerFrom.setIsOrgFirst(true);   // 授权手机号, 肯定是第一次
911
+        customerFrom.setIsProjectFirst(false);
991 912
         customerFrom.setCreateDate(now);
992 913
 
993
-        if (null != taBuilding) {
914
+        // 推广人
915
+        TaPerson recPerson = StringUtils.isEmpty(promoter) ? null : taPersonMapper.getById(promoter);
916
+        // 是否置业顾问
917
+        boolean isConsultant = null != recPerson && CommConstant.PERSON_REALTY_CONSULTANT.equals(recPerson.getPersonType());
918
+        // 推广人授权的楼盘
919
+        List<String> withBuildings = isConsultant ? taUserMapper.getBuildingIdsOf(recPerson.getUserId(), null) : null;
920
+        // 推荐楼盘
921
+        TaBuilding recBuilding = StringUtils.isEmpty(buildingId) ? null : taBuildingMapper.selectById(buildingId);
922
+
923
+        // 传入的楼盘与推广人是否有关联关系
924
+        boolean isAssociated = null != withBuildings && withBuildings.contains(buildingId);
925
+
926
+        // 如果有推荐楼盘
927
+        if (null != recBuilding) {
994 928
             customerFrom.setBuildingId(buildingId);
995
-            customerFrom.setBuildingName(taBuilding.getBuildingName());
996
-            QueryWrapper<TaCustomerFrom> queryWrapper = new QueryWrapper<TaCustomerFrom>();
997
-            queryWrapper.eq("person_id", person.getPersonId());
998
-            queryWrapper.eq("org_id", orgId);
999
-            queryWrapper.eq("building_id", buildingId);
1000
-            if (taCustomerFromMapper.selectList(queryWrapper).size() < 1){
1001
-                customerFrom.setIsProjectFirst(true);
1002
-            }
929
+            customerFrom.setBuildingName(recBuilding.getBuildingName());
930
+            customerFrom.setIsProjectFirst(true);
931
+
932
+            // 客户没楼盘 - 小程序级别公客, 则变成项目级别公客
933
+            if (StringUtils.isEmpty(customer.getBuildingId())) {
934
+                customer.setBuildingId(buildingId);
1003 935
 
1004
-            for (TaRecommendCustomer cust : customers) {
1005
-                if (buildingId.equals(cust.getBuildingId())) {
1006
-                    customerFrom.setCustomerId(cust.getCustomerId());
936
+                // 如果项目跟推广人是关联关系, 那么就是私客
937
+                if (isAssociated) {
938
+                    customer.setRealtyConsultant(recPerson.getUserId().toString());
1007 939
                 }
940
+
941
+                taRecommendCustomerMapper.updateById(customer);
1008 942
             }
1009
-        } else if (isConsultant && null != withBuildingList && withBuildingList.size() > 0) {
1010
-            // 没有楼盘则使用置业授权的楼盘
1011
-            customerFrom.setBuildingId(withBuildingList.get(0).getBuildingId());
1012
-            customerFrom.setBuildingName(withBuildingList.get(0).getBuildingName());
1013 943
         }
944
+
945
+        // 推荐人不为空
1014 946
         if (null != recPerson) {
1015 947
             customerFrom.setSharePersonId(recPerson.getPersonId());
1016
-            customerFrom.setSharePersonName(StringUtils.ifNull(recPerson.getName(), recPerson.getNickname()));
948
+            customerFrom.setSharePersonName(recPerson.getNickname());
949
+
950
+            // 如果没有关联关系
951
+            // 置业顾问推荐其他楼盘, 那么需要生成该置业的私客
952
+            if (isConsultant && !isAssociated) {
953
+                String consultantBuildingId = null != withBuildings ? withBuildings.get(0) : null;
954
+                if (!StringUtils.isEmpty(consultantBuildingId)) {
955
+                    TaBuilding onsultantBuilding = taBuildingMapper.selectById(consultantBuildingId);
956
+                    TaRecommendCustomer newCustomer = copyByPerosn(person, new TaRecommendCustomer());
957
+                    newCustomer.setBuildingId(onsultantBuilding.getBuildingId());
958
+                    newCustomer.setRealtyConsultant(recPerson.getUserId().toString());
959
+                    newCustomer.setRecommendPerson(recPerson.getPersonId());
960
+                    newCustomer.setVerifyStatus(CommConstant.VERIFY_AGREE);
961
+                    newCustomer.setStatus(CommConstant.CUSTOMER_REPORT);
962
+                    newCustomer.setEntryType(CommConstant.ENTRY_INPUT);
963
+                    newCustomer.setCreateDate(now);
964
+                    newCustomer.setReportDate(now);
965
+                    taRecommendCustomerMapper.insert(newCustomer);
966
+
967
+                    TaCustomerFrom customerFrom2 = new TaCustomerFrom();
968
+                    customerFrom2.setPersonId(person.getPersonId());
969
+                    customerFrom2.setPersonName(StringUtils.ifNull(person.getName(), person.getNickname()));
970
+                    customerFrom2.setCustomerId(newCustomer.getCustomerId());
971
+                    customerFrom2.setBuildingId(onsultantBuilding.getBuildingId());
972
+                    customerFrom2.setBuildingName(onsultantBuilding.getBuildingName());
973
+                    customerFrom2.setSharePersonId(recPerson.getPersonId());
974
+                    customerFrom2.setSharePersonName(recPerson.getNickname());
975
+                    customerFrom2.setTargetType(targetType);
976
+                    customerFrom2.setTargetId(targetId);
977
+                    customerFrom2.setTargetName(targetName);
978
+                    customerFrom2.setOrgId(orgId);
979
+                    customerFrom2.setSceneId(sceneId);
980
+                    customerFrom2.setIsOrgFirst(true);
981
+                    customerFrom2.setIsProjectFirst(true);
982
+                    customerFrom2.setCreateDate(now);
983
+                    taCustomerFromMapper.insert(customerFrom2);
984
+                }
985
+            }
1017 986
         }
1018 987
 
1019 988
         taCustomerFromMapper.insert(customerFrom);
1020 989
     }
1021 990
 
1022
-    private void fixCustomerPersonId(TaPerson person) {
1023
-        // 先更新之前的数据, 有一种可能的情况, 客户是推荐的,之前未进入过小程序
1024
-        // 后面所有的查询就可以不需要 phone , 改用 person_id 了
1025
-        QueryWrapper<TaRecommendCustomer> q1 = new QueryWrapper<>();
1026
-        q1.eq("org_id", person.getOrgId());
1027
-        q1.eq("phone", person.getPhone());
1028
-        q1.isNull("person_id");
1029
-        List<TaRecommendCustomer> customers = taRecommendCustomerMapper.selectList(q1);
1030
-        if (null != customers) {
1031
-            for (int i = 0; i < customers.size(); i++) {
1032
-                TaRecommendCustomer cust = customers.get(i);
1033
-                cust = copyByPerosn(person, cust);
1034
-                taRecommendCustomerMapper.updateById(cust);
991
+    private TaRecommendCustomer newOrFixCustomerBy(TaPerson person, String promoter, String buildingId) {
992
+        QueryWrapper<TaRecommendCustomer> query = new QueryWrapper<>();
993
+        query.eq("org_id", person.getOrgId());
994
+        query.eq("phone", person.getPhone());
995
+        query.orderByAsc("create_date");
996
+        List<TaRecommendCustomer> customers = taRecommendCustomerMapper.selectList(query);
997
+
998
+        TaRecommendCustomer customer = null;
999
+        if (null != customers && customers.size() > 0) {
1000
+            for (TaRecommendCustomer cust: customers) {
1001
+                if (null == customer && StringUtils.isEmpty(cust.getBuildingId())) {
1002
+                    customer = cust;
1003
+                }
1004
+
1005
+                if (!StringUtils.isEmpty(buildingId) && buildingId.equals(cust.getBuildingId())) {
1006
+                    customer = cust;
1007
+                }
1008
+
1009
+                // 先更新之前的数据, 有一种可能的情况, 客户是推荐的,之前未进入过小程序
1010
+                // 后面所有的查询就可以不需要 phone , 改用 person_id 了
1011
+                if (StringUtils.isEmpty(cust.getPersonId())) {
1012
+                    cust = copyByPerosn(person, cust);
1013
+                    taRecommendCustomerMapper.updateById(cust);
1014
+                }
1035 1015
             }
1036 1016
         }
1037
-    }
1038 1017
 
1018
+        if (null == customer) {
1019
+            LocalDateTime now = LocalDateTime.now();
1020
+            customer = copyByPerosn(person, new TaRecommendCustomer());
1021
+            customer.setVerifyStatus(CommConstant.VERIFY_AGREE);
1022
+            customer.setStatus(CommConstant.CUSTOMER_REPORT);
1023
+            customer.setEntryType(StringUtils.isEmpty(promoter) ? CommConstant.ENTRY_VOLUNTEER : CommConstant.ENTRY_INPUT);
1024
+            customer.setCreateDate(now);
1025
+            customer.setReportDate(now);
1026
+            customer.setRecommendPerson(promoter);
1027
+            taRecommendCustomerMapper.insert(customer);
1028
+        }
1029
+
1030
+        return customer;
1031
+    }
1039 1032
 
1040 1033
     /**
1041 1034
      * 此处逻辑按照复杂流程处理

+ 14
- 1
src/main/resources/mapper/TaRecommendCustomerMapper.xml Bestand weergeven

@@ -857,5 +857,18 @@ FROM
857 857
         order by s.report_date desc
858 858
     </select>
859 859
 
860
-
860
+    <select id="getCustomerByProjectOrOrg" resultType="com.huiju.estateagents.entity.TaRecommendCustomer">
861
+        SELECT
862
+            *
863
+        FROM
864
+            ta_recommend_customer t
865
+        WHERE
866
+            t.org_id = #{orgId}
867
+            AND t.person_id = #{personId}
868
+        ORDER BY
869
+            IF( t.building_id = #{buildingId}, 0, 1 ) ASC,
870
+            IF( ifnull( t.building_id, '' ) = '', 0, 1 ) ASC,
871
+            t.create_date ASC
872
+        LIMIT 1;
873
+    </select>
861 874
 </mapper>