package com.ruoyi.info.collection.service;

import com.ruoyi.info.collection.domain.CcdiEnterpriseBaseInfo;
import com.ruoyi.info.collection.domain.excel.CcdiEnterpriseBaseInfoExcel;
import com.ruoyi.info.collection.service.impl.CcdiEnterpriseBaseInfoImportServiceImpl;
import org.junit.jupiter.api.Test;

import java.util.HashSet;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

class CcdiEnterpriseBaseInfoImportServiceImplTest {

    private final CcdiEnterpriseBaseInfoImportServiceImpl service = new CcdiEnterpriseBaseInfoImportServiceImpl();

    @Test
    void validateAndBuildEntity_shouldRejectWhenDatabaseAlreadyContainsCreditCode() {
        CcdiEnterpriseBaseInfoExcel excel = buildExcel();

        RuntimeException exception = assertThrows(RuntimeException.class,
                () -> service.validateAndBuildEntity(excel, Set.of("91310000123456789A"), new HashSet<>(), "admin"));

        assertEquals("统一社会信用代码[91310000123456789A]已存在,请勿重复导入", exception.getMessage());
    }

    @Test
    void validateAndBuildEntity_shouldRejectWhenExcelContainsDuplicateCreditCode() {
        CcdiEnterpriseBaseInfoExcel excel = buildExcel();

        RuntimeException exception = assertThrows(RuntimeException.class,
                () -> service.validateAndBuildEntity(excel, Set.of(), new HashSet<>(Set.of("91310000123456789A")), "admin"));

        assertEquals("统一社会信用代码[91310000123456789A]在导入文件中重复,已跳过此条记录", exception.getMessage());
    }

    @Test
    void validateAndBuildEntity_shouldNormalizeEnumTextToCode() {
        CcdiEnterpriseBaseInfoExcel excel = buildExcel();
        excel.setRiskLevel("高风险");
        excel.setEntSource("一般企业");

        CcdiEnterpriseBaseInfo entity = service.validateAndBuildEntity(excel, Set.of(), new HashSet<>(), "admin");

        assertEquals("1", entity.getRiskLevel());
        assertEquals("GENERAL", entity.getEntSource());
        assertEquals("IMPORT", entity.getDataSource());
        assertEquals("admin", entity.getCreatedBy());
    }

    @Test
    void validateAndBuildEntity_shouldAllowBlankStatus() {
        CcdiEnterpriseBaseInfoExcel excel = buildExcel();
        excel.setStatus(null);

        CcdiEnterpriseBaseInfo entity = service.validateAndBuildEntity(excel, Set.of(), new HashSet<>(), "admin");

        assertNull(entity.getStatus());
        assertEquals("IMPORT", entity.getDataSource());
    }

    private CcdiEnterpriseBaseInfoExcel buildExcel() {
        CcdiEnterpriseBaseInfoExcel excel = new CcdiEnterpriseBaseInfoExcel();
        excel.setSocialCreditCode("91310000123456789A");
        excel.setEnterpriseName("测试企业");
        excel.setEnterpriseType("有限责任公司");
        excel.setEnterpriseNature("民营企业");
        excel.setIndustryClass("制造业");
        excel.setIndustryName("电子设备");
        excel.setStatus("存续");
        excel.setRiskLevel("1");
        excel.setEntSource("GENERAL");
        return excel;
    }
}
