package com.rj.service;

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

import com.rj.entity.VideoManagement;
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 VideoManagementServiceTest {

    @Autowired
    private IVideoManagementService videoManagementService;

    @Test
    void testSaveVideoManagement() {
        // 创建测试数据
        VideoManagement videoManagement = new VideoManagement();
        videoManagement.setVideoName("奥迪A6L试驾视频");
        videoManagement.setDealershipId("DEAL001");
        videoManagement.setDealershipName("北京奥迪4S店");
        videoManagement.setSalesId("SALES001");
        videoManagement.setSalesName("张三");
        videoManagement.setCustomerId("CUST001");
        videoManagement.setCustomerName("李四");
        videoManagement.setCustomerPhone("13800138000");
        videoManagement.setDuration(new BigDecimal("15.5"));
        videoManagement.setRecordTime(LocalDateTime.now().minusHours(2));
        videoManagement.setCreateTime(LocalDateTime.now());
        videoManagement.setUpdateTime(LocalDateTime.now());

        // 测试保存功能
        boolean result = videoManagementService.save(videoManagement);

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

    @Test
    void testGetVideoManagementById() {
        // 先保存一条测试数据
        VideoManagement videoManagement = new VideoManagement();
        videoManagement.setVideoName("宝马5系试驾视频");
        videoManagement.setDealershipId("DEAL002");
        videoManagement.setDealershipName("上海宝马4S店");
        videoManagement.setSalesId("SALES002");
        videoManagement.setSalesName("王五");
        videoManagement.setCustomerId("CUST002");
        videoManagement.setCustomerName("赵六");
        videoManagement.setCustomerPhone("13900139000");
        videoManagement.setDuration(new BigDecimal("20.0"));
        videoManagement.setRecordTime(LocalDateTime.now().minusHours(3));
        videoManagement.setCreateTime(LocalDateTime.now());
        videoManagement.setUpdateTime(LocalDateTime.now());
        
        videoManagementService.save(videoManagement);

        // 测试根据ID查询功能
        VideoManagement foundVideo = videoManagementService.getById(videoManagement.getId());

        // 验证查询结果
        assertNotNull(foundVideo);
        assertEquals("宝马5系试驾视频", foundVideo.getVideoName());
        assertEquals("上海宝马4S店", foundVideo.getDealershipName());
        assertEquals("王五", foundVideo.getSalesName());
        assertEquals("赵六", foundVideo.getCustomerName());
    }

    @Test
    void testGetVideoManagementByCustomerPhone() {
        // 先保存一条测试数据
        VideoManagement videoManagement = new VideoManagement();
        videoManagement.setVideoName("奔驰E级试驾视频");
        videoManagement.setDealershipId("DEAL003");
        videoManagement.setDealershipName("广州奔驰4S店");
        videoManagement.setSalesId("SALES003");
        videoManagement.setSalesName("孙七");
        videoManagement.setCustomerId("CUST003");
        videoManagement.setCustomerName("周八");
        videoManagement.setCustomerPhone("13700137000");
        videoManagement.setDuration(new BigDecimal("18.5"));
        videoManagement.setRecordTime(LocalDateTime.now().minusHours(4));
        videoManagement.setCreateTime(LocalDateTime.now());
        videoManagement.setUpdateTime(LocalDateTime.now());
        
        videoManagementService.save(videoManagement);

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

        // 验证查询结果
        assertNotNull(videoList);
        assertFalse(videoList.isEmpty());
        assertEquals("奔驰E级试驾视频", videoList.get(0).getVideoName());
        assertEquals("13700137000", videoList.get(0).getCustomerPhone());
        assertEquals("周八", videoList.get(0).getCustomerName());
    }

    @Test
    void testGetVideoManagementByCustomerPhoneWithNoResults() {
        // 测试查询不存在的客户手机号
        List<VideoManagement> videoList = videoManagementService.lambdaQuery()
                .eq(VideoManagement::getCustomerPhone, "00000000000")
                .list();

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

    @Test
    void testUpdateVideoManagement() {
        // 先保存一条测试数据
        VideoManagement videoManagement = new VideoManagement();
        videoManagement.setVideoName("雷克萨斯ES试驾视频");
        videoManagement.setDealershipId("DEAL004");
        videoManagement.setDealershipName("深圳雷克萨斯4S店");
        videoManagement.setSalesId("SALES004");
        videoManagement.setSalesName("吴九");
        videoManagement.setCustomerId("CUST004");
        videoManagement.setCustomerName("郑十");
        videoManagement.setCustomerPhone("13600136000");
        videoManagement.setDuration(new BigDecimal("25.0"));
        videoManagement.setRecordTime(LocalDateTime.now().minusHours(5));
        videoManagement.setCreateTime(LocalDateTime.now());
        videoManagement.setUpdateTime(LocalDateTime.now());
        
        videoManagementService.save(videoManagement);

        // 更新视频信息
        videoManagement.setVideoName("雷克萨斯ES详细试驾视频");
        videoManagement.setDuration(new BigDecimal("30.0"));
        videoManagement.setUpdateTime(LocalDateTime.now());

        // 测试更新功能
        boolean result = videoManagementService.updateById(videoManagement);

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

        // 重新查询验证更新结果
        VideoManagement updatedVideo = videoManagementService.getById(videoManagement.getId());
        assertEquals("雷克萨斯ES详细试驾视频", updatedVideo.getVideoName());
        assertEquals(new BigDecimal("30.0"), updatedVideo.getDuration());
    }

    @Test
    void testDeleteVideoManagement() {
        // 先保存一条测试数据
        VideoManagement videoManagement = new VideoManagement();
        videoManagement.setVideoName("沃尔沃S90试驾视频");
        videoManagement.setDealershipId("DEAL005");
        videoManagement.setDealershipName("杭州沃尔沃4S店");
        videoManagement.setSalesId("SALES005");
        videoManagement.setSalesName("钱十一");
        videoManagement.setCustomerId("CUST005");
        videoManagement.setCustomerName("孙十二");
        videoManagement.setCustomerPhone("13500135000");
        videoManagement.setDuration(new BigDecimal("22.5"));
        videoManagement.setRecordTime(LocalDateTime.now().minusHours(6));
        videoManagement.setCreateTime(LocalDateTime.now());
        videoManagement.setUpdateTime(LocalDateTime.now());
        
        videoManagementService.save(videoManagement);
        String videoId = videoManagement.getId();

        // 测试删除功能
        boolean result = videoManagementService.removeById(videoId);

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

        // 验证数据已被删除
        VideoManagement deletedVideo = videoManagementService.getById(videoId);
        assertNull(deletedVideo);
    }

    @Test
    void testBatchInsertVideoManagement() {
        // 创建30条测试数据
        for (int i = 1; i <= 30; i++) {
            VideoManagement videoManagement = new VideoManagement();
            videoManagement.setVideoName("试驾视频" + i);
            videoManagement.setDealershipId("DEAL" + String.format("%03d", i));
            videoManagement.setDealershipName("4S店" + i%5);
            videoManagement.setSalesId("SALES" + String.format("%03d", i%5));
            videoManagement.setSalesName("销售员" + i%5);
            videoManagement.setCustomerId("CUST" + String.format("%03d", i%5));
            videoManagement.setCustomerName("客户" + i%5);
            videoManagement.setCustomerPhone("13" + String.format("%08d", 80000000 + i));
            videoManagement.setDuration(new BigDecimal(String.valueOf(10 + i * 0.5)));
            videoManagement.setRecordTime(LocalDateTime.now().minusHours(i));
            videoManagement.setCreateTime(LocalDateTime.now().minusDays(i));
            videoManagement.setUpdateTime(LocalDateTime.now().minusHours(i));

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

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