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

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

    @Resource
    private GoodsVerifyServiceImpl goodsVerifyService;

    @Resource
    private GoodsVerifyMapper goodsVerifyMapper;

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

        // 调用
        Long goodsVerifyId = goodsVerifyService.createGoodsVerify(reqVO);
        // 断言
        assertNotNull(goodsVerifyId);
        // 校验记录的属性是否正确
        GoodsVerifyDO goodsVerify = goodsVerifyMapper.selectById(goodsVerifyId);
        assertPojoEquals(reqVO, goodsVerify);
    }

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

        // 调用
        goodsVerifyService.updateGoodsVerify(reqVO);
        // 校验是否更新正确
        GoodsVerifyDO goodsVerify = goodsVerifyMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, goodsVerify);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> goodsVerifyService.updateGoodsVerify(reqVO), GOODS_VERIFY_NOT_EXISTS);
    }

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

        // 调用
        goodsVerifyService.deleteGoodsVerify(id);
       // 校验数据不存在了
       assertNull(goodsVerifyMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> goodsVerifyService.deleteGoodsVerify(id), GOODS_VERIFY_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetGoodsVerifyPage() {
       // mock 数据
       GoodsVerifyDO dbGoodsVerify = randomPojo(GoodsVerifyDO.class, o -> { // 等会查询到
           o.setGoodsName(null);
           o.setGoodsId(null);
           o.setVerifyName(null);
           o.setVerifyAgencies(null);
           o.setVerifyStartTime(null);
           o.setVerifyEndTime(null);
           o.setVerifyDataUrl(null);
           o.setVerifyIsForver(null);
           o.setCreateTime(null);
           o.setBelongBusinessId(null);
           o.setBelongBusinessName(null);
       });
       goodsVerifyMapper.insert(dbGoodsVerify);
       // 测试 goodsName 不匹配
       goodsVerifyMapper.insert(cloneIgnoreId(dbGoodsVerify, o -> o.setGoodsName(null)));
       // 测试 goodsId 不匹配
       goodsVerifyMapper.insert(cloneIgnoreId(dbGoodsVerify, o -> o.setGoodsId(null)));
       // 测试 verifyName 不匹配
       goodsVerifyMapper.insert(cloneIgnoreId(dbGoodsVerify, o -> o.setVerifyName(null)));
       // 测试 verifyAgencies 不匹配
       goodsVerifyMapper.insert(cloneIgnoreId(dbGoodsVerify, o -> o.setVerifyAgencies(null)));
       // 测试 verifyStartTime 不匹配
       goodsVerifyMapper.insert(cloneIgnoreId(dbGoodsVerify, o -> o.setVerifyStartTime(null)));
       // 测试 verifyEndTime 不匹配
       goodsVerifyMapper.insert(cloneIgnoreId(dbGoodsVerify, o -> o.setVerifyEndTime(null)));
       // 测试 verifyDataUrl 不匹配
       goodsVerifyMapper.insert(cloneIgnoreId(dbGoodsVerify, o -> o.setVerifyDataUrl(null)));
       // 测试 verifyIsForver 不匹配
       goodsVerifyMapper.insert(cloneIgnoreId(dbGoodsVerify, o -> o.setVerifyIsForver(null)));
       // 测试 createTime 不匹配
       goodsVerifyMapper.insert(cloneIgnoreId(dbGoodsVerify, o -> o.setCreateTime(null)));
       // 测试 belongBusinessId 不匹配
       goodsVerifyMapper.insert(cloneIgnoreId(dbGoodsVerify, o -> o.setBelongBusinessId(null)));
       // 测试 belongBusinessName 不匹配
       goodsVerifyMapper.insert(cloneIgnoreId(dbGoodsVerify, o -> o.setBelongBusinessName(null)));
       // 准备参数
       GoodsVerifyPageReqVO reqVO = new GoodsVerifyPageReqVO();
       reqVO.setGoodsName(null);
       reqVO.setGoodsId(null);
       reqVO.setVerifyName(null);
       reqVO.setVerifyAgencies(null);
//       reqVO.setVerifyStartTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
//       reqVO.setVerifyEndTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setVerifyDataUrl(null);
       reqVO.setVerifyIsForver(null);
//       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setBelongBusinessId(null);
       reqVO.setBelongBusinessName(null);

       // 调用
       PageResult<GoodsVerifyDO> pageResult = goodsVerifyService.getGoodsVerifyPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbGoodsVerify, pageResult.getList().get(0));
    }

}
