puzhibing
4 天以前 a0e03b646f2c444b0f09687d24e9995112b99017
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
package com.ruoyi.account.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.account.api.model.TAppUserWhitelist;
import com.ruoyi.account.api.model.TChargingCard;
import com.ruoyi.account.api.model.TVinManagement;
import com.ruoyi.account.mapper.TAppUserWhitelistMapper;
import com.ruoyi.account.mapper.TChargingCardMapper;
import com.ruoyi.account.mapper.TVinManagementMapper;
import com.ruoyi.account.service.FleetUnbindService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 车队解绑服务实现(内部链路:other 服务删除车队时触发,无管理端用户上下文)
 * </p>
 *
 * @author mxcd
 * @since 2026-09-29
 */
@Service
public class FleetUnbindServiceImpl implements FleetUnbindService {
 
    @Resource
    private TVinManagementMapper vinManagementMapper;
 
    @Resource
    private TChargingCardMapper chargingCardMapper;
 
    @Resource
    private TAppUserWhitelistMapper appUserWhitelistMapper;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int unbindByFleetIds(List<Integer> fleetIds) {
        List<Integer> distinct = normalize(fleetIds);
        if (distinct.isEmpty()) {
            return 0;
        }
        LocalDateTime now = LocalDateTime.now();
        int total = 0;
        // 三表均在单事务内执行,任一失败整体回滚(调用方按失败中止车队删除,两侧数据保持一致)
        // 显式 set null 清空车队归属(MyBatis-Plus 字段策略会忽略实体 null,需走 UpdateWrapper)
        total += vinManagementMapper.update(null, new LambdaUpdateWrapper<TVinManagement>()
                .in(TVinManagement::getFleetId, distinct)
                .set(TVinManagement::getFleetId, null)
                .set(TVinManagement::getUpdateTime, now));
        total += chargingCardMapper.update(null, new LambdaUpdateWrapper<TChargingCard>()
                .in(TChargingCard::getFleetId, distinct)
                .set(TChargingCard::getFleetId, null)
                .set(TChargingCard::getUpdateTime, now));
        // 白名单实体无 updateTime 映射字段(表有 update_time 列),沿用现有解绑实现走 setSql
        total += appUserWhitelistMapper.update(null, new LambdaUpdateWrapper<TAppUserWhitelist>()
                .in(TAppUserWhitelist::getFleetId, distinct)
                .set(TAppUserWhitelist::getFleetId, null)
                .setSql("update_time = now()"));
        return total;
    }
 
    private List<Integer> normalize(List<Integer> fleetIds) {
        if (fleetIds == null || fleetIds.isEmpty()) {
            return Collections.emptyList();
        }
        return fleetIds.stream().filter(Objects::nonNull).distinct().collect(Collectors.toList());
    }
}