//package com.njcn.gather.aireport.testreport.component;
//
//import com.njcn.common.pojo.exception.BusinessException;
//import com.njcn.gather.aireport.clientunit.mapper.ClientUnitMapper;
//import com.njcn.gather.aireport.clientunit.pojo.po.ClientUnitPO;
//import com.njcn.gather.aireport.testdevice.mapper.TestDeviceMapper;
//import com.njcn.gather.aireport.testdevice.pojo.constant.TestDeviceConst;
//import com.njcn.gather.aireport.testdevice.pojo.po.TestDevicePO;
//import com.njcn.gather.aireport.testreport.mapper.TestReportGroupReportMapper;
//import com.njcn.gather.aireport.testreport.mapper.TestReportMapper;
//import com.njcn.gather.aireport.testreport.mapper.TestReportPointMapper;
//import com.njcn.gather.aireport.testreport.pojo.vo.TestReportLedgerValidateResultVO;
//import org.junit.jupiter.api.Assertions;
//import org.junit.jupiter.api.Test;
//import org.springframework.mock.web.MockMultipartFile;
//import org.springframework.web.multipart.MultipartFile;
//
//import java.util.Collections;
//import java.util.HashMap;
//import java.lang.reflect.InvocationTargetException;
//import java.lang.reflect.Method;
//import java.util.Map;
//
//import static org.mockito.Mockito.mock;
//
//class TestReportLedgerImportServiceTest {
//
//    @Test
//    void normalizeFileMapShouldRejectEmptyFiles() {
//        TestReportLedgerImportService service = createService();
//
//        BusinessException exception = Assertions.assertThrows(BusinessException.class,
//                () -> invokeNormalizeFileMap(service, new MultipartFile[0]));
//
//        Assertions.assertEquals("选择文件阶段：导入文件不能为空", exception.getMessage());
//    }
//
//    @Test
//    void resolveDeviceStateShouldSupportCodesAndNames() {
//        TestReportLedgerImportService service = createService();
//
//        Assertions.assertEquals(TestDeviceConst.STATE_RUNNING, invokeResolveDeviceState(service, "01", 2));
//        Assertions.assertEquals(TestDeviceConst.STATE_RUNNING, invokeResolveDeviceState(service, "在运", 2));
//        Assertions.assertEquals(TestDeviceConst.STATE_RETIRED, invokeResolveDeviceState(service, "02", 3));
//        Assertions.assertEquals(TestDeviceConst.STATE_RETIRED, invokeResolveDeviceState(service, "退运", 3));
//    }
//
//    @Test
//    void resolveDeviceStateShouldRejectUnexpectedState() {
//        TestReportLedgerImportService service = createService();
//
//        BusinessException exception = Assertions.assertThrows(BusinessException.class,
//                () -> invokeResolveDeviceState(service, "停用", 4));
//
//        Assertions.assertEquals("基础信息维护阶段：检测设备sheet第4行设备状态不正确", exception.getMessage());
//    }
//
//    @Test
//    void normalizeFileMapShouldSkipEmptyFileItemsAndKeepTrimmedNames() {
//        TestReportLedgerImportService service = createService();
//        MockMultipartFile validFile = new MockMultipartFile("files", " point-a.xlsx ", "application/octet-stream",
//                new byte[]{1});
//        MockMultipartFile emptyFile = new MockMultipartFile("files", "empty.xlsx", "application/octet-stream",
//                new byte[0]);
//
//        Map<String, MultipartFile> result = invokeNormalizeFileMap(service, new MultipartFile[]{emptyFile, validFile});
//
//        Assertions.assertEquals(1, result.size());
//        Assertions.assertSame(validFile, result.get("point-a.xlsx"));
//    }
//
//    @Test
//    void fillPointValidationResultShouldAppendReferenceErrorsToPointResult() {
//        TestReportLedgerImportService service = createService();
//        TestReportLedgerExcelParser.ParsedPointRow pointRow = new TestReportLedgerExcelParser.ParsedPointRow();
//        pointRow.setRowNo(2);
//        pointRow.setSubstationName("变电站A");
//        pointRow.setPointName("监测点A");
//        pointRow.setTestDeviceNo("DEV-001");
//        pointRow.setClientUnitName("委托单位A");
//        pointRow.setExcelAttachmentName("point-a.xlsx");
//        pointRow.setWordAttachmentName("point-a.docx");
//
//        TestReportLedgerExcelParser.ParsedLedger ledger = new TestReportLedgerExcelParser.ParsedLedger();
//        ledger.setPoints(Collections.singletonList(pointRow));
//
//        TestReportLedgerValidateResultVO result = new TestReportLedgerValidateResultVO();
//
//        invokeFillPointValidationResult(service, result, ledger, Collections.singleton("台账信息汇总.xlsx"),
//                new HashMap<String, TestDevicePO>(), new HashMap<String, ClientUnitPO>());
//
//        Assertions.assertEquals(1, result.getPoints().size());
//        Assertions.assertFalse(Boolean.TRUE.equals(result.getPoints().get(0).getComplete()));
//        Assertions.assertFalse(Boolean.TRUE.equals(result.getPoints().get(0).getTestDeviceNoValid()));
//        Assertions.assertEquals("第2行检测设备编号在数据库和检测设备sheet中都不存在",
//                result.getPoints().get(0).getTestDeviceNoMessage());
//        Assertions.assertFalse(Boolean.TRUE.equals(result.getPoints().get(0).getClientUnitNameValid()));
//        Assertions.assertEquals("第2行委托单位名称在数据库和委托单位sheet中都不存在",
//                result.getPoints().get(0).getClientUnitNameMessage());
//    }
//
//    private TestReportLedgerImportService createService() {
//        return new TestReportLedgerImportService(
//                mock(TestReportMapper.class),
//                mock(TestReportPointMapper.class),
//                mock(TestReportGroupReportMapper.class),
//                mock(TestDeviceMapper.class),
//                mock(ClientUnitMapper.class),
//                mock(TestReportLedgerExcelParser.class),
//                mock(TestReportStorageService.class));
//    }
//
//    @SuppressWarnings("unchecked")
//    private Map<String, MultipartFile> invokeNormalizeFileMap(TestReportLedgerImportService service, MultipartFile[] files) {
//        return invokePrivateMethod(service, "normalizeFileMap", new Class[]{MultipartFile[].class}, new Object[]{files});
//    }
//
//    private String invokeResolveDeviceState(TestReportLedgerImportService service, String stateName, Integer rowNo) {
//        return (String) invokePrivateMethod(service, "resolveDeviceState",
//                new Class[]{String.class, Integer.class}, new Object[]{stateName, rowNo});
//    }
//
//    private void invokeFillPointValidationResult(TestReportLedgerImportService service,
//                                                 TestReportLedgerValidateResultVO result,
//                                                 TestReportLedgerExcelParser.ParsedLedger ledger,
//                                                 java.util.Set<String> sessionFileNameSet,
//                                                 Map<String, TestDevicePO> dbDeviceMap,
//                                                 Map<String, ClientUnitPO> dbClientMap) {
//        invokePrivateMethod(service, "fillPointValidationResult",
//                new Class[]{TestReportLedgerValidateResultVO.class, TestReportLedgerExcelParser.ParsedLedger.class,
//                        java.util.Set.class, Map.class, Map.class},
//                new Object[]{result, ledger, sessionFileNameSet, dbDeviceMap, dbClientMap});
//    }
//
//    private Object invokePrivateMethod(TestReportLedgerImportService service, String methodName,
//                                       Class<?>[] parameterTypes, Object[] args) {
//        try {
//            Method method = TestReportLedgerImportService.class.getDeclaredMethod(methodName, parameterTypes);
//            method.setAccessible(true);
//            return method.invoke(service, args);
//        } catch (InvocationTargetException exception) {
//            Throwable targetException = exception.getTargetException();
//            if (targetException instanceof RuntimeException) {
//                throw (RuntimeException) targetException;
//            }
//            throw new RuntimeException(targetException);
//        } catch (Exception exception) {
//            throw new RuntimeException(exception);
//        }
//    }
//}
