package cn.iocoder.yudao.module.chain.service.blockcertificate;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.chain.controller.admin.blockcertificate.vo.BlockCertificatePageReqVO;
import cn.iocoder.yudao.module.chain.controller.admin.blockcertificate.vo.BlockCertificateUpdateReqVO;
import cn.iocoder.yudao.module.chain.dal.dataobject.blockcertificate.BlockCertificateDO;
import cn.iocoder.yudao.module.chain.dal.mysql.blockcertificate.BlockCertificateMapper;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Import;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
import static cn.iocoder.yudao.module.chain.enums.ErrorCodeConstants.BLOCK_CERTIFICATE_NOT_EXISTS;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

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

    @Resource
    private BlockCertificateServiceImpl blockCertificateService;

    @Resource
    private BlockCertificateMapper blockCertificateMapper;

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

        // 调用
        blockCertificateService.updateBlockCertificate(reqVO);
        // 校验是否更新正确
        BlockCertificateDO blockCertificate = blockCertificateMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, blockCertificate);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> blockCertificateService.updateBlockCertificate(reqVO), BLOCK_CERTIFICATE_NOT_EXISTS);
    }

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

        // 调用
        blockCertificateService.deleteBlockCertificate(id);
       // 校验数据不存在了
       assertNull(blockCertificateMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> blockCertificateService.deleteBlockCertificate(id), BLOCK_CERTIFICATE_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetBlockCertificatePage() {
       // mock 数据
       BlockCertificateDO dbBlockCertificate = randomPojo(BlockCertificateDO.class, o -> { // 等会查询到
           o.setChainOperateId(null);
           o.setOperateType(null);
           o.setOperateName(null);
           o.setBatchId(null);
           o.setLinkId(null);
           o.setLinkName(null);
           o.setChainName(null);
           o.setChainBlock(null);
           o.setBlockTime(null);
           o.setChainBlockIdentity(null);
           o.setChainState(null);
           o.setChainStatus(null);
           o.setRemarks(null);
           o.setUploadUser(null);
           o.setUploadVerify(null);
           o.setUploadTime(null);
           o.setUploadLocation(null);
           o.setBelongBusinessId(null);
           o.setBelongBusinessName(null);
       });
       blockCertificateMapper.insert(dbBlockCertificate);
       // 测试 chainOperateId 不匹配
       blockCertificateMapper.insert(cloneIgnoreId(dbBlockCertificate, o -> o.setChainOperateId(null)));
       // 测试 operateType 不匹配
       blockCertificateMapper.insert(cloneIgnoreId(dbBlockCertificate, o -> o.setOperateType(null)));
       // 测试 operateName 不匹配
       blockCertificateMapper.insert(cloneIgnoreId(dbBlockCertificate, o -> o.setOperateName(null)));
       // 测试 batchId 不匹配
       blockCertificateMapper.insert(cloneIgnoreId(dbBlockCertificate, o -> o.setBatchId(null)));
       // 测试 linkId 不匹配
       blockCertificateMapper.insert(cloneIgnoreId(dbBlockCertificate, o -> o.setLinkId(null)));
       // 测试 linkName 不匹配
       blockCertificateMapper.insert(cloneIgnoreId(dbBlockCertificate, o -> o.setLinkName(null)));
       // 测试 chainName 不匹配
       blockCertificateMapper.insert(cloneIgnoreId(dbBlockCertificate, o -> o.setChainName(null)));
       // 测试 chainBlock 不匹配
       blockCertificateMapper.insert(cloneIgnoreId(dbBlockCertificate, o -> o.setChainBlock(null)));
       // 测试 blockTime 不匹配
       blockCertificateMapper.insert(cloneIgnoreId(dbBlockCertificate, o -> o.setBlockTime(null)));
       // 测试 chainBlockIdentity 不匹配
       blockCertificateMapper.insert(cloneIgnoreId(dbBlockCertificate, o -> o.setChainBlockIdentity(null)));
       // 测试 chainState 不匹配
       blockCertificateMapper.insert(cloneIgnoreId(dbBlockCertificate, o -> o.setChainState(null)));
       // 测试 chainStatus 不匹配
       blockCertificateMapper.insert(cloneIgnoreId(dbBlockCertificate, o -> o.setChainStatus(null)));
       // 测试 remarks 不匹配
       blockCertificateMapper.insert(cloneIgnoreId(dbBlockCertificate, o -> o.setRemarks(null)));
       // 测试 uploadUser 不匹配
       blockCertificateMapper.insert(cloneIgnoreId(dbBlockCertificate, o -> o.setUploadUser(null)));
       // 测试 uploadVerify 不匹配
       blockCertificateMapper.insert(cloneIgnoreId(dbBlockCertificate, o -> o.setUploadVerify(null)));
       // 测试 uploadTime 不匹配
       blockCertificateMapper.insert(cloneIgnoreId(dbBlockCertificate, o -> o.setUploadTime(null)));
       // 测试 uploadLocation 不匹配
       blockCertificateMapper.insert(cloneIgnoreId(dbBlockCertificate, o -> o.setUploadLocation(null)));
       // 测试 belongBusinessId 不匹配
       blockCertificateMapper.insert(cloneIgnoreId(dbBlockCertificate, o -> o.setBelongBusinessId(null)));
       // 测试 belongBusinessName 不匹配
       blockCertificateMapper.insert(cloneIgnoreId(dbBlockCertificate, o -> o.setBelongBusinessName(null)));
       // 准备参数
       BlockCertificatePageReqVO reqVO = new BlockCertificatePageReqVO();
        LocalDateTime[] localDateTimes = LocalDateTimeUtils.buildBetweenTime(2023, 2, 1, 2023, 2, 28);
        Date[] dates = new Date[localDateTimes.length];
        for(int i=0; i<localDateTimes.length; i++) {
            dates[i] = Date.from(localDateTimes[i].atZone(ZoneId.systemDefault()).toInstant());
        }
       reqVO.setChainOperateId(null);
       reqVO.setOperateType(null);
       reqVO.setOperateName(null);
       reqVO.setBatchId(null);
       reqVO.setLinkId(null);
       reqVO.setLinkName(null);
       reqVO.setChainName(null);
       reqVO.setChainBlock(null);
       reqVO.setBlockTime(dates);
       reqVO.setChainBlockIdentity(null);
       reqVO.setChainState(null);
       reqVO.setChainStatus(null);
       reqVO.setRemarks(null);
       reqVO.setUploadUser(null);
       reqVO.setUploadVerify(null);
       reqVO.setUploadTime(dates);
       reqVO.setUploadLocation(null);
       reqVO.setBelongBusinessId(null);
       reqVO.setBelongBusinessName(null);

       // 调用
       PageResult<BlockCertificateDO> pageResult = blockCertificateService.getBlockCertificatePage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbBlockCertificate, pageResult.getList().get(0));
    }

}
