RopCommonServie.java
20.9 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
package cn.com.glsx.entmicro.service;
import cn.com.glsx.datahub.dto.UserEnvLabelDTO;
import cn.com.glsx.datahub.remote.UserProfileRemote;
import cn.com.glsx.entmicro.model.*;
import cn.com.glsx.entmicro.model.RopResponse;
import cn.com.glsx.entmicro.model.request.CustomerSearchRequest;
import cn.com.glsx.entmicro.model.request.MagicDynamicInfoRequest;
import cn.com.glsx.entmicro.model.request.MagicDynamicRequest;
import cn.com.glsx.entmicro.model.request.TagSearchRequest;
import cn.com.glsx.entmicro.model.response.*;
import cn.com.glsx.entmicro.model.request.mass.LabelRequest;
import cn.com.glsx.entmicro.util.DateFormatUtil;
import cn.com.glsx.entmicro.util.RopHttpUtil;
import cn.com.glsx.framework.core.beans.rpc.RpcPagination;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;
/**
* @author fengzhi
* @version 1.0
* @description: 外部rop服务
* @date 2021/10/13 14:10
*/
@Service
@Slf4j
public class RopCommonServie {
@Value("${datahub.baseurl}")
private String dataurl;
@Value("${datahub.getOneIdMethod}")
private String getOneIdMethod;
@Value("${datahub.getuserprofileMethod}")
private String getuserprofileMethod;
@Value("${datahub.getBatchUserLabel}")
private String getBatchUserLabel;
@Value("${datahub.getCustomerDataOverviewMethod}")
private String getCustomerDataOverviewMethod;
@Value("${datahub.getmultioneidscountbylabelsMethod}")
private String getmultioneidscountbylabelsMethod;
@Value("${datahub.getoneidlistMethod}")
private String getoneidlistMethod;
@Value("${datahub.syncEntCubeImpBatchMethod}")
private String syncEntCubeImpBatchMethod;
@Value("${datahub.getOneIdsByTag}")
private String getOneIdsByTag;
@Value("${datahub.getOneIdsByGroupId}")
private String getOneIdsByGroupId;
//v1.4.2屏蔽标签筛选配置
@Value("${datahub.labelFilterMethod:null}")
private String labelFilterMethod;
@Value("${datahub.queryCrowdCustomersByIdMethod}")
private String queryCrowdCustomersByIdMethod;
@Value("${datahub.queryCrowdGroupDetailsByCrowdGroupId}")
private String queryCrowdListByCrowdGroupIdMethod;
@Autowired
private MerchantService merchantService;
@Autowired(required = false)
private UserProfileRemote userProfileRemote;
/************************************** 数据侧 rop接口 **************************************/
/**
* 同步客户信息返回(客户主键id,oneid)
*/
public GetOneIDRopResponse getOneId(CustomerSync cust, String type) {
//type 1:新增 2:更新 3:删除
GetOneIDRopResponse res = null;
UserEnvLabelDTO.UserEnvLabelReq userEnvLabelReq = new UserEnvLabelDTO.UserEnvLabelReq();
//标签
userEnvLabelReq.setLabels(cust.getTags());
//门店(商户)ID
userEnvLabelReq.setMerchantId(cust.getMerchantid());
//oneId
userEnvLabelReq.setOneId(cust.getOneId());
//员工id
userEnvLabelReq.setStaffId(cust.getStaffId());
try {
log.info("rpc同步客户企业标签,params:{}", JSONArray.toJSONString(userEnvLabelReq));
UserEnvLabelDTO.UserEnvLabelRet userEnvLabelRet = userProfileRemote.synUserEnvLabels(userEnvLabelReq);
log.info("rpc同步客户企业标签到数据侧userEnvLabelRet: {}", JSONArray.toJSONString(userEnvLabelRet));
} catch (Exception e) {
e.printStackTrace();
log.info("rpc同步客户企业标签到数据侧异常params:{},exception:{}", userEnvLabelReq, e);
}
return res;
}
/**
* 企业魔方导入数据同步接口
*/
public List<CustomerMagicRopResponse> syncEntCubeImp(MagicDynamicRequest magicDynamicRequest, Integer channelId, Integer addway) {
//type 1:新增 2:更新 3:删除
List<CustomerMagicRopResponse> res = null;
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
//渠道
addParam("channel", channelId, params, false);
//门店(商户)ID
addParam("merchantId", magicDynamicRequest.getMerchantId(), params, false);
//来源
addParam("addWay", addway, params, false);
addParam("tableName", magicDynamicRequest.getTableName(), params, false);
//更新时间
String dateStr = DateFormatUtil.toString(new Date(), DateFormatUtil.pattern19);
addParam("updatedTime", dateStr, params, false);
List<MagicDynamicInfoRequest> customers = magicDynamicRequest.getCustomers();
addParam("customers", JSONObject.toJSONString(customers, SerializerFeature.WriteMapNullValue), params, false);
try {
RopResponse ropResponse = RopHttpUtil.post(params, RopResponse.class, dataurl, syncEntCubeImpBatchMethod);
log.info("rop魔方批量同步客户res:{},merchantId:{},channel:{},params:{}", ropResponse, magicDynamicRequest.getMerchantId(), channelId, params);
if (("0").equals(ropResponse.getCode())) {
String data = JSONObject.toJSONString(ropResponse.getData());
res = JSONObject.parseArray(data, CustomerMagicRopResponse.class);
log.info("===rop魔方批量同步客户成功,merchantId:{},channel:{},res:{}", magicDynamicRequest.getMerchantId(), channelId, res);
}
} catch (Exception e) {
e.printStackTrace();
log.info("同步魔方客户异常,merchantId:{},channel:{},params:{},exception:{}", magicDynamicRequest.getMerchantId(), channelId, params, e);
}
return res;
}
/**
* 客户信息(标签,车辆信息)
*/
public CustDetailRopRes getLabelVehicleInfo(String oneId, String merchantIds, Integer merchantId, Integer staffId) {
CustDetailRopRes res = null;
MerchantRopResponse merchantByMerchantNo = merchantService.findMerchantByMerchantNo(merchantId);
//商户模式
Integer merchantModel = merchantByMerchantNo.getMerchantModel();
MultiValueMap<String, String> params = new LinkedMultiValueMap<>(4);
addParam("req.merchantIdsStr", merchantIds, params, false);
addParam("req.oneId", oneId, params, false);
addParam("req.edition", merchantModel, params, false);
addParam("req.staffId", staffId, params, false);
try {
log.info("===请求客户信息,req:{}", params);
RopResponse ropResponse = RopHttpUtil.get(params, RopResponse.class, dataurl, getuserprofileMethod);
log.info("===客户信息,res:{}", ropResponse);
if (("0").equals(ropResponse.getCode())) {
String data = JSONObject.toJSONString(ropResponse.getData());
res = JSONObject.parseObject(data, CustDetailRopRes.class);
return res;
}
} catch (Exception e) {
e.printStackTrace();
log.info("查询客户信息异常,{}", e);
}
return res;
}
/**
* 批量获取客户标签
*/
public LabelBatchRopRes getLabels(List<String> oneId, Integer merchantId, Integer staffId) {
LabelBatchRopRes res = null;
MerchantRopResponse merchantByMerchantNo = merchantService.findMerchantByMerchantNo(merchantId);
//商户模式
Integer merchantModel = merchantByMerchantNo.getMerchantModel();
MultiValueMap<String, String> params = new LinkedMultiValueMap<>(4);
addParam("req.merchantId", merchantId, params, false);
for (int i = oneId.size() - 1; i >= 0; i--) {
addParam("req.oneIdList" + "[" + i + "]", oneId.get(i), params, false);
}
addParam("req.edition", merchantModel, params, false);
addParam("req.staffId", staffId, params, false);
try {
log.info("===请求客户信息,req:{}", params);
RopResponse ropResponse = RopHttpUtil.get(params, RopResponse.class, dataurl, getBatchUserLabel);
log.info("===客户信息,res:{}", ropResponse);
if (("0").equals(ropResponse.getCode())) {
String data = JSONObject.toJSONString(ropResponse.getData());
res = JSONObject.parseObject(data, LabelBatchRopRes.class);
return res;
}else {
log.error("查询客户信息异常,{}", ropResponse.getMessage());
}
} catch (Exception e) {
e.printStackTrace();
log.error("查询客户信息异常,{}", e);
}
return res;
}
/**
* 获取客户数据概览
*/
public CustomerDataViewRopResponse getCustomerDataView(String merchantId) {
CustomerDataViewRopResponse res = null;
MultiValueMap<String, String> params = new LinkedMultiValueMap<>(4);
addParam("req.merchantIdsStr", merchantId, params, false);
try {
log.info("===请求数据侧客户概览数据,req:{}", params);
RopResponse ropResponse = RopHttpUtil.post(params, RopResponse.class, dataurl, getCustomerDataOverviewMethod);
log.info("===返回数据侧客户概览数据,res:{}", ropResponse);
if (("0").equals(ropResponse.getCode())) {
String data = JSONObject.toJSONString(ropResponse.getData());
res = JSONObject.parseObject(data, CustomerDataViewRopResponse.class);
return res;
}
} catch (Exception e) {
e.printStackTrace();
log.info("请求数据侧客户概览数据异常,{}", e.getMessage(),e);
}
return res;
}
/**
* 通过标签获取客户数量
*/
public Map<String, Integer> getMutOneIdsCountByLables(List<String> labels, List<String> merchantIds) {
Map<String, Integer> res = null;
try {
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
if (!CollectionUtils.isEmpty(labels)) {
for (int i = 0; i < labels.size(); i++) {
params.add("labelList[" + i + "]", labels.get(i));
}
}
if (!CollectionUtils.isEmpty(merchantIds)) {
for (int i = 0; i < merchantIds.size(); i++) {
params.add("shopIdList[" + i + "]", merchantIds.get(i));
}
}
addParam("edition", "1", params, false);
RopResponse ropResponse = RopHttpUtil.get(params, RopResponse.class, dataurl, getmultioneidscountbylabelsMethod);
log.info("通过标签获取客户数量,params:{},ropResponse:{}", params, ropResponse);
if (("0").equals(ropResponse.getCode())) {
res = (Map<String, Integer>) ropResponse.getData();
log.info("通过标签获取客户数量,res:{}", res);
}
} catch (Exception e) {
e.printStackTrace();
log.info("通过标签获取客户数量异常,{}", e);
}
return res;
}
/**
* 根据标签获取oneId列表
* @param labelList 标签列表
* @param merchantId 门店号
* @param combineType 1-组之间并集,2-组之间交集
* @return
*/
public List<String> labelFilterCustomersOneId(List<LabelRequest> labelList, Integer merchantId,Integer combineType) {
MultiValueMap<String, String> params = new LinkedMultiValueMap<>(5);
params.add("req.pageSize", Integer.MAX_VALUE + "");
params.add("req.pageNum", "1");
addParam("req.merchantId", merchantId, params, false);
addParam("req.combineType", combineType, params, false);
if (labelList != null && !labelList.isEmpty()) {
for (int j = labelList.size() - 1; j >= 0; j--) {
LabelRequest labelRequest = labelList.get(j);
addParam("req.labelInfoList" + "[" + j + "]"+".groupId", labelRequest.getGroupId(), params, false);
addParam("req.labelInfoList" + "[" + j + "]"+".groupName", labelRequest.getGroupName(), params, false);
addParam("req.labelInfoList" + "[" + j + "]"+".groupType", labelRequest.getGroupType(), params, false);
addParam("req.labelInfoList" + "[" + j + "]"+".labelId", labelRequest.getLabelId(), params, false);
addParam("req.labelInfoList" + "[" + j + "]"+".labelName", labelRequest.getLabelName(), params, false);
}
}
try {
log.info("===标签获取ondId,req:{},url:{},method:{}", JSON.toJSONString(params),dataurl,getOneIdsByTag);
RopResponse ropResponse = RopHttpUtil.post(params, RopResponse.class, dataurl, getOneIdsByTag);
log.info("===标签获取ondId,resp:{}", JSON.toJSONString(ropResponse));
if (("0").equals(ropResponse.getCode())) {
Page<String> getOneIdsByGroupIdList = new Page<String>();
getOneIdsByGroupIdList = converRopPage(ropResponse);
return new ArrayList<>(getOneIdsByGroupIdList.getResult());
}
} catch (Exception e) {
log.info("根据标签过滤用户oneId列表异常:{}", e);
}
return null;
}
private void addParam(String name, Object val, MultiValueMap<String, String> params, boolean needEncode) {
if (null != val) {
try {
String en_val = String.valueOf(val);
if (needEncode) {
en_val = URLEncoder.encode(String.valueOf(val), "UTF-8");
}
params.add(name, en_val);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
log.info("addParam==name;{},val:{},encode异常msg:{}", name, val, e.getMessage());
}
}
}
/**
* 跟进标签查询客户oneIds
*
* @param pagination 分页条件
*/
public Page<String> getOneIdsByTag(RpcPagination<CustomerSearchRequest> pagination) {
Page<String> oneIds = new Page<String>();
MultiValueMap<String, String> params = new LinkedMultiValueMap<>(4);
params.add("req.pageSize", pagination.getPageSize() + "");
params.add("req.pageNum", pagination.getPageNum() + "");
CustomerSearchRequest condition = pagination.getCondition();
addParam("req.merchantId", condition.getMerchantId(), params, false);
// 1- 组之间取并集 2-组之间取交集
addParam("req.combineType", null == condition.getCombineType() ? 1 : condition.getCombineType(), params, false);
List<TagSearchRequest> tagLists = condition.getTagLists();
if (!CollectionUtils.isEmpty(tagLists)) {
for (int i = tagLists.size() - 1; i >= 0; i--) {
TagSearchRequest tagSearchRequest = tagLists.get(i);
addParam("req.labelInfoList" + "[" + i + "]" + ".groupId", tagSearchRequest.getGroupId(), params, false);
// addParam("req.labelInfoList" + "[" + i + "]" + ".groupName", tagSearchRequest.getGroupName(), params, false);
addParam("req.labelInfoList" + "[" + i + "]" + ".groupType", tagSearchRequest.getGroupType(), params, false);
addParam("req.labelInfoList" + "[" + i + "]" + ".labelId", tagSearchRequest.getLabelId(), params, false);
// addParam("req.labelInfoList" + "[" + i + "]" + ".labelName", tagSearchRequest.getLabelName(), params, false);
}
}
try {
log.info("datahub标签查询客户oneIds参数: {}", JSONArray.toJSONString(params));
RopResponse ropResponse = RopHttpUtil.get(params, RopResponse.class, dataurl, getOneIdsByTag);
log.info("跟进标签查询客户oneIds: {}", JSONArray.toJSONString(ropResponse));
if (("0").equals(ropResponse.getCode())) {
oneIds = converRopPage(ropResponse);
log.info("标签查询返回的oneIds{}",oneIds);
}
} catch (Exception e) {
e.printStackTrace();
log.info("跟进标签查询客户oneIds,{}", e);
}
return oneIds;
}
/**
* 客户分群查询客户oneIds
*
* @param pagination 分页条件
*/
public Page<String> getOneIdsByGroupId(RpcPagination<CustomerSearchRequest> pagination) {
Page<String> getOneIdsByGroupIdList = new Page<String>();
MultiValueMap<String, String> params = new LinkedMultiValueMap<>(4);
params.add("req.pageNum", pagination.getPageNum() + "");
params.add("req.pageSize", pagination.getPageSize() + "");
CustomerSearchRequest condition = pagination.getCondition();
params.add("req.crowdId", condition.getCrowdId() + "");
List<Integer> merchantIds = condition.getMerchantIds();
if (!CollectionUtils.isEmpty(merchantIds)){
for (int i = merchantIds.size() - 1; i >= 0; i--) {
addParam("req.merchantIdList" + "[" + i + "]", merchantIds.get(i), params, false);
}
}
try {
RopResponse ropResponse = RopHttpUtil.get(params, RopResponse.class, dataurl, getOneIdsByGroupId);
log.info("客户分群查询客户oneIds,ropResponse:{}",ropResponse);
if (("0").equals(ropResponse.getCode())) {
getOneIdsByGroupIdList = converRopPage(ropResponse);
}
} catch (Exception e) {
e.printStackTrace();
log.info("客户分群查询客户oneIds,{}", e);
}
return getOneIdsByGroupIdList;
}
/**
* 自定义分群查询客户oneIds
*
* @param pagination 分页条件
*/
public Page<String> queryCrowdCustomersById(RpcPagination<CustomerSearchRequest> pagination) {
Page<String> getOneIdsByGroupIdList = new Page<String>();
MultiValueMap<String, String> params = new LinkedMultiValueMap<>(4);
params.add("req.pageNum", pagination.getPageNum() + "");
params.add("req.pageSize", pagination.getPageSize() + "");
CustomerSearchRequest condition = pagination.getCondition();
params.add("req.crowdId", condition.getCrowdId() + "");
List<Integer> merchantIds = condition.getMerchantIds();
if (!CollectionUtils.isEmpty(merchantIds)){
for (int i = merchantIds.size() - 1; i >= 0; i--) {
addParam("req.merchantIdList" + "[" + i + "]", merchantIds.get(i), params, false);
}
}
try {
RopResponse ropResponse = RopHttpUtil.get(params, RopResponse.class, dataurl, queryCrowdCustomersByIdMethod);
log.info("自定义分群查询客户oneIdsropResponse:{}",ropResponse);
if (("0").equals(ropResponse.getCode())) {
getOneIdsByGroupIdList = converRopPage(ropResponse);
}
} catch (Exception e) {
e.printStackTrace();
log.info("客户分群查询客户oneIds异常,{}", e);
}
return getOneIdsByGroupIdList;
}
/**
* 通过分群Id查询人群列表详情请求
*
* @param groupId 分群id
* @return
*/
public CrowdGroupDetailRopResponse queryCrowdListByCrowdGroupId(Integer groupId) {
CrowdGroupDetailRopResponse crowdUnitDataList = new CrowdGroupDetailRopResponse();
MultiValueMap<String, String> params = new LinkedMultiValueMap<>(1);
params.add("req.crowdGroupId", groupId + "");
try {
RopResponse ropResponse = RopHttpUtil.get(params, RopResponse.class, dataurl, queryCrowdListByCrowdGroupIdMethod);
log.info("ropResponse:{}",ropResponse);
if (("0").equals(ropResponse.getCode())) {
String data = JSONObject.toJSONString(ropResponse.getData());
crowdUnitDataList = JSONObject.parseObject(data, CrowdGroupDetailRopResponse.class);
}
} catch (Exception e) {
e.printStackTrace();
log.info("queryCrowdListByCrowdGroupIdMethod服务异常");
}
return crowdUnitDataList;
}
public static Page<String> converRopPage(RopResponse ropResponse) {
Page<String> converPage = new Page<String>();
Map<String, Object> page = (Map<String, Object>) ropResponse.getData();
CustOneIdModel parse = JSON.parseObject(JSONObject.toJSONString(page), CustOneIdModel.class);
converPage.setPageSize(parse.getPageSize());
converPage.setPageNum(parse.getPageNum());
converPage.setTotal(parse.getTotal());
converPage.addAll(CollectionUtils.isEmpty(parse.getOneIdList()) ? new ArrayList<>() : parse.getOneIdList());
return converPage;
}
}