ScrmMagicCubeCustomerService.java
37.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
package cn.com.glsx.entmicro.service;
import cn.com.glsx.entmicro.Enum.FieldTypeNameEnum;
import cn.com.glsx.entmicro.Enum.MagicAutoImportConfigTypeEnum;
import cn.com.glsx.entmicro.constants.Constants;
import cn.com.glsx.entmicro.constants.MagicCubeImportConstants;
import cn.com.glsx.entmicro.dto.DataGroupCustomerDTO;
import cn.com.glsx.entmicro.dto.DataTotalCustomerDTO;
import cn.com.glsx.entmicro.entity.magic.*;
import cn.com.glsx.entmicro.mapper.MagicTemplateConfigInfoMapper;
import cn.com.glsx.entmicro.mapper.MagicTemplateConfigMapper;
import cn.com.glsx.entmicro.mapper.ScrmMagicCubeCustomerCountMapper;
import cn.com.glsx.entmicro.mapper.ScrmMagicCubeCustomerMapper;
import cn.com.glsx.entmicro.model.request.MagicCubeImportRecordQueryRequest;
import cn.com.glsx.entmicro.model.request.MagicDynamicInfoRequest;
import cn.com.glsx.entmicro.model.request.MagicDynamicRequest;
import cn.com.glsx.entmicro.model.response.CustomerMagicResponse;
import cn.com.glsx.entmicro.model.response.ScrmMagicCubeCustomerCountResponse;
import cn.com.glsx.entmicro.model.response.ScrmMagicCubeCustomerResponese;
import cn.com.glsx.entmicro.model.response.UserResponse;
import cn.com.glsx.entmicro.remote.CustomerRemote;
import cn.com.glsx.entmicro.remote.DataCustomerRemote;
import cn.com.glsx.entmicro.service.common.CommonService;
import cn.com.glsx.entmicro.service.drainage.DracImService;
import cn.com.glsx.entmicro.util.DateUtil;
import cn.com.glsx.entmicro.util.TextUtil;
import cn.com.glsx.entmicro.util.UserLocalUtils;
import cn.com.glsx.framework.core.beans.rpc.RpcPagination;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.framework.core.enums.DefaultErrorEnum;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.framework.core.util.RpcAssert;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @Author yeliqing
* @Version 1.0
* @Description: 对魔方导入的客户表进行操作的服务
* @Date 2021/11/6 15:38
*/
@Service
@Slf4j
public class ScrmMagicCubeCustomerService {
@Resource
private ScrmMagicCubeCustomerMapper scrmMagicCubeCustomerMapper;
@Resource
private ScrmMagicCubeCustomerCountMapper scrmMagicCubeCustomerCountMapper;
@Autowired
private CommonService commonService;
@Resource
private DataCustomerRemote dataCustomerRemote;
@Resource
private MagicTemplateConfigMapper magicTemplateConfigMapper;
@Resource
private MagicTemplateConfigInfoMapper magicTemplateConfigInfoMapper;
@Resource
private CustomerRemote customerRemote;
@Resource
private DracImService dracImService;
@Value("${addway.magic}")
private Integer addwayMagic;
//同步增加相应字段
public void addFields(List<MagicTemplateConfigInfo> list, List<Map<String, Object>> mapList, ScrmMagicCubeCustomerCount customerCount) {
MagicTemplateConfigInfo maxIndex = list.stream().max(Comparator.comparing(MagicTemplateConfigInfo::getCloumnIndex)).get();
Integer index = maxIndex.getCloumnIndex();
List<MagicTemplateConfigInfo> defaultFields = getDefaultFields(index);
for (int i = 0; i < defaultFields.size(); i++) {
MagicTemplateConfigInfo info = defaultFields.get(i);
list.add(info);
String cloumnName = info.getCloumnName();
if ("created_by".equals(cloumnName) || "updated_by".equals(cloumnName)) {
Integer createdId = UserLocalUtils.getCreatedId();
for (int j = 0; j < mapList.size(); j++) {
addField(mapList.get(j), info, createdId);
}
} else if ("created_time".equals(cloumnName) || "updated_time".equals(cloumnName)) {
for (int j = 0; j < mapList.size(); j++) {
addField(mapList.get(j), info, customerCount.getCreatedTime());
}
} else if ("import_flag".equals(cloumnName)) {
for (int j = 0; j < mapList.size(); j++) {
addField(mapList.get(j), info, customerCount.getImportFlag());
}
} else {
for (int j = 0; j < mapList.size(); j++) {
addField(mapList.get(j), info, 0);
}
}
}
}
public Map<String, Object> addField(Map<String, Object> map, MagicTemplateConfigInfo info, Object obj) {
map.put(info.getFieldName(), obj);
return map;
}
public List<MagicTemplateConfigInfo> getDefaultFields(Integer index) {
List<MagicTemplateConfigInfo> list = new LinkedList<>();
MagicTemplateConfigInfo info = new MagicTemplateConfigInfo();
info.setCloumnName("created_by");
info.setCloumnIndex(++index);
info.setFieldComment("创建人");
info.setFieldName("创建人");
info.setFieldLength(11);
info.setFieldPrimary(0);
info.setFieldType(FieldTypeNameEnum.INTEGER.getFieldType());
list.add(info);
MagicTemplateConfigInfo info1 = new MagicTemplateConfigInfo();
info1.setCloumnName("created_time");
info1.setCloumnIndex(++index);
info1.setFieldComment("创建时间");
info1.setFieldName("创建时间");
info1.setFieldLength(null);
info1.setFieldPrimary(0);
info1.setFieldType(FieldTypeNameEnum.DATE.getFieldType());
list.add(info1);
MagicTemplateConfigInfo info2 = new MagicTemplateConfigInfo();
info2.setCloumnName("updated_by");
info2.setCloumnIndex(++index);
info2.setFieldComment("更新人");
info2.setFieldName("更新人");
info2.setFieldLength(11);
info2.setFieldPrimary(0);
info2.setFieldType(FieldTypeNameEnum.INTEGER.getFieldType());
list.add(info2);
MagicTemplateConfigInfo info3 = new MagicTemplateConfigInfo();
info3.setCloumnName("updated_time");
info3.setCloumnIndex(++index);
info3.setFieldComment("更新时间");
info3.setFieldName("更新时间");
info3.setFieldLength(null);
info3.setFieldPrimary(0);
info3.setFieldType(FieldTypeNameEnum.DATE.getFieldType());
list.add(info3);
MagicTemplateConfigInfo info4 = new MagicTemplateConfigInfo();
info4.setCloumnName("del_flag");
info4.setCloumnIndex(++index);
info4.setFieldComment("是否删除");
info4.setFieldName("是否删除");
info4.setFieldLength(2);
info4.setFieldPrimary(0);
info4.setFieldType(FieldTypeNameEnum.INTEGER.getFieldType());
list.add(info4);
MagicTemplateConfigInfo info5 = new MagicTemplateConfigInfo();
info5.setCloumnName("import_flag");
info5.setCloumnIndex(++index);
info5.setFieldComment("导入批次时间戳");
info5.setFieldName("导入批次时间戳");
info5.setFieldLength(13);
info5.setFieldPrimary(0);
info5.setFieldType(FieldTypeNameEnum.VARCHAR.getFieldType());
list.add(info5);
MagicTemplateConfigInfo info6 = new MagicTemplateConfigInfo();
info6.setCloumnName("import_type");
info6.setCloumnIndex(++index);
info6.setFieldComment("导入类型");
info6.setFieldName("导入类型");
info6.setFieldLength(1);
info6.setFieldPrimary(0);
info6.setFieldType(FieldTypeNameEnum.INTEGER.getFieldType());
list.add(info6);
MagicTemplateConfigInfo info7 = new MagicTemplateConfigInfo();
info7.setCloumnName("customer_type");
info7.setCloumnIndex(++index);
info7.setFieldComment("是否新增");
info7.setFieldName("是否新增");
info7.setFieldLength(1);
info7.setFieldPrimary(0);
info7.setFieldType(FieldTypeNameEnum.INTEGER.getFieldType());
list.add(info7);
MagicTemplateConfigInfo info8 = new MagicTemplateConfigInfo();
info8.setCloumnName("wait_renewal_flag");
info8.setCloumnIndex(++index);
info8.setFieldComment("是否待续保客户");
info8.setFieldName("是否待续保客户");
info8.setFieldLength(1);
info8.setFieldPrimary(0);
info8.setFieldType(FieldTypeNameEnum.INTEGER.getFieldType());
list.add(info8);
MagicTemplateConfigInfo info9 = new MagicTemplateConfigInfo();
info9.setCloumnName("wait_regular_maintain_flag");
info9.setCloumnIndex(++index);
info9.setFieldComment("是否待首保客户");
info9.setFieldName("是否待首保客户");
info9.setFieldLength(1);
info9.setFieldPrimary(0);
info9.setFieldType(FieldTypeNameEnum.INTEGER.getFieldType());
list.add(info9);
return list;
}
//数据处理
@Transactional
public void insertDatas(TableConfig tableConfig, List<Map<String, Object>> mapList, Map<String, MagicTemplateConfigInfo> needheadTitleMap) {
RpcAssert.assertNotNull(tableConfig, DefaultErrorEnum.DATA_INVALID);
//List<Map<String, Object>> List<Map<字段名称,字段值>>
MagicTemplateConfig config = tableConfig.getMagicTemplateConfig();
List<MagicTemplateConfigInfo> infos = tableConfig.getMagicTemplateConfigInfoList().stream().sorted(Comparator.comparing(MagicTemplateConfigInfo::getCloumnIndex)).collect(Collectors.toList());
StringBuilder sqlFields = new StringBuilder();
String sqlname = null;
List<String> sqls = new ArrayList<>();
infos.forEach(info->{
String cloumnName = info.getCloumnName();
sqlFields.append(cloumnName).append(",");
});
if (sqlFields.length()>0){
sqlname = sqlFields.substring(0,sqlFields.length()-1);
}
mapList.forEach(map->{
if (map!=null){
//值
String value = null;
StringBuilder builder = new StringBuilder();
infos.forEach(k->{
Object o = map.get(k.getFieldName());
if (o!=null){
Integer fieldType = k.getFieldType();
if (fieldType!=null&&fieldType==FieldTypeNameEnum.DATE.getFieldType()){
if (o instanceof Date){
o = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(o);
}
builder.append("'").append(o).append("'").append(",");
}else if (fieldType!=null&&(fieldType==FieldTypeNameEnum.CHAR.getFieldType()||fieldType==FieldTypeNameEnum.VARCHAR.getFieldType()||fieldType==FieldTypeNameEnum.TEXT.getFieldType())){
builder.append("'").append(o).append("'").append(",");
}else {
builder.append(o).append(",");
}
}else {
builder.append(o).append(",");
}
});
if (builder.length()>0){
value=builder.substring(0,builder.length()-1);
sqls.add(value);
}
}
});
try {
//限制每次保存条数
int limit = 2000;
if (sqls.size()>limit){
int num = sqls.size()/limit;
for (int i = 0; i < num; i++) {
List<String> sqlList = sqls.subList(i * limit, (i + 1) * limit);
Integer insert = magicTemplateConfigMapper.insertCustomerList(config.getTableName(), sqlname, sqlList);
log.info("分批保存内循环======数据库影响行数:{}", insert);
}
List<String> endList = sqls.subList(num * limit, sqls.size());
if (endList.size()>0){
Integer insert = magicTemplateConfigMapper.insertCustomerList(config.getTableName(), sqlname, endList);
log.info("分批保存循环后======数据库影响行数:{}", insert);
}
}else {
Integer i = magicTemplateConfigMapper.insertCustomerList(config.getTableName(),sqlname,sqls);
log.info("一次性保存======数据库影响行数:{}", i);
}
log.info("----------------------魔方导入数据插入完成----------------------------");
} catch (Exception e) {
log.info("魔方导入保存数据异常:{}",e);
throw new RpcServiceException(DefaultErrorEnum.DATABASE_ERROR);
}
}
//按字段顺序排序
private List<Map<String, Object>> mapListSorted(List<Map<String, Object>> mapList, List<MagicTemplateConfigInfo> infos) {
List<Map<String, Object>> list = new ArrayList<>();
for (Map<String, Object> oMap : mapList) {
Map<String, Object> sortedMap = sortedMap(oMap, infos);
list.add(sortedMap);
}
return list;
}
//按字段顺序排序
private Map<String, Object> sortedMap(Map<String, Object> map, List<MagicTemplateConfigInfo> infos) {
List<MagicTemplateConfigInfo> list = sortedList(infos);
Map<String, Object> lmap = new LinkedHashMap<>();
for (int i = 0; i < list.size(); i++) {
MagicTemplateConfigInfo info = list.get(i);
if (info.getFieldPrimary() == 1) {
continue;
}
Object val = map.get(info.getFieldName());
lmap.put(info.getFieldName(), val);
}
return lmap;
}
//按字段顺序排序
public List<MagicTemplateConfigInfo> sortedList(List<MagicTemplateConfigInfo> infos) {
if (infos == null || infos.isEmpty()) {
return infos;
}
return infos.stream().sorted(Comparator.comparing(MagicTemplateConfigInfo::getCloumnIndex)).collect(Collectors.toList());
}
/**
* 返回数据库中的所有表字段信息
* @param merchantId
* @return
*/
public Map<String, MagicTemplateConfigInfo> getHeadTitleMap(Integer merchantId) {
TableConfig tableConfig = getTableConfig(merchantId);
RpcAssert.assertNotNull(tableConfig, DefaultErrorEnum.DATA_NOT_EXIST);
List<MagicTemplateConfigInfo> fieldList = tableConfig.getMagicTemplateConfigInfoList();
if(!fieldList.isEmpty()){
return fieldList.stream().collect(Collectors.toMap(MagicTemplateConfigInfo::getFieldName, Function.identity()));
}
// Map<String, String> sortedMap = new LinkedHashMap<>();
// for (int i = 0; i < fieldList.size(); i++) {
// sortedMap.put(fieldList.get(i).getFieldName());
// }
return null;
}
/**
* 根据merchantId获取配置信息
*
* @param merchantId
* @return
*/
public TableConfig getTableConfig(Integer merchantId) {
RpcAssert.assertNotNull(merchantId, DefaultErrorEnum.PARAMETER_NULL, "merchantId param is not null!");
Example example = new Example(MagicTemplateConfig.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("merchantId", merchantId).andEqualTo("type", MagicAutoImportConfigTypeEnum.MAGIC_IMPORT.getType());
List<MagicTemplateConfig> magicTemplateConfigs = magicTemplateConfigMapper.selectByExample(example);
if (magicTemplateConfigs.isEmpty()) {
throw new RpcServiceException("未配置魔方导入模板!");
// Example defaultExample = new Example(MagicTemplateConfig.class);
// Example.Criteria condition = defaultExample.createCriteria();
// condition.andEqualTo("merchantId", Constants.MAGIC_DEFAULT_TEMPLATE_MERCHANTID).andEqualTo("type", 1);
// magicTemplateConfigs = magicTemplateConfigMapper.selectByExample(defaultExample);
// if (magicTemplateConfigs.isEmpty()) {
// log.error("魔方导入默认模板不存在!");
// return null;
// }
}
MagicTemplateConfig magicTemplateConfig = magicTemplateConfigs.get(0);
List<MagicTemplateConfigInfo> collect = getMagicTemplateConfigInfo(magicTemplateConfig.getId());
if (collect==null||collect.isEmpty()){
throw new RpcServiceException("未配置魔方导入模板字段!");
}
TableConfig tableConfig = new TableConfig();
tableConfig.setMagicTemplateConfig(magicTemplateConfig);
tableConfig.setMagicTemplateConfigInfoList(collect);
return tableConfig;
}
/**
* 根据配置ID 获取模板配置信息
* @param configId 魔方模板配置ID
* @return
*/
public List<MagicTemplateConfigInfo> getMagicTemplateConfigInfo(Integer configId) {
if (configId==null) {
return null;
}
Example example1 = new Example(MagicTemplateConfigInfo.class);
example1.createCriteria().andEqualTo("configId", configId);
example1.orderBy("cloumnIndex").asc();
List<MagicTemplateConfigInfo> magicTemplateConfigInfos = magicTemplateConfigInfoMapper.selectByExample(example1);
return magicTemplateConfigInfos;
}
/**
* 封装表头
*
* @param magicTemplateConfigInfoList
* @return
*/
public List<List<String>> head(List<MagicTemplateConfigInfo> magicTemplateConfigInfoList) {
List<MagicTemplateConfigInfo> collect = magicTemplateConfigInfoList.stream().sorted(Comparator.comparing(MagicTemplateConfigInfo::getCloumnIndex)).collect(Collectors.toList());
List<List<String>> list = ListUtils.newArrayList();
for (int i = 0; i < collect.size(); i++) {
if (collect.get(i).getFieldPrimary() == 1) {
continue;
}
List<String> head = ListUtils.newArrayList();
head.add(collect.get(i).getFieldName());
list.add(head);
}
return list;
}
/**
* 批量插入
*
* @param list
* @return
*/
public Integer insert(List<ScrmMagicCubeCustomer> list) {
if (CollectionUtils.isNotEmpty(list)) {
return scrmMagicCubeCustomerMapper.insertList(list);
} else {
return null;
}
}
/**
* 插入记录分页查询
*
* @param pagination
* @return
*/
public Page<ScrmMagicCubeCustomerCountResponse> magicCubeImportRecordQueryPage(RpcPagination<MagicCubeImportRecordQueryRequest> pagination) {
PageHelper.startPage(pagination.getPageNum(), pagination.getPageSize());
List<ScrmMagicCubeCustomerCountResponse> list = scrmMagicCubeCustomerCountMapper.selectByCondition(pagination.getCondition());
createdNameSet(list);
// Integer merchantId = pagination.getCondition().getMerchantId();
// createTotalCount(list,merchantId);
Page<ScrmMagicCubeCustomerCountResponse> pages = (Page<ScrmMagicCubeCustomerCountResponse>) list;
return pages;
}
private void createTotalCount(List<ScrmMagicCubeCustomerCountResponse> list, Integer merchantId) {
if (list == null || list.isEmpty()) {
return;
}
// 查询客户分群数据
RpcResponse<DataGroupCustomerDTO> groupCount = dataCustomerRemote.statisticsCustomerGroupCount(merchantId);
log.info("调用统计客户分群数据接口返回:{}", JSON.toJSONString(groupCount));
DataGroupCustomerDTO groupCountResult = new DataGroupCustomerDTO();
if (groupCount.isSuccess()) {
groupCountResult = groupCount.getResult();
} else {
log.error("魔方导入调用统计客户分群数据接口失败!");
}
// 查询客户数据
RpcResponse<DataTotalCustomerDTO> totalCount = dataCustomerRemote.statisticsCustomerTotalCount(merchantId);
log.info("调用统计客户数据接口返回:{}", JSON.toJSONString(totalCount));
DataTotalCustomerDTO totalCountResult = new DataTotalCustomerDTO();
if (totalCount.isSuccess()) {
totalCountResult = totalCount.getResult();
} else {
log.error("魔方导入调用统计客户数据接口失败!");
}
for (ScrmMagicCubeCustomerCountResponse countResponse : list) {
countResponse.setWaitAddEntmicroTotal(groupCountResult.getUnQyCount());
countResponse.setWaitTotal(groupCountResult.getUnRenewCount());
countResponse.setWaitRegularMaintainTotal(groupCountResult.getUnRegularCount());
countResponse.setTotalNumber(totalCountResult.getTotalCount());
countResponse.setTotalEntmicroNumber(totalCountResult.getQyCount());
countResponse.setTotalMagicCubeNumber(totalCountResult.getMagicCount());
}
}
/**
* 封装回显导入人名称
*
* @param list
*/
private void createdNameSet(List<ScrmMagicCubeCustomerCountResponse> list) {
if (list == null || list.isEmpty()) {
return;
}
for (ScrmMagicCubeCustomerCountResponse countResponse : list) {
countResponse.setImportStaffName(selectStaffName(countResponse.getCreatedBy()));
}
}
/**
* 根据id查询导入记录
*
* @param id
* @return
*/
public ScrmMagicCubeCustomerCountResponse magicCubeImportRecordQueryById(Integer id) {
RpcAssert.assertNotNull(id, DefaultErrorEnum.PARAMETER_NULL, "params must not be null");
ScrmMagicCubeCustomerCountResponse countResponse = scrmMagicCubeCustomerCountMapper.selectById(id);
countResponse.setImportStaffName(selectStaffName(countResponse.getCreatedBy()));
return countResponse;
}
public List<ScrmMagicCubeCustomerResponese> selectByImportFlag(String importFlag) {
RpcAssert.assertNotNull(importFlag, DefaultErrorEnum.PARAMETER_NULL, "params must not be null");
return scrmMagicCubeCustomerMapper.selectByImportFlag(importFlag);
}
public List<ScrmMagicCubeCustomerResponese> selectWaitRenewalByImportFlag(String importFlag) {
RpcAssert.assertNotNull(importFlag, DefaultErrorEnum.PARAMETER_NULL, "params must not be null");
return scrmMagicCubeCustomerMapper.selectWaitRenewalByImportFlag(importFlag);
}
public List<ScrmMagicCubeCustomerResponese> selectWaitRegularMaintainByImportFlag(String importFlag) {
RpcAssert.assertNotNull(importFlag, DefaultErrorEnum.PARAMETER_NULL, "params must not be null");
return scrmMagicCubeCustomerMapper.selectWaitRegularMaintainByImportFlag(importFlag);
}
/**
* 获取员工名称
*
* @param createdBy
* @return
*/
private String selectStaffName(Integer createdBy) {
if (createdBy == null) {
return null;
}
UserResponse userResponse = new UserResponse();
userResponse.setCreatedBy(createdBy);
List<UserResponse> userResponses = commonService.setUser(Arrays.asList(userResponse));
if (userResponses != null && !userResponses.isEmpty()) {
return userResponses.get(0).getCreatedName();
}
return null;
}
/**
* 数据处理
*
* @param tableConfig
* @param mapList
* @param customerCount
* @param needheadTitleMap
* @return 返回异常数据列表
*/
public List<Map<Integer, Object>> datasHandle(TableConfig tableConfig, List<Map<String, Object>> mapList, ScrmMagicCubeCustomerCount customerCount, Map<String, MagicTemplateConfigInfo> needheadTitleMap) {
//数据异常列表
List<Map<Integer, Object>> errorList = null;
List<Map<String, Object>> errorStringList = new LinkedList<>();
//set去重
Set<String> phoneSet = new HashSet<>();
//待添加企微的客户手机号
Set<String> waitAddQW = new HashSet<>();
List<Map<String, Object>> mapListNew = new LinkedList<>();
List<MagicTemplateConfigInfo> infos = new ArrayList<>();
needheadTitleMap.forEach((k,v)->{infos.add(v);});
if (infos.isEmpty()){
log.info("表头数据为空!");
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
}
MagicDynamicRequest magicDynamicRequest = new MagicDynamicRequest();
magicDynamicRequest.setMerchantId(tableConfig.getMagicTemplateConfig().getMerchantId());
magicDynamicRequest.setTableName(tableConfig.getMagicTemplateConfig().getTableName());
// Map<String, MagicTemplateConfigInfo> infoMap = infos.stream().filter(config -> needheadTitleMap.get(config.getFieldName())!=null).collect(Collectors.toMap(MagicTemplateConfigInfo::getFieldName, Function.identity()));
List<MagicDynamicInfoRequest> list = new ArrayList<>(mapList.size());
for (int i = 0; i < mapList.size(); i++) {
Map<String, Object> map = mapList.get(i);
Map<String, Object> custAttr = new HashMap<>();
Map<String, Object> baseAttr = new HashMap<>();
StringBuilder errorMsgBuilder = new StringBuilder();
MagicDynamicInfoRequest magicDynamicInfoRequest = new MagicDynamicInfoRequest();
magicDynamicInfoRequest.setAddWay(addwayMagic);
Map<String, Object> errMap = new LinkedHashMap<>(map.size());
for (Map.Entry<String, Object> entry : map.entrySet()) {
errMap.put(entry.getKey(), entry.getValue());
MagicTemplateConfigInfo info = needheadTitleMap.get(entry.getKey());
String cloumnName = info.getCloumnName();
Object value = entry.getValue();
if (Objects.equals(cloumnName, "customer_phone") && value == null) {
errorMsgBuilder.append("【").append(entry.getKey()).append("】").append("唯一标记必填!");
baseAttr.clear();
custAttr.clear();
break;
}
if (Objects.equals(cloumnName, "car_number")){
//车牌牌照截取
if (value != null){
value= TextUtil.carNumberSubstring(value.toString());
}
}
Object matching = regularMatching(value, info);
boolean equals = Objects.equals(matching, value);
if ((Objects.equals(cloumnName, "customer_phone") && !equals)){
errorMsgBuilder.append("【").append(entry.getKey()).append("】").append(info.getCloumnErrorPrompt());
baseAttr.clear();
custAttr.clear();
break;
}
//去重手机号
if (Objects.equals(cloumnName, "customer_phone") && equals && !phoneSet.add(matching.toString())) {
errorMsgBuilder.append("【").append(entry.getKey()).append("】").append("重复的唯一标记!");
baseAttr.clear();
custAttr.clear();
break;
}
if (Objects.equals(cloumnName, "customer_phone")){
magicDynamicInfoRequest.setCustomerPhone(value.toString());
}
//正则校验不通过,则丢弃值
if (!equals) {
errorMsgBuilder.append("【").append(entry.getKey()).append("】").append(info.getCloumnErrorPrompt());
value = null;
map.put(entry.getKey(),null);
}
Integer fieldBase = info.getFieldBase();
if (1 == fieldBase) {
baseAttr.put(cloumnName, value);
} else {
custAttr.put(cloumnName, value);
}
map.put(entry.getKey(),value);
}
if (baseAttr.size() > 0 || custAttr.size() > 0) {
magicDynamicInfoRequest.setBaseAttr(baseAttr);
magicDynamicInfoRequest.setCustAttr(custAttr);
list.add(magicDynamicInfoRequest);
mapListNew.add(map);
}
if (errorMsgBuilder.length() > 0) {
errMap.put(Constants.MAGIC_ERROR_MSG_FIELD, errorMsgBuilder.toString());
errorStringList.add(errMap);
}
}
errorList = createErrorList(errorStringList, tableConfig);
if (list.isEmpty()){
log.info("数据解析校验后,符合要求的数据条数为0!");
customerCount.setImportNumber(0);
customerCount.setEntmicroNumber(0);
customerCount.setWaitAddEntmicroNumber(0);
customerCount.setNewCustomerNumber(0);
customerCount.setWaitRegularMaintainNumber(0);
customerCount.setWaitRenewalNumber(0);
return errorList;
}
magicDynamicRequest.setCustomers(list);
//配置和数据增加默认公共字段
addFields(infos, mapListNew, customerCount);
//数据比对
RpcResponse<List<CustomerMagicResponse>> listRpcResponse = null;
try {
log.info("魔方导入数据比对接口入参:{}",JSON.toJSONString(magicDynamicRequest));
listRpcResponse = customerRemote.addCustomerMagic(magicDynamicRequest);
} catch (Exception e) {
log.info("魔方导入数据比对接口异常:{}", e);
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR, "魔方导入数据比对失败!");
}
log.info("魔方导入客户数据比对接口返回:{}", JSON.toJSONString(listRpcResponse));
if (listRpcResponse.isFailed()) {
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR, "魔方导入数据比对失败!");
}
List<CustomerMagicResponse> responses = listRpcResponse.getResult();
if (responses == null || responses.isEmpty()) {
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR, "魔方导入数据比对返回数据列表为空!");
}
//唯一字段
MagicTemplateConfigInfo magicTemplateConfigInfo = infos.stream().filter(s -> Objects.equals(s.getCloumnName(), "customer_phone")).collect(Collectors.toList()).get(0);
responses.forEach(s -> {
mapListNew.forEach(map -> {
String phone = (String) map.get(magicTemplateConfigInfo.getFieldName());
if (s.getCustomerPhone().equals(phone)) {
map.put("导入类型",s.getImportType());
map.put("是否新增",s.getCustomerType());
map.put("是否待续保客户",s.getWaitRenewalFlag());
map.put("是否待首保客户",s.getWaitRegularMaintainFlag());
}
});
if (s.getImportType() != null && s.getImportType() == MagicCubeImportConstants.importType) {
//企微客户
Integer entmicroNumber = customerCount.getEntmicroNumber();
customerCount.setEntmicroNumber(entmicroNumber == null ? 1 : (entmicroNumber + 1));
} else {
//待加企微
waitAddQW.add(s.getCustomerPhone());
Integer waitAddEntmicroNumber = customerCount.getWaitAddEntmicroNumber();
customerCount.setWaitAddEntmicroNumber(waitAddEntmicroNumber == null ? 1 : (waitAddEntmicroNumber + 1));
}
//新增
if (s.getCustomerType() != null && s.getCustomerType() == MagicCubeImportConstants.customerType) {
Integer newCustomerNumber = customerCount.getNewCustomerNumber();
customerCount.setNewCustomerNumber(newCustomerNumber == null ? 1 : (newCustomerNumber + 1));
}
//待续保
if (s.getWaitRenewalFlag() != null && s.getWaitRenewalFlag() == MagicCubeImportConstants.waitRenewalFlag) {
Integer waitRenewalNumber = customerCount.getWaitRenewalNumber();
customerCount.setWaitRenewalNumber(waitRenewalNumber == null ? 1 : (waitRenewalNumber + 1));
}
//待定保
if (s.getWaitRegularMaintainFlag() != null && s.getWaitRegularMaintainFlag() == MagicCubeImportConstants.waitRegularMaintainFlag) {
Integer waitRegularMaintainNumber = customerCount.getWaitRegularMaintainNumber();
customerCount.setWaitRegularMaintainNumber(waitRegularMaintainNumber == null ? 1 : (waitRegularMaintainNumber + 1));
}
});
customerCount.setImportNumber(phoneSet.size());
tableConfig.setMagicTemplateConfigInfoList(infos);
insertDatas(tableConfig, mapListNew, needheadTitleMap);
//调用接口同步数据
if (waitAddQW.size() > 0) {
try {
List<String> waitAddQwPhones = new ArrayList<>(waitAddQW);
dracImService.syncDarc(waitAddQwPhones, Integer.parseInt(customerCount.getMerchantId()), customerCount.getCreatedBy(),1);
waitAddQW.clear();
} catch (Exception e) {
log.info("魔方导入调用接口同步数据异常:{}", e);
// e.printStackTrace();
// throw new RpcServiceException("魔方导入调用接口同步数据失败!");
}
}
return errorList;
}
//组装错误列表
public List<Map<Integer, Object>> createErrorList(List<Map<String, Object>> errorStringList, TableConfig tableConfig) {
List<MagicTemplateConfigInfo> infos = tableConfig.getMagicTemplateConfigInfoList();
List<Map<Integer, Object>> errMapList = new LinkedList<>();
for (int i = 0; i < errorStringList.size(); i++) {
Map<String, Object> map = errorStringList.get(i);
Map<Integer, Object> repMap = new LinkedHashMap<>();
for (int j = 0; j < infos.size(); j++) {
MagicTemplateConfigInfo info = infos.get(j);
Object o = map.get(info.getFieldName());
repMap.put(j, o);
}
//增加错误说明字段
Object o = map.get(Constants.MAGIC_ERROR_MSG_FIELD);
repMap.put(repMap.size(), o);
errMapList.add(repMap);
}
return errMapList;
}
//字段数据正则校验
public Object regularMatching(Object obj, MagicTemplateConfigInfo info) {
if (obj == null) {
return null;
}
if (info.getFieldType()==FieldTypeNameEnum.DATE.getFieldType()){
boolean b = DateUtil.isDate(obj.toString().trim());
return b ? obj : info.getCloumnErrorPrompt();
}
boolean matches = obj.toString().trim().replace("\n","")
.replace("\t","")
.replace("\r","")
.replace("\f","").matches(info.getCloumnRegular());
return matches ? obj : info.getCloumnErrorPrompt();
}
//获取示例数据
public List<Map<Integer, Object>> getDemoData(List<List<String>> head) {
List<Map<Integer, Object>> list = new LinkedList<>();
Map<Integer, Object> map = new LinkedHashMap<>();
head.forEach(s -> {
String h = s.get(0);
switch (h) {
case "姓名":
map.put(0, "张三");
break;
case "手机号码":
map.put(1, "11012341234");
break;
case "性别":
map.put(2, 1);
break;
case "身份证号码":
map.put(3, "452536000010112145");
break;
case "车牌号":
map.put(4, "粤B U872N");
break;
case "车架号":
map.put(5, "LNBMCUAK0LT118040");
break;
case "购车日期":
map.put(6, "2021/11/8");
break;
case "近期保养日期":
map.put(7, "2021/11/8");
break;
case "近期到店日期":
map.put(8, "2021/11/8");
break;
case "商业险生效日期":
map.put(9, "2021/11/8");
break;
case "商业险到期日期":
map.put(10, "2021/11/8");
break;
case "品牌":
map.put(11, "奥迪");
break;
case "车系":
map.put(12, "奥迪A6L");
break;
case "车型":
map.put(13, "2022款 40 TFSI 豪华致雅型");
break;
case "填写说明":
map.put(14, "性别:0-未知 1-男性 2-女性;\n" +
"姓名长度不能超过50字符;\n" +
"手机号码必须是11位必填;\n" +
"身份证号码18位,有字母请大写;\n" +
"车牌号最长8位;\n" +
"车架号最长17位;\n" +
"日期类必须按此格式:yyyy/MM/dd;\n" +
"品牌、车型和车系字符长度不能超过50字符。");
break;
default:
break;
}
});
list.add(map);
return list;
}
}