package com.rj.service;

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

import com.rj.entity.ProjectManagement;
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 ProjectManagementServiceTest {

    @Autowired
    private IProjectManagementService projectManagementService;

    @Test
    void testSaveProjectManagement() {
        // 创建测试数据
        ProjectManagement projectManagement = new ProjectManagement();
        projectManagement.setDealershipId("DEAL001");
        projectManagement.setProjectName("奥迪A6L项目");
        projectManagement.setScriptVersion(1);
        projectManagement.setScriptPublishTime(LocalDateTime.now().minusDays(7));
        projectManagement.setSalespersonCount(15);
        projectManagement.setAvgRecordingDuration(new BigDecimal("18.5"));
        projectManagement.setStep("进行中");
        projectManagement.setGroupName("奥迪品牌组");
        projectManagement.setCreateTime(LocalDateTime.now());
        projectManagement.setDescription("奥迪A6L车型销售项目，包含话术培训和录音管理");
        projectManagement.setStatus(true);
        projectManagement.setUpdateTime(LocalDateTime.now());

        // 测试保存功能
        boolean result = projectManagementService.save(projectManagement);

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

    @Test
    void testGetProjectManagementById() {
        // 先保存一条测试数据
        ProjectManagement projectManagement = new ProjectManagement();
        projectManagement.setDealershipId("DEAL002");
        projectManagement.setProjectName("宝马5系项目");
        projectManagement.setScriptVersion(2);
        projectManagement.setScriptPublishTime(LocalDateTime.now().minusDays(5));
        projectManagement.setSalespersonCount(20);
        projectManagement.setAvgRecordingDuration(new BigDecimal("22.0"));
        projectManagement.setStep("已完成");
        projectManagement.setGroupName("宝马品牌组");
        projectManagement.setCreateTime(LocalDateTime.now());
        projectManagement.setDescription("宝马5系车型销售项目，已完成话术培训");
        projectManagement.setStatus(true);
        projectManagement.setUpdateTime(LocalDateTime.now());
        
        projectManagementService.save(projectManagement);

        // 测试根据ID查询功能
        ProjectManagement foundProject = projectManagementService.getById(projectManagement.getId());

        // 验证查询结果
        assertNotNull(foundProject);
        assertEquals("宝马5系项目", foundProject.getProjectName());
        assertEquals(2, foundProject.getScriptVersion());
        assertEquals(20, foundProject.getSalespersonCount());
        assertEquals("已完成", foundProject.getStep());
    }

    @Test
    void testGetProjectManagementByName() {
        // 先保存一条测试数据
        ProjectManagement projectManagement = new ProjectManagement();
        projectManagement.setDealershipId("DEAL003");
        projectManagement.setProjectName("奔驰E级项目");
        projectManagement.setScriptVersion(1);
        projectManagement.setScriptPublishTime(LocalDateTime.now().minusDays(3));
        projectManagement.setSalespersonCount(18);
        projectManagement.setAvgRecordingDuration(new BigDecimal("20.5"));
        projectManagement.setStep("进行中");
        projectManagement.setGroupName("奔驰品牌组");
        projectManagement.setCreateTime(LocalDateTime.now());
        projectManagement.setDescription("奔驰E级车型销售项目，正在进行中");
        projectManagement.setStatus(true);
        projectManagement.setUpdateTime(LocalDateTime.now());
        
        projectManagementService.save(projectManagement);

        // 测试根据项目名称查询功能
        List<ProjectManagement> projectList = projectManagementService.lambdaQuery()
                .eq(ProjectManagement::getProjectName, "奔驰E级项目")
                .list();

        // 验证查询结果
        assertNotNull(projectList);
        assertFalse(projectList.isEmpty());
        assertEquals("奔驰E级项目", projectList.get(0).getProjectName());
        assertEquals("DEAL003", projectList.get(0).getDealershipId());
        assertEquals("奔驰品牌组", projectList.get(0).getGroupName());
    }

    @Test
    void testGetProjectManagementByNameWithNoResults() {
        // 测试查询不存在的项目名称
        List<ProjectManagement> projectList = projectManagementService.lambdaQuery()
                .eq(ProjectManagement::getProjectName, "不存在的项目")
                .list();

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

    @Test
    void testUpdateProjectManagement() {
        // 先保存一条测试数据
        ProjectManagement projectManagement = new ProjectManagement();
        projectManagement.setDealershipId("DEAL004");
        projectManagement.setProjectName("雷克萨斯ES项目");
        projectManagement.setScriptVersion(1);
        projectManagement.setScriptPublishTime(LocalDateTime.now().minusDays(2));
        projectManagement.setSalespersonCount(12);
        projectManagement.setAvgRecordingDuration(new BigDecimal("16.5"));
        projectManagement.setStep("准备中");
        projectManagement.setGroupName("雷克萨斯品牌组");
        projectManagement.setCreateTime(LocalDateTime.now());
        projectManagement.setDescription("雷克萨斯ES车型销售项目，正在准备中");
        projectManagement.setStatus(true);
        projectManagement.setUpdateTime(LocalDateTime.now());
        
        projectManagementService.save(projectManagement);

        // 更新项目信息
        projectManagement.setScriptVersion(2);
        projectManagement.setSalespersonCount(15);
        projectManagement.setStep("进行中");
        projectManagement.setDescription("雷克萨斯ES车型销售项目，已开始进行");
        projectManagement.setUpdateTime(LocalDateTime.now());

        // 测试更新功能
        boolean result = projectManagementService.updateById(projectManagement);

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

        // 重新查询验证更新结果
        ProjectManagement updatedProject = projectManagementService.getById(projectManagement.getId());
        assertEquals(2, updatedProject.getScriptVersion());
        assertEquals(15, updatedProject.getSalespersonCount());
        assertEquals("进行中", updatedProject.getStep());
        assertEquals("雷克萨斯ES车型销售项目，已开始进行", updatedProject.getDescription());
    }

    @Test
    void testDeleteProjectManagement() {
        // 先保存一条测试数据
        ProjectManagement projectManagement = new ProjectManagement();
        projectManagement.setDealershipId("DEAL005");
        projectManagement.setProjectName("沃尔沃S90项目");
        projectManagement.setScriptVersion(1);
        projectManagement.setScriptPublishTime(LocalDateTime.now().minusDays(1));
        projectManagement.setSalespersonCount(10);
        projectManagement.setAvgRecordingDuration(new BigDecimal("15.0"));
        projectManagement.setStep("已完成");
        projectManagement.setGroupName("沃尔沃品牌组");
        projectManagement.setCreateTime(LocalDateTime.now());
        projectManagement.setDescription("沃尔沃S90车型销售项目，已完成");
        projectManagement.setStatus(false);
        projectManagement.setUpdateTime(LocalDateTime.now());
        
        projectManagementService.save(projectManagement);
        String projectId = projectManagement.getId();

        // 测试删除功能
        boolean result = projectManagementService.removeById(projectId);

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

        // 验证数据已被删除
        ProjectManagement deletedProject = projectManagementService.getById(projectId);
        assertNull(deletedProject);
    }

    @Test
    void testBatchInsertProjectManagement() {
        // 创建30条测试数据
        for (int i = 1; i <= 30; i++) {
            ProjectManagement projectManagement = new ProjectManagement();
            projectManagement.setDealershipId("DEAL" + String.format("%03d", i));
            projectManagement.setProjectName("项目" + i);
            projectManagement.setScriptVersion(1 + (i % 5)); // 1-5版本
            projectManagement.setScriptPublishTime(LocalDateTime.now().minusDays(i));
            projectManagement.setSalespersonCount(8 + (i % 15)); // 8-22个销售人员
            projectManagement.setAvgRecordingDuration(new BigDecimal(String.valueOf(12 + i * 0.5)));
            projectManagement.setStep(getRandomStep());
            projectManagement.setGroupName(getRandomGroupName());
            projectManagement.setCreateTime(LocalDateTime.now().minusDays(i));
            projectManagement.setDescription("项目" + i + "的描述信息，包含详细的项目说明");
            projectManagement.setStatus(i % 4 != 0); // 每4个中停用1个
            projectManagement.setUpdateTime(LocalDateTime.now().minusHours(i));

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

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

    /**
     * 获取随机的项目步骤
     */
    private String getRandomStep() {
        String[] steps = {"准备中", "进行中", "已完成", "暂停中", "已取消"};
        return steps[(int) (Math.random() * steps.length)];
    }

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