package com.rj.service;

/**
 * Author: 李中华  wx: spllzh  email(qq): 28668817@qq.com
 * Date: 2025/8/5 19:02
 **/

import com.rj.entity.Dealership;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

//@SpringBootTest(classes = AISmartCard20250803Application.class)
class DealershipServiceTest {

    @Autowired
    private IDealershipService dealershipService;

    @Test
    void testSaveDealership() {
        // 创建测试数据
        Dealership dealership = new Dealership();
        dealership.setDealershipName("北京奥迪4S店");
        dealership.setDealershipCode("BJ_AUDI_001");
        dealership.setRemainingHours(new BigDecimal("120.5"));
        dealership.setSalesCount(15);
        dealership.setAvgRecordings(25);
        dealership.setAvgRecordingDuration(new BigDecimal("18.5"));
        dealership.setRegion("华北");
        dealership.setGroupName("奥迪品牌组");
        dealership.setLanguage("中文");
        dealership.setDealershipType("4S店");
        dealership.setStatus(true);
        dealership.setCreateTime(LocalDateTime.now());
        dealership.setUpdateTime(LocalDateTime.now());

        // 测试保存功能
        boolean result = dealershipService.save(dealership);

        // 验证保存成功
        assertTrue(result);
        assertNotNull(dealership.getId());
    }

    @Test
    void testGetDealershipById() {
        // 先保存一条测试数据
        Dealership dealership = new Dealership();
        dealership.setDealershipName("上海宝马4S店");
        dealership.setDealershipCode("SH_BMW_001");
        dealership.setRemainingHours(new BigDecimal("95.0"));
        dealership.setSalesCount(20);
        dealership.setAvgRecordings(30);
        dealership.setAvgRecordingDuration(new BigDecimal("22.0"));
        dealership.setRegion("华东");
        dealership.setGroupName("宝马品牌组");
        dealership.setLanguage("中文");
        dealership.setDealershipType("4S店");
        dealership.setStatus(true);
        dealership.setCreateTime(LocalDateTime.now());
        dealership.setUpdateTime(LocalDateTime.now());
        
        dealershipService.save(dealership);

        // 测试根据ID查询功能
        Dealership foundDealership = dealershipService.getById(dealership.getId());

        // 验证查询结果
        assertNotNull(foundDealership);
        assertEquals("上海宝马4S店", foundDealership.getDealershipName());
        assertEquals("SH_BMW_001", foundDealership.getDealershipCode());
        assertEquals(20, foundDealership.getSalesCount());
        assertEquals("华东", foundDealership.getRegion());
    }

    @Test
    void testGetDealershipByCode() {
        // 先保存一条测试数据
        Dealership dealership = new Dealership();
        dealership.setDealershipName("广州奔驰4S店");
        dealership.setDealershipCode("GZ_BENZ_001");
        dealership.setRemainingHours(new BigDecimal("150.0"));
        dealership.setSalesCount(18);
        dealership.setAvgRecordings(28);
        dealership.setAvgRecordingDuration(new BigDecimal("20.5"));
        dealership.setRegion("华南");
        dealership.setGroupName("奔驰品牌组");
        dealership.setLanguage("中文");
        dealership.setDealershipType("4S店");
        dealership.setStatus(true);
        dealership.setCreateTime(LocalDateTime.now());
        dealership.setUpdateTime(LocalDateTime.now());
        
        dealershipService.save(dealership);

        // 测试根据经销商代码查询功能
        List<Dealership> dealershipList = dealershipService.lambdaQuery()
                .eq(Dealership::getDealershipCode, "GZ_BENZ_001")
                .list();

        // 验证查询结果
        assertNotNull(dealershipList);
        assertFalse(dealershipList.isEmpty());
        assertEquals("广州奔驰4S店", dealershipList.get(0).getDealershipName());
        assertEquals("GZ_BENZ_001", dealershipList.get(0).getDealershipCode());
        assertEquals("华南", dealershipList.get(0).getRegion());
    }

    @Test
    void testGetDealershipByCodeWithNoResults() {
        // 测试查询不存在的经销商代码
        List<Dealership> dealershipList = dealershipService.lambdaQuery()
                .eq(Dealership::getDealershipCode, "NOT_EXIST_001")
                .list();

        // 验证查询结果为空
        assertNotNull(dealershipList);
        assertTrue(dealershipList.isEmpty());
    }

    @Test
    void testUpdateDealership() {
        // 先保存一条测试数据
        Dealership dealership = new Dealership();
        dealership.setDealershipName("深圳雷克萨斯4S店");
        dealership.setDealershipCode("SZ_LEXUS_001");
        dealership.setRemainingHours(new BigDecimal("80.0"));
        dealership.setSalesCount(12);
        dealership.setAvgRecordings(22);
        dealership.setAvgRecordingDuration(new BigDecimal("16.5"));
        dealership.setRegion("华南");
        dealership.setGroupName("雷克萨斯品牌组");
        dealership.setLanguage("中文");
        dealership.setDealershipType("4S店");
        dealership.setStatus(true);
        dealership.setCreateTime(LocalDateTime.now());
        dealership.setUpdateTime(LocalDateTime.now());
        
        dealershipService.save(dealership);

        // 更新经销商信息
        dealership.setSalesCount(15);
        dealership.setAvgRecordings(25);
        dealership.setRemainingHours(new BigDecimal("100.0"));
        dealership.setUpdateTime(LocalDateTime.now());

        // 测试更新功能
        boolean result = dealershipService.updateById(dealership);

        // 验证更新成功
        assertTrue(result);

        // 重新查询验证更新结果
        Dealership updatedDealership = dealershipService.getById(dealership.getId());
        assertEquals(15, updatedDealership.getSalesCount());
        assertEquals(25, updatedDealership.getAvgRecordings());
        assertEquals(new BigDecimal("100.0"), updatedDealership.getRemainingHours());
    }

    @Test
    void testDeleteDealership() {
        // 先保存一条测试数据
        Dealership dealership = new Dealership();
        dealership.setDealershipName("杭州沃尔沃4S店");
        dealership.setDealershipCode("HZ_VOLVO_001");
        dealership.setRemainingHours(new BigDecimal("60.0"));
        dealership.setSalesCount(10);
        dealership.setAvgRecordings(18);
        dealership.setAvgRecordingDuration(new BigDecimal("15.0"));
        dealership.setRegion("华东");
        dealership.setGroupName("沃尔沃品牌组");
        dealership.setLanguage("中文");
        dealership.setDealershipType("4S店");
        dealership.setStatus(true);
        dealership.setCreateTime(LocalDateTime.now());
        dealership.setUpdateTime(LocalDateTime.now());
        
        dealershipService.save(dealership);
        String dealershipId = dealership.getId();

        // 测试删除功能
        boolean result = dealershipService.removeById(dealershipId);

        // 验证删除成功
        assertTrue(result);

        // 验证数据已被删除
        Dealership deletedDealership = dealershipService.getById(dealershipId);
        assertNull(deletedDealership);
    }

    @Test
    void testBatchInsertDealership() {
        // 创建30条测试数据
        for (int i = 1; i <= 30; i++) {
            Dealership dealership = new Dealership();
            dealership.setDealershipName("4S店" + i);
            dealership.setDealershipCode("DEAL" + String.format("%03d", i));
            dealership.setRemainingHours(new BigDecimal(String.valueOf(50 + i * 2.5)));
            dealership.setSalesCount(8 + (i % 15)); // 8-22个销售人员
            dealership.setAvgRecordings(15 + (i % 20)); // 15-34条录音
            dealership.setAvgRecordingDuration(new BigDecimal(String.valueOf(12 + i * 0.3)));
            dealership.setRegion(getRandomRegion());
            dealership.setGroupName(getRandomGroupName());
            dealership.setLanguage("中文");
            dealership.setDealershipType("4S店");
            dealership.setStatus(i % 5 != 0); // 每5个中停用1个
            dealership.setCreateTime(LocalDateTime.now().minusDays(i));
            dealership.setUpdateTime(LocalDateTime.now().minusHours(i));

            // 保存数据
            boolean result = dealershipService.save(dealership);
            assertTrue(result, "第" + i + "条数据保存失败");
            assertNotNull(dealership.getId(), "第" + i + "条数据ID为空");
        }

        // 验证总共插入了30条数据
        long count = dealershipService.count();
        assertTrue(count >= 30, "插入的数据数量不足30条，实际数量：" + count);
        
        System.out.println("成功插入30条测试数据，当前数据库总记录数：" + count);
    }

    /**
     * 获取随机的地区
     */
    private String getRandomRegion() {
        String[] regions = {"华北", "华东", "华南", "华中", "西南", "西北", "东北"};
        return regions[(int) (Math.random() * regions.length)];
    }

    /**
     * 获取随机的品牌组
     */
    private String getRandomGroupName() {
        String[] groups = {
            "奥迪品牌组", "宝马品牌组", "奔驰品牌组", "雷克萨斯品牌组", "沃尔沃品牌组",
            "凯迪拉克品牌组", "英菲尼迪品牌组", "讴歌品牌组", "捷豹品牌组", "路虎品牌组",
            "保时捷品牌组", "玛莎拉蒂品牌组", "法拉利品牌组", "兰博基尼品牌组"
        };
        return groups[(int) (Math.random() * groups.length)];
    }
} 