package com.rj.service;

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

import com.rj.entity.AudioManagement;
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 AudioManagementServiceTest {

    @Autowired
    private IAudioManagementService audioManagementService;

    @Test
    void testSaveAudioManagement() {
        // 创建测试数据
        AudioManagement audioManagement = new AudioManagement();
        audioManagement.setRecordingName("测试录音");
        audioManagement.setRecordingTime(LocalDateTime.now());
        audioManagement.setSalesId("SALES001");
        audioManagement.setSalesName("张三");
        audioManagement.setDuration(new BigDecimal("15.5"));
        audioManagement.setCustomerId("CUST001");
        audioManagement.setCustomerName("李四");
        audioManagement.setCustomerPhone("13800138000");
        audioManagement.setUploadTime(LocalDateTime.now());
        audioManagement.setIntentionLevel("高意向");
        audioManagement.setDealershipId("DEAL001");
        audioManagement.setDealershipName("北京4S店");
        audioManagement.setProjectId("PROJ001");
        audioManagement.setProjectName("奥迪A6L项目");
        audioManagement.setScriptModel("标准话术");
        audioManagement.setUploadStatus("已上传");
        audioManagement.setServiceStatus("已同步");
        audioManagement.setIsMerged(false);
        audioManagement.setRemarks("测试录音备注");
        audioManagement.setCompanyType("4S店");
        audioManagement.setEditStatus("未编辑");
        audioManagement.setInfoCardType("意向车型");
        audioManagement.setInfoCarddescription("奥迪A6L 2.0T");
        audioManagement.setCreateTime(LocalDateTime.now());
        audioManagement.setUpdateTime(LocalDateTime.now());

        // 测试保存功能
        boolean result = audioManagementService.save(audioManagement);

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

    @Test
    void testGetAudioManagementById() {
        // 先保存一条测试数据
        AudioManagement audioManagement = new AudioManagement();
        audioManagement.setRecordingName("查询测试录音");
        audioManagement.setRecordingTime(LocalDateTime.now());
        audioManagement.setSalesId("SALES002");
        audioManagement.setSalesName("王五");
        audioManagement.setDuration(new BigDecimal("20.0"));
        audioManagement.setCustomerId("CUST002");
        audioManagement.setCustomerName("赵六");
        audioManagement.setCustomerPhone("13900139000");
        audioManagement.setUploadTime(LocalDateTime.now());
        audioManagement.setIntentionLevel("中意向");
        audioManagement.setDealershipId("DEAL002");
        audioManagement.setDealershipName("上海4S店");
        audioManagement.setProjectId("PROJ002");
        audioManagement.setProjectName("宝马5系项目");
        audioManagement.setScriptModel("高级话术");
        audioManagement.setUploadStatus("已上传");
        audioManagement.setServiceStatus("已同步");
        audioManagement.setIsMerged(false);
        audioManagement.setRemarks("查询测试录音备注");
        audioManagement.setCompanyType("4S店");
        audioManagement.setEditStatus("未编辑");
        audioManagement.setInfoCardType("购买情况");
        audioManagement.setInfoCarddescription("首次购买");
        audioManagement.setCreateTime(LocalDateTime.now());
        audioManagement.setUpdateTime(LocalDateTime.now());
        
        audioManagementService.save(audioManagement);

        // 测试根据ID查询功能
        AudioManagement foundAudio = audioManagementService.getById(audioManagement.getId());

        // 验证查询结果
        assertNotNull(foundAudio);
        assertEquals("查询测试录音", foundAudio.getRecordingName());
        assertEquals("王五", foundAudio.getSalesName());
        assertEquals("赵六", foundAudio.getCustomerName());
        assertEquals("13900139000", foundAudio.getCustomerPhone());
    }

    @Test
    void testGetAudioManagementByCustomerPhone() {
        // 先保存一条测试数据
        AudioManagement audioManagement = new AudioManagement();
        audioManagement.setRecordingName("手机号查询测试");
        audioManagement.setRecordingTime(LocalDateTime.now());
        audioManagement.setSalesId("SALES003");
        audioManagement.setSalesName("孙七");
        audioManagement.setDuration(new BigDecimal("25.5"));
        audioManagement.setCustomerId("CUST003");
        audioManagement.setCustomerName("周八");
        audioManagement.setCustomerPhone("13700137000");
        audioManagement.setUploadTime(LocalDateTime.now());
        audioManagement.setIntentionLevel("低意向");
        audioManagement.setDealershipId("DEAL003");
        audioManagement.setDealershipName("广州4S店");
        audioManagement.setProjectId("PROJ003");
        audioManagement.setProjectName("奔驰E级项目");
        audioManagement.setScriptModel("基础话术");
        audioManagement.setUploadStatus("已上传");
        audioManagement.setServiceStatus("已同步");
        audioManagement.setIsMerged(false);
        audioManagement.setRemarks("手机号查询测试备注");
        audioManagement.setCompanyType("4S店");
        audioManagement.setEditStatus("未编辑");
        audioManagement.setInfoCardType("来访目的");
        audioManagement.setInfoCarddescription("了解车型");
        audioManagement.setCreateTime(LocalDateTime.now());
        audioManagement.setUpdateTime(LocalDateTime.now());
        
        audioManagementService.save(audioManagement);

        // 测试根据客户手机号查询功能
        List<AudioManagement> audioList = audioManagementService.lambdaQuery()
                .eq(AudioManagement::getCustomerPhone, "13700137000")
                .list();

        // 验证查询结果
        assertNotNull(audioList);
        assertFalse(audioList.isEmpty());
        assertEquals("手机号查询测试", audioList.get(0).getRecordingName());
        assertEquals("13700137000", audioList.get(0).getCustomerPhone());
        assertEquals("周八", audioList.get(0).getCustomerName());
    }

    @Test
    void testGetAudioManagementByCustomerPhoneWithNoResults() {
        // 测试查询不存在的手机号
        List<AudioManagement> audioList = audioManagementService.lambdaQuery()
                .eq(AudioManagement::getCustomerPhone, "00000000000")
                .list();

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

    @Test
    void testUpdateAudioManagement() {
        // 先保存一条测试数据
        AudioManagement audioManagement = new AudioManagement();
        audioManagement.setRecordingName("更新测试录音");
        audioManagement.setRecordingTime(LocalDateTime.now());
        audioManagement.setSalesId("SALES004");
        audioManagement.setSalesName("吴九");
        audioManagement.setDuration(new BigDecimal("30.0"));
        audioManagement.setCustomerId("CUST004");
        audioManagement.setCustomerName("郑十");
        audioManagement.setCustomerPhone("13600136000");
        audioManagement.setUploadTime(LocalDateTime.now());
        audioManagement.setIntentionLevel("高意向");
        audioManagement.setDealershipId("DEAL004");
        audioManagement.setDealershipName("深圳4S店");
        audioManagement.setProjectId("PROJ004");
        audioManagement.setProjectName("雷克萨斯ES项目");
        audioManagement.setScriptModel("专业话术");
        audioManagement.setUploadStatus("已上传");
        audioManagement.setServiceStatus("已同步");
        audioManagement.setIsMerged(false);
        audioManagement.setRemarks("更新测试录音备注");
        audioManagement.setCompanyType("4S店");
        audioManagement.setEditStatus("未编辑");
        audioManagement.setInfoCardType("试驾结果");
        audioManagement.setInfoCarddescription("满意");
        audioManagement.setCreateTime(LocalDateTime.now());
        audioManagement.setUpdateTime(LocalDateTime.now());
        
        audioManagementService.save(audioManagement);

        // 更新录音信息
        audioManagement.setRemarks("已更新的备注信息");
        audioManagement.setIntentionLevel("中意向");
        audioManagement.setUpdateTime(LocalDateTime.now());

        // 测试更新功能
        boolean result = audioManagementService.updateById(audioManagement);

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

        // 重新查询验证更新结果
        AudioManagement updatedAudio = audioManagementService.getById(audioManagement.getId());
        assertEquals("已更新的备注信息", updatedAudio.getRemarks());
        assertEquals("中意向", updatedAudio.getIntentionLevel());
    }

    @Test
    void testDeleteAudioManagement() {
        // 先保存一条测试数据
        AudioManagement audioManagement = new AudioManagement();
        audioManagement.setRecordingName("删除测试录音");
        audioManagement.setRecordingTime(LocalDateTime.now());
        audioManagement.setSalesId("SALES005");
        audioManagement.setSalesName("钱十一");
        audioManagement.setDuration(new BigDecimal("18.5"));
        audioManagement.setCustomerId("CUST005");
        audioManagement.setCustomerName("孙十二");
        audioManagement.setCustomerPhone("13500135000");
        audioManagement.setUploadTime(LocalDateTime.now());
        audioManagement.setIntentionLevel("高意向");
        audioManagement.setDealershipId("DEAL005");
        audioManagement.setDealershipName("杭州4S店");
        audioManagement.setProjectId("PROJ005");
        audioManagement.setProjectName("沃尔沃S90项目");
        audioManagement.setScriptModel("标准话术");
        audioManagement.setUploadStatus("已上传");
        audioManagement.setServiceStatus("已同步");
        audioManagement.setIsMerged(false);
        audioManagement.setRemarks("删除测试录音备注");
        audioManagement.setCompanyType("4S店");
        audioManagement.setEditStatus("未编辑");
        audioManagement.setInfoCardType("付款方式");
        audioManagement.setInfoCarddescription("全款");
        audioManagement.setCreateTime(LocalDateTime.now());
        audioManagement.setUpdateTime(LocalDateTime.now());
        
        audioManagementService.save(audioManagement);
        String audioId = audioManagement.getId();

        // 测试删除功能
        boolean result = audioManagementService.removeById(audioId);

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

        // 验证数据已被删除
        AudioManagement deletedAudio = audioManagementService.getById(audioId);
        assertNull(deletedAudio);
    }

    @Test
    void testBatchInsertAudioManagement() {
        // 创建30条测试数据
        for (int i = 1; i <= 30; i++) {
            AudioManagement audioManagement = new AudioManagement();
            audioManagement.setRecordingName("批量测试录音_" + String.format("%02d", i));
            audioManagement.setRecordingTime(LocalDateTime.now().minusMinutes(i * 5));
            audioManagement.setSalesId("SALES" + String.format("%03d", i));
            audioManagement.setSalesName("销售员" + i);
            audioManagement.setDuration(new BigDecimal(String.valueOf(10 + i * 0.5)));
            audioManagement.setCustomerId("CUST" + String.format("%03d", i));
            audioManagement.setCustomerName("客户" + i);
            audioManagement.setCustomerPhone("13" + String.format("%08d", 80000000 + i));
            audioManagement.setUploadTime(LocalDateTime.now().minusHours(i));
            audioManagement.setIntentionLevel(getRandomIntentionLevel());
            audioManagement.setDealershipId("DEAL" + String.format("%03d", i));
            audioManagement.setDealershipName("4S店" + i);
            audioManagement.setProjectId("PROJ" + String.format("%03d", i));
            audioManagement.setProjectName("项目" + i);
            audioManagement.setScriptModel(getRandomScriptModel());
            audioManagement.setUploadStatus("已上传");
            audioManagement.setServiceStatus("已同步");
            audioManagement.setIsMerged(i % 3 == 0); // 每3条数据合并一次
            audioManagement.setRemarks("批量测试录音备注_" + i);
            audioManagement.setCompanyType("4S店");
            audioManagement.setEditStatus(i % 2 == 0 ? "已编辑" : "未编辑");
            audioManagement.setInfoCardType(getRandomInfoCardType());
            audioManagement.setInfoCarddescription(getRandomInfoCardDescription());
            audioManagement.setCreateTime(LocalDateTime.now().minusDays(i));
            audioManagement.setUpdateTime(LocalDateTime.now().minusHours(i));

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

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

    /**
     * 获取随机的意向级别
     */
    private String getRandomIntentionLevel() {
        String[] levels = {"高意向", "中意向", "低意向"};
        return levels[(int) (Math.random() * levels.length)];
    }

    /**
     * 获取随机的话术模型
     */
    private String getRandomScriptModel() {
        String[] models = {"标准话术", "高级话术", "基础话术", "专业话术", "定制话术"};
        return models[(int) (Math.random() * models.length)];
    }

    /**
     * 获取随机的信息卡类型
     */
    private String getRandomInfoCardType() {
        String[] types = {"意向车型", "购买情况", "来访目的", "试驾结果", "付款方式", "试驾专员"};
        return types[(int) (Math.random() * types.length)];
    }

    /**
     * 获取随机的信息卡描述
     */
    private String getRandomInfoCardDescription() {
        String[] descriptions = {
            "奥迪A6L 2.0T", "宝马5系 2.0T", "奔驰E级 2.0T", "雷克萨斯ES 2.0L",
            "首次购买", "置换购买", "增购", "了解车型", "试驾体验", "价格咨询",
            "满意", "一般", "不满意", "全款", "分期", "贷款", "张专员", "李专员", "王专员"
        };
        return descriptions[(int) (Math.random() * descriptions.length)];
    }
} 