EntmicroStaffService.java
67 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
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
package cn.com.glsx.entmicro.service;
import cn.com.glsx.entmicro.config.RedisLock;
import cn.com.glsx.entmicro.constants.Constants;
import cn.com.glsx.entmicro.entity.EntmicroAuthorize;
import cn.com.glsx.entmicro.entity.EntmicroFollowerType;
import cn.com.glsx.entmicro.entity.EntmicroStaff;
import cn.com.glsx.entmicro.entity.EntmicroStaffDepartRelation;
import cn.com.glsx.entmicro.manager.CustomerSyncToBigDataComponent;
import cn.com.glsx.entmicro.mapper.EntmicroDepartmentMapper;
import cn.com.glsx.entmicro.mapper.EntmicroStaffMapper;
import cn.com.glsx.entmicro.model.MerchantRopResponse;
import cn.com.glsx.entmicro.model.MerchantStaffModel;
import cn.com.glsx.entmicro.model.StaffRopResponse;
import cn.com.glsx.entmicro.model.request.*;
import cn.com.glsx.entmicro.model.request.wechatcloudcontrol.login.WechatDepositRequest;
import cn.com.glsx.entmicro.model.response.*;
import cn.com.glsx.entmicro.model.wechatcloudcontrol.CloudControlBaseResponse;
import cn.com.glsx.entmicro.model.wechatcloudcontrol.LoginCallBackModel;
import cn.com.glsx.entmicro.model.wechatcloudcontrol.StaffCardResponse;
import cn.com.glsx.entmicro.model.wechatcloudcontrol.StaffInfoRequest;
import cn.com.glsx.entmicro.service.wechatcloudcontrol.manager.WechatCloudControlManager;
import cn.com.glsx.entmicro.util.EmojiUtils;
import cn.com.glsx.entmicro.util.UUIDUtils;
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.enums.ErrorEnum;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.scrm.proxy.dto.merchant.ListMerchantRequest;
import cn.com.glsx.scrm.wechat.dto.workwechat.WorkWechatBaseRequestDTO;
import cn.com.glsx.scrm.wechat.dto.workwechat.external.response.WorkWechatExternalContactFollowUserListResponseDTO;
import cn.com.glsx.scrm.wechat.dto.workwechat.user.request.GetWorkWechatUserListRequestDTO;
import cn.com.glsx.scrm.wechat.dto.workwechat.user.request.GetWorkWechatUserRequestDTO;
import cn.com.glsx.scrm.wechat.dto.workwechat.user.request.GetWorkWechatUserSimpleListRequestDTO;
import cn.com.glsx.scrm.wechat.dto.workwechat.user.response.WorkWechatUserListResponseDTO;
import cn.com.glsx.scrm.wechat.dto.workwechat.user.response.WorkWechatUserResponseDTO;
import cn.com.glsx.scrm.wechat.dto.workwechat.user.response.WorkWechatUserSimpleListResponseDTO;
import cn.com.glsx.scrm.wechat.dto.workwechat.user.response.WorkWechatUserSimpleResponseDTO;
import cn.com.glsx.scrm.wechat.remote.WorkWechatExternalContactRemote;
import cn.com.glsx.scrm.wechat.remote.WorkWechatUserRemote;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.glsx.plat.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author Lichongyuan
* @Description:
* @date 2021/9/16 11:33
*/
@Service
@Slf4j
public class EntmicroStaffService {
@Resource
private EntmicroStaffMapper entmicroStaffMapper;
@Resource
private WorkWechatUserRemote workWechatUserRemote;
@Autowired
private MerchantService merchantService;
@Autowired
private WorkWechatExternalContactRemote workWechatExternalContactRemote;
@Resource
private EntmicroDepartmentMapper entmicroDepartmentMapper;
@Autowired
private EntmicroDepartmentPathService entmicroDepartmentPathService;
@Autowired
private EntmicroStaffDepartRelationService entmicroStaffDepartRelationService;
@Autowired
private CustomerSyncToBigDataComponent customerSyncToBigDataComponent;
@Autowired
private RedisLock redisLock;
private final static String LOCK_ID = "staff";
@Autowired
private WechatCloudControlManager wechatCloudControlManager;
/**
* 根据公用的跟进类型id查询员工
*/
public List<EntmicroStaffResponse> listStaffByDefalutCode(Integer followerTypeId, Integer merchantId) {
return entmicroStaffMapper.listStaffByDefalutCode(followerTypeId, merchantId);
}
/**
* 根据跟进类型id查询员工
*/
public List<EntmicroStaffResponse> listStaffByFollowerTypeId(Integer followerTypeId, Integer merchantId) {
return entmicroStaffMapper.listStaffByFollowerTypeId(followerTypeId, merchantId);
}
/**
* 新增员工信息
*/
@Transactional(rollbackFor = RpcServiceException.class)
public void saveEntmicroStaff(EntmicroStaff entmicroStaff, boolean flag) {
boolean lock = redisLock.getLock(LOCK_ID + entmicroStaff.getUserId(), 10 * 1000);
try {
if (lock) {
log.info("新增员工信息{}", entmicroStaff);
EntmicroStaffResponse entmicrosta = findByMerchantIdAndUserId(entmicroStaff);
if (entmicrosta != null) {
log.info("员工已经存在{}", entmicroStaff.getStaffName());
return;
}
entmicroStaff.setCreatedTime(new Date());
entmicroStaff.setStaffUuid(UUIDUtils.getUUID());
entmicroStaffMapper.insertUseGeneratedKeys(entmicroStaff);
}
} catch (Exception e) {
log.info("更新客户信息异常{}", e);
} finally {
redisLock.releaseLock(LOCK_ID + entmicroStaff.getUserId());
}
List<EntmicroStaffDepartRelation> entmicroStaffDepartRelationList = entmicroStaff.getEntmicroStaffDepartRelationList();
// 1.新增员工的部门关联表
entmicroStaffDepartRelationService.insertList(buildStaffDepartRelation(entmicroStaffDepartRelationList, entmicroStaff.getId()));
// 2.同步企微数据
syncEntmicroStaff(buildSyncStaffDepart(entmicroStaffDepartRelationList, entmicroStaff), "add");
}
/**
* 查询商户下面的员工
*
* @param entmicroStaff 入参
* @return
*/
public EntmicroStaffResponse findByMerchantIdAndUserId(EntmicroStaff entmicroStaff) {
//重复判断
List<EntmicroDepartmentResponse> authorizeDepartment = entmicroDepartmentMapper.findAuthorizeDepartment(entmicroStaff.getMerchantid());
if (CollectionUtils.isNotEmpty(authorizeDepartment)) {
List<Integer> collect = authorizeDepartment.stream().map(EntmicroDepartmentResponse::getRelevanceMerchant).collect(Collectors.toList());
EntmicroStaffResponse entmicrosta = entmicroStaffMapper.findByMerchantIdAndUserId(entmicroStaff, collect);
return entmicrosta;
}
return null;
}
/**
* 查询企微部门下面的员工
*
* @param merchantId 商户号
* @param departmentId 企微部门id
* @return
*/
public List<EntmicroStaffResponse> findStaffByEntDepartment(Integer merchantId, Integer departmentId) {
//重复判断
List<EntmicroStaffResponse> entmicroStaffResponseList = new ArrayList<>();
List<EntmicroDepartmentResponse> authorizeDepartment = entmicroDepartmentMapper.findAuthorizeDepartment(merchantId);
if (CollectionUtils.isNotEmpty(authorizeDepartment)) {
List<Integer> collect = authorizeDepartment.stream().map(EntmicroDepartmentResponse::getRelevanceMerchant).collect(Collectors.toList());
List<Integer> subordinateDep = new ArrayList<>();
if (departmentId != null) {
Integer id = entmicroDepartmentMapper.findIdByDepartmentId(departmentId, merchantId);
subordinateDep = entmicroDepartmentPathService.findSubordinateDep(id);
}
entmicroStaffResponseList = entmicroStaffMapper.findStaffByEntDepartment(collect, subordinateDep);
}
return entmicroStaffResponseList;
}
private List<EntmicroStaffDepartRelation> buildStaffDepartRelation(List<EntmicroStaffDepartRelation> entmicroStaffDepartRelationList, Integer id) {
entmicroStaffDepartRelationList.forEach(index -> {
index.setStaffId(id);
index.setDelFlag(Constants.NORMAL_FLAG);
index.setCreatedTime(new Date());
});
return entmicroStaffDepartRelationList;
}
/**
* 同步员工到账户
*
* @param entmicroStaff
* @param type 1.add新增 2.update更新 3.delete 删除
*/
public void syncEntmicroStaff(List<EntmicroStaff> entmicroStaff, String type) {
if (CollectionUtils.isNotEmpty(entmicroStaff)) {
entmicroStaff.forEach(index -> {
index.setType(type);
});
log.info("同步员工{}到账户系统", entmicroStaff);
// 过滤手机号为空的客户
List<EntmicroStaff> collect = entmicroStaff.stream().filter(index -> StringUtils.isNotEmpty(index.getStaffPhone())).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(collect)) {
merchantService.syncEntmicroStaff(entmicroStaff);
}
}
}
/**
* 更新员工信息
*
* @param entmicroStaff
*/
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = RpcServiceException.class)
public void updateEntmicroStaff(EntmicroStaff entmicroStaff, boolean flag) {
log.info("更新员工信息{}", JSONArray.toJSONString(entmicroStaff));
// 1.根据员工id查询出员工所在的部门
/* if (StringUtils.isEmpty(entmicroStaff.getStaffPhone())) {
log.info("未获取到手机号{}", entmicroStaff.getUserId());
return;
}*/
List<EntmicroStaffDepartRelation> departByStaffId = null;
EntmicroStaffResponse entmicrosta = findByMerchantIdAndUserId(entmicroStaff);
if (entmicrosta != null) {
//员工userId是否改变
if (StringUtils.isNotEmpty(entmicroStaff.getNewUserID())) {
entmicroStaff.setUserId(entmicroStaff.getNewUserID());
}
entmicroStaff.setOldMerchantId(entmicrosta.getMerchantId());
entmicroStaff.setId(entmicrosta.getId());
entmicroStaff.setStaffPhone(entmicroStaff.getStaffPhone() == null ? entmicrosta.getStaffPhone() : entmicroStaff.getStaffPhone());
//获取当前员工的所有部门关联
departByStaffId = entmicroStaffDepartRelationService.findDepartByStaffId(entmicroStaff.getId());
if (CollectionUtils.isNotEmpty(departByStaffId)) {
if (StringUtils.isNotEmpty(entmicroStaff.getStaffPhone())) {
List<EntmicroStaff> collect = departByStaffId.stream().filter(index -> !entmicroStaff.getStaffPhone().equals(index.getStaffPhone()) && StringUtils.isNotEmpty(index.getStaffPhone())).map(index -> {
EntmicroStaff newEntmicroStaff = new EntmicroStaff();
String staffPhone = index.getStaffPhone();
entmicroStaff.setOldPhone(staffPhone);
if (!entmicroStaff.getStaffPhone().equals(staffPhone)) {
log.info("手机号变更{},to,{},merchantId: {}", staffPhone, entmicroStaff.getStaffPhone(), index.getRelevanceMerchant());
BeanUtils.copyProperties(entmicroStaff, newEntmicroStaff);
newEntmicroStaff.setMerchantid(index.getRelevanceMerchant());
}
return newEntmicroStaff;
}).collect(Collectors.toList());
syncEntmicroStaff(collect, "update");
}
}
entmicroStaff.setUpdatedTime(new Date());
entmicroStaffMapper.updateByPrimaryKeySelective(entmicroStaff);
} else {
// 2024-01-15 员工不存在则新增
saveEntmicroStaff(entmicroStaff, entmicroStaff.isFlag());
return;
}
List<EntmicroStaffDepartRelation> entmicroStaffDepartRelationList = entmicroStaff.getEntmicroStaffDepartRelationList();
log.info("企微员工部门{}", entmicroStaffDepartRelationList);
log.info("scrm员工部门{}", departByStaffId);
List<EntmicroStaffDepartRelation> finalDepartByStaffId = departByStaffId;
List<EntmicroStaffDepartRelation> addlist = entmicroStaffDepartRelationList.stream().filter(index -> !finalDepartByStaffId.contains(index)).collect(Collectors.toList());
List<EntmicroStaffDepartRelation> updatelist = entmicroStaffDepartRelationList.stream().filter(index -> finalDepartByStaffId.contains(index)).collect(Collectors.toList());
List<EntmicroStaffDepartRelation> deletelist = departByStaffId.stream().filter(index -> !entmicroStaffDepartRelationList.contains(index)).collect(Collectors.toList());
// 集团版同步员工,门店版不同步
List<Integer> updateMerchantIdList = null;
List<Integer> addMerchantIdList = null;
if (CollectionUtils.isNotEmpty(updatelist)) {
updateMerchantIdList = updatelist.stream().map(EntmicroStaffDepartRelation::getRelevanceMerchant).collect(Collectors.toList());
// 更新部门信息
log.info("更新员工部门{}", updatelist);
if (CollectionUtils.isNotEmpty(updatelist)) {
syncEntmicroStaff(buildSyncStaffDepart(updatelist, entmicroStaff), "update");
}
}
if (CollectionUtils.isNotEmpty(addlist)) {
// 1.新增员工的部门关联表
addMerchantIdList = addlist.stream().map(EntmicroStaffDepartRelation::getRelevanceMerchant).collect(Collectors.toList());
log.info("新增员工部门{}", addlist);
entmicroStaffDepartRelationService.insertList(buildStaffDepartRelation(addlist, entmicroStaff.getId()));
// 更新部门信息
if (CollectionUtils.isNotEmpty(addlist)) {
List<EntmicroStaff> entmicroStaffList = buildSyncStaffDepart(addlist, entmicroStaff);
syncEntmicroStaff(entmicroStaffList, "add");
customerSyncToBigDataComponent.syncCustomerToBigData(entmicroStaffList, "0");
}
}
if (CollectionUtils.isNotEmpty(deletelist)) {
// 删除部门信息
log.info("更新员工部门{}", deletelist);
entmicroStaffDepartRelationService.deleteStaffDepartment(buildStaffDepartRelation(deletelist, entmicroStaff.getId()));
log.info("同步员工删除{}到账户系统", entmicroStaff);
if (CollectionUtils.isNotEmpty(deletelist)) {
List<EntmicroStaff> entmicroStaffList = buildSyncStaffDepart(deletelist, entmicroStaff);
// 过滤出还有变更部门或者新增的门店
if (CollectionUtils.isNotEmpty(updateMerchantIdList)) {
List<Integer> finalCollect = updateMerchantIdList;
entmicroStaffList = entmicroStaffList.stream().filter(index -> !finalCollect.contains(index.getMerchantid())).collect(Collectors.toList());
}
if (CollectionUtils.isNotEmpty(addMerchantIdList)) {
List<Integer> finalCollect = addMerchantIdList;
entmicroStaffList = entmicroStaffList.stream().filter(index -> !finalCollect.contains(index.getMerchantid())).collect(Collectors.toList());
}
if (CollectionUtils.isNotEmpty(entmicroStaffList)) {
syncEntmicroStaff(entmicroStaffList, "delete");
customerSyncToBigDataComponent.syncCustomerToBigData(entmicroStaffList, "2");
}
}
}
}
/**
* 删除员工信息
*
* @param entmicroStaff
*/
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = RpcServiceException.class)
public void deleteEntmicroStaff(EntmicroStaff entmicroStaff) {
EntmicroStaffResponse entmicrosta = findByMerchantIdAndUserId(entmicroStaff);
if (entmicrosta != null) {
entmicroStaff.setId(entmicrosta.getId());
entmicroStaff.setStaffPhone(entmicrosta.getStaffPhone());
}
entmicroStaff.setDelFlag(Constants.DEL_FLAG);
entmicroStaffMapper.deleteEntmicroStaff(entmicroStaff);
// 删除员工所在的部门
List<EntmicroStaffDepartRelation> departByStaffId = entmicroStaffDepartRelationService.findDepartByStaffId(entmicroStaff.getId());
entmicroStaffDepartRelationService.deleteStaff(entmicroStaff.getId());
if (CollectionUtils.isNotEmpty(departByStaffId)) {
// 删除部门信息
entmicroStaff.setDelFlag(Constants.NORMAL_FLAG);
log.info("同步员工删除{}到账户系统", departByStaffId);
if (CollectionUtils.isNotEmpty(departByStaffId)) {
syncEntmicroStaff(buildSyncStaffDepart(departByStaffId, entmicroStaff), "delete");
}
}
//TODO 删除和活码的关联 删除和商城的关联 删除和客户的关联
}
/**
* 主动拉取企微所有员工
*/
public void syncStaff(SyncStaffRpcRequest syncStaffRpcRequest) {
//TODO 商户模式判断
try {
syncEntmicroStaff(syncStaffRpcRequest.getMerchantId(), true, null, true);
} catch (RpcServiceException e) {
log.error(syncStaffRpcRequest.getMerchantId() + ":同步异常", e);
}
}
/**
* 同步企微员工(所有)
*
* @param merchantId 商户号
*/
@Transactional(rollbackFor = RpcServiceException.class)
public List<EntmicroStaff> syncEntmicroStaff(Integer merchantId, boolean flag, Integer departmentId, boolean allFlag) {
//获取远程数据
log.info("开始同步员工--------------{},{}", merchantId, departmentId);
List<EntmicroStaff> addList = new ArrayList<>();
List<EntmicroStaff> remoteList = getEntmicroStaff(merchantId, departmentId == null ? 1 : departmentId);
if (CollectionUtils.isNotEmpty(remoteList)) {
//获取本地数据
log.info("同步员工size--------------{},{},{}", merchantId, departmentId, remoteList.size());
List<EntmicroStaffResponse> localList = findStaffByEntDepartment(merchantId, departmentId);
//转换同一类型进行过滤
List<EntmicroStaff> localTransformList = localList.stream().map(index -> {
EntmicroStaff entmicroStaff = new EntmicroStaff();
BeanUtils.copyProperties(index, entmicroStaff);
entmicroStaff.setMerchantid(merchantId);
return entmicroStaff;
}).collect(Collectors.toList());
//过滤新增/更新/删除数据
addList = remoteList.stream().filter(index -> !localTransformList.contains(index)).collect(Collectors.toList());
List<EntmicroStaff> deleteList = localTransformList.stream().filter(index -> !remoteList.contains(index)).collect(Collectors.toList());
List<EntmicroStaff> updateList = new ArrayList<>();
localTransformList.forEach(localIndex -> {
if (remoteList.contains(localIndex)) {
//取出具体数据
remoteList.forEach(index -> {
if (localIndex.getUserId().equals(index.getUserId()) && index.getDelFlag() != Constants.DEL_FLAG) {
updateList.add(index);
}
});
}
});
//新增
if (CollectionUtils.isNotEmpty(addList)) {
addList.forEach(index -> {
try {
saveEntmicroStaff(index, flag);
} catch (RpcServiceException e) {
log.error(index.getStaffName() + "新增异常", e);
}
});
}
//删除
if (CollectionUtils.isNotEmpty(deleteList)) {
deleteList.forEach(index -> {
deleteEntmicroStaff(index);
});
}
//更新
if (CollectionUtils.isNotEmpty(updateList)) {
updateList.forEach(index -> {
try {
updateEntmicroStaff(index, flag);
} catch (RpcServiceException e) {
log.error(index.getStaffName() + "更新异常", e);
}
});
}
}
if (allFlag) {
return remoteList;
} else {
return addList;
}
}
/**
* 更新员工外部联系人权限
*
* @param
*/
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = RpcServiceException.class)
public void updateAddOutsider(List<String> userId, Integer merchantId, byte isAddOutsider) {
entmicroStaffMapper.updateAddOutsider(userId, merchantId, isAddOutsider);
}
private Integer findDepartMerchantNo(EntmicroDepartmentResponse departmentResponse, List<EntmicroDepartmentResponse> departmentByMerchatId) {
if (departmentResponse.getRelevanceMerchant() == null) {
// 取关联部门的商户号
EntmicroDepartmentResponse department = departmentByMerchatId.stream().filter(depart -> depart.getDepartmentId().intValue() == departmentResponse.getEntmicroParentId()).findFirst().get();
return findDepartMerchantNo(department, departmentByMerchatId);
} else {
return departmentResponse.getRelevanceMerchant();
}
}
/**
* 获取企微员工
*
* @param merchantId 商户号
* @param departId 部门id
* @return
*/
public List<EntmicroStaff> getEntmicroStaff(Integer merchantId, Integer departId) {
List<EntmicroStaff> collect = new ArrayList<>();
GetWorkWechatUserListRequestDTO getWorkWechatUserRequestDTO = new GetWorkWechatUserListRequestDTO();
getWorkWechatUserRequestDTO.setMerchantId(merchantId);
getWorkWechatUserRequestDTO.setDepartmentId(departId);
log.info("获取企微员工入参: {}", JSONArray.toJSONString(getWorkWechatUserRequestDTO));
RpcResponse<WorkWechatUserListResponseDTO> userList = workWechatUserRemote.getUserList(getWorkWechatUserRequestDTO);
log.info("获取企微员工result: {}", JSONArray.toJSONString(userList));
if (userList.isSuccess()) {
WorkWechatUserListResponseDTO result = userList.getResult();
List<WorkWechatUserResponseDTO> workWechatUserResponseDTOS = result.getUserList();
if (CollectionUtils.isNotEmpty(workWechatUserResponseDTOS)) {
List<EntmicroDepartmentResponse> departmentByMerchatId = entmicroDepartmentMapper.selectByDepartmentId(merchantId);
collect = workWechatUserResponseDTOS.stream().map(index -> {
// 查询库里对应的部门id
log.info("部门主部门id{},部门id{}", index.getDepartment(), index.getMainDepartment());
List<EntmicroStaffDepartRelation> entmicroStaffDepartRelations = new ArrayList<>();
EntmicroStaff entmicroStaff = new EntmicroStaff();
//entmicroStaff.setDepartmentId(departmentResponse.getId() + "");
entmicroStaff.setUserId(index.getUserId());
entmicroStaff.setCreatedBy(merchantId);
entmicroStaff.setCreatedTime(new Date());
entmicroStaff.setDelFlag(index.getStatus().equals(1) ? Constants.NORMAL_FLAG : Constants.OTHER_FLAG);
String staff_name = StringUtils.isNotEmpty(index.getName()) ? EmojiUtils.parseToHtmlHexadecimal(index.getName()) : index.getName();
entmicroStaff.setStaffName(staff_name);
entmicroStaff.setStaffEmail(index.getEmail());
//entmicroStaff.setMerchantid(departmentResponse.getRelevanceMerchant() == null ? merchantId : departmentResponse.getRelevanceMerchant());
entmicroStaff.setStaffTitle(index.getPosition());
entmicroStaff.setMerchantid(merchantId);
entmicroStaff.setStaffPhone(index.getMobile());
entmicroStaff.setStatus(index.getStatus());
entmicroStaff.setStaffSex(index.getGender());
entmicroStaff.setThumbAvatar(index.getThumbAvatar());
entmicroStaff.setQrCode(index.getQrCode());
Integer[] department = index.getDepartment();
for (Integer integer : department) {
EntmicroStaffDepartRelation entmicroStaffDepartRelation = new EntmicroStaffDepartRelation();
// 2024-01-15 修复所属部门不存在异常问题,可见范围部门不存在问题
List<EntmicroDepartmentResponse> entmicroDepartmentResponses = departmentByMerchatId.stream()
.filter(ind -> ind.getDepartmentId().equals(integer)).collect(
Collectors.toList());
if(CollectionUtils.isNotEmpty(entmicroDepartmentResponses)){
EntmicroDepartmentResponse entmicroDepartmentResponse = entmicroDepartmentResponses.get(
0);
entmicroStaffDepartRelation.setDepartmentId(entmicroDepartmentResponse.getId());
Integer departMerchantNo = findDepartMerchantNo(entmicroDepartmentResponse, departmentByMerchatId);
entmicroStaffDepartRelation.setRelevanceMerchant(departMerchantNo);
entmicroStaffDepartRelation.setStaffPhone(index.getMobile());
entmicroStaffDepartRelations.add(entmicroStaffDepartRelation);
}
}
entmicroStaff.setEntmicroStaffDepartRelationList(entmicroStaffDepartRelations);
return entmicroStaff;
}).collect(Collectors.toList());
}
} else {
log.info("调用员工详情失败入參{}", getWorkWechatUserRequestDTO);
ErrorEnum error = userList.getError();
throw new RpcServiceException(error.getValue(), error.getDisplayName());
}
return collect;
}
/**
* 获取企微员工
*
* @param merchantId 商户号
* @param userId 员工userid
* @return
*/
public EntmicroStaff getEntmicroSimpleStaff(Integer merchantId, String userId) {
GetWorkWechatUserRequestDTO getWorkWechatUserRequestDTO = new GetWorkWechatUserRequestDTO();
getWorkWechatUserRequestDTO.setMerchantId(merchantId);
getWorkWechatUserRequestDTO.setUserId(userId);
log.info("获取企微员工入参: {}", JSONArray.toJSONString(getWorkWechatUserRequestDTO));
RpcResponse<WorkWechatUserResponseDTO> user = workWechatUserRemote.getUser(getWorkWechatUserRequestDTO);
log.info("获取企微员工result: {}", JSONArray.toJSONString(user));
if (user.isSuccess()) {
WorkWechatUserResponseDTO index = user.getResult();
List<EntmicroDepartmentResponse> departmentByMerchatId = entmicroDepartmentMapper.selectByDepartmentId(merchantId);
// 查询库里对应的部门id
log.info("部门主部门id{},部门id{}", index.getDepartment(), index.getMainDepartment());
List<EntmicroStaffDepartRelation> entmicroStaffDepartRelations = new ArrayList<>();
EntmicroStaff entmicroStaff = new EntmicroStaff();
entmicroStaff.setCreatedBy(merchantId);
entmicroStaff.setUserId(index.getUserId());
entmicroStaff.setCreatedTime(new Date());
entmicroStaff.setDelFlag(index.getStatus().equals(1) ? Constants.NORMAL_FLAG : Constants.OTHER_FLAG);
String staff_name = StringUtils.isNotEmpty(index.getName()) ? EmojiUtils.parseToHtmlHexadecimal(index.getName()) : index.getName();
entmicroStaff.setStaffEmail(index.getEmail());
entmicroStaff.setStaffName(staff_name);
entmicroStaff.setMerchantid(merchantId);
entmicroStaff.setStaffPhone(index.getMobile());
entmicroStaff.setStaffTitle(index.getPosition());
entmicroStaff.setStatus(index.getStatus());
entmicroStaff.setStaffSex(index.getGender());
entmicroStaff.setQrCode(index.getQrCode());
entmicroStaff.setThumbAvatar(index.getThumbAvatar());
Integer[] department = index.getDepartment();
for (Integer integer : department) {
EntmicroStaffDepartRelation entmicroStaffDepartRelation = new EntmicroStaffDepartRelation();
Optional<EntmicroDepartmentResponse> first = departmentByMerchatId.stream()
.filter(ind -> ind.getDepartmentId().equals(integer)).findFirst();
if(first.isPresent()) {
EntmicroDepartmentResponse departmentResponse = first.get();
entmicroStaffDepartRelation.setDepartmentId(departmentResponse.getId());
Integer departMerchantNo = findDepartMerchantNo(departmentResponse,
departmentByMerchatId);
entmicroStaffDepartRelation.setStaffPhone(index.getMobile());
entmicroStaffDepartRelation.setRelevanceMerchant(departMerchantNo);
entmicroStaffDepartRelations.add(entmicroStaffDepartRelation);
}
}
entmicroStaff.setEntmicroStaffDepartRelationList(entmicroStaffDepartRelations);
return entmicroStaff;
}
return null;
}
/**
* 查询拥有客户联系权限的员工
*/
public List<EntmicroStaffResponse> listIsAddOutsiderStaff(EntmicroStaffRequest entmicroStaffRequest) {
//emoji解码搜索条件
entmicroStaffRequest.setEntmicroName(StringUtils.isNotEmpty(entmicroStaffRequest.getStaffName()) ? EmojiUtils.parseToHtmlHexadecimal(entmicroStaffRequest.getStaffName()) : entmicroStaffRequest.getStaffName());
List<EntmicroStaffResponse> entmicroStaffResponseList = entmicroStaffMapper.listIsAddOutSiderStaff(entmicroStaffRequest);
//emoji转化
if (CollectionUtils.isNotEmpty(entmicroStaffResponseList)) {
entmicroStaffResponseList.forEach(index -> {
String staffName = index.getStaffName() != null ? EmojiUtils.parseToUnicode(index.getStaffName()) : null;
index.setStaffName(staffName);
});
}
return entmicroStaffResponseList;
}
/**
* 查询授权商户
*
* @param merchantId 商户号
* @return
*/
public Integer findSupMerchantNo(Integer merchantId) {
MerchantRopResponse merchantByMerchantNo = merchantService.findMerchantByMerchantNo(merchantId);
if (merchantByMerchantNo == null) {
return null;
}
Integer merchantModel = merchantByMerchantNo.getMerchantModel();
if (merchantModel != null) {
if (merchantModel.intValue() == Constants.STORE_MODEL) {
return merchantId;
} else {
return merchantService.findSuperlativeMerchant(merchantId);
}
}
return null;
}
/**
* 查询授权商户集合
*
* @param merchantId
* @return
*/
public List<Integer> findAuthorizeMerchantNo(Integer merchantId) {
return entmicroDepartmentMapper.findAuthorizeMerchantNo(merchantId);
}
/**
* 查询授权部门
*
* @param merchantId
* @return
*/
public List<EntmicroDepartmentResponse> findAuthorizeDepartment(Integer merchantId) {
return entmicroDepartmentMapper.findAuthorizeDepartment(merchantId);
}
/**
* 同步有外部联系人权限的员工
*
* @param merchantId
*/
public void syncIsAddOutSiderStaffList(Integer merchantId) {
// 查询授权的商户
Integer merchantNo = entmicroDepartmentMapper.findSupMerchantNo(merchantId);
List<EntmicroStaff> remoteList = getExternalContactFollowUserList(merchantNo);
log.info("企微外部联系人权限的员工数据: {}", remoteList);
//获取本地数据
EntmicroStaffRequest entmicroStaffRequest = new EntmicroStaffRequest();
entmicroStaffRequest.setMerchantId(merchantId);
List<EntmicroStaffResponse> localList = entmicroStaffMapper.findStaffByMerchantId(entmicroStaffRequest);
log.info("同步有外部联系人权限的员工本地数据: {}", localList);
//转化本地数据
List<EntmicroStaff> localTransformList = localList.stream().map(index -> {
EntmicroStaff entmicroStaff = new EntmicroStaff();
BeanUtils.copyProperties(index, entmicroStaff);
entmicroStaff.setMerchantid(merchantId);
return entmicroStaff;
}).collect(Collectors.toList());
//对比删除的数据
log.info("同步有外部联系人权限的员工转化本地数据: {}", localTransformList);
List<EntmicroStaff> deleteList = new ArrayList<>();
if (CollectionUtils.isNotEmpty(remoteList)) {
deleteList = localTransformList.stream().filter(index -> !remoteList.contains(index)).collect(Collectors.toList());
try {
//企微的所有数据都更新本地外部联系人状态
List<String> userId = remoteList.stream().map(EntmicroStaff::getUserId).collect(Collectors.toList());
log.info("更新外部联系员工: {}", userId);
updateAddOutsider(userId, merchantId, Constants.IS_ADD_OUTSIDER_FLAG);
} catch (RpcServiceException e) {
log.info("同步新增外部联系员工失败{}", remoteList);
throw new RpcServiceException(e.getError().getValue(), e.getMessage());
}
} else {
deleteList = localTransformList;
}
//企微没有的数据都删除
if (CollectionUtils.isNotEmpty(deleteList)) {
try {
List<String> deleteUserId = deleteList.stream().map(EntmicroStaff::getUserId).collect(Collectors.toList());
log.info("删除外部联系员工: {}", deleteUserId);
updateAddOutsider(deleteUserId, merchantId, Constants.NO_ADD_OUTSIDER_FLAG);
} catch (RpcServiceException e) {
log.info("同步删除外部联系员工失败{}", deleteList);
throw new RpcServiceException(e.getError().getValue(), e.getMessage());
}
}
}
/**
* 获取有外部联系人权限的员工
*
* @param merchantId
* @return
*/
public List<EntmicroStaff> getExternalContactFollowUserList(Integer merchantId) {
List<EntmicroStaff> remoteList = new ArrayList<>();
WorkWechatBaseRequestDTO remoteParam = new WorkWechatBaseRequestDTO();
remoteParam.setMerchantId(merchantId);
//获取远程数据
RpcResponse<WorkWechatExternalContactFollowUserListResponseDTO> externalContactFollowUserList = workWechatExternalContactRemote.getExternalContactFollowUserList(remoteParam);
if (externalContactFollowUserList.isSuccess()) {
WorkWechatExternalContactFollowUserListResponseDTO result = externalContactFollowUserList.getResult();
log.info("获取有外部联系人权限的员工: {}", JSONArray.toJSONString(result));
//获取跟进员工的UserId
List<String> followUser = result.getFollowUser();
//转化为本地数据
if (CollectionUtils.isNotEmpty(followUser)) {
remoteList = followUser.stream().map(index -> {
EntmicroStaff entmicroStaff = new EntmicroStaff();
entmicroStaff.setUserId(index);
entmicroStaff.setMerchantid(merchantId);
return entmicroStaff;
}).collect(Collectors.toList());
}
} else {
log.info("获取企微外部联系人权限员工失败入参{}", JSONArray.toJSONString(remoteParam));
ErrorEnum error = externalContactFollowUserList.getError();
throw new RpcServiceException(error.getValue(), error.getDisplayName());
}
return remoteList;
}
/**
* userid 查询员工
*
* @return
*/
public Map<String, EntmicroStaffResponse> getIdsByUserIds(List<String> userids, Integer merchantId) {
Map<String, EntmicroStaffResponse> entmicroStaffResponse = entmicroStaffMapper.findStaffMapByUserIds(userids, merchantId);
return entmicroStaffResponse;
}
/**
* userid 查询员工
*
* @return
*/
public List<EntmicroStaffResponse> getListByUserIds(List<String> userids, Integer merchantId) {
List<EntmicroStaffResponse> entmicroStaffResponse = entmicroStaffMapper.findStaffListByUserIds(userids, merchantId);
return entmicroStaffResponse;
}
/*
*//**
* staffid 查询员工
*
* @return
*//*
public Map<Integer, EntmicroStaffFollowResponse> getByFollowIds(List<Integer> satffIds) {
Map<Integer, EntmicroStaffFollowResponse> entmicroStaffResponse = entmicroStaffMapper.findStaffByFollowIds(satffIds);
return entmicroStaffResponse;
}*/
/**
* 员工id查询商户号
*
* @return
*/
public List<String> findMerchantIdsByStaffIds(List<Integer> staffIds) {
List<String> res = entmicroStaffMapper.findMerchantIdsByStaffIds(staffIds);
return res;
}
/**
* 企微端登录
*
* @param phoneAuthorizeRequest
*/
public void entmicroLogin(PhoneAuthorizeRequest phoneAuthorizeRequest) {
log.info("企微同步关联{}", JSONArray.toJSONString(phoneAuthorizeRequest));
EntmicroStaff entmicroStaff = new EntmicroStaff();
entmicroStaff.setUserId(phoneAuthorizeRequest.getUserId());
entmicroStaff.setStaffPhone(phoneAuthorizeRequest.getPhone());
entmicroStaff.setMerchantid(phoneAuthorizeRequest.getMerchantId());
List<EntmicroStaffResponse> entmicroStaffResponses = entmicroStaffMapper.listByStaffPhone(entmicroStaff);
if (CollectionUtils.isNotEmpty(entmicroStaffResponses)) {
EntmicroStaffResponse entmicroStaffResponse = entmicroStaffResponses.get(0);
Integer merchantId = entmicroStaffResponse.getMerchantId();
// 1.如果部门未关联 则和登录的商户关联上
log.info("当前门店id{}", merchantId);
/* if (!phoneAuthorizeRequest.getMerchantId().equals(merchantId)) {
entmicroDepartmentService.releEntmicro(phoneAuthorizeRequest.getMerchantId(), entmicroStaffResponse.getDepartmentId(), entmicroStaffResponse.getDepartmentName(), merchantId);
}*/
}
}
/**
* 构建同步到账户的员工
*
* @param list 部门list
* @param entmicroStaff 授权的商户号
*/
public List<EntmicroStaff> buildSyncStaffDepart(List<EntmicroStaffDepartRelation> list, EntmicroStaff
entmicroStaff) {
log.info("同步员工商户号{}", entmicroStaff.getMerchantid());
List<EntmicroStaff> entmicroStaffList = new ArrayList<>();
MerchantRopResponse merchantByMerchantNo = merchantService.findMerchantByMerchantNo(entmicroStaff.getMerchantid());
Integer merchantModel = merchantByMerchantNo.getMerchantModel();
// 门店版同步账号到账户系统 员工有效
if (Constants.NORMAL_FLAG == entmicroStaff.getDelFlag()) {
list.forEach(index -> {
EntmicroStaff entStaff = new EntmicroStaff();
BeanUtils.copyProperties(entmicroStaff, entStaff);
// 集团版未关联企微的员工不同步
log.info("同步员工{}", index);
Integer relevanceMerchant = index.getRelevanceMerchant();
if (Constants.STORE_MODEL != merchantModel.intValue()) {
if (!entmicroStaff.getMerchantid().equals(relevanceMerchant)) {
entStaff.setMerchantid(relevanceMerchant);
log.info("新增员工{}", entStaff);
entmicroStaffList.add(entStaff);
}
} else {
// 门店版所有同步
entStaff.setMerchantid(relevanceMerchant);
entmicroStaffList.add(entStaff);
}
});
}
return entmicroStaffList;
}
/**
* 查询跟进人 - 通过员工部门关系ID查询
*
* @param staffIdList 入参
* @return
*/
public List<EntmicroStaffResponse> findStaffByStaffId(List<Integer> staffIdList) {
return entmicroStaffMapper.getStaffById(staffIdList);
}
/**
* 查询跟进人 - 通过staffId查询
*
* @param staffIdList 入参
* @return
*/
public List<EntmicroStaffResponse> findStaffByStaffIds(List<Integer> staffIdList) {
if (staffIdList == null || staffIdList.isEmpty()) {
return null;
}
return entmicroStaffMapper.getStaffByIds(staffIdList);
}
/**
* 查询跟进人
*
* @param staffRequestDTO 入参
* @return
*/
public List<EntmicroStaffResponse> findFollowStaff(StaffRequestDTO staffRequestDTO) {
List<EntmicroStaffResponse> responseList = entmicroStaffMapper.findFollowStaff(staffRequestDTO);
//emoji转化
responseList.forEach(index -> {
index.setStaffName(StringUtils.isNotEmpty(index.getStaffName()) ? EmojiUtils.parseToUnicode(index.getStaffName()) : null);
index.setDepartmentName(StringUtils.isNotEmpty(index.getDepartmentName()) ? EmojiUtils.parseToUnicode(index.getDepartmentName()) : null);
index.setEntmicroName(StringUtils.isNotEmpty(index.getEntmicroName()) ? EmojiUtils.parseToUnicode(index.getEntmicroName()) : null);
});
return responseList;
}
/**
* uuid查询员工
*
* @param staffRequestDTO
* @return
*/
public EntmicroStaffResponse findStaffDetail(StaffRequestDTO staffRequestDTO) {
EntmicroStaffResponse staffDetail = entmicroStaffMapper.findStaffDetail(staffRequestDTO.getUserUuid());
if (staffDetail != null) {
//emoji转化
staffDetail.setEntmicroName(StringUtils.isNotEmpty(staffDetail.getEntmicroName()) ? EmojiUtils.parseToUnicode(staffDetail.getEntmicroName()) : null);
staffDetail.setStaffName(StringUtils.isNotEmpty(staffDetail.getStaffName()) ? EmojiUtils.parseToUnicode(staffDetail.getStaffName()) : null);
staffDetail.setDepartmentName(StringUtils.isNotEmpty(staffDetail.getDepartmentName()) ? EmojiUtils.parseToUnicode(staffDetail.getDepartmentName()) : null);
}
return staffDetail;
}
/**
* 根据uuid查询员工
* @param list
* @return
*/
public List<EntmicroStaffResponse> findStaffDetailList(List<StaffRequestDTO> list){
List<EntmicroStaffResponse> entStaffList = entmicroStaffMapper.findMerchantstaffDetail(list);
log.info("根据uuid查询员工{}", entStaffList);
return entStaffList;
}
/**
* 更新员工uuid
*
* @param dictDataResponseList
*/
@Transactional(rollbackFor = RpcServiceException.class)
public void updateUUID(List<MerchantStaffModel> dictDataResponseList) {
entmicroStaffMapper.updateUUID(dictDataResponseList);
}
/**
* 跟进员工id查询
*
* @param integerList
* @return
*/
public Map<Integer, EntmicroStaffResponse> findStaffDetailByRelationId(List<Integer> integerList) {
return entmicroStaffMapper.findStaffDetailByRelationId(integerList);
}
/**
* 根据staffIds批量查询
*
* @param staffIds 员工id
* @return
*/
public Map<Integer, EntmicroStaffResponse> findStaffDetailByIds(List<Integer> staffIds, String staffName) {
return entmicroStaffMapper.findStaffDetailById(staffIds, staffName);
}
/**
* 跟进staffId查询
*
* @param id 员工id
* @return
*/
public EntmicroStaffResponse findStaffDetailById(Integer id) {
List<Integer> integerList = new ArrayList<>();
integerList.add(id);
Map<Integer, EntmicroStaffResponse> staffDetailByIds = findStaffDetailByIds(integerList, null);
return staffDetailByIds.get(id);
}
/**
* 查询部门下面的员工
*
* @param departmentIds 部门ids
* @param isfollower
* @return
*/
public List<EntmicroStaffResponse> findStaffByDepartmentIds(List<Integer> departmentIds, Boolean isfollower) {
return entmicroStaffMapper.findStaffByDepartmentIds(departmentIds, isfollower);
}
/**
* 查询商户下面的员工
*
* @param entmicroStaffRequest 入参
* @return
*/
public List<EntmicroStaffResponse> findlistEntmicroStaff(EntmicroStaffRequest entmicroStaffRequest) {
return entmicroStaffMapper.findlistEntmicroStaff(entmicroStaffRequest);
}
public List<String> findHavingCustomer(Integer merchantId, Boolean flag) {
return entmicroStaffMapper.findHavingCustomer(merchantId, flag);
}
/**
* 查询群主
*
* @param lordCustomerGroupRequest 部门ids
* @return
*/
public List<EntmicroStaffResponse> findLordGroup(LordCustomerGroupRequest lordCustomerGroupRequest) {
return entmicroStaffMapper.findLordGroup(lordCustomerGroupRequest);
}
public List<String> findAllUserIdsByDepartId(int merchantId, Integer departmentId) {
List<String> userIds = new ArrayList<>();
GetWorkWechatUserSimpleListRequestDTO getWorkWechatUserSimpleListRequestDTO = new GetWorkWechatUserSimpleListRequestDTO();
getWorkWechatUserSimpleListRequestDTO.setMerchantId(merchantId);
getWorkWechatUserSimpleListRequestDTO.setDepartmentId(departmentId);
RpcResponse<WorkWechatUserSimpleListResponseDTO> userSimpleList = workWechatUserRemote.getUserSimpleList(getWorkWechatUserSimpleListRequestDTO);
if (userSimpleList.isSuccess()) {
WorkWechatUserSimpleListResponseDTO result = userSimpleList.getResult();
List<WorkWechatUserSimpleResponseDTO> userList = result.getUserList();
if (CollectionUtils.isNotEmpty(userList)) {
userIds = userList.stream().map(WorkWechatUserSimpleResponseDTO::getUserId).collect(Collectors.toList());
}
}
return userIds;
}
/**
* 获取当前商户的所有员工
*
* @return
*/
public List<EntmicroStaffResponse> findStaffByMerchantId(EntmicroStaffRequest entmicroStaffRequest) {
List<EntmicroStaffResponse> staffByMerchantId = entmicroStaffMapper.findStaffByMerchantId(entmicroStaffRequest);
Map<Integer, Map> followMap = new HashMap<>();
// 封装跟进类型名字
if (CollectionUtils.isNotEmpty(staffByMerchantId)) {
List<Integer> followTypeId = staffByMerchantId.stream().map(EntmicroStaffResponse::getFollowerTypeId).filter(Objects::nonNull).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(followTypeId)) {
followMap = entmicroStaffMapper.findFollowNameByIds(followTypeId);
}
}
// staffByMerchantId.forEach(index -> {
// index.setStaffName(StringUtils.isNotEmpty(index.getStaffName()) ? EmojiUtils.parseToUnicode(index.getStaffName()) : index.getStaffName());
// Integer followerTypeId = index.getFollowerTypeId();
// if(followerTypeId != null && finalFollowMap.containsKey(followerTypeId)){
// index.setFollowerTypeName(finalFollowMap.get(followerTypeId).get(0).getFollowName());
// }
// });
for (EntmicroStaffResponse staffResponse : staffByMerchantId) {
staffResponse.setStaffName(StringUtils.isNotEmpty(staffResponse.getStaffName()) ? EmojiUtils.parseToUnicode(staffResponse.getStaffName()) : staffResponse.getStaffName());
Integer followerTypeId = staffResponse.getFollowerTypeId();
if (followerTypeId != null && followMap.containsKey(followerTypeId)) {
String followTypeName = followMap.get(followerTypeId).get("follow_name").toString();
staffResponse.setFollowerTypeName(followTypeName);
}
}
return staffByMerchantId;
}
/**
* 获取当前客户的跟进员工
*
* @param mergeId
* @return
*/
public List<EntmicroStaffResponse> findStaffIdByCustomerIds(List<Integer> mergeId) {
return entmicroStaffMapper.findStaffIdByCustomerIds(mergeId);
}
/**
* 根据userId查询员工信息
*
* @param userIds 员工userIds
* @param merchantId 商户号
* @return
*/
public Map<String, EntmicroStaffResponse> findStaffDetailByUserIds(List<String> userIds, Integer merchantId) {
return entmicroStaffMapper.findStaffDetailByUserIds(userIds, merchantId);
}
/**
* 客户userId获取跟进人
*
* @param userId
* @return
*/
public List<EntmicroStaffResponse> getFollowerByCusUserId(String userId) {
return entmicroStaffMapper.getFollowerByCusUserId(userId);
}
/**
* 根据业务类型查询跟进员工列表
*
* @param merchantId 商户号
* @param businessType 业务类型 (1,2,3...)
* @return
*/
public List<EntmicroStaffResponse> getEntmicroByBusinessType(String businessType, Integer merchantId) {
if (merchantId == null) {
return new ArrayList<>();
}
String[] typeArray = {};
if (!StringUtils.isEmpty(businessType)) {
typeArray = businessType.split(",");
}
List<EntmicroStaffResponse> staffList = entmicroStaffMapper.getEntmicroByBusinessType(merchantId, typeArray);
return staffList;
}
public List<StaffFollowResponse> filterFollowerListByCondition(List<String> oneIdsResp, Integer merchantId) {
if (oneIdsResp == null || oneIdsResp.isEmpty()) {
return null;
}
int m = 2000;
if (oneIdsResp.size() <= m) {
List<StaffFollowResponse> list = entmicroStaffMapper.filterFollowerListByCondition(oneIdsResp, merchantId);
return list;
}
List<StaffFollowResponse> list = new ArrayList<>();
int num = oneIdsResp.size();
int n = oneIdsResp.size() / m;
for (int i = 0; i < n; i++) {
List<String> subList = oneIdsResp.subList(m * i, (i + 1) * m);
List<StaffFollowResponse> responses = entmicroStaffMapper.filterFollowerListByCondition(subList, merchantId);
list.addAll(responses);
}
if (n * m < num) {
List<String> subList = oneIdsResp.subList(n * m, oneIdsResp.size());
List<StaffFollowResponse> responses = entmicroStaffMapper.filterFollowerListByCondition(subList, merchantId);
list.addAll(responses);
}
return list;
}
/**
* @return
*/
public EntmicroStaff getStaffByPrimaryKey(Integer staffId) {
Example example = new Example(EntmicroStaff.class);
example.createCriteria().andEqualTo("id", staffId);
EntmicroStaff entmicroStaff = entmicroStaffMapper.selectOneByExample(example);
return entmicroStaff;
}
/**
* 更新员工信息
*
* @param updateStaffSensitiveInfoRequest
*/
public void updateStaffSensitiveInfo(UpdateStaffSensitiveInfoRequest updateStaffSensitiveInfoRequest) {
try {
EntmicroStaff updatestaff = getEntmicroSimpleStaff(updateStaffSensitiveInfoRequest.getMerchantId(), updateStaffSensitiveInfoRequest.getUserid());
if (updatestaff != null) {
updatestaff.setStaffPhone(updateStaffSensitiveInfoRequest.getMobile());
updatestaff.setQrCode(updateStaffSensitiveInfoRequest.getQrCode());
updatestaff.setThumbAvatar(updateStaffSensitiveInfoRequest.getAvatar());
updatestaff.setStaffSex(updateStaffSensitiveInfoRequest.getGender());
updatestaff.setStaffEmail(updateStaffSensitiveInfoRequest.getEmail());
updateEntmicroStaff(updatestaff, false);
}
} catch (Exception e) {
log.info("更新员工信息异常{}", e.getMessage());
}
}
/**
* 查询门店下面开启状态的员工
*
* @param merchantStartStaffRequest 入参
* @return
*/
public List<EntmicroStaffResponse> findMerchantStartStaff(MerchantStartStaffRequest merchantStartStaffRequest) {
List<EntmicroStaffResponse> entmicroStaffResponseList = entmicroStaffMapper.findMerchantStartStaff(merchantStartStaffRequest);
Integer openJoinStatus = merchantStartStaffRequest.getOpenJoinStatus();
// 组装员工的开启状态
if (CollectionUtils.isNotEmpty(entmicroStaffResponseList)) {
List<StaffRopResponse> joinEntMicroStaffByMerchantNo = merchantService.findJoinEntMicroStaffByMerchantNo(merchantStartStaffRequest.getMerchantId());
if (CollectionUtils.isNotEmpty(joinEntMicroStaffByMerchantNo)) {
List<String> collect = joinEntMicroStaffByMerchantNo.stream().map(StaffRopResponse::getStaffPhone).collect(Collectors.toList());
entmicroStaffResponseList = entmicroStaffResponseList.stream().peek(v -> {
if (collect.contains(v.getStaffPhone())) {
v.setOpenJoinStatus(1);
}
}).collect(Collectors.toList());
}
// 没有手机号的员工:未授权
entmicroStaffResponseList.forEach(v -> {
if (StringUtils.isEmpty(v.getStaffPhone())) {
v.setOpenJoinStatus(2);
}
if (v.getOpenJoinStatus() == null) {
v.setOpenJoinStatus(0);
}
});
if (openJoinStatus == null) {
return entmicroStaffResponseList;
}
entmicroStaffResponseList = entmicroStaffResponseList.stream().filter(p -> p.getOpenJoinStatus().equals(openJoinStatus)).collect(Collectors.toList());
}
log.info("查询门店下的员工状态:openJoinStatus={},查询结果={}", openJoinStatus, entmicroStaffResponseList);
return entmicroStaffResponseList;
}
/**
* 员工id获取员工信息
*
* @param staffId 用“ , ”拼接的多个员工id字符串
* @return
*/
public List<EntmicroStaffResponse> getInfoBySplitId(String staffId) {
if (StringUtils.isBlank(staffId)) {
return null;
}
String[] split = staffId.split(",");
List<String> list = Arrays.asList(split);
List<Integer> staffIds = list.stream().map(index -> Integer.parseInt(index)).collect(Collectors.toList());
List<EntmicroStaffResponse> staffByIds = entmicroStaffMapper.getStaffByIds(staffIds);
return staffByIds;
}
/**
* 企微托管管理
*
* @return 企微托管列表
*/
public RpcPagination<EntmicroStaffResponse> wechatDepositList(RpcPagination<WechatDepositRequest> wechatDepositRequest) {
PageHelper.startPage(wechatDepositRequest.getPageNum(), wechatDepositRequest.getPageSize());
Page<EntmicroStaffResponse> staffResponsePage = entmicroStaffMapper.wechatDepositList(wechatDepositRequest.getCondition());
List<Integer> collect = staffResponsePage.stream().map(EntmicroStaffResponse::getId).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(collect)) {
List<EntmicroStaffResponse> merchantListByUserIds = entmicroStaffMapper.findMerchantListByUserIds(collect);
List<MerchantRopResponse> merchantListByCondition = null;
if (CollectionUtils.isNotEmpty(merchantListByUserIds)) {
StringBuffer stringBuffer = new StringBuffer();
merchantListByUserIds.forEach(index -> {
if (index.getMerchantIds().length() > 1) {
stringBuffer.append(index.getMerchantIds()).append(",");
}
});
ListMerchantRequest listMerchantRequest = new ListMerchantRequest();
listMerchantRequest.setMerchantNoStrSet(stringBuffer.toString());
merchantListByCondition = merchantService.findMerchantListByCondition(listMerchantRequest);
}
for (EntmicroStaffResponse entmicroStaffResponse : staffResponsePage) {
Optional<EntmicroStaffResponse> first = merchantListByUserIds.stream().filter(ind -> ind.getId().equals(entmicroStaffResponse.getId())).findFirst();
if (first.isPresent()) {
StringBuffer stringBuffer = new StringBuffer();
EntmicroStaffResponse staffResponse = first.get();
String merchantId = staffResponse.getMerchantIds();
String[] split = merchantId.split(",");
for (int i = 0; i < split.length; i++) {
String s = split[i];
Optional<MerchantRopResponse> merchantRopResponse = merchantListByCondition.stream().filter(index -> index.getMerchantNo().toString().equals(s)).findFirst();
if (merchantRopResponse.isPresent()) {
stringBuffer.append(merchantRopResponse.get().getMerchantName());
if (i < split.length - 1) {
stringBuffer.append("|");
}
}
}
entmicroStaffResponse.setMerchantName(stringBuffer.toString());
}
}
}
return RpcPagination.copyPage(staffResponsePage);
}
/**
* 企微托管管理
*
* @return 企微托管列表
*/
public RpcPagination<EntmicroStaffResponse> wechatDepositNotAccessList(RpcPagination<WechatDepositRequest> wechatDepositRequest) {
PageHelper.startPage(wechatDepositRequest.getPageNum(), wechatDepositRequest.getPageSize());
Page<EntmicroStaffResponse> staffResponsePage = entmicroStaffMapper.wechatDepositNotAccessList(wechatDepositRequest.getCondition());
return RpcPagination.copyPage(staffResponsePage);
}
/**
* 匹配员工记录
*
* @param loginCallBackModel 登录记录
* @param entmicroAuthorize 企业信息
* @return
*/
public Integer matchStaff(LoginCallBackModel loginCallBackModel, EntmicroAuthorize entmicroAuthorize) {
if (entmicroAuthorize.getMerchantId() != null) {
// 接入员工匹配
loginCallBackModel.setMerchantNo(entmicroAuthorize.getMerchantId());
}
if (entmicroAuthorize.getAccessScrmFlag() != null) {
// 未接入的员工匹配
loginCallBackModel.setEntmicroId(entmicroAuthorize.getId());
}
String user_id = loginCallBackModel.getUserId();
// 登录过滤
if (entmicroAuthorize.getMerchantId() != null && loginCallBackModel.getLoginStatus() != null && loginCallBackModel.getLoginStatus().intValue() == Constants.CloudControlLogin.CLOUD_CONTROL_LOGIN) {
if (loginCallBackModel.getMobile() != null) {
loginCallBackModel.setUserId(null);
}
}
EntmicroStaff entmicroStaff = findStaffByPhoneAndMerchantNo(loginCallBackModel);
if (entmicroStaff == null) {
// 如果是未接入的企微,新增员工信息
if (entmicroAuthorize.getAccessScrmFlag() != null) {
entmicroStaff = new EntmicroStaff(loginCallBackModel.getRealName(), loginCallBackModel.getEmail(), loginCallBackModel.getMobile(), loginCallBackModel.getJob(), loginCallBackModel.getUserId(), loginCallBackModel.getEntmicroId());
CloudControlBaseResponse<StaffCardResponse> staffCard = wechatCloudControlManager.getStaffCard(new StaffInfoRequest(loginCallBackModel.getUserId()));
if (staffCard.getSuccess()) {
entmicroStaff.setQrCode(staffCard.getData().getQrcodeUrl());
}
entmicroStaff.setCreatedTime(new Date());
entmicroStaff.setLoginStatus(loginCallBackModel.getLoginStatus());
entmicroStaff.setLastLoginTime(loginCallBackModel.getLocalTime());
entmicroStaff.setDelFlag((byte) Constants.NORMAL_FLAG);
entmicroStaffMapper.insertUseGeneratedKeys(entmicroStaff);
return entmicroStaff.getId();
} else {
log.info("已接入的SCRM企微未匹配到{}", loginCallBackModel);
return null;
}
}
Date lastLoginTime = entmicroStaff.getLastLoginTime();
Integer loginStatus = loginCallBackModel.getLoginStatus();
if (loginStatus == null) {
// 如果心跳时间大于最后登录时间,默认登录,小于的话不处理
if (lastLoginTime == null || loginCallBackModel.getLocalTime().after(lastLoginTime)) {
loginCallBackModel.setLoginStatus(Constants.CloudControlLogin.CLOUD_CONTROL_LOGIN);
} else {
return null;
}
}
entmicroStaff.setLoginStatus(loginCallBackModel.getLoginStatus());
entmicroStaff.setLastLoginTime(loginCallBackModel.getLocalTime());
entmicroStaff.setIdBefEncryption(user_id);
entmicroStaffMapper.updateByPrimaryKeySelective(entmicroStaff);
return entmicroStaff.getId();
}
/**
* 根据员工标识查询员工
*
* @param loginCallBackModel 回调入参
* @return
*/
public EntmicroStaff findStaffByPhoneAndMerchantNo(LoginCallBackModel loginCallBackModel) {
return entmicroStaffMapper.findStaffByPhoneAndMerchantNo(loginCallBackModel);
}
/**
* 保存云控登录的员工
*
* @param loginCallBackModel 登录信息
* @param departmentId 部门id
*/
public void saveCloudControStaff(LoginCallBackModel loginCallBackModel, Integer departmentId) {
EntmicroStaff entmicroStaff = new EntmicroStaff();
entmicroStaff.setIdBefEncryption(loginCallBackModel.getUserId());
entmicroStaff.setUserId(loginCallBackModel.getUserId());
entmicroStaff.setCreatedBy(loginCallBackModel.getMerchantNo());
entmicroStaff.setStaffPhone(loginCallBackModel.getMobile());
entmicroStaff.setStaffName(loginCallBackModel.getName());
entmicroStaff.setThumbAvatar(loginCallBackModel.getAvatorUrl());
entmicroStaff.setStaffTitle(loginCallBackModel.getJob());
entmicroStaff.setStaffEmail(loginCallBackModel.getEmail());
entmicroStaff.setStaffSex(loginCallBackModel.getSex() + "");
entmicroStaff.setDelFlag(Constants.NORMAL_FLAG);
entmicroStaff.setCreatedTime(new Date());
EntmicroStaffResponse entmicrosta = findByMerchantIdAndUserId(entmicroStaff);
if (entmicrosta == null) {
entmicroStaffMapper.insertUseGeneratedKeys(entmicroStaff);
EntmicroStaffDepartRelation entmicroStaffDepartRelation = new EntmicroStaffDepartRelation();
entmicroStaffDepartRelation.setDepartmentId(departmentId);
entmicroStaffDepartRelation.setStaffId(entmicroStaff.getId());
entmicroStaffDepartRelation.setRelevanceMerchant(loginCallBackModel.getMerchantNo());
List<EntmicroStaffDepartRelation> entmicroStaffDepartRelationList = entmicroStaff.getEntmicroStaffDepartRelationList();
// 1.新增员工的部门关联表
entmicroStaffDepartRelationService.insertList(buildStaffDepartRelation(entmicroStaffDepartRelationList, entmicroStaff.getId()));
// 2.同步企微数据
syncEntmicroStaff(buildSyncStaffDepart(entmicroStaffDepartRelationList, entmicroStaff), "add");
}
}
/**
* 企微自动离线
*/
public void automaticOffline() {
entmicroStaffMapper.automaticOffline();
}
/**
* 根据条件查询跟进专员
*
* @param entmicroFollowerStaffRequest
* @return
*/
public List<EntmicroFollowerStaffResponse> listEntmicroFollowerStaff(EntmicroFollowerStaffRequest entmicroFollowerStaffRequest) {
return entmicroStaffMapper.listEntmicroFollower(entmicroFollowerStaffRequest);
}
/**
* 根据员工标识查询员工
*
* @param list 回调入参
* @return
*/
public List<EntmicroStaff> findStaffByEncryptionIds(List<String> list) {
return entmicroStaffMapper.findStaffByEncryptionIds(list);
}
/**
* 查询园区店员工并且注册了企微
*
* @param autMerchantNo 授权商户号
* @param merchantNo 园区店商户号
* @return
*/
public List<EntmicroStaffResponse> findResidentEntmicroStaff(Integer autMerchantNo, Integer merchantNo) {
List<EntmicroStaffResponse> entmicroStaffResponses = null;
List<StaffDetailResponse> staffDetailResponses = merchantService.staffDetailByMerchantId(merchantNo);
if (CollectionUtils.isNotEmpty(staffDetailResponses)) {
List<String> phoneList = staffDetailResponses.stream().filter(index -> StringUtils.isNotEmpty(index.getStaffPhone())).map(StaffDetailResponse::getStaffPhone).collect(Collectors.toList());
List<EntmicroStaff> staffByPhoneListAndMerchantNo = entmicroStaffMapper.findStaffByPhoneListAndMerchantNo(phoneList, autMerchantNo);
if (CollectionUtils.isNotEmpty(staffByPhoneListAndMerchantNo)) {
entmicroStaffResponses = staffByPhoneListAndMerchantNo.stream().map(index -> {
EntmicroStaffResponse entmicroStaffResponse = new EntmicroStaffResponse();
BeanUtils.copyProperties(index, entmicroStaffResponse);
return entmicroStaffResponse;
}).collect(Collectors.toList());
}
}
return entmicroStaffResponses;
}
/**
* 查询商户下面的员工
*
* @param entmicroStaffRequest 入参
* @return
*/
public List<EntmicroStaffResponse> findStaffListByPhone(EntmicroStaffRequest entmicroStaffRequest) {
return entmicroStaffMapper.findStaffListByPhone(entmicroStaffRequest);
}
public List<EntmicroStaffResponse> findStaffListByTopMerchantId(EntmicroStaffRequest request){
Integer supMerchantNo = request.getMerchantId();
log.info("获取客户集团员工跟进关系-授权商户 supMerchantNo:{}",supMerchantNo);
List<Integer> merchantTreeId = merchantService.findMerchantTreeId(supMerchantNo);
log.info("获取客户集团员工跟进关系-商户树 merchantTreeId:{}",merchantTreeId);
request.setMerchantIdList(merchantTreeId);
return entmicroStaffMapper.findStaffListByMerchantIdList(request);
}
}