package com.rj.service;

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

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

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

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

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

    @Autowired
    private ICustomerManagementService customerManagementService;

    @Test
    void testSaveCustomerManagement() {
        // 创建测试数据
        CustomerManagement customerManagement = new CustomerManagement();
        customerManagement.setCustomerName("张三");
        customerManagement.setContact("13800138000");
        customerManagement.setDealershipId("DEAL001");
        customerManagement.setDealershipName("北京4S店");
        customerManagement.setSalesId("SALES001");
        customerManagement.setSalesName("李四");
        customerManagement.setRecordingCount(5);
        customerManagement.setStage("奥迪A6L 2.0T");
        customerManagement.setInfoCard("高意向客户，有购买意向");
        customerManagement.setCreateTime(LocalDateTime.now());
        customerManagement.setUpdateTime(LocalDateTime.now());

        // 测试保存功能
        boolean result = customerManagementService.save(customerManagement);

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

    @Test
    void testGetCustomerManagementById() {
        // 先保存一条测试数据
        CustomerManagement customerManagement = new CustomerManagement();
        customerManagement.setCustomerName("王五");
        customerManagement.setContact("13900139000");
        customerManagement.setDealershipId("DEAL002");
        customerManagement.setDealershipName("上海4S店");
        customerManagement.setSalesId("SALES002");
        customerManagement.setSalesName("赵六");
        customerManagement.setRecordingCount(3);
        customerManagement.setStage("宝马5系 2.0T");
        customerManagement.setInfoCard("中意向客户，需要跟进");
        customerManagement.setCreateTime(LocalDateTime.now());
        customerManagement.setUpdateTime(LocalDateTime.now());
        
        customerManagementService.save(customerManagement);

        // 测试根据ID查询功能
        CustomerManagement foundCustomer = customerManagementService.getById(customerManagement.getId());

        // 验证查询结果
        assertNotNull(foundCustomer);
        assertEquals("王五", foundCustomer.getCustomerName());
        assertEquals("13900139000", foundCustomer.getContact());
        assertEquals("赵六", foundCustomer.getSalesName());
        assertEquals("宝马5系 2.0T", foundCustomer.getStage());
    }

    @Test
    void testGetCustomerManagementByContact() {
        // 先保存一条测试数据
        CustomerManagement customerManagement = new CustomerManagement();
        customerManagement.setCustomerName("孙七");
        customerManagement.setContact("13700137000");
        customerManagement.setDealershipId("DEAL003");
        customerManagement.setDealershipName("广州4S店");
        customerManagement.setSalesId("SALES003");
        customerManagement.setSalesName("周八");
        customerManagement.setRecordingCount(8);
        customerManagement.setStage("奔驰E级 2.0T");
        customerManagement.setInfoCard("低意向客户，需要更多沟通");
        customerManagement.setCreateTime(LocalDateTime.now());
        customerManagement.setUpdateTime(LocalDateTime.now());
        
        customerManagementService.save(customerManagement);

        // 测试根据联系方式查询功能
        List<CustomerManagement> customerList = customerManagementService.lambdaQuery()
                .eq(CustomerManagement::getContact, "13700137000")
                .list();

        // 验证查询结果
        assertNotNull(customerList);
        assertFalse(customerList.isEmpty());
        assertEquals("孙七", customerList.get(0).getCustomerName());
        assertEquals("13700137000", customerList.get(0).getContact());
        assertEquals("周八", customerList.get(0).getSalesName());
    }

    @Test
    void testGetCustomerManagementByContactWithNoResults() {
        // 测试查询不存在的联系方式
        List<CustomerManagement> customerList = customerManagementService.lambdaQuery()
                .eq(CustomerManagement::getContact, "00000000000")
                .list();

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

    @Test
    void testUpdateCustomerManagement() {
        // 先保存一条测试数据
        CustomerManagement customerManagement = new CustomerManagement();
        customerManagement.setCustomerName("吴九");
        customerManagement.setContact("13600136000");
        customerManagement.setDealershipId("DEAL004");
        customerManagement.setDealershipName("深圳4S店");
        customerManagement.setSalesId("SALES004");
        customerManagement.setSalesName("郑十");
        customerManagement.setRecordingCount(12);
        customerManagement.setStage("雷克萨斯ES 2.0L");
        customerManagement.setInfoCard("高意向客户，准备购买");
        customerManagement.setCreateTime(LocalDateTime.now());
        customerManagement.setUpdateTime(LocalDateTime.now());
        
        customerManagementService.save(customerManagement);

        // 更新客户信息
        customerManagement.setRecordingCount(15);
        customerManagement.setStage("雷克萨斯ES 2.5L");
        customerManagement.setInfoCard("高意向客户，已确定购买");
        customerManagement.setUpdateTime(LocalDateTime.now());

        // 测试更新功能
        boolean result = customerManagementService.updateById(customerManagement);

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

        // 重新查询验证更新结果
        CustomerManagement updatedCustomer = customerManagementService.getById(customerManagement.getId());
        assertEquals(15, updatedCustomer.getRecordingCount());
        assertEquals("雷克萨斯ES 2.5L", updatedCustomer.getStage());
        assertEquals("高意向客户，已确定购买", updatedCustomer.getInfoCard());
    }

    @Test
    void testDeleteCustomerManagement() {
        // 先保存一条测试数据
        CustomerManagement customerManagement = new CustomerManagement();
        customerManagement.setCustomerName("钱十一");
        customerManagement.setContact("13500135000");
        customerManagement.setDealershipId("DEAL005");
        customerManagement.setDealershipName("杭州4S店");
        customerManagement.setSalesId("SALES005");
        customerManagement.setSalesName("孙十二");
        customerManagement.setRecordingCount(6);
        customerManagement.setStage("沃尔沃S90 2.0T");
        customerManagement.setInfoCard("中意向客户，需要试驾");
        customerManagement.setCreateTime(LocalDateTime.now());
        customerManagement.setUpdateTime(LocalDateTime.now());
        
        customerManagementService.save(customerManagement);
        String customerId = customerManagement.getId();

        // 测试删除功能
        boolean result = customerManagementService.removeById(customerId);

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

        // 验证数据已被删除
        CustomerManagement deletedCustomer = customerManagementService.getById(customerId);
        assertNull(deletedCustomer);
    }

    @Test
    void testBatchInsertCustomerManagement() {
        // 创建30条测试数据
        for (int i = 1; i <= 30; i++) {
            CustomerManagement customerManagement = new CustomerManagement();
            customerManagement.setCustomerName("客户" + i);
            customerManagement.setContact("13" + String.format("%08d", 80000000 + i));
            customerManagement.setDealershipId("DEAL" + String.format("%03d", i));
            customerManagement.setDealershipName("4S店" + i);
            customerManagement.setSalesId("SALES" + String.format("%03d", i));
            customerManagement.setSalesName("销售员" + i);
            customerManagement.setRecordingCount(1 + (i % 10)); // 1-10条录音
            customerManagement.setStage(getRandomStage());
            customerManagement.setInfoCard(getRandomInfoCard());
            customerManagement.setCreateTime(LocalDateTime.now().minusDays(i));
            customerManagement.setUpdateTime(LocalDateTime.now().minusHours(i));

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

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

    /**
     * 获取随机的阶段（测试占位字符串）
     */
    private String getRandomStage() {
        String[] models = {
            "奥迪A6L 2.0T", "宝马5系 2.0T", "奔驰E级 2.0T", "雷克萨斯ES 2.0L",
            "沃尔沃S90 2.0T", "凯迪拉克CT6 2.0T", "英菲尼迪Q70L 2.0T", "讴歌TLX 2.0T",
            "捷豹XF 2.0T", "路虎揽胜 3.0T", "保时捷Panamera 2.9T", "玛莎拉蒂Ghibli 3.0T"
        };
        return models[(int) (Math.random() * models.length)];
    }

    /**
     * 获取随机的信息卡内容
     */
    private String getRandomInfoCard() {
        String[] infoCards = {
            "高意向客户，有购买意向", "中意向客户，需要跟进", "低意向客户，需要更多沟通",
            "高意向客户，准备购买", "中意向客户，需要试驾", "低意向客户，价格敏感",
            "高意向客户，已确定购买", "中意向客户，在比较中", "低意向客户，观望中",
            "高意向客户，等待提车", "中意向客户，需要优惠", "低意向客户，需要教育"
        };
        return infoCards[(int) (Math.random() * infoCards.length)];
    }
} 