LoginService.java
51.6 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
package cn.com.glsx.dj.service;
import cn.com.glsx.dj.constant.RopConstants;
import cn.com.glsx.dj.enums.RopErrorEnum;
import cn.com.glsx.dj.peanalysis.dto.DataBizQueryParamsDTO;
import cn.com.glsx.dj.peanalysis.dto.DataBusinessDTO;
import cn.com.glsx.dj.peanalysis.dto.job.BiRecord;
import cn.com.glsx.dj.peanalysis.dto.req.output.SceneBaseReq;
import cn.com.glsx.dj.peanalysis.enums.LoginRoleEnum;
import cn.com.glsx.dj.peanalysis.model.ProvinceRganization;
import cn.com.glsx.dj.peanalysis.remote.BusinessAnalysisRemote;
import cn.com.glsx.dj.peanalysis.remote.BusinessSceneAnalysisRemote;
import cn.com.glsx.dj.peanalysis.remote.GetListDataRespRemote;
import cn.com.glsx.dj.request.LoginByPhoneRequest;
import cn.com.glsx.dj.request.MerchantIdPermissionRequest;
import cn.com.glsx.dj.response.LoginUserInfoResponse;
import cn.com.glsx.dj.smartcarlife.dto.GoodsOrderDTO;
import cn.com.glsx.dj.smartcarlife.dto.ServiceMerchantDTO;
import cn.com.glsx.dj.smartcarlife.dto.ShopMerchantConfigDTO;
import cn.com.glsx.dj.smartcarlife.dto.ShopMerchantDirectorConfigDTO;
import cn.com.glsx.dj.smartcarlife.enums.CarLifeEnum;
import cn.com.glsx.dj.smartcarlife.enums.CarLifeEnum.BusinessType;
import cn.com.glsx.dj.smartcarlife.enums.CarLifeEnum.OrderStatus;
import cn.com.glsx.dj.smartcarlife.enums.DirectorTypeEnum;
import cn.com.glsx.dj.smartcarlife.enums.SceneTypeEnum;
import cn.com.glsx.dj.smartcarlife.model.PeLoginLog;
import cn.com.glsx.dj.smartcarlife.model.ServiceMerchant;
import cn.com.glsx.dj.smartcarlife.model.ShopMerchantConfig;
import cn.com.glsx.dj.smartcarlife.remote.*;
import cn.com.glsx.dj.util.CommonUtil;
import cn.com.glsx.dj.util.DateUtils;
import cn.com.glsx.framework.core.beans.rpc.RpcPagination;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.framework.rop.response.RopResponse;
import cn.com.glsx.merchants.dto.merchant.CarBrandProvinceRequestDTO;
import cn.com.glsx.merchants.model.request.MerchantStaffRequest;
import cn.com.glsx.merchants.model.request.merchant.BrandRequest;
import cn.com.glsx.merchants.model.request.merchant.MerchantBasicInfoRequest;
import cn.com.glsx.merchants.model.response.MerchantProvinceDTO;
import cn.com.glsx.merchants.model.response.MerchantStaffResponse;
import cn.com.glsx.merchants.model.response.merchant.MerchantBasicInfoListResponse;
import cn.com.glsx.merchants.remote.MerchantRemote;
import cn.com.glsx.merchants.remote.MerchantStaffRemote;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rop.session.SimpleSession;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;
@Slf4j
@Service
public class LoginService {
//无权限时商户号ID
public static final int NO_PERMISSION_MERCHANT_ID = -1;
public static final List<Integer> NO_PERMISSION_LIST = new ArrayList<>(Arrays.asList(NO_PERMISSION_MERCHANT_ID));//没有权限时的列表数据
public static final List<String> SELLER_NO_PERMISSION_LIST = new ArrayList<>(Arrays.asList("-1"));//没有权限时的列表数据
@Autowired
private MerchantStaffRemote merchantStaffRemote;
@Autowired
private Environment env;
@Autowired
private ServiceMerchantConfigurationRemote serviceMerchantConfigurationRemote;
@Autowired
private GoodsOrderRemote goodsOrderRemote;
@Autowired
private MerchantRemote merchantRemote;
@Autowired
private PeLoginLogRemote peLoginLogRemote;
@Autowired
private ServiceMerchantRemote serviceMerchantRemote;
@Autowired
private ShopMerchantDirectorConfigRemote shopMerchantDirectorConfigRemote;
@Autowired
private BusinessAnalysisRemote businessAnalysisRemote;
@Autowired
private BusinessSceneAnalysisRemote businessSceneAnalysisRemote;
@Autowired
private GetListDataRespRemote getListDataRespRemote;
public RopResponse<LoginUserInfoResponse> loginByPhone(LoginByPhoneRequest request) {
LoginUserInfoResponse loginUserInfo = loginByPhone(request.getPhone());
if(loginUserInfo == null) {
return RopResponse.result(RopErrorEnum.LOGIN_NO_PERMISSION_ERROR);
}
String sessionId = UUID.randomUUID().toString();
loginUserInfo.setSessionId(sessionId);
SimpleSession session = new SimpleSession();
session.setAttribute(RopConstants.LOGIN_SESSION, loginUserInfo);
request.getRopRequestContext().addSession(sessionId, session);
if(request.getPageType() != null) {
PeLoginLog record = PeLoginLog.builder()
.createdBy(loginUserInfo.getName())
.loginIp(request.getRopRequestContext().getIp())
.loginTime(new Date()).pageType(request.getPageType())
.phone(loginUserInfo.getPhone()).loginRole(loginUserInfo.getLoginRole().byteValue())
.userName(loginUserInfo.getName()).userUuid(loginUserInfo.getUuid())
.build();
peLoginLogRemote.insert(record);
}
return RopResponse.success(loginUserInfo);
}
public LoginUserInfoResponse loginByPhone(String phone) {
MerchantStaffRequest staffRequest = new MerchantStaffRequest();
staffRequest.setMerchantNo(env.getProperty("glsk_merchant_id", Integer.class, 44205577));
staffRequest.setStaffPhoneList(Arrays.asList(phone));
List<MerchantStaffResponse> staffList = merchantStaffRemote.findMerchantStaffListRemote(staffRequest).getResult();
log.info("login Staff:{}",staffList);
if(CollectionUtils.isEmpty(staffList)) {
return null;
}
MerchantStaffResponse staff = staffList.stream()
.filter(p -> Objects.nonNull(p.getDelFlag()) && p.getDelFlag() == 0)
.findFirst().orElse(null);
if (staff == null){
log.error("login staff not exist, staffList:{}", staffList);
return null;
}
LoginUserInfoResponse loginUserInfo = LoginUserInfoResponse.builder()
.uuid(staff.getUuid()).phone(staff.getStaffPhone()).name(staff.getStaffName()).roleName(staff.getRoleName())
.build();
setDataPermission(loginUserInfo);
return loginUserInfo;
}
public List<Integer> filterAreaSupervisionPermission(List<Integer> merchantIdList,MerchantIdPermissionRequest request) {
if(CommonUtil.isNoPermission(merchantIdList)) {
return NO_PERMISSION_LIST;
}
if(StringUtils.isNoneBlank(request.getAreaSupervisionUserId())) {
ShopMerchantDirectorConfigDTO condition = ShopMerchantDirectorConfigDTO.builder()
.directorUserId(request.getAreaSupervisionUserId())
.merchantIdList(merchantIdList)
.build();
if(Objects.equals(request.getSceneType(),SceneTypeEnum.SCENE1_1.getValue())){
condition.setDirectorType(DirectorTypeEnum.AREA_SUPERVISION_1_1.getValue());
}else if(Objects.equals(request.getSceneType(),SceneTypeEnum.SCENE1_0.getValue())){
condition.setDirectorType(DirectorTypeEnum.AREA_SUPERVISION_2_1.getValue());
}else {
return NO_PERMISSION_LIST;
}
List<ShopMerchantDirectorConfigDTO> list = shopMerchantDirectorConfigRemote.list(condition).getResult();
if(CollectionUtils.isEmpty(list)) {
return NO_PERMISSION_LIST;
}
List<Integer> permissionList = list.stream().map(ShopMerchantDirectorConfigDTO::getMerchantId)
.filter(item -> !Objects.isNull(item)).distinct().collect(Collectors.toList());
if(CollectionUtils.isEmpty(permissionList)) {
return NO_PERMISSION_LIST;
}
return permissionList;
}
return merchantIdList;
}
public List<Integer> filterStoreTypeAndCategoryPermission(List<Integer> merchantIdList, MerchantIdPermissionRequest request) {
if(CommonUtil.isNoPermissionAndNoEmpty(merchantIdList)) {
return NO_PERMISSION_LIST;
}
if(Objects.isNull(request.getStoreType()) && Objects.isNull(request.getBusinessCategory())) {
return merchantIdList;
}
List<BiRecord> list = businessSceneAnalysisRemote.getBiRecords(null, merchantIdList, request.getCycleTime(), request.getCycleTimeEnd()).getResult();
if(CollectionUtils.isEmpty(list)) {
return NO_PERMISSION_LIST;
}
List<Integer> permissionList = new ArrayList<>();
Map<Integer, List<BiRecord>> groupByMerchant = list.stream().collect(Collectors.groupingBy(p -> p.getMerchantId()));
// 当门店的所有记录都是老店,才为老店
// 当门店的所有记录都是单品类,才为单品类
groupByMerchant.forEach((merchantId, records) -> {
// 检查经营品类条件
boolean categoryCondition = true; // 默认为true,表示不进行经营品类过滤
if (request.getBusinessCategory() != null) {
int categoryCount = 0;
categoryCount = records.stream().anyMatch(p -> p.getHasBenefits() != null && p.getHasBenefits() == 1) ? categoryCount + 1 : categoryCount;
categoryCount = records.stream().anyMatch(p -> p.getHasLightMod() != null && p.getHasLightMod() == 1) ? categoryCount + 1 : categoryCount;
categoryCount = records.stream().anyMatch(p -> p.getHasExtWarranty() != null && p.getHasExtWarranty() == 1) ? categoryCount + 1 : categoryCount;
categoryCount = records.stream().anyMatch(p -> p.getHasUsedCar() != null && p.getHasUsedCar() == 1) ? categoryCount + 1 : categoryCount;
boolean isSingle = categoryCount == 1;
boolean isMulti = categoryCount > 1;
if (request.getBusinessCategory() == 1) {
categoryCondition = isSingle; // 单品类
} else if (request.getBusinessCategory() == 2) {
categoryCondition = isMulti; // 多品类
} else {
categoryCondition = false; // 不满足条件
}
}
// 检查门店类型条件
boolean storeTypeCondition = true; // 默认为true,表示不进行门店类型过滤
if (request.getStoreType() != null) {
// 是否属于老店
boolean isOldStore = records.stream().allMatch(p -> {
Integer storeType = p.getStoreTypeByBizs(request.getBizs());
return storeType != null && storeType == 2;
});
if (request.getStoreType() == 1) {
storeTypeCondition = !isOldStore; // 新店
} else if (request.getStoreType() == 2) {
storeTypeCondition = isOldStore; // 老店
} else {
storeTypeCondition = false; // 不满足条件
}
}
// 同时满足经营品类和门店类型条件时,才添加到权限列表
if (categoryCondition && storeTypeCondition) {
permissionList.add(merchantId);
}
});
if(CollectionUtils.isEmpty(permissionList)) {
return NO_PERMISSION_LIST;
}
return permissionList;
}
public List<Integer> filterDirectorPermission(List<Integer> merchantIdList,MerchantIdPermissionRequest request){
if(CommonUtil.isNoPermission(merchantIdList)) {
return NO_PERMISSION_LIST;
}
if(StringUtils.isNoneBlank(request.getServiceDirectorUserId()) || StringUtils.isNoneBlank(request.getSelllerUserId())) {
ShopMerchantConfigDTO condition = ShopMerchantConfigDTO.builder().build();
condition.setMerchantIdList(merchantIdList);
if(Objects.equals(request.getSceneType(), SceneTypeEnum.SCENE1_0.getValue())) {
condition.setQuotationDirectorUserId(request.getSelllerUserId());
condition.setQuotationServiceDirectorUserId(request.getServiceDirectorUserId());
}else if(Objects.equals(request.getSceneType(), SceneTypeEnum.SCENE1_1.getValue())) {
condition.setBoutiqueDirectorUserId(request.getSelllerUserId());
condition.setBoutiqueServiceDirectorUserId(request.getServiceDirectorUserId());
}
List<ShopMerchantConfigDTO> list = serviceMerchantConfigurationRemote.listUser(condition).getResult();
if(CollectionUtils.isEmpty(list)) {
return NO_PERMISSION_LIST;
}
List<Integer> permissionList = list.stream().map(ShopMerchantConfig::getMerchantId)
.filter(item -> !Objects.isNull(item)).distinct().collect(Collectors.toList());
if(CollectionUtils.isEmpty(permissionList)) {
return NO_PERMISSION_LIST;
}
return permissionList;
}
return merchantIdList;
}
public List<Integer> filterBrandAndProvincePermission(List<Integer> merchantIdList,MerchantIdPermissionRequest request) {
if(CommonUtil.isNoPermission(merchantIdList)) {
return NO_PERMISSION_LIST;
}
if(!Objects.isNull(request.getCarBrandId()) || StringUtils.isNoneBlank(request.getProvinceCode()) || StringUtils.isNoneBlank(request.getCityCode())) {
//过滤不是改省份城市的商户
List<MerchantBasicInfoListResponse> merchantList = findMerchant(merchantIdList, request);
if (CollectionUtils.isEmpty(merchantList)) {
return NO_PERMISSION_LIST;
}
List<Integer> idList = merchantList.stream().map(MerchantBasicInfoListResponse::getMerchantId).filter(item -> !Objects.isNull(item)).distinct().collect(Collectors.toList());
if(CollectionUtils.isEmpty(idList)) {
return NO_PERMISSION_LIST;
}
return idList;
}
return merchantIdList;
}
public List<MerchantBasicInfoListResponse> findMerchant(List<Integer> merchantIdList,MerchantIdPermissionRequest request) {
MerchantBasicInfoRequest condition = new MerchantBasicInfoRequest();
condition.setPageNum(1);
condition.setPageSize(merchantIdList.size());
condition.setMerchantIdList(merchantIdList);
if (request != null) {
condition.setProvinceCode(request.getProvinceCode());
condition.setCityCode(request.getCityCode());
if(!Objects.isNull(request.getCarBrandId())) {
//过滤没有此主营品牌的商户
BrandRequest brandRequest = new BrandRequest();
brandRequest.setBrandId(request.getCarBrandId());
brandRequest.setBrandLevel(1);
condition.setBrandRequestList(Arrays.asList(brandRequest));
}
}
RpcPagination<MerchantBasicInfoListResponse> pagination = merchantRemote.findMerchantBasicInfoList(condition).getResult();
if(Objects.isNull(pagination) || CollectionUtils.isEmpty(pagination.getList())) {
return null;
}
return pagination.getList();
}
public List<Integer> filterBusinessTypePermission(List<Integer> merchantIdList,MerchantIdPermissionRequest request) {
if(CommonUtil.isNoPermission(merchantIdList)) {
return NO_PERMISSION_LIST;
}
List<Byte> filterBusinessTypeList = new ArrayList<>();//需要过滤掉的业务类型
if(Objects.equals(request.getSceneType(), SceneTypeEnum.SCENE1_1.getValue())) {
filterBusinessTypeList.add(BusinessType.DIRECT_CHANNEL.getValue());
}
if(Objects.nonNull(request.getBusinessType()) || CollectionUtils.isNotEmpty(request.getBusinessTypeList())
|| Objects.equals(request.getSceneType(), SceneTypeEnum.SCENE1_1.getValue())) {
List<ServiceMerchant> smList = serviceMerchantRemote.listBase(ServiceMerchantDTO.builder().merchantIdList(merchantIdList).build()).getResult();
if(CollectionUtils.isEmpty(smList)) {
return NO_PERMISSION_LIST;
}
List<Integer> idList = smList.stream().filter(item -> {
if(Objects.isNull(item) || Objects.isNull(item.getMerchantId())) {
return false;
}
//针对轻改业务,自动屏蔽业务性质=“直营渠道”、“系统实施”的门店;
if(Objects.equals(request.getSceneType(), SceneTypeEnum.SCENE1_1.getValue())) {
if(Objects.equals(item.getBusinessType(), BusinessType.DIRECT_CHANNEL.getValue())
|| Objects.equals(item.getBusinessType(), BusinessType.SYSTEM_IMPLEMENT_DJ.getValue())
|| Objects.equals(item.getBusinessType(), BusinessType.SYSTEM_IMPLEMENT_JTD.getValue())){
return false;
}
}
if(request.getBusinessType() != null) {
return Objects.equals(request.getBusinessType(), item.getBusinessType());
}else if(CollectionUtils.isNotEmpty(request.getBusinessTypeList())) {
return item.getBusinessType() != null && request.getBusinessTypeList().contains(item.getBusinessType());
}
return true;
}).map(ServiceMerchant::getMerchantId).distinct().collect(Collectors.toList());
if(CollectionUtils.isEmpty(idList)) {
return NO_PERMISSION_LIST;
}
return idList;
}
return merchantIdList;
}
public List<Integer> getCycleOrderPermission(MerchantIdPermissionRequest request){
if(Objects.isNull(request) || Objects.isNull(request.getSceneType()) || Objects.isNull(request.getCycleType()) || Objects.isNull(request.getCycleTime())) {
return NO_PERMISSION_LIST;
}
List<Integer> allMerchantIdList = CommonUtil.getMerchantIds(request.getSceneType());
if(CollectionUtils.isNotEmpty(allMerchantIdList) && allMerchantIdList.size() == 1 &&
Objects.equals(allMerchantIdList.get(0), NO_PERMISSION_MERCHANT_ID)) {
return NO_PERMISSION_LIST;
}
String startDate = null;
String endDate = null;
if(Objects.equals(request.getCycleType(), MerchantIdPermissionRequest.CYCLE_TYPE_DAY)) {
Date cycleDate = DateUtils.parseDate(request.getCycleTime().substring(0,10), DateUtils.DATE_FORMAT);
startDate = DateUtils.formatDate(DateUtils.getDayOfFirst(DateUtils.addDate(cycleDate, "day", -1)),DateUtils.DATE_FORMAT_DATE_TIME);
endDate = DateUtils.formatDate(DateUtils.getDayOfEnd(cycleDate),DateUtils.DATE_FORMAT_DATE_TIME);
}else if(Objects.equals(request.getCycleType(), MerchantIdPermissionRequest.CYCLE_TYPE_WEEK)) {
Date cycleDate = DateUtils.parseDate(DateUtils.getMondayByDate(request.getCycleTime().substring(0,10)), DateUtils.DATE_FORMAT);
startDate = DateUtils.formatDate(DateUtils.getDayOfFirst(DateUtils.addDate(cycleDate, "day", -7)),DateUtils.DATE_FORMAT_DATE_TIME);
endDate = DateUtils.getSundayByDate(request.getCycleTime().substring(0,10)).concat(" 23:59:59");
}else if(Objects.equals(request.getCycleType(), MerchantIdPermissionRequest.CYCLE_TYPE_MONTH)) {
String monthFirstAndLastDayTime = DateUtils.getMonthFirstAndLastDayTime(request.getCycleTime().substring(0,7));
startDate = DateUtils.getFirstDayLastMonth(DateUtils.parseDate(monthFirstAndLastDayTime.split(",")[0], DateUtils.DATE_FORMAT_DATE_TIME)).concat(" 00:00:00");
endDate = monthFirstAndLastDayTime.split(",")[1];
}else if(Objects.equals(request.getCycleType(), MerchantIdPermissionRequest.CYCLE_TYPE_CUSTOMIZE)) {
startDate = request.getCycleTime();
endDate = request.getCycleTimeEnd();
}
if(Objects.isNull(startDate) || Objects.isNull(endDate)) {
return NO_PERMISSION_LIST;
}
GoodsOrderDTO condition = GoodsOrderDTO.builder().merchantIdList(allMerchantIdList).sceneType(request.getSceneType())
.statusList(Arrays.asList(OrderStatus.NOT_UPGRADE.getValue(),OrderStatus.PAYED.getValue(),
OrderStatus.FINISHED.getValue()))
.merchantNameOrStaffName(request.getMerchantNameOrStaffName())
.startDate(startDate).endDate(endDate).build();
List<GoodsOrderDTO> orderMerchantList = goodsOrderRemote.listOrderMerchant(condition).getResult();
if(CollectionUtils.isEmpty(orderMerchantList)) {
return NO_PERMISSION_LIST;
}
List<Integer> merchantIdList = orderMerchantList.stream().map(GoodsOrderDTO::getMerchantId).collect(Collectors.toList());
//屏蔽指定门店
List<Integer> maskMerchantIds = CommonUtil.convert2Integer(StringUtils.split(env.getProperty("permission.mask.merchant.ids"), ",")) ;
if(CollectionUtils.isNotEmpty(maskMerchantIds)) {
merchantIdList.removeAll(maskMerchantIds);
}
if(CollectionUtils.isEmpty(merchantIdList)) {
return NO_PERMISSION_LIST;
}
return merchantIdList;
}
public List<String> getCycleOrderSellerPermission(MerchantIdPermissionRequest request){
if(Objects.isNull(request) || Objects.isNull(request.getSceneType()) || Objects.isNull(request.getCycleType()) || Objects.isNull(request.getCycleTime())) {
return SELLER_NO_PERMISSION_LIST;
}
List<Integer> allMerchantIdList = CommonUtil.getMerchantIds(request.getSceneType());
if(CollectionUtils.isNotEmpty(allMerchantIdList) && allMerchantIdList.size() == 1 &&
Objects.equals(allMerchantIdList.get(0), NO_PERMISSION_MERCHANT_ID)) {
return SELLER_NO_PERMISSION_LIST;
}
String startDate = null;
String endDate = null;
if(Objects.equals(request.getCycleType(), MerchantIdPermissionRequest.CYCLE_TYPE_DAY)) {
Date cycleDate = DateUtils.parseDate(request.getCycleTime().substring(0,10), DateUtils.DATE_FORMAT);
startDate = DateUtils.formatDate(DateUtils.getDayOfFirst(DateUtils.addDate(cycleDate, "day", -1)),DateUtils.DATE_FORMAT_DATE_TIME);
endDate = DateUtils.formatDate(DateUtils.getDayOfEnd(cycleDate),DateUtils.DATE_FORMAT_DATE_TIME);
}else if(Objects.equals(request.getCycleType(), MerchantIdPermissionRequest.CYCLE_TYPE_WEEK)) {
Date cycleDate = DateUtils.parseDate(DateUtils.getMondayByDate(request.getCycleTime().substring(0,10)), DateUtils.DATE_FORMAT);
startDate = DateUtils.formatDate(DateUtils.getDayOfFirst(DateUtils.addDate(cycleDate, "day", -7)),DateUtils.DATE_FORMAT_DATE_TIME);
endDate = DateUtils.getSundayByDate(request.getCycleTime().substring(0,10)).concat(" 23:59:59");
}else if(Objects.equals(request.getCycleType(), MerchantIdPermissionRequest.CYCLE_TYPE_MONTH)) {
String monthFirstAndLastDayTime = DateUtils.getMonthFirstAndLastDayTime(request.getCycleTime().substring(0,7));
startDate = DateUtils.getFirstDayLastMonth(DateUtils.parseDate(monthFirstAndLastDayTime.split(",")[0], DateUtils.DATE_FORMAT_DATE_TIME)).concat(" 00:00:00");
endDate = monthFirstAndLastDayTime.split(",")[1];
}else if(Objects.equals(request.getCycleType(), MerchantIdPermissionRequest.CYCLE_TYPE_CUSTOMIZE)) {
startDate = request.getCycleTime();
endDate = request.getCycleTimeEnd();
}
if(Objects.isNull(startDate) || Objects.isNull(endDate)) {
return SELLER_NO_PERMISSION_LIST;
}
GoodsOrderDTO condition = GoodsOrderDTO.builder().merchantIdList(allMerchantIdList).sceneType(request.getSceneType())
.statusList(Arrays.asList(OrderStatus.NOT_UPGRADE.getValue(),OrderStatus.PAYED.getValue(),
OrderStatus.FINISHED.getValue()))
.startDate(startDate).endDate(endDate).build();
List<GoodsOrderDTO> orderMerchantList = goodsOrderRemote.listOrderSellerUserId(condition).getResult();
if(CollectionUtils.isEmpty(orderMerchantList)) {
return SELLER_NO_PERMISSION_LIST;
}
List<String> sellerUserIdList = orderMerchantList.stream()
.filter(item -> Objects.nonNull(item) && StringUtils.isNotBlank(item.getSellerUserId()))
.map(GoodsOrderDTO::getSellerUserId)
.distinct().collect(Collectors.toList());
if(CollectionUtils.isEmpty(sellerUserIdList)) {
return SELLER_NO_PERMISSION_LIST;
}
return sellerUserIdList;
}
/**
*
* @Description:
* @param request
* @return map key值为商户号,value为员工uuid列表
* @author zhoulm
*/
public Map<Integer,List<String>> getCycleOrderMerchantSellerPermission(MerchantIdPermissionRequest request){
Map<Integer,List<String>> map = new HashMap<>();
map.put(NO_PERMISSION_MERCHANT_ID, Arrays.asList(String.valueOf(NO_PERMISSION_MERCHANT_ID)));
if(Objects.isNull(request) || Objects.isNull(request.getSceneType()) || Objects.isNull(request.getCycleType()) || Objects.isNull(request.getCycleTime())) {
return map;
}
List<Integer> allMerchantIdList = CommonUtil.getMerchantIds(request.getSceneType());
if(CollectionUtils.isNotEmpty(allMerchantIdList) && allMerchantIdList.size() == 1 &&
Objects.equals(allMerchantIdList.get(0), NO_PERMISSION_MERCHANT_ID)) {
return map;
}
String startDate = null;
String endDate = null;
if(Objects.equals(request.getCycleType(), MerchantIdPermissionRequest.CYCLE_TYPE_DAY)) {
Date cycleDate = DateUtils.parseDate(request.getCycleTime().substring(0,10), DateUtils.DATE_FORMAT);
startDate = DateUtils.formatDate(DateUtils.getDayOfFirst(DateUtils.addDate(cycleDate, "day", -1)),DateUtils.DATE_FORMAT_DATE_TIME);
endDate = DateUtils.formatDate(DateUtils.getDayOfEnd(cycleDate),DateUtils.DATE_FORMAT_DATE_TIME);
}else if(Objects.equals(request.getCycleType(), MerchantIdPermissionRequest.CYCLE_TYPE_WEEK)) {
Date cycleDate = DateUtils.parseDate(DateUtils.getMondayByDate(request.getCycleTime().substring(0,10)), DateUtils.DATE_FORMAT);
startDate = DateUtils.formatDate(DateUtils.getDayOfFirst(DateUtils.addDate(cycleDate, "day", -7)),DateUtils.DATE_FORMAT_DATE_TIME);
endDate = DateUtils.getSundayByDate(request.getCycleTime().substring(0,10)).concat(" 23:59:59");
}else if(Objects.equals(request.getCycleType(), MerchantIdPermissionRequest.CYCLE_TYPE_MONTH)) {
String monthFirstAndLastDayTime = DateUtils.getMonthFirstAndLastDayTime(request.getCycleTime().substring(0,7));
startDate = DateUtils.getFirstDayLastMonth(DateUtils.parseDate(monthFirstAndLastDayTime.split(",")[0], DateUtils.DATE_FORMAT_DATE_TIME)).concat(" 00:00:00");
endDate = monthFirstAndLastDayTime.split(",")[1];
}else if(Objects.equals(request.getCycleType(), MerchantIdPermissionRequest.CYCLE_TYPE_CUSTOMIZE)) {
startDate = request.getCycleTime();
endDate = request.getCycleTimeEnd();
}
if(Objects.isNull(startDate) || Objects.isNull(endDate)) {
return map;
}
GoodsOrderDTO condition = GoodsOrderDTO.builder().merchantIdList(allMerchantIdList).sceneType(request.getSceneType())
.statusList(Arrays.asList(OrderStatus.NOT_UPGRADE.getValue(),OrderStatus.PAYED.getValue(),
OrderStatus.FINISHED.getValue()))
.startDate(startDate).endDate(endDate).build();
List<GoodsOrderDTO> orderMerchantList = goodsOrderRemote.listOrderSellerUserId(condition).getResult();
if(CollectionUtils.isEmpty(orderMerchantList)) {
return map;
}
Map<Integer,List<String>> merchantSellerUserMap = orderMerchantList.stream()
.filter(item -> Objects.nonNull(item) && Objects.nonNull(item.getMerchantId()) &&
StringUtils.isNotBlank(item.getSellerUserId()))
.collect(Collectors.groupingBy(
GoodsOrderDTO::getMerchantId, // 分组的键
Collectors.mapping(GoodsOrderDTO::getSellerUserId, // 分组的值
Collectors.toList())));
if(MapUtils.isEmpty(merchantSellerUserMap)) {
return map;
}
return merchantSellerUserMap;
}
public void setDataPermission(LoginUserInfoResponse loginUserInfo) {
loginUserInfo.setScene0PermissionMerchantIds(NO_PERMISSION_LIST);
loginUserInfo.setScene1PermissionMerchantIds(NO_PERMISSION_LIST);
if(StringUtils.isEmpty(loginUserInfo.getRoleName())) {
log.info("用户角色为空,userInfo:{}",loginUserInfo);
loginUserInfo.setLoginRole(LoginRoleEnum.NONE.getValue());
return;
}
if(Objects.equals(loginUserInfo.getRoleName(), env.getProperty("rear.supervision.role.name","区域运营"))) {
boolean isLeader = false;
RpcResponse<List<ProvinceRganization>> rpcResponse = businessAnalysisRemote.getByUuid(loginUserInfo.getUuid());
Set<String> set = new HashSet<>();
if (rpcResponse != null && rpcResponse.getResult() != null && !rpcResponse.getResult().isEmpty()) {
for (ProvinceRganization provinceOrganization : rpcResponse.getResult()) {
if (provinceOrganization != null && provinceOrganization.getProvinceId() != null) {
set.add(provinceOrganization.getProvinceId().toString());
isLeader = true;
}
}
}
// 区域运营大区经理
if (isLeader) {
String regionIds = String.join(",", set);
loginUserInfo.setRegionId(regionIds);
loginUserInfo.setLoginRole(LoginRoleEnum.AREA_SUPERVISION_LEADER.getValue());
setAreaLeaderPermission(loginUserInfo);
} else {
//仅可以查看作为区域督导的门店的数据
setAreaSupervisionPermission(loginUserInfo);
loginUserInfo.setLoginRole(LoginRoleEnum.AREA_SUPERVISION.getValue());
}
}else if(Objects.equals(loginUserInfo.getRoleName(), env.getProperty("service.boss.role.name","服务商老板"))) {
//仅可以查看作为服务商老板的门店的数据
setScene1ServicePermission(loginUserInfo);
setScene0ServicePermission(loginUserInfo);
loginUserInfo.setLoginRole(LoginRoleEnum.SERVICE_BOSS.getValue());
}else if(Objects.equals(loginUserInfo.getRoleName(), env.getProperty("gl.sale.role.name","广联销售"))) {
boolean isLeader = false;
RpcResponse<List<ProvinceRganization>> rpcResponse = businessAnalysisRemote.getSaleRegionByUuid(loginUserInfo.getUuid());
Set<String> set = new HashSet<>();
if (rpcResponse != null && rpcResponse.getResult() != null && !rpcResponse.getResult().isEmpty()) {
for (ProvinceRganization provinceOrganization : rpcResponse.getResult()) {
if (provinceOrganization != null && provinceOrganization.getProvinceId() != null) {
set.add(provinceOrganization.getProvinceId().toString());
isLeader = true;
}
}
}
if (isLeader) {
String regionIds = String.join(",", set);
loginUserInfo.setRegionId(regionIds);
loginUserInfo.setLoginRole(LoginRoleEnum.SALE_LEADER.getValue());
setAreaLeaderPermission(loginUserInfo);
} else {
//仅可以查看作为业务负责人的门店的数据
setScene1SalePermission(loginUserInfo);
setScene0SalePermission(loginUserInfo);
loginUserInfo.setLoginRole(LoginRoleEnum.SALE.getValue());
}
}else if(Objects.equals(loginUserInfo.getRoleName(), env.getProperty("flight.supervision.role.name","飞行督导"))){
//仅可以查看作为飞行督导的门店的数据
setFlightSupervisionPermission(loginUserInfo);
loginUserInfo.setLoginRole(LoginRoleEnum.FLIGHT_SUPERVISION.getValue());
}else if(Objects.equals(loginUserInfo.getRoleName(), env.getProperty("online.supervision.role.name","线上运营"))){
//仅可以查看作为飞行督导的门店的数据
setOnlineSupervisionPermission(loginUserInfo);
loginUserInfo.setLoginRole(LoginRoleEnum.ONLINE_SUPERVISION.getValue());
} else {
//其他角色,查看全部
loginUserInfo.setScene0PermissionMerchantIds(null);
loginUserInfo.setScene1PermissionMerchantIds(null);
loginUserInfo.setLoginRole(LoginRoleEnum.OTHER.getValue());
}
}
/**
* 设置大区经理的权限门店
*/
private void setAreaLeaderPermission(LoginUserInfoResponse loginUserInfo) {
String regionId = loginUserInfo.getRegionId();
if (StringUtils.isBlank(regionId)) {
log.warn("regionId is null");
return;
}
String[] regionIds = regionId.split(",");
List<Integer> provinceIdList = Arrays.stream(regionIds)
.filter(StringUtils::isNotBlank)
.map(idStr -> {
try {
return Integer.parseInt(idStr.trim());
} catch (NumberFormatException e) {
log.warn("区域ID格式错误,忽略: {}", idStr);
return null;
}
})
.filter(Objects::nonNull)
.collect(Collectors.toList());
CarBrandProvinceRequestDTO dto = new CarBrandProvinceRequestDTO();
dto.setProvinceIds(provinceIdList);
log.info("根据品牌和省份查询商户列表。req:{}", JSONObject.toJSONString(dto));
RpcResponse<List<MerchantProvinceDTO>> merchantListByBrandIdsProvince = merchantRemote.findMerchantListByBrandIdsProvince(dto);
if (!merchantListByBrandIdsProvince.isSuccess()) {
throw new RpcServiceException("查询商户列表失败,失败原因{}", merchantListByBrandIdsProvince.getMessage());
}
List<MerchantProvinceDTO> result = merchantListByBrandIdsProvince.getResult();
if (CollectionUtils.isEmpty(result)) {
log.warn("查询商户列表为空");
return;
}
List<Integer> merchantIds = result.stream().map(MerchantProvinceDTO::getMerchantId).distinct().collect(Collectors.toList());
// todo 区分场景
loginUserInfo.setScene0PermissionMerchantIds(merchantIds);
loginUserInfo.setScene1PermissionMerchantIds(merchantIds);
}
//设置区域督导1.1,2.0场景数据权限
private void setAreaSupervisionPermission(LoginUserInfoResponse loginUserInfo) {
List<ShopMerchantDirectorConfigDTO> list = shopMerchantDirectorConfigRemote.list(ShopMerchantDirectorConfigDTO.builder()
.directorTypes(Arrays.asList(DirectorTypeEnum.AREA_SUPERVISION_1_1.getValue(),DirectorTypeEnum.AREA_SUPERVISION_2_1.getValue()))
.directorUserId(loginUserInfo.getUuid())
.build()).getResult();
if(CollectionUtils.isNotEmpty(list)) {
List<Integer> scene0PermissionMerchantIds = new ArrayList<>();
List<Integer> scene1PermissionMerchantIds = new ArrayList<>();
for(ShopMerchantDirectorConfigDTO c:list) {
if(Objects.equals(c.getDirectorType(), DirectorTypeEnum.AREA_SUPERVISION_1_1.getValue())){
scene1PermissionMerchantIds.add(c.getMerchantId());
}else if(Objects.equals(c.getDirectorType(), DirectorTypeEnum.AREA_SUPERVISION_2_1.getValue())){
scene0PermissionMerchantIds.add(c.getMerchantId());
}
}
if(CollectionUtils.isNotEmpty(scene0PermissionMerchantIds)) {
loginUserInfo.setScene0PermissionMerchantIds(scene0PermissionMerchantIds);
}
if(CollectionUtils.isNotEmpty(scene1PermissionMerchantIds)) {
loginUserInfo.setScene1PermissionMerchantIds(scene1PermissionMerchantIds);
}
}
}
//设置飞行督导1.1,2.0场景数据权限
private void setFlightSupervisionPermission(LoginUserInfoResponse loginUserInfo) {
List<ShopMerchantConfigDTO> list = serviceMerchantConfigurationRemote.listUser(ShopMerchantConfigDTO.builder()
.flightSupervisionUserId(loginUserInfo.getUuid())
.build()).getResult();
if(CollectionUtils.isNotEmpty(list)) {
List<Integer> permissionList = list.stream().map(ShopMerchantConfig::getMerchantId)
.filter(item -> !Objects.isNull(item)).distinct().collect(Collectors.toList());
loginUserInfo.setScene0PermissionMerchantIds(permissionList);
loginUserInfo.setScene1PermissionMerchantIds(permissionList);
}
}
//设置服务商老板1.1场景数据权限
private void setScene1ServicePermission(LoginUserInfoResponse loginUserInfo){
List<ShopMerchantConfigDTO> list = serviceMerchantConfigurationRemote.listUser(ShopMerchantConfigDTO.builder()
.boutiqueServiceDirectorUserId(loginUserInfo.getUuid())
.build()).getResult();
if(CollectionUtils.isNotEmpty(list)) {
List<Integer> permissionList = list.stream().map(ShopMerchantConfig::getMerchantId)
.filter(item -> !Objects.isNull(item)).distinct().collect(Collectors.toList());
loginUserInfo.setScene1PermissionMerchantIds(permissionList);
}
}
//设置服务商老板2.0场景数据权限
private void setScene0ServicePermission(LoginUserInfoResponse loginUserInfo){
List<ShopMerchantConfigDTO> list = serviceMerchantConfigurationRemote.listUser(ShopMerchantConfigDTO.builder()
.quotationServiceDirectorUserId(loginUserInfo.getUuid())
.build()).getResult();
if(CollectionUtils.isNotEmpty(list)) {
List<Integer> permissionList = list.stream().map(ShopMerchantConfig::getMerchantId).collect(Collectors.toList());
loginUserInfo.setScene0PermissionMerchantIds(permissionList);
}
}
//设置广联销售1.1场景数据权限
private void setScene1SalePermission(LoginUserInfoResponse loginUserInfo){
List<ShopMerchantConfigDTO> list = serviceMerchantConfigurationRemote.listUser(ShopMerchantConfigDTO.builder()
.boutiqueDirectorUserId(loginUserInfo.getUuid())
.build()).getResult();
if(CollectionUtils.isNotEmpty(list)) {
List<Integer> permissionList = list.stream().map(ShopMerchantConfig::getMerchantId)
.filter(item -> !Objects.isNull(item)).distinct().collect(Collectors.toList());
loginUserInfo.setScene1PermissionMerchantIds(permissionList);
}
}
//设置广联销售2.0场景数据权限
private void setScene0SalePermission(LoginUserInfoResponse loginUserInfo){
List<ShopMerchantConfigDTO> list = serviceMerchantConfigurationRemote.listUser(ShopMerchantConfigDTO.builder()
.quotationDirectorUserId(loginUserInfo.getUuid())
.build()).getResult();
if(CollectionUtils.isNotEmpty(list)) {
List<Integer> permissionList = list.stream().map(ShopMerchantConfig::getMerchantId).collect(Collectors.toList());
loginUserInfo.setScene0PermissionMerchantIds(permissionList);
}
}
private void setOnlineSupervisionPermission(LoginUserInfoResponse loginUserInfo) {
List<ShopMerchantDirectorConfigDTO> list = shopMerchantDirectorConfigRemote.list(ShopMerchantDirectorConfigDTO.builder()
.directorTypes(Arrays.asList(DirectorTypeEnum.DF_DIRECTOR.getValue(),DirectorTypeEnum.DF_DIRECTOR_2_1.getValue()))
.directorUserId(loginUserInfo.getUuid())
.build()).getResult();
if(CollectionUtils.isNotEmpty(list)) {
List<Integer> scene0PermissionMerchantIds = new ArrayList<>();
List<Integer> scene1PermissionMerchantIds = new ArrayList<>();
for(ShopMerchantDirectorConfigDTO c:list) {
if(Objects.equals(c.getDirectorType(), DirectorTypeEnum.DF_DIRECTOR.getValue())){
scene1PermissionMerchantIds.add(c.getMerchantId());
}else if(Objects.equals(c.getDirectorType(), DirectorTypeEnum.DF_DIRECTOR_2_1.getValue())){
scene0PermissionMerchantIds.add(c.getMerchantId());
}
}
if(CollectionUtils.isNotEmpty(scene0PermissionMerchantIds)) {
loginUserInfo.setScene0PermissionMerchantIds(scene0PermissionMerchantIds);
}
if(CollectionUtils.isNotEmpty(scene1PermissionMerchantIds)) {
loginUserInfo.setScene1PermissionMerchantIds(scene1PermissionMerchantIds);
}
}
}
public List<Integer> filterBusinessMerchant(MerchantIdPermissionRequest request, List<Integer> merchantIdList) {
if (CommonUtil.isNoPermissionAndNoEmpty(merchantIdList)) {
return NO_PERMISSION_LIST;
}
if(Objects.nonNull(request.getBusinessType()) || CollectionUtils.isNotEmpty(request.getBusinessTypeList())
|| Objects.equals(request.getSceneType(), SceneTypeEnum.SCENE1_1.getValue())) {
List<ServiceMerchant> smList = serviceMerchantRemote.listBase(ServiceMerchantDTO.builder().merchantIdList(merchantIdList).build()).getResult();
if(CollectionUtils.isEmpty(smList)) {
return NO_PERMISSION_LIST;
}
List<Integer> idList = smList.stream().filter(item -> {
if(Objects.isNull(item) || Objects.isNull(item.getMerchantId())) {
return false;
}
//针对轻改业务,自动屏蔽业务性质=“直营渠道”、“系统实施”的门店;
if(Objects.equals(request.getSceneType(), SceneTypeEnum.SCENE1_1.getValue())) {
if(Objects.equals(item.getBusinessType(), CarLifeEnum.BusinessType.DIRECT_CHANNEL.getValue())
|| Objects.equals(item.getBusinessType(), CarLifeEnum.BusinessType.SYSTEM_IMPLEMENT_DJ.getValue())
|| Objects.equals(item.getBusinessType(), CarLifeEnum.BusinessType.SYSTEM_IMPLEMENT_JTD.getValue())){
return false;
}
}
if(request.getBusinessType() != null) {
return Objects.equals(request.getBusinessType(), item.getBusinessType());
}else if(CollectionUtils.isNotEmpty(request.getBusinessTypeList())) {
return item.getBusinessType() != null && request.getBusinessTypeList().contains(item.getBusinessType());
}
return true;
}).map(ServiceMerchant::getMerchantId).distinct().collect(Collectors.toList());
if(CollectionUtils.isEmpty(idList)) {
return NO_PERMISSION_LIST;
}
return idList;
} else {
return merchantIdList;
}
}
public List<Integer> getMerchantIdsByRegionId(String regionId) {
if (StringUtils.isEmpty(regionId)) {
return new ArrayList<>();
}
String[] regionIds = regionId.split(",");
List<Integer> provinceIdList = Arrays.stream(regionIds)
.filter(StringUtils::isNotBlank)
.map(idStr -> {
try {
return Integer.parseInt(idStr.trim());
} catch (NumberFormatException e) {
log.warn("区域ID格式错误,忽略: {}", idStr);
return null;
}
})
.filter(Objects::nonNull)
.collect(Collectors.toList());
CarBrandProvinceRequestDTO dto = new CarBrandProvinceRequestDTO();
dto.setProvinceIds(provinceIdList);
log.info("根据品牌和省份查询商户列表: {}", JSONObject.toJSONString(dto));
RpcResponse<List<MerchantProvinceDTO>> merchantListByBrandIdsProvince = merchantRemote.findMerchantListByBrandIdsProvince(
dto);
if (merchantListByBrandIdsProvince.isSuccess()) {
List<MerchantProvinceDTO> result = merchantListByBrandIdsProvince.getResult();
if (org.springframework.util.CollectionUtils.isEmpty(result)) {
return new ArrayList<>();
}
return result.stream().map(MerchantProvinceDTO::getMerchantId).distinct()
.collect(Collectors.toList());
} else {
throw new RpcServiceException("查询商户列表失败,失败原因{}",
merchantListByBrandIdsProvince.getMessage());
}
}
public List<Integer> filterBrand(List<Integer> allMerchantId, Integer brandId, String strProvinceId, String search) {
if (CommonUtil.isNoPermissionAndNoEmpty(allMerchantId)) {
return NO_PERMISSION_LIST;
}
Integer provinceId = StringUtils.isNotBlank(strProvinceId) ? Integer.valueOf(strProvinceId) : null;
// 是否通过品牌,区域
boolean filterBoolean = ObjectUtil.isNotEmpty(brandId) || ObjectUtil.isNotEmpty(provinceId) || StringUtils.isNotBlank(search);
List<MerchantProvinceDTO> filterMerchantResponse;
if (filterBoolean) {
log.info("过滤品牌[{}], 区域[{}]", brandId, provinceId);
// 筛选品牌,区域后的商户id
CarBrandProvinceRequestDTO carBrandProvinceRequestDTO = new CarBrandProvinceRequestDTO();
carBrandProvinceRequestDTO.setFirstBrandIdList(ObjectUtil.isNotEmpty(brandId) ? Collections.singletonList(brandId) : null);
carBrandProvinceRequestDTO.setProvinceIds(ObjectUtil.isNotEmpty(provinceId) ? Collections.singletonList(provinceId) : null);
carBrandProvinceRequestDTO.setMerchantNoList(allMerchantId);
carBrandProvinceRequestDTO.setMerchantName(search);
RpcResponse<List<MerchantProvinceDTO>> filterMerchantIdsResponse = merchantRemote.findMerchantListByBrandIdsProvince(carBrandProvinceRequestDTO);
filterMerchantResponse = filterMerchantIdsResponse.getResult();
// 如果为空则返回空结果
if (CollUtil.isEmpty(filterMerchantResponse)) {
return NO_PERMISSION_LIST;
}
List<Integer> merchantIdList = filterMerchantResponse.stream().map(MerchantProvinceDTO::getMerchantId).filter(Objects::nonNull).collect(Collectors.toList());
if (CollUtil.isEmpty(merchantIdList)) {
return NO_PERMISSION_LIST;
}
if (CollectionUtils.isEmpty(allMerchantId)) {
return merchantIdList;
}
List<Integer> intersectionMerchant = new ArrayList<>(org.apache.commons.collections4.CollectionUtils.intersection(merchantIdList, allMerchantId));
if (CollectionUtils.isEmpty(intersectionMerchant)) {
return NO_PERMISSION_LIST;
}
return intersectionMerchant;
} else {
return allMerchantId;
}
}
public List<Integer> filterOutputMerchant(MerchantIdPermissionRequest request, List<Integer> merchantIdList) {
if (!request.isQueryWithDataStat()) {
return merchantIdList;
}
String startDate = request.getCycleTime();
String endDate = request.getCycleTimeEnd();
merchantIdList = mergeRequestMerchant(request.getMerchantIds(), merchantIdList);
log.info("merge request merchant. {}", merchantIdList);
if(CollectionUtils.isNotEmpty(merchantIdList) && merchantIdList.size() == 1 &&
Objects.equals(merchantIdList.get(0), NO_PERMISSION_MERCHANT_ID)) {
return NO_PERMISSION_LIST;
}
DataBizQueryParamsDTO queryParams = new DataBizQueryParamsDTO();
queryParams.setBizs(request.getBizs());
queryParams.setBrandIds(env.getProperty("data.stats.default.brand.id", String.class, "-1,-2,3454,-4"));
queryParams.setMerchantIds(StringUtils.join(merchantIdList, ","));
queryParams.setItemCodes("A036"); //产值
queryParams.setStartDate(startDate);
queryParams.setEndDate(endDate);
queryParams.setDateType(convertDateType(request.getCycleType()));
queryParams.setExts("merchantIds");
log.debug("获取指标数据: {}", JSON.toJSONString(queryParams));
RpcResponse<List<DataBusinessDTO>> dataResp = getListDataRespRemote.getListDataResp(queryParams);
if(dataResp.isFailed()) {
log.error("获取数据统计失败, dataResp:{}", dataResp);
throw new RuntimeException(dataResp.getMessage());
}
List<DataBusinessDTO> dataList = dataResp.getResult();
if (CollectionUtils.isEmpty(dataList)) {
return NO_PERMISSION_LIST;
}
// 模糊搜索
if (StringUtils.isNotBlank(request.getMerchantNameOrStaffName())) {
dataList = dataList.stream()
.filter(item -> StringUtils.isNotBlank(item.getMerchantName()) && item.getMerchantName().contains(request.getMerchantNameOrStaffName()))
.collect(Collectors.toList());
}
merchantIdList = dataList.stream().map(DataBusinessDTO::getMerchantId).distinct().collect(Collectors.toList());
log.info("当前用户权限门店(过滤产值):[{}]", merchantIdList);
return merchantIdList;
}
private Integer convertDateType(Integer cycleType) {
switch (cycleType) {
case 1:
return 0; // 按指定时段
case 4:
return 4; // 全部
case 2:
return 1; // 按周
case 3:
return 2; // 按月
case 5:
return 3; // 按年
default:
throw new RpcServiceException("不支持的周期类型");
}
}
private List<Integer> mergeRequestMerchant(List<Integer> reqMerchantIds, List<Integer> allMerchantIdList) {
// 无需过滤传参
if (CollectionUtils.isEmpty(reqMerchantIds)) {
return allMerchantIdList;
}
// 全部权限,就返回指定门店
if (CollectionUtils.isEmpty(allMerchantIdList)) {
return reqMerchantIds;
}
// 无权限
if(allMerchantIdList.size() == 1 && Objects.equals(allMerchantIdList.get(0), LoginService.NO_PERMISSION_MERCHANT_ID)) {
return LoginService.NO_PERMISSION_LIST;
}
// 取交集
allMerchantIdList = new ArrayList<>(CollectionUtils.intersection(reqMerchantIds, allMerchantIdList));
if (CollectionUtils.isEmpty(allMerchantIdList)) {
// 这时权限为空的话,就属于无权限了
return LoginService.NO_PERMISSION_LIST;
}
return allMerchantIdList;
}
public List<Integer> filterDdyjMerchant(List<Integer> merchantIdList, SceneBaseReq request) {
if(CommonUtil.isNoPermissionAndNoEmpty(merchantIdList)) {
return NO_PERMISSION_LIST;
}
if(Objects.isNull(request.getDdyjMerchantFlag()) ) {
return merchantIdList;
}
if (!isWarningCycle(request)) {
log.info("仅在周月时才生效");
return merchantIdList;
}
// 1. 调用远程接口获取兜底门店列表
RpcResponse<List<Integer>> backUpStoresResp = businessSceneAnalysisRemote.findBackUpStores(request);
// 2. 异常处理:如果远程调用失败
if (backUpStoresResp.isFailed()) {
log.error("获取兜底门店失败, errorMsg: {}, response: {}", backUpStoresResp.getMessage(), backUpStoresResp);
throw new RpcServiceException("获取兜底门店列表失败: " + backUpStoresResp.getMessage());
}
// 3. 获取结果列表
List<Integer> backupStoreIdList = backUpStoresResp.getResult();
// 4. 边界处理:如果兜底列表为空
// 逻辑判断:
// - 如果兜底列表为空,且用户选的是"是"(1):结果为空列表。
// - 如果兜底列表为空,且用户选的是"否"(0):结果为原列表(因为没有要排除的)。
// 为了代码简洁,这里先统一判空返回 NO_PERMISSION_LIST (如果您业务上 flag=0 且列表为空时应返回全量,请移除此块或调整逻辑)
if (CollectionUtils.isEmpty(backupStoreIdList)) {
// 如果业务含义是:没有兜底门店时,无论选什么都是无权限/空列表,则保持此处逻辑
// 如果业务含义是:选"否"时,没兜底门店就返回全部,请将此 if 块移到 flag==1 的判断内部
if (request.getDdyjMerchantFlag() != null && request.getDdyjMerchantFlag() == 1) {
return NO_PERMISSION_LIST;
}
// 如果 flag=0 且列表为空,说明没有要排除的,直接返回原列表 (视具体业务需求而定,此处暂按返回原列表处理,防止误杀)
// 如果您的原逻辑是只要查不到兜底列表就直接返回 NO_PERMISSION_LIST,请保留原样:
// return NO_PERMISSION_LIST;
// 下面按更通用的逻辑:列表为空时,如果是"否",返回全部;如果是"是",返回空。
return (request.getDdyjMerchantFlag() != null && request.getDdyjMerchantFlag() == 1)
? NO_PERMISSION_LIST
: merchantIdList;
}
// 5. 性能优化:转换为 HashSet,将查找复杂度从 O(N) 降为 O(1)
Set<Integer> backupStoreSet = new HashSet<>(backupStoreIdList);
// 6. 核心过滤逻辑
Byte flag = request.getDdyjMerchantFlag();
if (flag != null && flag == 1) {
log.info("执行兜底预警过滤模式,保留兜底门店。原始数量: {}, 兜底库数量: {}", merchantIdList.size(), backupStoreSet.size());
if(CollectionUtils.isNotEmpty(merchantIdList)){
List<Integer> collected = merchantIdList.stream()
.filter(backupStoreSet::contains)
.collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(collected)){
return collected;
}else{
return NO_PERMISSION_LIST;
}
}else{
return backupStoreIdList;
}
} else {
if (CollectionUtils.isNotEmpty(backupStoreIdList)) {
List<Integer> originExcludeIds = request.getExcludeIds();
// 如果为 null,初始化为一个新列表;否则使用原列表
List<Integer> newExcludeIds = originExcludeIds == null ? new ArrayList<>() : new ArrayList<>(originExcludeIds);
// 直接追加,无需再判断 backupStoreIdList 是否为空(addAll 内部会处理空集合,但通常建议确保 backupStoreIdList 不为 null)
newExcludeIds.addAll(backupStoreIdList);
request.setExcludeIds(newExcludeIds);
}
return merchantIdList;
}
}
public List<Integer> filterStyjMerchant(List<Integer> merchantIdList, SceneBaseReq req) {
if(CommonUtil.isNoPermissionAndNoEmpty(merchantIdList)) {
return NO_PERMISSION_LIST;
}
if(Objects.isNull(req.getStyjMerchantFlag())) {
return merchantIdList;
}
if (!isWarningCycle(req)) {
log.info("仅在周月时才生效");
return merchantIdList;
}
// 1. 调用远程接口获取生态预警门店列表
// 注意:请确认 req 中是否有对应的 flag 字段,这里假设为 getIsDelayWarningFlag()
RpcResponse<List<Integer>> delayWarningResp = businessSceneAnalysisRemote.getDelayWarningStore(req);
// 2. 异常处理
if (delayWarningResp.isFailed()) {
log.error("获取生态预警门店失败, response: {}", delayWarningResp);
throw new RuntimeException("获取生态预警门店列表失败: " + delayWarningResp.getMessage());
}
// 3. 获取结果
List<Integer> warningStoreList = delayWarningResp.getResult();
// 4. 获取筛选标志位 (请根据实际 DTO 字段名修改,例如: getIsDelayWarning(), getFlag() 等)
// 假设 1 代表"是",0 或 null 代表"否"
Byte flag = req.getStyjMerchantFlag();
// 5. 处理空列表情况
if (CollectionUtils.isEmpty(warningStoreList)) {
// 如果预警列表为空:
// - 选"是":结果为空 (因为没有命中的) -> 返回 NO_PERMISSION_LIST
// - 选"否":结果为全部 (因为没有要排除的) -> 返回 merchantIdList
if (flag != null && flag == 1) {
return NO_PERMISSION_LIST;
} else {
// 选"否"且无预警名单,直接返回原列表,无需过滤
return merchantIdList;
}
}
// 6. 转换为 HashSet 提升性能 O(1)
Set<Integer> warningStoreSet = new HashSet<>(warningStoreList);
// 7. 核心过滤逻辑
if (flag != null && flag == 1) {
// 场景:1.是 (只看生态预警门店)
// 逻辑:保留【在】预警列表中的门店
log.info("执行生态预警过滤[是],原始数量: {}, 预警库数量: {}, 结果数量: {}",
merchantIdList.size(), warningStoreSet.size(),
merchantIdList.stream().filter(warningStoreSet::contains).count());
if(CollectionUtils.isNotEmpty(merchantIdList)){
List<Integer> collected = merchantIdList.stream()
.filter(warningStoreSet::contains)
.collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(collected)){
return collected;
}else{
return NO_PERMISSION_LIST;
}
}else{
return warningStoreList;
}
} else {
if (CollectionUtils.isNotEmpty(warningStoreList)) {
List<Integer> originExcludeIds = req.getExcludeIds();
// 如果为 null,初始化为一个新列表;否则使用原列表
List<Integer> newExcludeIds = originExcludeIds == null ? new ArrayList<>() : new ArrayList<>(originExcludeIds);
// 直接追加,无需再判断 backupStoreIdList 是否为空(addAll 内部会处理空集合,但通常建议确保 backupStoreIdList 不为 null)
newExcludeIds.addAll(warningStoreList);
req.setExcludeIds(newExcludeIds);
}
return merchantIdList;
}
}
/**
* 判断当前周期是否支持预警相关过滤。
*
* @param req 查询参数
* @return true-周/月,false-其他周期
*/
private boolean isWarningCycle(SceneBaseReq req) {
return req != null
&& (Objects.equals(req.getCycleType(), MerchantIdPermissionRequest.CYCLE_TYPE_WEEK)
|| Objects.equals(req.getCycleType(), MerchantIdPermissionRequest.CYCLE_TYPE_MONTH));
}
}