puzhibing
8 小时以前 88580dae55106dcf4a8f80693b90638e07af02fa
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
package com.ruoyi.order.service.impl;
 
import com.ruoyi.account.api.feignClient.VinManagementClient;
import com.ruoyi.account.api.model.TVinManagement;
import com.ruoyi.chargingPile.api.feignClient.AccountingStrategyClient;
import com.ruoyi.chargingPile.api.feignClient.AccountingStrategyDetailClient;
import com.ruoyi.chargingPile.api.feignClient.ChargingGunClient;
import com.ruoyi.chargingPile.api.model.TAccountingStrategy;
import com.ruoyi.chargingPile.api.model.TAccountingStrategyDetail;
import com.ruoyi.chargingPile.api.model.TChargingGun;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.order.api.dto.DeviceStartChargingDTO;
import com.ruoyi.order.api.model.TVinAccountingStrategyDetailOrder;
import com.ruoyi.order.api.model.TVinAccountingStrategyOrder;
import com.ruoyi.order.api.model.TVinChargingOrder;
import com.ruoyi.order.api.vo.ChargingOrderVO;
import com.ruoyi.order.api.vo.DeviceStartChargingVO;
import com.ruoyi.order.mapper.TVinChargingOrderMapper;
import com.ruoyi.order.support.ChargeAccountSupport;
import com.ruoyi.order.service.TVinAccountingStrategyDetailOrderService;
import com.ruoyi.order.service.TVinAccountingStrategyOrderService;
import com.ruoyi.other.api.domain.TFleet;
import com.ruoyi.other.api.domain.TGroupCompany;
import com.ruoyi.other.api.feignClient.ChargeAuthorizationClient;
import com.ruoyi.other.api.feignClient.GroupCompanyClient;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.util.ReflectionTestUtils;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
/**
 * VIN码充电订单「所属公司/所属车队」单测:
 * 1) 建单时把 VIN 管理记录的 companyId/fleetId 作为快照写入订单;
 * 2) 列表/导出按订单快照 companyId/fleetId 批量远程取名,失败或缺失时保持为空且不抛异常。
 */
@ExtendWith(MockitoExtension.class)
class TVinChargingOrderCompanyFleetTest {
 
    private static final String VIN = "LFV3A23C8K3123456";
 
    @InjectMocks
    private TVinChargingOrderServiceImpl service;
 
    @Mock
    private TVinChargingOrderMapper mapper;
    @Mock
    private ChargingGunClient chargingGunClient;
    @Mock
    private VinManagementClient vinManagementClient;
    @Mock
    private GroupCompanyClient groupCompanyClient;
    @Mock
    private ChargeAuthorizationClient chargeAuthorizationClient;
    @Mock
    private ChargeAccountSupport chargeAccountSupport;
    @Mock
    private AccountingStrategyClient accountingStrategyClient;
    @Mock
    private AccountingStrategyDetailClient accountingStrategyDetailClient;
    @Mock
    private TVinAccountingStrategyOrderService vinAccountingStrategyOrderService;
    @Mock
    private TVinAccountingStrategyDetailOrderService vinAccountingStrategyDetailOrderService;
    @Mock
    private org.redisson.api.RedissonClient redissonClient;
    @Mock
    private org.redisson.api.RLock lock;
 
    private void fillCompanyFleetNames(List<ChargingOrderVO> list) {
        ReflectionTestUtils.invokeMethod(service, "fillCompanyFleetNames", list);
    }
 
    @Test
    void writesCompanyAndFleetSnapshotWhenBuildingOrder() throws Exception {
        ReflectionTestUtils.setField(service, "baseMapper", mapper);
        when(redissonClient.getLock(anyString())).thenReturn(lock);
        when(lock.tryLock(anyLong(), anyLong(), any(TimeUnit.class))).thenReturn(true);
        lenient().when(lock.isHeldByCurrentThread()).thenReturn(true);
        // 建单落库回填主键(策略快照按订单ID写入,严格模式下无ID会直接启动失败)
        when(mapper.insert(any(TVinChargingOrder.class))).thenAnswer(invocation -> {
            invocation.getArgument(0, TVinChargingOrder.class).setId(1001L);
            return 1;
        });
 
        TChargingGun gun = new TChargingGun();
        gun.setId(6);
        gun.setAccountingStrategyId(77);
        when(chargingGunClient.getChargingGunByCode(any())).thenReturn(R.ok(gun));
 
        // 严格模式:策略快照必须可查(枪策略优先),策略或明细缺失将终止启动
        TAccountingStrategy strategy = new TAccountingStrategy();
        strategy.setId(77);
        when(accountingStrategyClient.getAccountingStrategyById(77)).thenReturn(R.ok(strategy));
        TAccountingStrategyDetail detail = new TAccountingStrategyDetail();
        detail.setAccountingStrategyId(77);
        when(accountingStrategyDetailClient.getListByAccountingStrategyId(77))
                .thenReturn(R.ok(Collections.singletonList(detail)));
        when(vinAccountingStrategyOrderService.save(any(TVinAccountingStrategyOrder.class))).thenReturn(true);
        when(vinAccountingStrategyDetailOrderService.saveBatch(anyList())).thenReturn(true);
 
        TVinManagement vinRecord = new TVinManagement();
        vinRecord.setId(1L);
        vinRecord.setVin(VIN);
        vinRecord.setPlateNumber("川A123456");
        vinRecord.setStatus(1);
        vinRecord.setCompanyId(12);
        vinRecord.setFleetId(3);
        when(vinManagementClient.getVinByCode(VIN)).thenReturn(R.ok(vinRecord));
 
        // 共享账户模式企业:未配置充电授权按默认下发金额 200 放行,锁定企业额度后返回该余额
        TGroupCompany company = new TGroupCompany();
        company.setId(12);
        company.setName("甲公司");
        company.setStatus(1);
        company.setAccountMode(1);
        company.setAvailableBalance(new BigDecimal("500.00"));
        when(groupCompanyClient.getCompanyById(12)).thenReturn(R.ok(company));
        when(chargeAuthorizationClient.getDetailByCompanyId(12)).thenReturn(R.ok());
        when(groupCompanyClient.lockBalance(eq(12), any(BigDecimal.class)))
                .thenReturn(R.ok(new BigDecimal("300.00")));
 
        DeviceStartChargingDTO dto = new DeviceStartChargingDTO();
        dto.setChargingPileCode("P001");
        dto.setChargingGunCode("G001");
        dto.setAccount(new StringBuilder(VIN).reverse().toString());
 
        DeviceStartChargingVO vo = service.startVinCharging(dto);
 
        assertEquals(Integer.valueOf(1), vo.getAuthentication());
        assertEquals(0, new BigDecimal("200.00").compareTo(vo.getBalance()));
        ArgumentCaptor<TVinChargingOrder> captor = ArgumentCaptor.forClass(TVinChargingOrder.class);
        verify(mapper).insert(captor.capture());
        TVinChargingOrder saved = captor.getValue();
        assertEquals(Integer.valueOf(12), saved.getCompanyId());
        assertEquals(Integer.valueOf(3), saved.getFleetId());
        assertEquals(Long.valueOf(1L), saved.getVinId());
        assertEquals(VIN, saved.getActualVin());
        assertEquals(VIN, saved.getExpectedVin());
        assertEquals("川A123456", saved.getPlateNum());
    }
 
    @Test
    void fillsCompanyAndFleetNamesFromOrderSnapshot() {
        when(groupCompanyClient.listCompaniesByIds(anyList()))
                .thenReturn(R.ok(Collections.singletonList(company(12, "甲公司"))));
        when(groupCompanyClient.listFleetsByIds(anyList()))
                .thenReturn(R.ok(Collections.singletonList(fleet(3, "一队"))));
 
        ChargingOrderVO withFleet = order(12, 3);
        ChargingOrderVO companyOnly = order(12, null);
        ChargingOrderVO unmatched = order(99, 88);
        List<ChargingOrderVO> list = new ArrayList<>(Arrays.asList(withFleet, companyOnly, unmatched));
 
        fillCompanyFleetNames(list);
 
        assertEquals("甲公司", withFleet.getCompanyName());
        assertEquals("一队", withFleet.getFleetName());
        assertEquals("甲公司", companyOnly.getCompanyName());
        assertNull(companyOnly.getFleetName());
        assertNull(unmatched.getCompanyName());
        assertNull(unmatched.getFleetName());
 
        ArgumentCaptor<List<Integer>> companyIds = ArgumentCaptor.forClass(List.class);
        verify(groupCompanyClient).listCompaniesByIds(companyIds.capture());
        assertTrue(companyIds.getValue().containsAll(Arrays.asList(12, 99)));
        assertEquals(2, companyIds.getValue().size());
        ArgumentCaptor<List<Integer>> fleetIds = ArgumentCaptor.forClass(List.class);
        verify(groupCompanyClient).listFleetsByIds(fleetIds.capture());
        assertTrue(fleetIds.getValue().containsAll(Arrays.asList(3, 88)));
        assertEquals(2, fleetIds.getValue().size());
    }
 
    @Test
    void keepsNamesBlankWhenRemoteFails() {
        when(groupCompanyClient.listCompaniesByIds(anyList())).thenReturn(R.fail("公司服务调用失败"));
        when(groupCompanyClient.listFleetsByIds(anyList())).thenThrow(new RuntimeException("车队服务不可用"));
 
        ChargingOrderVO item = order(12, 3);
        List<ChargingOrderVO> list = new ArrayList<>(Collections.singletonList(item));
 
        fillCompanyFleetNames(list);
 
        assertNull(item.getCompanyName());
        assertNull(item.getFleetName());
    }
 
    @Test
    void skipsRemoteCallsWhenOrdersHaveNoOwnership() {
        ChargingOrderVO item = new ChargingOrderVO();
        item.setCode("VD202609280001");
        List<ChargingOrderVO> list = new ArrayList<>(Collections.singletonList(item));
 
        fillCompanyFleetNames(list);
 
        verify(groupCompanyClient, never()).listCompaniesByIds(anyList());
        verify(groupCompanyClient, never()).listFleetsByIds(anyList());
        assertNull(item.getCompanyName());
        assertNull(item.getFleetName());
    }
 
    private ChargingOrderVO order(Integer companyId, Integer fleetId) {
        ChargingOrderVO item = new ChargingOrderVO();
        item.setCode("VD202609280002");
        item.setCompanyId(companyId);
        item.setFleetId(fleetId);
        return item;
    }
 
    private TGroupCompany company(Integer id, String name) {
        TGroupCompany company = new TGroupCompany();
        company.setId(id);
        company.setName(name);
        return company;
    }
 
    private TFleet fleet(Integer id, String fleetName) {
        TFleet fleet = new TFleet();
        fleet.setId(id);
        fleet.setFleetName(fleetName);
        return fleet;
    }
}