package cn.iocoder.yudao.module.basic.service.dealerinfo;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;

import javax.annotation.Resource;

import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;

import cn.iocoder.yudao.module.basic.controller.admin.dealerinfo.vo.*;
import cn.iocoder.yudao.module.basic.dal.dataobject.dealerinfo.DealerInfoDO;
import cn.iocoder.yudao.module.basic.dal.mysql.dealerinfo.DealerInfoMapper;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import javax.annotation.Resource;
import org.springframework.context.annotation.Import;
import java.util.*;
import java.time.LocalDateTime;

import static cn.hutool.core.util.RandomUtil.*;
import static cn.iocoder.yudao.module.basic.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.*;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.*;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
* {@link DealerInfoServiceImpl} 的单元测试类
*
* @author 芋道源码
*/
@Import(DealerInfoServiceImpl.class)
public class DealerInfoServiceImplTest extends BaseDbUnitTest {

    @Resource
    private DealerInfoServiceImpl dealerInfoService;

    @Resource
    private DealerInfoMapper dealerInfoMapper;

    @Test
    public void testCreateDealerInfo_success() {
        // 准备参数
        DealerInfoCreateReqVO reqVO = randomPojo(DealerInfoCreateReqVO.class);

        // 调用
        Long dealerInfoId = dealerInfoService.createDealerInfo(reqVO);
        // 断言
        assertNotNull(dealerInfoId);
        // 校验记录的属性是否正确
        DealerInfoDO dealerInfo = dealerInfoMapper.selectById(dealerInfoId);
        assertPojoEquals(reqVO, dealerInfo);
    }

    @Test
    public void testUpdateDealerInfo_success() {
        // mock 数据
        DealerInfoDO dbDealerInfo = randomPojo(DealerInfoDO.class);
        dealerInfoMapper.insert(dbDealerInfo);// @Sql: 先插入出一条存在的数据
        // 准备参数
        DealerInfoUpdateReqVO reqVO = randomPojo(DealerInfoUpdateReqVO.class, o -> {
            o.setId(dbDealerInfo.getId()); // 设置更新的 ID
        });

        // 调用
        dealerInfoService.updateDealerInfo(reqVO);
        // 校验是否更新正确
        DealerInfoDO dealerInfo = dealerInfoMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, dealerInfo);
    }

    @Test
    public void testUpdateDealerInfo_notExists() {
        // 准备参数
        DealerInfoUpdateReqVO reqVO = randomPojo(DealerInfoUpdateReqVO.class);

        // 调用, 并断言异常
        assertServiceException(() -> dealerInfoService.updateDealerInfo(reqVO), DEALER_INFO_NOT_EXISTS);
    }

    @Test
    public void testDeleteDealerInfo_success() {
        // mock 数据
        DealerInfoDO dbDealerInfo = randomPojo(DealerInfoDO.class);
        dealerInfoMapper.insert(dbDealerInfo);// @Sql: 先插入出一条存在的数据
        // 准备参数
        Long id = dbDealerInfo.getId();

        // 调用
        dealerInfoService.deleteDealerInfo(id);
       // 校验数据不存在了
       assertNull(dealerInfoMapper.selectById(id));
    }

    @Test
    public void testDeleteDealerInfo_notExists() {
        // 准备参数
        Long id = randomLongId();

        // 调用, 并断言异常
        assertServiceException(() -> dealerInfoService.deleteDealerInfo(id), DEALER_INFO_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetDealerInfoPage() {
       // mock 数据
       DealerInfoDO dbDealerInfo = randomPojo(DealerInfoDO.class, o -> { // 等会查询到
           o.setDealerName(null);
           o.setDealerId(null);
           o.setBelongBusinessId(null);
           o.setBelongBusinessName(null);
           o.setBusinessCode(null);
           o.setContactPhone(null);
           o.setContactName(null);
           o.setCommonSaleArea(null);
           o.setCommonSaleAreaId(null);
           o.setCreateTime(null);
           o.setRemarks(null);
       });
       dealerInfoMapper.insert(dbDealerInfo);
       // 测试 dealerName 不匹配
       dealerInfoMapper.insert(cloneIgnoreId(dbDealerInfo, o -> o.setDealerName(null)));
       // 测试 dealerId 不匹配
       dealerInfoMapper.insert(cloneIgnoreId(dbDealerInfo, o -> o.setDealerId(null)));
       // 测试 belongBusinessId 不匹配
       dealerInfoMapper.insert(cloneIgnoreId(dbDealerInfo, o -> o.setBelongBusinessId(null)));
       // 测试 belongBusinessName 不匹配
       dealerInfoMapper.insert(cloneIgnoreId(dbDealerInfo, o -> o.setBelongBusinessName(null)));
       // 测试 businessCode 不匹配
       dealerInfoMapper.insert(cloneIgnoreId(dbDealerInfo, o -> o.setBusinessCode(null)));
       // 测试 contactPhone 不匹配
       dealerInfoMapper.insert(cloneIgnoreId(dbDealerInfo, o -> o.setContactPhone(null)));
       // 测试 contactName 不匹配
       dealerInfoMapper.insert(cloneIgnoreId(dbDealerInfo, o -> o.setContactName(null)));
       // 测试 commonSaleArea 不匹配
       dealerInfoMapper.insert(cloneIgnoreId(dbDealerInfo, o -> o.setCommonSaleArea(null)));
       // 测试 commonSaleAreaId 不匹配
       dealerInfoMapper.insert(cloneIgnoreId(dbDealerInfo, o -> o.setCommonSaleAreaId(null)));
       // 测试 createTime 不匹配
       dealerInfoMapper.insert(cloneIgnoreId(dbDealerInfo, o -> o.setCreateTime(null)));
       // 测试 remarks 不匹配
       dealerInfoMapper.insert(cloneIgnoreId(dbDealerInfo, o -> o.setRemarks(null)));
       // 准备参数
       DealerInfoPageReqVO reqVO = new DealerInfoPageReqVO();
       reqVO.setDealerName(null);
       reqVO.setDealerId(null);
       reqVO.setBelongBusinessId(null);
       reqVO.setBelongBusinessName(null);
       reqVO.setBusinessCode(null);
       reqVO.setContactPhone(null);
       reqVO.setContactName(null);
       reqVO.setCommonSaleArea(null);
       reqVO.setCommonSaleAreaId(null);
//       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setRemarks(null);

       // 调用
       PageResult<DealerInfoDO> pageResult = dealerInfoService.getDealerInfoPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbDealerInfo, pageResult.getList().get(0));
    }

}
