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

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.vendorinfo.vo.*;
import cn.iocoder.yudao.module.basic.dal.dataobject.vendorinfo.VendorInfoDO;
import cn.iocoder.yudao.module.basic.dal.mysql.vendorinfo.VendorInfoMapper;
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 VendorInfoServiceImpl} 的单元测试类
*
* @author 芋道源码
*/
@Import(VendorInfoServiceImpl.class)
public class VendorInfoServiceImplTest extends BaseDbUnitTest {

    @Resource
    private VendorInfoServiceImpl vendorInfoService;

    @Resource
    private VendorInfoMapper vendorInfoMapper;

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

        // 调用
        Long vendorInfoId = vendorInfoService.createVendorInfo(reqVO);
        // 断言
        assertNotNull(vendorInfoId);
        // 校验记录的属性是否正确
        VendorInfoDO vendorInfo = vendorInfoMapper.selectById(vendorInfoId);
        assertPojoEquals(reqVO, vendorInfo);
    }

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

        // 调用
        vendorInfoService.updateVendorInfo(reqVO);
        // 校验是否更新正确
        VendorInfoDO vendorInfo = vendorInfoMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, vendorInfo);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> vendorInfoService.updateVendorInfo(reqVO), VENDOR_INFO_NOT_EXISTS);
    }

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

        // 调用
        vendorInfoService.deleteVendorInfo(id);
       // 校验数据不存在了
       assertNull(vendorInfoMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> vendorInfoService.deleteVendorInfo(id), VENDOR_INFO_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetVendorInfoPage() {
       // mock 数据
       VendorInfoDO dbVendorInfo = randomPojo(VendorInfoDO.class, o -> { // 等会查询到
           o.setVendorName(null);
           o.setVendorId(null);
           o.setVendorType(null);
           o.setBelongBusinessId(null);
           o.setBelongBusinessName(null);
           o.setRemarks(null);
           o.setBusinessCode(null);
           o.setContactPhone(null);
           o.setContactName(null);
           o.setArea(null);
           o.setCreateTime(null);
           o.setRealName(null);
           o.setIdCard(null);
       });
       vendorInfoMapper.insert(dbVendorInfo);
       // 测试 vendorName 不匹配
       vendorInfoMapper.insert(cloneIgnoreId(dbVendorInfo, o -> o.setVendorName(null)));
       // 测试 vendorId 不匹配
       vendorInfoMapper.insert(cloneIgnoreId(dbVendorInfo, o -> o.setVendorId(null)));
       // 测试 vendorType 不匹配
       vendorInfoMapper.insert(cloneIgnoreId(dbVendorInfo, o -> o.setVendorType(null)));
       // 测试 belongBusinessId 不匹配
       vendorInfoMapper.insert(cloneIgnoreId(dbVendorInfo, o -> o.setBelongBusinessId(null)));
       // 测试 belongBusinessName 不匹配
       vendorInfoMapper.insert(cloneIgnoreId(dbVendorInfo, o -> o.setBelongBusinessName(null)));
       // 测试 remarks 不匹配
       vendorInfoMapper.insert(cloneIgnoreId(dbVendorInfo, o -> o.setRemarks(null)));
       // 测试 businessCode 不匹配
       vendorInfoMapper.insert(cloneIgnoreId(dbVendorInfo, o -> o.setBusinessCode(null)));
       // 测试 contactPhone 不匹配
       vendorInfoMapper.insert(cloneIgnoreId(dbVendorInfo, o -> o.setContactPhone(null)));
       // 测试 contactName 不匹配
       vendorInfoMapper.insert(cloneIgnoreId(dbVendorInfo, o -> o.setContactName(null)));
       // 测试 area 不匹配
       vendorInfoMapper.insert(cloneIgnoreId(dbVendorInfo, o -> o.setArea(null)));
       // 测试 createTime 不匹配
       vendorInfoMapper.insert(cloneIgnoreId(dbVendorInfo, o -> o.setCreateTime(null)));
       // 测试 realName 不匹配
       vendorInfoMapper.insert(cloneIgnoreId(dbVendorInfo, o -> o.setRealName(null)));
       // 测试  idCard 不匹配
       vendorInfoMapper.insert(cloneIgnoreId(dbVendorInfo, o -> o.setIdCard(null)));
       // 准备参数
       VendorInfoPageReqVO reqVO = new VendorInfoPageReqVO();
       reqVO.setVendorName(null);
       reqVO.setVendorId(null);
       reqVO.setVendorType(null);
       reqVO.setBelongBusinessId(null);
       reqVO.setBelongBusinessName(null);
       reqVO.setRemarks(null);
       reqVO.setBusinessCode(null);
       reqVO.setContactPhone(null);
       reqVO.setContactName(null);
       reqVO.setArea(null);
       //reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setRealName(null);
       reqVO.setIdCard(null);

       // 调用
       PageResult<VendorInfoDO> pageResult = vendorInfoService.getVendorInfoPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbVendorInfo, pageResult.getList().get(0));
    }

}
