EntmicroFollowService.java
7.43 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
package cn.com.glsx.entmicro.service;
import cn.com.glsx.entmicro.entity.Customer;
import cn.com.glsx.entmicro.entity.EntmicroCustomerFollow;
import cn.com.glsx.entmicro.mapper.CustomerMapper;
import cn.com.glsx.entmicro.mapper.EntmicroCustomerFollowMapper;
import cn.com.glsx.entmicro.model.request.GetStaffFollowCustomerRequest;
import cn.com.glsx.entmicro.model.response.CustomerFollowStaffResponse;
import cn.com.glsx.entmicro.model.response.CustomerInfo;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* @author fengzhi
* @Description: 客户Service
* @date 2021/9/16 14:40
* matches 120
*/
@Service
@Slf4j
public class EntmicroFollowService {
@Autowired
private EntmicroCustomerFollowMapper customerFollowMapper;
@Autowired
private CustomerMapper customerMapper;
@Resource
private EntmicroStaffService entmicroStaffService;
@Resource
private MerchantService merchantService;
/**
* 批量插入跟进人信息
*
* @param customerFollowRelations 跟进人
* @return
*/
@Transactional(rollbackFor = RpcServiceException.class)
public EntmicroCustomerFollow insert(List<EntmicroCustomerFollow> customerFollowRelations) {
EntmicroCustomerFollow entmicroCustomerFollow = new EntmicroCustomerFollow();
if (CollectionUtils.isEmpty(customerFollowRelations)) {
return entmicroCustomerFollow;
}
log.info("当前客户和跟进员关联信息{}", customerFollowRelations);
//判断如果当前跟进人和客户关联存在则不给重复插入
List<Integer> customerIdList = customerFollowRelations.stream().map(EntmicroCustomerFollow::getCustomerId).collect(Collectors.toList());
Set<Integer> staffId = customerFollowRelations.stream().map(EntmicroCustomerFollow::getStaffId).collect(Collectors.toSet());
Integer customerId = customerIdList.get(0);
List<EntmicroCustomerFollow> localList = customerFollowMapper.findRelationByStaffId(customerId, staffId);
//对比两端数据
List<EntmicroCustomerFollow> saveList = customerFollowRelations.stream().filter(index -> !localList.contains(index)).collect(Collectors.toList());
if (CollectionUtils.isEmpty(saveList)) {
return entmicroCustomerFollow;
}
if (saveList.size() == 1) {
entmicroCustomerFollow = saveList.get(0);
customerFollowMapper.insertUseGeneratedKeys(entmicroCustomerFollow);
} else {
customerFollowMapper.insertList(saveList);
}
return entmicroCustomerFollow;
}
/**
* 删除跟进人关系
*/
public boolean delete(String userId, String externalUserId, Integer merchantId) {
// 删除跟进人之后判断这个客户是否还有其他的跟进人,没有更新删除好友状态
List<EntmicroCustomerFollow> entmicroCustomerFollowList = customerFollowMapper.findFollowByExternalUserId(externalUserId, merchantId);
log.info("客户与跟进人关系:{}", JSONArray.toJSONString(entmicroCustomerFollowList));
//空就说明没有跟进人 是删除状态
if (CollectionUtils.isEmpty(entmicroCustomerFollowList)) {
return true;
}
log.info("跟进人数量: {}", entmicroCustomerFollowList.size());
List<EntmicroCustomerFollow> collect = entmicroCustomerFollowList.stream().filter(index -> index.getUserid().equals(userId)).collect(Collectors.toList());
log.info("当前需要删除的跟进人userId: {}", collect);
if (!CollectionUtils.isEmpty(collect)) {
EntmicroCustomerFollow entmicroCustomerFollow = collect.get(0);
Integer followId = entmicroCustomerFollow.getId();
customerFollowMapper.deleteRelation(followId);
// 删除好友状态
Integer customerId = entmicroCustomerFollow.getCustomerId();
if (entmicroCustomerFollowList.size() == 1) {
log.info("删除好友:{}", externalUserId);
customerMapper.deleteaById(customerId);
return true;
} else {
log.info("删除好友失败: {}", externalUserId);
return false;
}
}
return false;
}
public void deleteList(List<EntmicroCustomerFollow> buildFollowParam) {
buildFollowParam.stream().forEach(follow -> {
customerFollowMapper.deleteCustomer(follow.getId());
});
}
public void updatelist(List<EntmicroCustomerFollow> buildFollowParam) {
customerFollowMapper.updatelist(buildFollowParam);
}
public List<EntmicroCustomerFollow> findAllFollowByExternalUserId(String externalUserId) {
return customerFollowMapper.findAllFollowByExternalUserId(externalUserId);
}
/**
* id获取关联数据
*
* @return
*/
public CustomerFollowStaffResponse findRelationById(Integer id) {
return customerFollowMapper.findRelationById(id);
}
public List<CustomerFollowStaffResponse> findRelationByUserId(String id) {
return customerFollowMapper.findRelationByUserId(id);
}
/**
* 修改手机号
*/
public void updatePhone(EntmicroCustomerFollow entmicroCustomerFollow) {
customerFollowMapper.updatePhone(entmicroCustomerFollow);
}
public CustomerInfo findByRelationId(Integer id) {
return customerFollowMapper.findByRelationId(id);
}
/**
* 员工id查客户
*
* @return
*/
public List<EntmicroCustomerFollow> findByStaffId(Integer staffId) {
return customerFollowMapper.findByStaffId(staffId);
}
/**
* 补充修改手机号
*
* @param customer
*/
public void updateBatchInfo(List<Customer> customer) {
customerFollowMapper.updateBatchInfo(customer);
}
/**
* 获取客户集团员工跟进关系
*
* @param request
* @return
*/
public Boolean getStaffFollowCustomer(GetStaffFollowCustomerRequest request) {
Integer merchantId = request.getMerchantId();
String customerPhone = request.getCustomerPhone();
List<String> staffUserIdList = request.getStaffUserIdList();
if (merchantId == null || StringUtils.isEmpty(customerPhone) || CollectionUtils.isEmpty(staffUserIdList)) {
throw new RpcServiceException("param must not be empty");
}
Integer supMerchantNo = entmicroStaffService.findSupMerchantNo(merchantId);
log.info("获取客户集团员工跟进关系-授权商户 supMerchantNo:{}",supMerchantNo);
List<Integer> merchantTreeId = merchantService.findMerchantTreeId(supMerchantNo);
log.info("获取客户集团员工跟进关系-商户树 merchantTreeId:{}",merchantTreeId);
if (CollectionUtils.isEmpty(merchantTreeId)) return false;
List<EntmicroCustomerFollow> customerStaffFollow = customerFollowMapper.findCustomerStaffFollow(merchantTreeId, customerPhone, staffUserIdList);
return !CollectionUtils.isEmpty(customerStaffFollow);
}
}