puzhibing
2026-09-29 010b1028b7121ae37d76441af1df7a2d9f698fc5
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
package com.ruoyi.account.service.impl;
 
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.account.api.dto.AppUserWhitelistAllocateDTO;
import com.ruoyi.account.api.dto.AppUserWhitelistAllocateItemDTO;
import com.ruoyi.account.api.dto.AppUserWhitelistImportDTO;
import com.ruoyi.account.api.dto.AppUserWhitelistQueryDTO;
import com.ruoyi.account.api.dto.AppUserWhitelistSaveDTO;
import com.ruoyi.account.api.model.TAppUser;
import com.ruoyi.account.api.model.TAppUserWhitelist;
import com.ruoyi.account.api.model.TAppUserWhitelistSite;
import com.ruoyi.account.api.model.TCompanyAllocateRecord;
import com.ruoyi.account.api.vo.AppUserWhitelistAllocateVO;
import com.ruoyi.account.api.vo.AppUserWhitelistMatchVO;
import com.ruoyi.account.api.vo.AppUserWhitelistImportErrorVO;
import com.ruoyi.account.api.vo.AppUserWhitelistImportResultVO;
import com.ruoyi.account.mapper.TAppUserWhitelistMapper;
import com.ruoyi.account.service.TAppUserService;
import com.ruoyi.account.service.TAppUserWhitelistService;
import com.ruoyi.account.service.TAppUserWhitelistSiteService;
import com.ruoyi.account.service.TCompanyAllocateRecordService;
import com.ruoyi.chargingPile.api.domain.SiteMenu;
import com.ruoyi.chargingPile.api.feignClient.PartnerClient;
import com.ruoyi.chargingPile.api.feignClient.SiteClient;
import com.ruoyi.chargingPile.api.model.Partner;
import com.ruoyi.chargingPile.api.model.Site;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.domain.BasePojo;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.other.api.domain.TFleet;
import com.ruoyi.other.api.domain.TGroupCompany;
import com.ruoyi.other.api.feignClient.GroupCompanyClient;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.feignClient.SysUserClient;
import com.ruoyi.system.api.model.LoginUser;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
 
/**
 * 白名单用户服务实现
 */
@Service
public class TAppUserWhitelistServiceImpl extends ServiceImpl<TAppUserWhitelistMapper, TAppUserWhitelist> implements TAppUserWhitelistService {
 
    private static final Logger log = LoggerFactory.getLogger(TAppUserWhitelistServiceImpl.class);
 
    private static final Pattern PHONE_PATTERN = Pattern.compile("^1\\d{10}$");
    private static final String IMPORT_HEADER_PHONE = "用户手机号";
    private static final int BENEFIT_ALL_FREE = 1;
    private static final int BENEFIT_ELECTRIC_FREE = 2;
    private static final int BENEFIT_SERVICE_FREE = 3;
    private static final int SCOPE_ALL_SITE = 1;
    private static final int SCOPE_SELECTED_SITE = 2;
    private static final int SCOPE_PARTNER = 3;
    private static final int STATUS_ENABLED = 1;
    private static final int STATUS_DISABLED = 2;
    /** 姓名最大长度(对应 varchar(64)) */
    private static final int MAX_NAME_LENGTH = 64;
 
    /** 公司账户模式:2=分配模式(可设置初始分配额度并扣减公司可用余额) */
    private static final int ACCOUNT_MODE_ALLOCATION = 2;
    /** 初始分配额度上限(对应 decimal(12,2)) */
    private static final BigDecimal MAX_INIT_QUOTA = new BigDecimal("99999999.99");
    /** 初始分配额度并发锁前缀:新增按手机号、编辑按ID串行,防止并发下重复扣减或退还公司余额 */
    private static final String QUOTA_LOCK_PREFIX = "APP_USER_WHITELIST_QUOTA:";
    /** 额度分配:单次最多分配的白名单数量,防止单次请求过大 */
    private static final int MAX_ALLOCATE_WHITELIST_COUNT = 200;
    /** 白名单额度分配并发锁前缀:按公司串行,防止同公司并发分配重复占用可分配余额 */
    private static final String ALLOCATE_LOCK_PREFIX = "APP_USER_WHITELIST_QUOTA:COMPANY:";
    /** 获取锁最长等待时间(秒) */
    private static final long LOCK_WAIT_SECONDS = 5L;
    /** 锁自动释放时间(秒),兜住异常未解锁的场景 */
    private static final long LOCK_LEASE_SECONDS = 30L;
 
    @Resource
    private TAppUserWhitelistSiteService whitelistSiteService;
    @Resource
    private TAppUserService appUserService;
    @Resource
    private SiteClient siteClient;
    @Resource
    private PartnerClient partnerClient;
    @Resource
    private TokenService tokenService;
    @Resource
    private SysUserClient sysUserClient;
    @Resource
    private PlatformTransactionManager transactionManager;
    @Autowired
    private GroupCompanyClient groupCompanyClient;
    @Autowired
    private RedissonClient redissonClient;
 
    @Resource
    private TCompanyAllocateRecordService companyAllocateRecordService;
 
    @Override
    public Page<TAppUserWhitelist> pageList(AppUserWhitelistQueryDTO queryDTO) {
        if (queryDTO == null) {
            queryDTO = new AppUserWhitelistQueryDTO();
        }
        Integer currentPartnerId = getCurrentPartnerId();
        String username = getUsername();
        LambdaQueryChainWrapper<TAppUserWhitelist> query = this.lambdaQuery()
                .like(StringUtils.isNotBlank(queryDTO.getPhone()), TAppUserWhitelist::getPhone, queryDTO.getPhone())
                .like(StringUtils.isNotBlank(queryDTO.getName()), TAppUserWhitelist::getName, queryDTO.getName())
                .eq(queryDTO.getCompanyId() != null, TAppUserWhitelist::getCompanyId, queryDTO.getCompanyId())
                .eq(queryDTO.getFleetId() != null, TAppUserWhitelist::getFleetId, queryDTO.getFleetId())
                .eq(queryDTO.getStatus() != null, TAppUserWhitelist::getStatus, queryDTO.getStatus())
                .eq(TAppUserWhitelist::getDelFlag, false);
        if (currentPartnerId != null) {
            if (StringUtils.isNotBlank(username)) {
                query.and(wrapper -> wrapper.eq(TAppUserWhitelist::getPartnerId, currentPartnerId)
                        .or()
                        .eq(TAppUserWhitelist::getCreateBy, username));
            } else {
                query.eq(TAppUserWhitelist::getPartnerId, currentPartnerId);
            }
        }
        Page<TAppUserWhitelist> page = query.orderByDesc(BasePojo::getCreateTime)
                .page(Page.of(queryDTO.getPageCurr(), queryDTO.getPageSize()));
        fillDisplay(page.getRecords());
        return page;
    }
 
    @Override
    public TAppUserWhitelist detail(Long id) {
        TAppUserWhitelist whitelist = this.getById(id);
        if (whitelist == null) {
            throw new ServiceException("白名单用户不存在");
        }
        assertPartnerVisible(whitelist);
        fillDisplay(Collections.singletonList(whitelist));
        return whitelist;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveWhitelist(AppUserWhitelistSaveDTO saveDTO) {
        validateSaveDTO(saveDTO);
        if (saveDTO.getId() == null) {
            createWhitelist(saveDTO);
        } else {
            updateWhitelist(saveDTO);
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(String ids) {
        if (StringUtils.isBlank(ids)) {
            return;
        }
        for (String id : ids.split(",")) {
            if (StringUtils.isBlank(id)) {
                continue;
            }
            Long whitelistId = Long.valueOf(id);
            TAppUserWhitelist whitelist = this.getById(whitelistId);
            if (whitelist == null) {
                continue;
            }
            assertPartnerVisible(whitelist);
            this.removeById(whitelistId);
            whitelistSiteService.remove(Wrappers.lambdaQuery(TAppUserWhitelistSite.class)
                    .eq(TAppUserWhitelistSite::getWhitelistId, whitelistId));
        }
    }
 
    /**
     * 批量解绑车队:仅清空车队归属(fleet_id 置空),公司、余额、状态等不受影响;
     * 未绑定车队的选中记录忽略不计
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int unbindFleet(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new ServiceException("请选择需要解绑车队的白名单用户");
        }
        List<Long> distinct = ids.stream().filter(Objects::nonNull).distinct().collect(Collectors.toList());
        if (distinct.isEmpty()) {
            throw new ServiceException("请选择需要解绑车队的白名单用户");
        }
        List<TAppUserWhitelist> records = listByIds(distinct);
        if (records.size() != distinct.size()) {
            throw new ServiceException("部分白名单用户不存在或已删除");
        }
        for (TAppUserWhitelist record : records) {
            assertPartnerVisible(record);
        }
        List<Long> boundIds = records.stream()
                .filter(record -> record.getFleetId() != null)
                .map(TAppUserWhitelist::getId)
                .collect(Collectors.toList());
        if (boundIds.isEmpty()) {
            return 0;
        }
        // 显式 set null 清空车队归属(MyBatis-Plus 字段策略会忽略实体 null,需走 UpdateWrapper)
        boolean updated = lambdaUpdate()
                .in(TAppUserWhitelist::getId, boundIds)
                .set(TAppUserWhitelist::getFleetId, null)
                .set(TAppUserWhitelist::getUpdateBy, getUsername())
                .setSql("update_time = now()")
                .update();
        if (!updated) {
            throw new ServiceException("白名单用户车队解绑失败,请重试");
        }
        return boundIds.size();
    }
 
    @Override
    public AppUserWhitelistImportResultVO importWhitelist(MultipartFile file) throws Exception {
        if (file == null || file.isEmpty()) {
            throw new ServiceException("导入文件不能为空");
        }
        int titleNum = getImportTitleNum(file);
        ExcelUtil<AppUserWhitelistImportDTO> util = new ExcelUtil<>(AppUserWhitelistImportDTO.class);
        List<AppUserWhitelistImportDTO> rows = util.importExcel(file.getInputStream(), titleNum);
        if (rows == null || rows.isEmpty()) {
            throw new ServiceException("导入数据不能为空");
        }
 
        Map<String, AppUserWhitelistSaveDTO> validRows = new LinkedHashMap<>();
        Map<String, Integer> validRowNumbers = new HashMap<>();
        List<AppUserWhitelistImportErrorVO> errors = new ArrayList<>();
        Set<String> importPhones = new HashSet<>();
        for (int i = 0; i < rows.size(); i++) {
            AppUserWhitelistImportDTO row = rows.get(i);
            int rowNum = titleNum + i + 2;
            List<String> rowErrors = validateImportRow(row);
            if (!rowErrors.isEmpty()) {
                errors.add(new AppUserWhitelistImportErrorVO(rowNum, normalize(row.getPhone()), String.join(";", rowErrors)));
                continue;
            }
            AppUserWhitelistSaveDTO dto = buildSaveDTO(row);
            if (!importPhones.add(dto.getPhone())) {
                errors.add(new AppUserWhitelistImportErrorVO(rowNum, dto.getPhone(), "导入文件中手机号重复"));
                continue;
            }
            if (getByPhone(dto.getPhone()) != null) {
                errors.add(new AppUserWhitelistImportErrorVO(rowNum, dto.getPhone(), "该手机号已存在白名单配置"));
                continue;
            }
            validRows.put(dto.getPhone(), dto);
            validRowNumbers.put(dto.getPhone(), rowNum);
        }
 
        TransactionTemplate rowTransaction = new TransactionTemplate(transactionManager);
        rowTransaction.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
        int successCount = 0;
        for (Map.Entry<String, AppUserWhitelistSaveDTO> entry : validRows.entrySet()) {
            try {
                rowTransaction.execute(status -> {
                    createWhitelist(entry.getValue());
                    return null;
                });
                successCount++;
            } catch (Exception exception) {
                errors.add(new AppUserWhitelistImportErrorVO(validRowNumbers.get(entry.getKey()), entry.getKey(),
                        getImportSaveErrorMessage(exception)));
            }
        }
 
        AppUserWhitelistImportResultVO result = new AppUserWhitelistImportResultVO();
        result.setTotalCount(rows.size());
        result.setErrorCount(errors.size());
        result.setSuccessCount(successCount);
        result.setErrorMessage(errors);
        return result;
    }
 
    private String getImportSaveErrorMessage(Exception exception) {
        Throwable current = exception;
        while (current != null) {
            if (current instanceof ServiceException && StringUtils.isNotBlank(current.getMessage())) {
                return current.getMessage();
            }
            current = current.getCause();
        }
        return "数据保存失败";
    }
 
    /**
     * 判断指定用户在指定站点是否命中启用状态的白名单规则
     *
     * @param appUserId APP用户ID
     * @param siteId 站点ID
     * @return 白名单命中结果
     */
    @Override
    public AppUserWhitelistMatchVO matchWhitelist(Long appUserId, Integer siteId) {
        AppUserWhitelistMatchVO emptyResult = new AppUserWhitelistMatchVO();
        if (appUserId == null || siteId == null) {
            return emptyResult;
        }
        TAppUser appUser = appUserService.getById(appUserId);
        if (appUser == null || StringUtils.isBlank(appUser.getPhone())) {
            return emptyResult;
        }
        TAppUserWhitelist whitelist = this.lambdaQuery()
                .eq(TAppUserWhitelist::getPhone, appUser.getPhone())
                .eq(TAppUserWhitelist::getStatus, STATUS_ENABLED)
                .eq(TAppUserWhitelist::getDelFlag, false)
                .last("LIMIT 1")
                .one();
        if (whitelist == null || !isWhitelistScopeMatched(whitelist, siteId)) {
            return emptyResult;
        }
        AppUserWhitelistMatchVO result = new AppUserWhitelistMatchVO();
        result.setMatched(true);
        result.setWhitelistId(whitelist.getId());
        result.setBenefitType(whitelist.getBenefitType());
        result.setScopeType(whitelist.getScopeType());
        result.setBenefitName(getBenefitName(whitelist.getBenefitType()));
        result.setCompanyId(whitelist.getCompanyId());
        result.setFleetId(whitelist.getFleetId());
        return result;
    }
 
    @Override
    public List<TAppUserWhitelist> listByCompany(Integer companyId) {
        if (companyId == null) {
            return Collections.emptyList();
        }
        return lambdaQuery()
                .eq(TAppUserWhitelist::getCompanyId, companyId)
                .eq(TAppUserWhitelist::getDelFlag, false)
                .orderByDesc(BasePojo::getCreateTime)
                .list();
    }
 
    @Override
    public AppUserWhitelistAllocateVO getAllocateOverview(Integer companyId) {
        if (companyId == null) {
            throw new ServiceException("请选择公司");
        }
        TGroupCompany company = loadCompany(companyId);
        if (company == null) {
            throw new ServiceException("公司不存在或已删除");
        }
        return buildAllocateOverview(company);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AppUserWhitelistAllocateVO allocateQuota(AppUserWhitelistAllocateDTO allocateDTO) {
        Map<Long, BigDecimal> amounts = validateAllocateRequest(allocateDTO);
        Integer companyId = allocateDTO.getCompanyId();
        // 公司必须存在、启用且为分配模式(共享模式不支持额度分配)
        TGroupCompany company = loadAllocationCompany(companyId);
        BigDecimal total = amounts.values().stream()
                .reduce(BigDecimal.ZERO, BigDecimal::add)
                .setScale(2, RoundingMode.HALF_UP);
        // 同公司分配串行化,避免并发下重复占用可分配余额
        RLock lock = redissonClient.getLock(ALLOCATE_LOCK_PREFIX + companyId);
        boolean locked = tryQuotaLock(lock, "该公司额度分配正在处理中,请稍后重试");
        try {
            List<TAppUserWhitelist> whitelists = listByIds(amounts.keySet());
            if (whitelists == null || whitelists.size() != amounts.size()) {
                throw new ServiceException("部分白名单用户不存在或已删除");
            }
            for (TAppUserWhitelist whitelist : whitelists) {
                // 合作商账号仅能分配自身可见的白名单记录
                assertPartnerVisible(whitelist);
                BigDecimal amount = amounts.get(whitelist.getId());
                if (!companyId.equals(whitelist.getCompanyId())) {
                    throw new ServiceException("白名单用户【" + whitelist.getPhone() + "】不属于所选公司,无法分配额度");
                }
                if (!Integer.valueOf(STATUS_ENABLED).equals(whitelist.getStatus())) {
                    throw new ServiceException("白名单用户【" + whitelist.getPhone() + "】已停用,禁止分配额度");
                }
                BigDecimal balance = normalizeQuota(whitelist.getBalance());
                if (balance.add(amount).compareTo(MAX_INIT_QUOTA) > 0) {
                    throw new ServiceException("白名单用户【" + whitelist.getPhone() + "】分配后余额超过上限"
                            + MAX_INIT_QUOTA.toPlainString());
                }
            }
            // 先扣公司可分配余额(条件更新,余额不足直接失败),失败无需补偿
            R<BigDecimal> deductResponse = groupCompanyClient.deductBalance(companyId, total);
            if (deductResponse == null || !R.isSuccess(deductResponse)) {
                throw new ServiceException(buildDeductFailureMessage(deductResponse));
            }
            try {
                String operator = getUsername();
                for (TAppUserWhitelist whitelist : whitelists) {
                    BigDecimal amount = amounts.get(whitelist.getId());
                    BigDecimal before = normalizeQuota(whitelist.getBalance());
                    if (baseMapper.addBalance(whitelist.getId(), amount, operator) != 1) {
                        throw new ServiceException("白名单用户【" + whitelist.getPhone() + "】额度分配失败,请重试");
                    }
                    recordWhitelistAllocate(whitelist.getPhone(), whitelist.getName(), companyId, before, amount,
                            TCompanyAllocateRecord.SOURCE_ALLOCATE, buildWhitelistAllocateRemark(company));
                }
            } catch (RuntimeException ex) {
                // 白名单余额变更随事务回滚,公司余额为远程扣减需显式退还
                refundQuietly(companyId, total);
                throw ex;
            }
            AppUserWhitelistAllocateVO vo = buildAllocateOverview(company);
            vo.setAvailableBalance(normalizeMoney(deductResponse.getData()));
            return vo;
        } finally {
            unlockQuota(lock, locked);
        }
    }
 
    /**
     * 根据白名单范围类型判断当前站点是否在可用范围内
     *
     * @param whitelist 白名单配置
     * @param siteId 站点ID
     * @return true=命中范围
     */
    private boolean isWhitelistScopeMatched(TAppUserWhitelist whitelist, Integer siteId) {
        if (Objects.equals(whitelist.getScopeType(), SCOPE_ALL_SITE)) {
            return whitelist.getPartnerId() == null || isSiteBelongsToPartner(siteId, whitelist.getPartnerId());
        }
        if (Objects.equals(whitelist.getScopeType(), SCOPE_SELECTED_SITE)) {
            return whitelistSiteService.lambdaQuery()
                    .eq(TAppUserWhitelistSite::getWhitelistId, whitelist.getId())
                    .eq(TAppUserWhitelistSite::getSiteId, siteId)
                    .count() > 0;
        }
        if (Objects.equals(whitelist.getScopeType(), SCOPE_PARTNER)) {
            R<List<Site>> siteResult = siteClient.getSiteByIds(Collections.singletonList(siteId));
            List<Site> sites = siteResult == null || siteResult.getData() == null ? Collections.emptyList() : siteResult.getData();
            return !sites.isEmpty() && Objects.equals(sites.get(0).getPartnerId(), whitelist.getPartnerId());
        }
        return false;
    }
 
    private int getImportTitleNum(MultipartFile file) throws Exception {
        try (InputStream inputStream = file.getInputStream(); Workbook workbook = WorkbookFactory.create(inputStream)) {
            Sheet sheet = workbook.getSheetAt(0);
            if (sheet == null) {
                throw new ServiceException("导入文件sheet不存在");
            }
            int maxRow = Math.min(sheet.getLastRowNum(), 5);
            for (int i = 0; i <= maxRow; i++) {
                Row row = sheet.getRow(i);
                if (isImportHeaderRow(row)) {
                    return i;
                }
            }
        }
        throw new ServiceException("导入文件表头不正确,请下载最新模板后填写");
    }
 
    private boolean isImportHeaderRow(Row row) {
        if (row == null) {
            return false;
        }
        ExcelUtil<AppUserWhitelistImportDTO> util = new ExcelUtil<>(AppUserWhitelistImportDTO.class);
        return IMPORT_HEADER_PHONE.equals(normalize(String.valueOf(util.getCellValue(row, 0))));
    }
 
    private void validateSaveDTO(AppUserWhitelistSaveDTO saveDTO) {
        if (saveDTO == null) {
            throw new ServiceException("请求参数不能为空");
        }
        saveDTO.setPhone(normalize(saveDTO.getPhone()));
        if (!isValidPhone(saveDTO.getPhone())) {
            throw new ServiceException("请输入正确的用户手机号");
        }
        saveDTO.setName(normalize(saveDTO.getName()));
        if (saveDTO.getName() != null && saveDTO.getName().isEmpty()) {
            saveDTO.setName(null);
        }
        if (StringUtils.isNotBlank(saveDTO.getName()) && saveDTO.getName().length() > MAX_NAME_LENGTH) {
            throw new ServiceException("姓名长度不能超过" + MAX_NAME_LENGTH + "个字符");
        }
        // 状态不传默认启用,仅允许1=启用、2=停用
        Integer status = saveDTO.getStatus() == null ? STATUS_ENABLED : saveDTO.getStatus();
        if (!Objects.equals(status, STATUS_ENABLED) && !Objects.equals(status, STATUS_DISABLED)) {
            throw new ServiceException("请选择正确的状态");
        }
        saveDTO.setStatus(status);
        // 享受权益不传默认全部免费,传了仍按原规则校验
        Integer benefitType = saveDTO.getBenefitType() == null ? BENEFIT_ALL_FREE : saveDTO.getBenefitType();
        if (!isValidBenefit(benefitType)) {
            throw new ServiceException("请选择正确的享受权益");
        }
        saveDTO.setBenefitType(benefitType);
        // 白名单电站不传默认全部电站,传了仍按原规则校验
        Integer scopeType = saveDTO.getScopeType() == null ? SCOPE_ALL_SITE : saveDTO.getScopeType();
        if (!isValidScope(scopeType)) {
            throw new ServiceException("请选择正确的白名单电站范围");
        }
        saveDTO.setScopeType(scopeType);
        if (Objects.equals(scopeType, SCOPE_SELECTED_SITE)
                && (saveDTO.getSiteIds() == null || saveDTO.getSiteIds().isEmpty())) {
            throw new ServiceException("请选择白名单电站");
        }
        if (Objects.equals(scopeType, SCOPE_PARTNER) && saveDTO.getPartnerId() == null) {
            throw new ServiceException("请选择加盟商");
        }
        validatePartnerDataScope(saveDTO);
        TAppUserWhitelist exists = getByPhone(saveDTO.getPhone());
        if (saveDTO.getId() == null && exists != null) {
            throw new ServiceException("该手机号已存在白名单配置");
        }
        if (saveDTO.getId() != null && exists != null && !Objects.equals(exists.getId(), saveDTO.getId())) {
            throw new ServiceException("该手机号已存在白名单配置");
        }
    }
 
    private void createWhitelist(AppUserWhitelistSaveDTO saveDTO) {
        if (getByPhone(saveDTO.getPhone()) != null) {
            throw new ServiceException("该手机号已存在白名单配置");
        }
        // 同手机号并发新增串行化:手机号重复校验与额度扣减在同一临界区内
        RLock lock = redissonClient.getLock(QUOTA_LOCK_PREFIX + "PHONE:" + saveDTO.getPhone());
        boolean locked = tryQuotaLock(lock, "该手机号正在处理中,请稍后重试");
        List<Runnable> compensations = new ArrayList<>();
        try {
            if (getByPhone(saveDTO.getPhone()) != null) {
                throw new ServiceException("该手机号已存在白名单配置");
            }
            BigDecimal quota = normalizeQuota(saveDTO.getBalance());
            Integer companyId = resolveRelationCompanyId(saveDTO);
            if (quota.signum() > 0) {
                requireAllocationCompany(companyId);
                deductBalance(companyId, quota);
                compensations.add(() -> refundQuietly(companyId, quota));
            }
            TAppUserWhitelist whitelist = new TAppUserWhitelist();
            TAppUser appUser = appUserService.lambdaQuery()
                    .eq(TAppUser::getPhone, saveDTO.getPhone())
                    .last("LIMIT 1")
                    .one();
            whitelist.setCreateBy(getUsername());
            whitelist.setAppUserId(appUser == null ? null : appUser.getId());
            whitelist.setPhone(saveDTO.getPhone());
            whitelist.setName(saveDTO.getName());
            whitelist.setBenefitType(saveDTO.getBenefitType());
            whitelist.setScopeType(saveDTO.getScopeType());
            whitelist.setPartnerId(resolveWhitelistPartnerId(saveDTO));
            whitelist.setCompanyId(companyId);
            whitelist.setFleetId(saveDTO.getFleetId());
            whitelist.setBalance(quota.signum() > 0 ? quota : null);
            whitelist.setStatus(saveDTO.getStatus());
            whitelist.setUpdateBy(getUsername());
            whitelist.setRemark(saveDTO.getRemark());
            whitelist.setDelFlag(false);
            if (whitelist.getId() == null) {
                this.save(whitelist);
            }
 
            whitelistSiteService.deleteByWhitelistId(whitelist.getId());
            if (Objects.equals(saveDTO.getScopeType(), SCOPE_SELECTED_SITE)) {
                Long whitelistId = whitelist.getId();
                List<TAppUserWhitelistSite> sites = saveDTO.getSiteIds().stream()
                        .filter(Objects::nonNull)
                        .distinct()
                        .map(siteId -> {
                            TAppUserWhitelistSite site = new TAppUserWhitelistSite();
                            site.setWhitelistId(whitelistId);
                            site.setSiteId(siteId);
                            site.setDelFlag(false);
                            return site;
                        }).collect(Collectors.toList());
                if (!sites.isEmpty()) {
                    whitelistSiteService.saveBatch(sites);
                }
            }
            recordWhitelistAllocate(whitelist.getPhone(), whitelist.getName(), companyId, BigDecimal.ZERO, quota,
                    TCompanyAllocateRecord.SOURCE_CREATE, "新增白名单初始分配额度");
        } catch (RuntimeException ex) {
            compensate(compensations);
            throw ex;
        } finally {
            unlockQuota(lock, locked);
        }
    }
 
    private void updateWhitelist(AppUserWhitelistSaveDTO saveDTO) {
        TAppUserWhitelist existing = this.getById(saveDTO.getId());
        if (existing == null) {
            throw new ServiceException("白名单用户不存在");
        }
        assertPartnerVisible(existing);
        // 同白名单并发编辑串行化:额度差额计算、公司余额扣减/退还必须在临界区内,防止重复扣减或重复退还
        RLock lock = redissonClient.getLock(QUOTA_LOCK_PREFIX + saveDTO.getId());
        boolean locked = tryQuotaLock(lock, "该白名单额度正在处理中,请稍后重试");
        List<Runnable> compensations = new ArrayList<>();
        // 分配记录延迟到白名单落库成功后写入:失败时公司余额已回补,不应留下流水
        List<Runnable> recordTasks = new ArrayList<>();
        try {
            TAppUserWhitelist whitelist = this.getById(saveDTO.getId());
            if (whitelist == null) {
                throw new ServiceException("白名单用户不存在");
            }
            assertPartnerVisible(whitelist);
            BigDecimal newQuota = normalizeQuota(saveDTO.getBalance());
            Integer newCompanyId = resolveRelationCompanyId(saveDTO);
            if (newQuota.signum() > 0) {
                requireAllocationCompany(newCompanyId);
            }
            Integer oldCompanyId = whitelist.getCompanyId();
            BigDecimal oldQuota = normalizeQuota(whitelist.getBalance());
            if (Objects.equals(oldCompanyId, newCompanyId)) {
                // 同一公司按差额调整:调增扣差额,调减退差额
                BigDecimal diff = newQuota.subtract(oldQuota);
                if (diff.signum() > 0 && newCompanyId != null) {
                    deductBalance(newCompanyId, diff);
                    compensations.add(() -> refundQuietly(newCompanyId, diff));
                    recordTasks.add(() -> recordWhitelistAllocate(saveDTO.getPhone(), saveDTO.getName(), newCompanyId,
                            oldQuota, diff, TCompanyAllocateRecord.SOURCE_EDIT, "编辑白名单调整初始分配额度"));
                } else if (diff.signum() < 0 && newCompanyId != null) {
                    BigDecimal refund = diff.negate();
                    refundBalance(newCompanyId, refund);
                    compensations.add(() -> deductQuietly(newCompanyId, refund));
                    recordTasks.add(() -> recordWhitelistAllocate(saveDTO.getPhone(), saveDTO.getName(), newCompanyId,
                            oldQuota, diff, TCompanyAllocateRecord.SOURCE_EDIT, "编辑白名单调减初始分配额度"));
                }
            } else {
                // 跨公司:旧公司全额退还,新公司全额扣减
                if (oldCompanyId != null && oldQuota.signum() > 0) {
                    refundBalance(oldCompanyId, oldQuota);
                    compensations.add(() -> deductQuietly(oldCompanyId, oldQuota));
                    recordTasks.add(() -> recordWhitelistAllocate(saveDTO.getPhone(), saveDTO.getName(), oldCompanyId,
                            oldQuota, oldQuota.negate(), TCompanyAllocateRecord.SOURCE_EDIT,
                            "编辑白名单调整所属公司(原公司退还)"));
                }
                if (newCompanyId != null && newQuota.signum() > 0) {
                    deductBalance(newCompanyId, newQuota);
                    compensations.add(() -> refundQuietly(newCompanyId, newQuota));
                    recordTasks.add(() -> recordWhitelistAllocate(saveDTO.getPhone(), saveDTO.getName(), newCompanyId,
                            BigDecimal.ZERO, newQuota, TCompanyAllocateRecord.SOURCE_EDIT, "编辑白名单调整所属公司"));
                }
            }
            TAppUser appUser = appUserService.lambdaQuery()
                    .eq(TAppUser::getPhone, saveDTO.getPhone())
                    .last("LIMIT 1")
                    .one();
            whitelist.setAppUserId(appUser == null ? null : appUser.getId());
            whitelist.setPhone(saveDTO.getPhone());
            whitelist.setName(saveDTO.getName());
            whitelist.setBenefitType(saveDTO.getBenefitType());
            whitelist.setScopeType(saveDTO.getScopeType());
            whitelist.setPartnerId(resolveWhitelistPartnerId(saveDTO));
            whitelist.setCompanyId(newCompanyId);
            whitelist.setFleetId(saveDTO.getFleetId());
            whitelist.setBalance(newQuota.signum() > 0 ? newQuota : null);
            whitelist.setStatus(saveDTO.getStatus());
            whitelist.setUpdateBy(getUsername());
            whitelist.setRemark(saveDTO.getRemark());
            whitelist.setDelFlag(false);
            this.baseMapper.updateByIdIgnoreLogic(whitelist);
 
            whitelistSiteService.deleteByWhitelistId(whitelist.getId());
            if (Objects.equals(saveDTO.getScopeType(), SCOPE_SELECTED_SITE)) {
                Long whitelistId = whitelist.getId();
                List<TAppUserWhitelistSite> sites = saveDTO.getSiteIds().stream()
                        .filter(Objects::nonNull)
                        .distinct()
                        .map(siteId -> {
                            TAppUserWhitelistSite site = new TAppUserWhitelistSite();
                            site.setWhitelistId(whitelistId);
                            site.setSiteId(siteId);
                            site.setDelFlag(false);
                            return site;
                        }).collect(Collectors.toList());
                if (!sites.isEmpty()) {
                    whitelistSiteService.saveBatch(sites);
                }
            }
            recordTasks.forEach(Runnable::run);
        } catch (RuntimeException ex) {
            compensate(compensations);
            throw ex;
        } finally {
            unlockQuota(lock, locked);
        }
    }
 
    /**
     * 查询手机号对应的有效白名单配置,逻辑删除记录允许重新新增。
     *
     * @param phone 用户手机号
     * @return 有效白名单配置
     */
    private TAppUserWhitelist getByPhone(String phone) {
        return this.baseMapper.selectValidByPhone(phone);
    }
 
    /** 写入白名单分配流水(分配为正数、回收为负数;公司为空或金额为0时自动忽略) */
    private void recordWhitelistAllocate(String phone, String name, Integer companyId, BigDecimal beforeBalance,
                                         BigDecimal amount, int changeSource, String remark) {
        try {
            companyAllocateRecordService.recordAllocate(TCompanyAllocateRecord.USER_TYPE_WHITELIST, phone, name,
                    companyId, changeSource, beforeBalance, amount, remark);
        } catch (RuntimeException ex) {
            // 流水为审计数据,写入失败不阻断白名单额度变更主流程,仅记录日志便于排查
            log.error("白名单{}分配流水写入失败:{}", phone, ex.getMessage(), ex);
        }
    }
 
    private List<String> validateImportRow(AppUserWhitelistImportDTO row) {
        List<String> errors = new ArrayList<>();
        if (row == null) {
            errors.add("空行");
            return errors;
        }
        String phone = normalize(row.getPhone());
        if (!isValidPhone(phone)) {
            errors.add("用户手机号格式错误");
        }
        return errors;
    }
 
    /** 导入数据统一按默认值处理:全部免费 + 全部电站 + 启用 */
    private AppUserWhitelistSaveDTO buildSaveDTO(AppUserWhitelistImportDTO row) {
        AppUserWhitelistSaveDTO dto = new AppUserWhitelistSaveDTO();
        dto.setPhone(normalize(row.getPhone()));
        dto.setBenefitType(BENEFIT_ALL_FREE);
        dto.setScopeType(SCOPE_ALL_SITE);
        dto.setSiteIds(Collections.emptyList());
        dto.setStatus(STATUS_ENABLED);
        return dto;
    }
 
    private void fillDisplay(List<TAppUserWhitelist> records) {
        if (records == null || records.isEmpty()) {
            return;
        }
        Map<Integer, Site> siteMap = getSiteMap(records);
        Map<Integer, Partner> partnerMap = getPartnerMap(records, siteMap);
        for (TAppUserWhitelist record : records) {
            record.setUid(record.getId() == null ? null : record.getId().toString());
            record.setBenefitName(getBenefitName(record.getBenefitType()));
            record.setScopeName(getScopeName(record.getScopeType()));
            if (record.getPartnerId() != null) {
                Partner partner = partnerMap.get(record.getPartnerId());
                record.setPartnerName(partner == null ? null : partner.getName());
            }
            List<TAppUserWhitelistSite> sites = whitelistSiteService.lambdaQuery()
                    .eq(TAppUserWhitelistSite::getWhitelistId, record.getId())
                    .list();
            for (TAppUserWhitelistSite siteItem : sites) {
                Site site = siteMap.get(siteItem.getSiteId());
                if (site != null) {
                    siteItem.setSiteCode(site.getCode());
                    siteItem.setSiteName(site.getName());
                    siteItem.setSiteType(site.getSiteType());
                    siteItem.setBusinessCategory(site.getBusinessCategory());
                    siteItem.setStatus(site.getStatus());
                    Partner partner = partnerMap.get(site.getPartnerId());
                    siteItem.setPartnerName(partner == null ? null : partner.getName());
                }
            }
            record.setSiteList(sites);
            record.setSiteIds(sites.stream().map(TAppUserWhitelistSite::getSiteId).collect(Collectors.toList()));
        }
        fillCompanyNames(records);
        fillFleetNames(records);
    }
 
    /** 列表回填所属公司名称(远程失败仅记录日志,不影响列表查询) */
    private void fillCompanyNames(List<TAppUserWhitelist> records) {
        Map<Integer, String> nameMap = new HashMap<>();
        for (TAppUserWhitelist record : records) {
            Integer companyId = record.getCompanyId();
            if (companyId == null) {
                continue;
            }
            if (nameMap.containsKey(companyId)) {
                record.setCompanyName(nameMap.get(companyId));
                continue;
            }
            String companyName = null;
            try {
                TGroupCompany company = loadCompany(companyId);
                companyName = company == null ? null : company.getName();
            } catch (Exception ex) {
                log.warn("回填公司名称失败,companyId={}", companyId, ex);
            }
            nameMap.put(companyId, companyName);
            record.setCompanyName(companyName);
        }
    }
 
    /** 列表回填所属车队名称(批量查询,远程失败仅记录日志,不影响列表查询) */
    private void fillFleetNames(List<TAppUserWhitelist> records) {
        List<Integer> fleetIds = records.stream()
                .map(TAppUserWhitelist::getFleetId)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
        if (fleetIds.isEmpty()) {
            return;
        }
        try {
            R<List<TFleet>> response = groupCompanyClient.listFleetsByIds(fleetIds);
            if (response == null || !R.isSuccess(response) || response.getData() == null) {
                return;
            }
            Map<Integer, String> nameMap = new HashMap<>();
            for (TFleet fleet : response.getData()) {
                if (fleet != null && fleet.getId() != null) {
                    nameMap.put(fleet.getId(), fleet.getFleetName());
                }
            }
            records.forEach(record -> record.setFleetName(nameMap.get(record.getFleetId())));
        } catch (Exception ex) {
            log.warn("回填车队名称失败,fleetIds={}", fleetIds, ex);
        }
    }
 
    private Map<Integer, Site> getSiteMap(List<TAppUserWhitelist> records) {
        List<Long> whitelistIds = records.stream().map(TAppUserWhitelist::getId).collect(Collectors.toList());
        List<TAppUserWhitelistSite> siteLinks = whitelistSiteService.lambdaQuery()
                .in(TAppUserWhitelistSite::getWhitelistId, whitelistIds)
                .list();
        List<Integer> siteIds = siteLinks.stream().map(TAppUserWhitelistSite::getSiteId).distinct().collect(Collectors.toList());
        if (siteIds.isEmpty()) {
            return Collections.emptyMap();
        }
        R<List<Site>> siteResult = siteClient.getSiteByIds(siteIds);
        List<Site> sites = siteResult == null || siteResult.getData() == null ? Collections.emptyList() : siteResult.getData();
        return sites.stream().collect(Collectors.toMap(Site::getId, site -> site, (a, b) -> a));
    }
 
    private Map<Integer, Partner> getPartnerMap(List<TAppUserWhitelist> records, Map<Integer, Site> siteMap) {
        Map<Integer, Partner> partnerMap = new HashMap<>();
        Set<Integer> partnerIds = new HashSet<>();
        for (TAppUserWhitelist record : records) {
            if (record.getPartnerId() != null) {
                partnerIds.add(record.getPartnerId());
            }
        }
        if (siteMap != null) {
            for (Site site : siteMap.values()) {
                if (site != null && site.getPartnerId() != null) {
                    partnerIds.add(site.getPartnerId());
                }
            }
        }
        for (Integer partnerId : partnerIds) {
            R<Partner> partnerResult = partnerClient.getPartnerById(partnerId);
            if (partnerResult != null && partnerResult.getData() != null) {
                partnerMap.put(partnerId, partnerResult.getData());
            }
        }
        return partnerMap;
    }
 
    private void validatePartnerDataScope(AppUserWhitelistSaveDTO saveDTO) {
        Integer currentPartnerId = getCurrentPartnerId();
        if (currentPartnerId == null) {
            return;
        }
        if (Objects.equals(saveDTO.getScopeType(), SCOPE_PARTNER)
                && !Objects.equals(saveDTO.getPartnerId(), currentPartnerId)) {
            throw new ServiceException("合作商账号只能选择本合作商");
        }
        if (Objects.equals(saveDTO.getScopeType(), SCOPE_SELECTED_SITE)) {
            Set<Integer> authSiteIds = getCurrentPartnerSiteIds(currentPartnerId);
            if (authSiteIds.isEmpty()) {
                throw new ServiceException("当前合作商没有可用电站权限");
            }
            List<Integer> unauthorizedSiteIds = saveDTO.getSiteIds().stream()
                    .filter(Objects::nonNull)
                    .filter(siteId -> !authSiteIds.contains(siteId))
                    .collect(Collectors.toList());
            if (!unauthorizedSiteIds.isEmpty()) {
                throw new ServiceException("只能选择当前合作商负责的电站");
            }
        }
    }
 
    private void assertPartnerVisible(TAppUserWhitelist whitelist) {
        Integer currentPartnerId = getCurrentPartnerId();
        if (currentPartnerId == null || whitelist == null) {
            return;
        }
        if (Objects.equals(whitelist.getPartnerId(), currentPartnerId)) {
            return;
        }
        if (StringUtils.isNotBlank(whitelist.getCreateBy()) && Objects.equals(whitelist.getCreateBy(), getUsername())) {
            return;
        }
        throw new ServiceException("无权操作该白名单数据");
    }
 
    private Integer resolveWhitelistPartnerId(AppUserWhitelistSaveDTO saveDTO) {
        Integer currentPartnerId = getCurrentPartnerId();
        if (currentPartnerId != null) {
            return currentPartnerId;
        }
        return Objects.equals(saveDTO.getScopeType(), SCOPE_PARTNER) ? saveDTO.getPartnerId() : null;
    }
 
    /** 校验并回填所属公司与车队:车队必须归属所选公司;未选公司时按车队自动带出公司 */
    private Integer resolveRelationCompanyId(AppUserWhitelistSaveDTO saveDTO) {
        Integer companyId = saveDTO.getCompanyId();
        if (companyId != null && loadCompany(companyId) == null) {
            throw new ServiceException("所属公司不存在或已删除");
        }
        Integer fleetId = saveDTO.getFleetId();
        if (fleetId != null) {
            TFleet fleet = loadFleet(fleetId);
            if (fleet == null) {
                throw new ServiceException("所属车队不存在或已删除");
            }
            if (companyId == null) {
                companyId = fleet.getCompanyId();
            } else if (!companyId.equals(fleet.getCompanyId())) {
                throw new ServiceException("所属车队不属于所选公司");
            }
        }
        return companyId;
    }
 
    /** 规范化初始分配额度:null 视为 0,不允许负数、超过2位小数或超出上限 */
    private BigDecimal normalizeQuota(BigDecimal quota) {
        if (quota == null) {
            return BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP);
        }
        if (quota.signum() < 0) {
            throw new ServiceException("初始分配额度不能小于0");
        }
        if (quota.stripTrailingZeros().scale() > 2) {
            throw new ServiceException("初始分配额度最多保留2位小数");
        }
        BigDecimal normalized = quota.setScale(2, RoundingMode.HALF_UP);
        if (normalized.compareTo(MAX_INIT_QUOTA) > 0) {
            throw new ServiceException("初始分配额度不能超过" + MAX_INIT_QUOTA.toPlainString());
        }
        return normalized;
    }
 
    /** 校验公司为启用且分配模式(可设置初始分配额度) */
    private void requireAllocationCompany(Integer companyId) {
        if (companyId == null) {
            throw new ServiceException("请选择所属公司后再填写初始分配额度");
        }
        TGroupCompany company = loadCompany(companyId);
        if (company == null) {
            throw new ServiceException("所属公司不存在或已删除");
        }
        if (!Integer.valueOf(ACCOUNT_MODE_ALLOCATION).equals(company.getAccountMode())) {
            throw new ServiceException("仅分配模式的公司可填写初始分配额度");
        }
        if (!Integer.valueOf(1).equals(company.getStatus())) {
            throw new ServiceException("所属公司已停用,无法填写初始分配额度");
        }
    }
 
    private TGroupCompany loadCompany(Integer companyId) {
        R<TGroupCompany> response = groupCompanyClient.getCompanyById(companyId);
        if (response == null || !R.isSuccess(response)) {
            throw new ServiceException("公司信息查询失败:" + (response == null ? "服务无响应" : response.getMsg()));
        }
        return response.getData();
    }
 
    /**
     * 额度分配:加载公司并校验为启用且分配模式。
     * 与初始分配额度校验分开,便于按场景返回更准确的提示,且不影响既有链路。
     */
    private TGroupCompany loadAllocationCompany(Integer companyId) {
        if (companyId == null) {
            throw new ServiceException("请选择公司");
        }
        TGroupCompany company = loadCompany(companyId);
        if (company == null) {
            throw new ServiceException("公司不存在或已删除");
        }
        if (!Integer.valueOf(ACCOUNT_MODE_ALLOCATION).equals(company.getAccountMode())) {
            throw new ServiceException("该公司为共享模式,不支持额度分配");
        }
        if (!Integer.valueOf(1).equals(company.getStatus())) {
            throw new ServiceException("公司已停用,无法分配额度");
        }
        return company;
    }
 
    /** 组装额度分配概览(可分配余额、总账户余额) */
    private AppUserWhitelistAllocateVO buildAllocateOverview(TGroupCompany company) {
        AppUserWhitelistAllocateVO vo = new AppUserWhitelistAllocateVO();
        vo.setCompanyId(company.getId());
        vo.setCompanyName(company.getName());
        vo.setAccountMode(company.getAccountMode());
        vo.setAvailableBalance(normalizeMoney(company.getAvailableBalance()));
        vo.setTotalRechargeAmount(normalizeMoney(company.getTotalRechargeAmount()));
        return vo;
    }
 
    /** 金额归一化:null 视为 0,统一保留2位小数 */
    private BigDecimal normalizeMoney(BigDecimal amount) {
        return (amount == null ? BigDecimal.ZERO : amount).setScale(2, RoundingMode.HALF_UP);
    }
 
    /** 校验额度分配请求并返回 白名单记录ID → 本次分配金额(保持提交顺序) */
    private Map<Long, BigDecimal> validateAllocateRequest(AppUserWhitelistAllocateDTO dto) {
        if (dto == null) {
            throw new ServiceException("分配参数不能为空");
        }
        if (dto.getCompanyId() == null) {
            throw new ServiceException("请选择公司");
        }
        if (dto.getItems() == null || dto.getItems().isEmpty()) {
            throw new ServiceException("请至少为一条白名单用户填写本次分配金额");
        }
        if (dto.getItems().size() > MAX_ALLOCATE_WHITELIST_COUNT) {
            throw new ServiceException("单次最多分配" + MAX_ALLOCATE_WHITELIST_COUNT + "条白名单用户");
        }
        Map<Long, BigDecimal> amounts = new LinkedHashMap<>();
        for (AppUserWhitelistAllocateItemDTO item : dto.getItems()) {
            if (item == null || item.getWhitelistId() == null) {
                throw new ServiceException("分配明细中存在无效白名单用户");
            }
            if (amounts.containsKey(item.getWhitelistId())) {
                throw new ServiceException("分配明细中存在重复白名单用户");
            }
            validateAmount(item.getAmount(), "分配金额");
            if (item.getAmount().compareTo(MAX_INIT_QUOTA) > 0) {
                throw new ServiceException("单条白名单用户分配金额不能超过" + MAX_INIT_QUOTA.toPlainString());
            }
            amounts.put(item.getWhitelistId(), item.getAmount().setScale(2, RoundingMode.HALF_UP));
        }
        return amounts;
    }
 
    /** 校验金额:必填、大于0、最多2位小数 */
    private void validateAmount(BigDecimal amount, String fieldName) {
        if (amount == null) {
            throw new ServiceException(fieldName + "不能为空");
        }
        if (amount.signum() <= 0) {
            throw new ServiceException(fieldName + "必须大于0");
        }
        if (amount.stripTrailingZeros().scale() > 2) {
            throw new ServiceException(fieldName + "最多保留2位小数");
        }
    }
 
    /** 公司余额扣减失败提示:余额不足场景给出可读文案 */
    private String buildDeductFailureMessage(R<BigDecimal> response) {
        if (response == null) {
            return "公司可用余额扣减失败:服务无响应";
        }
        String msg = response.getMsg();
        if (msg != null && msg.contains("余额不足")) {
            return "公司可分配余额不足,无法完成本次分配";
        }
        return "公司可用余额扣减失败:" + msg;
    }
 
    /** 组装额度分配流水备注(含来源公司) */
    private String buildWhitelistAllocateRemark(TGroupCompany company) {
        String companyName = company == null || company.getName() == null ? "" : company.getName();
        String remark = "额度分配(公司:" + companyName + ")";
        return remark.length() > 255 ? remark.substring(0, 255) : remark;
    }
 
    private TFleet loadFleet(Integer fleetId) {
        R<TFleet> response = groupCompanyClient.getFleetById(fleetId);
        if (response == null || !R.isSuccess(response)) {
            throw new ServiceException("车队信息查询失败:" + (response == null ? "服务无响应" : response.getMsg()));
        }
        return response.getData();
    }
 
    /** 扣减公司可用余额(余额不足或服务异常时抛出业务异常) */
    private void deductBalance(Integer companyId, BigDecimal amount) {
        R<BigDecimal> response = groupCompanyClient.deductBalance(companyId, amount);
        if (response == null || !R.isSuccess(response)) {
            throw new ServiceException("公司可用余额扣减失败:" + (response == null ? "服务无响应" : response.getMsg()));
        }
    }
 
    /** 退还公司可用余额(失败时抛出业务异常) */
    private void refundBalance(Integer companyId, BigDecimal amount) {
        R<BigDecimal> response = groupCompanyClient.refundBalance(companyId, amount);
        if (response == null || !R.isSuccess(response)) {
            throw new ServiceException("公司可用余额退还失败:" + (response == null ? "服务无响应" : response.getMsg()));
        }
    }
 
    /** 静默退还(补偿场景使用,失败仅记录日志) */
    private void refundQuietly(Integer companyId, BigDecimal amount) {
        try {
            R<BigDecimal> response = groupCompanyClient.refundBalance(companyId, amount);
            if (response == null || !R.isSuccess(response)) {
                log.error("公司可用余额退还失败,需人工核对,companyId={}, amount={}, msg={}",
                        companyId, amount, response == null ? "服务无响应" : response.getMsg());
            }
        } catch (Exception ex) {
            log.error("公司可用余额退还异常,需人工核对,companyId={}, amount={}", companyId, amount, ex);
        }
    }
 
    /** 静默扣减(补偿场景使用,失败仅记录日志) */
    private void deductQuietly(Integer companyId, BigDecimal amount) {
        try {
            R<BigDecimal> response = groupCompanyClient.deductBalance(companyId, amount);
            if (response == null || !R.isSuccess(response)) {
                log.error("公司可用余额回滚扣减失败,需人工核对,companyId={}, amount={}, msg={}",
                        companyId, amount, response == null ? "服务无响应" : response.getMsg());
            }
        } catch (Exception ex) {
            log.error("公司可用余额回滚扣减异常,需人工核对,companyId={}, amount={}", companyId, amount, ex);
        }
    }
 
    /** 保存/更新失败时反向补偿已执行的余额操作(倒序执行) */
    private void compensate(List<Runnable> compensations) {
        for (int i = compensations.size() - 1; i >= 0; i--) {
            try {
                compensations.get(i).run();
            } catch (Exception ex) {
                log.error("公司可用余额补偿失败,需人工核对", ex);
            }
        }
    }
 
    /** 抢占并发锁,抢占失败抛出业务异常 */
    private boolean tryQuotaLock(RLock lock, String busyMessage) {
        try {
            if (!lock.tryLock(LOCK_WAIT_SECONDS, LOCK_LEASE_SECONDS, TimeUnit.SECONDS)) {
                throw new ServiceException(busyMessage);
            }
            return true;
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
            throw new ServiceException(busyMessage);
        }
    }
 
    private void unlockQuota(RLock lock, boolean locked) {
        if (!locked) {
            return;
        }
        try {
            if (lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        } catch (Exception ex) {
            log.warn("释放白名单额度并发锁失败", ex);
        }
    }
 
    private Set<Integer> getCurrentPartnerSiteIds(Integer partnerId) {
        R<Set<Integer>> siteResult = partnerClient.authSite(partnerId, SiteMenu.SITE_LIST.getValue());
        return siteResult == null || siteResult.getData() == null ? Collections.emptySet() : siteResult.getData();
    }
 
    private boolean isSiteBelongsToPartner(Integer siteId, Integer partnerId) {
        if (siteId == null || partnerId == null) {
            return false;
        }
        R<List<Site>> siteResult = siteClient.getSiteByIds(Collections.singletonList(siteId));
        List<Site> sites = siteResult == null || siteResult.getData() == null ? Collections.emptyList() : siteResult.getData();
        return !sites.isEmpty() && Objects.equals(sites.get(0).getPartnerId(), partnerId);
    }
 
    private Integer getCurrentPartnerId() {
        SysUser sysUser = getCurrentSysUser();
        if (sysUser == null || !Objects.equals(sysUser.getRoleType(), 2)) {
            return null;
        }
        return sysUser.getObjectId();
    }
 
    private SysUser getCurrentSysUser() {
        LoginUser loginUser = null;
        try {
            loginUser = tokenService.getLoginUser();
        } catch (Exception e) {
            // ignore and try security context fallback
        }
        if (loginUser == null) {
            try {
                loginUser = SecurityUtils.getLoginUser();
            } catch (Exception e) {
                // ignore and fall through to null
            }
        }
        if (loginUser == null) {
            return null;
        }
        if (loginUser.getSysUser() != null && loginUser.getSysUser().getRoleType() != null) {
            return loginUser.getSysUser();
        }
        if (loginUser.getUserid() == null) {
            return loginUser.getSysUser();
        }
        R<SysUser> userResult = sysUserClient.getSysUser(loginUser.getUserid());
        if (userResult != null && userResult.getData() != null) {
            return userResult.getData();
        }
        return loginUser.getSysUser();
    }
 
    private boolean isValidPhone(String phone) {
        return StringUtils.isNotBlank(phone) && PHONE_PATTERN.matcher(phone).matches();
    }
 
    private boolean isValidBenefit(Integer benefitType) {
        return Objects.equals(benefitType, BENEFIT_ALL_FREE)
                || Objects.equals(benefitType, BENEFIT_ELECTRIC_FREE)
                || Objects.equals(benefitType, BENEFIT_SERVICE_FREE);
    }
 
    private boolean isValidScope(Integer scopeType) {
        return Objects.equals(scopeType, SCOPE_ALL_SITE)
                || Objects.equals(scopeType, SCOPE_SELECTED_SITE)
                || Objects.equals(scopeType, SCOPE_PARTNER);
    }
 
    private String normalize(String value) {
        return value == null ? null : value.trim();
    }
 
    private String getBenefitName(Integer benefitType) {
        if (Objects.equals(benefitType, BENEFIT_ALL_FREE)) {
            return "全部免费";
        }
        if (Objects.equals(benefitType, BENEFIT_ELECTRIC_FREE)) {
            return "免电费";
        }
        if (Objects.equals(benefitType, BENEFIT_SERVICE_FREE)) {
            return "免服务费";
        }
        return "";
    }
 
    private String getScopeName(Integer scopeType) {
        if (Objects.equals(scopeType, SCOPE_ALL_SITE)) {
            return "全部电站";
        }
        if (Objects.equals(scopeType, SCOPE_SELECTED_SITE)) {
            return "部分电站";
        }
        if (Objects.equals(scopeType, SCOPE_PARTNER)) {
            return "指定合作商";
        }
        return "";
    }
 
    private String getUsername() {
        try {
            String username = SecurityUtils.getUsername();
            if (StringUtils.isNotBlank(username)) {
                return username;
            }
        } catch (Exception e) {
            // ignore and try token context fallback
        }
        try {
            LoginUser loginUser = tokenService.getLoginUser();
            if (loginUser != null) {
                if (StringUtils.isNotBlank(loginUser.getUsername())) {
                    return loginUser.getUsername();
                }
                if (loginUser.getSysUser() != null && StringUtils.isNotBlank(loginUser.getSysUser().getUserName())) {
                    return loginUser.getSysUser().getUserName();
                }
            }
        } catch (Exception e) {
            // ignore and fall through to null
        }
        return null;
    }
}