package cn.iocoder.yudao.module.product.service.harvestinfo;

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.product.controller.admin.harvestinfo.vo.HarvestInfoCreateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.harvestinfo.vo.HarvestInfoExportReqVO;
import cn.iocoder.yudao.module.product.controller.admin.harvestinfo.vo.HarvestInfoPageReqVO;
import cn.iocoder.yudao.module.product.controller.admin.harvestinfo.vo.HarvestInfoUpdateReqVO;
import cn.iocoder.yudao.module.product.dal.dataobject.harvestinfo.HarvestInfoDO;
import cn.iocoder.yudao.module.product.dal.mysql.harvestinfo.HarvestInfoMapper;
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 java.util.List;

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.product.enums.ErrorCodeConstants.HARVEST_INFO_NOT_EXISTS;
import static org.junit.jupiter.api.Assertions.*;

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

    @Resource
    private HarvestInfoServiceImpl harvestInfoService;

    @Resource
    private HarvestInfoMapper harvestInfoMapper;

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

        // 调用
        Long harvestInfoId = harvestInfoService.createHarvestInfo(reqVO);
        // 断言
        assertNotNull(harvestInfoId);
        // 校验记录的属性是否正确
        HarvestInfoDO harvestInfo = harvestInfoMapper.selectById(harvestInfoId);
        assertPojoEquals(reqVO, harvestInfo);
    }

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

        // 调用
        harvestInfoService.updateHarvestInfo(reqVO);
        // 校验是否更新正确
        HarvestInfoDO harvestInfo = harvestInfoMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, harvestInfo);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> harvestInfoService.updateHarvestInfo(reqVO), HARVEST_INFO_NOT_EXISTS);
    }

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

        // 调用
        harvestInfoService.deleteHarvestInfo(id);
       // 校验数据不存在了
       assertNull(harvestInfoMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> harvestInfoService.deleteHarvestInfo(id), HARVEST_INFO_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetHarvestInfoPage() {
       // mock 数据
       HarvestInfoDO dbHarvestInfo = randomPojo(HarvestInfoDO.class, o -> { // 等会查询到
           o.setHarverstId(null);
           o.setHarverstTime(null);
           o.setCreateTime(null);
           o.setBlockChainBlockIdentity(null);
           o.setBlockState(null);
           o.setBlockChainId(null);
       });
       harvestInfoMapper.insert(dbHarvestInfo);
       // 测试 harverstId 不匹配
       harvestInfoMapper.insert(cloneIgnoreId(dbHarvestInfo, o -> o.setHarverstId(null)));
       // 测试 harverstTime 不匹配
       harvestInfoMapper.insert(cloneIgnoreId(dbHarvestInfo, o -> o.setHarverstTime(null)));
       // 测试 createTime 不匹配
       harvestInfoMapper.insert(cloneIgnoreId(dbHarvestInfo, o -> o.setCreateTime(null)));
       // 测试 blockChainBlockIdentity 不匹配
       harvestInfoMapper.insert(cloneIgnoreId(dbHarvestInfo, o -> o.setBlockChainBlockIdentity(null)));
       // 测试 blockState 不匹配
       harvestInfoMapper.insert(cloneIgnoreId(dbHarvestInfo, o -> o.setBlockState(null)));
       // 测试 blockChainId 不匹配
       harvestInfoMapper.insert(cloneIgnoreId(dbHarvestInfo, o -> o.setBlockChainId(null)));
       // 准备参数
       HarvestInfoPageReqVO reqVO = new HarvestInfoPageReqVO();
       reqVO.setHarverstId(null);
        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.setHarverstTime(dates);
       reqVO.setCreateTime(dates);
       reqVO.setBelongBusinessId(null);
       reqVO.setBelongBusinessName(null);
       reqVO.setBlockChainBlockIdentity(null);
       reqVO.setBlockState(null);
       reqVO.setBlockChainId(null);

       // 调用
       PageResult<HarvestInfoDO> pageResult = harvestInfoService.getHarvestInfoPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbHarvestInfo, pageResult.getList().get(0));
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetHarvestInfoList() {
       // mock 数据
       HarvestInfoDO dbHarvestInfo = randomPojo(HarvestInfoDO.class, o -> { // 等会查询到
           o.setHarverstId(null);
           o.setHarverstTime(null);
           o.setCreateTime(null);
           o.setBlockChainBlockIdentity(null);
           o.setBlockState(null);
           o.setBlockChainId(null);
       });
       harvestInfoMapper.insert(dbHarvestInfo);
       // 测试 harverstId 不匹配
       harvestInfoMapper.insert(cloneIgnoreId(dbHarvestInfo, o -> o.setHarverstId(null)));
       // 测试 harverstTime 不匹配
       harvestInfoMapper.insert(cloneIgnoreId(dbHarvestInfo, o -> o.setHarverstTime(null)));
       // 测试 createTime 不匹配
       harvestInfoMapper.insert(cloneIgnoreId(dbHarvestInfo, o -> o.setCreateTime(null)));
       // 测试 blockChainBlockIdentity 不匹配
       harvestInfoMapper.insert(cloneIgnoreId(dbHarvestInfo, o -> o.setBlockChainBlockIdentity(null)));
       // 测试 blockState 不匹配
       harvestInfoMapper.insert(cloneIgnoreId(dbHarvestInfo, o -> o.setBlockState(null)));
       // 测试 blockChainId 不匹配
       harvestInfoMapper.insert(cloneIgnoreId(dbHarvestInfo, o -> o.setBlockChainId(null)));
       // 准备参数
       HarvestInfoExportReqVO reqVO = new HarvestInfoExportReqVO();
       reqVO.setHarverstId(null);
        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.setHarverstTime(dates);
       reqVO.setCreateTime(dates);
       reqVO.setBelongBusinessId(null);
       reqVO.setBelongBusinessName(null);
       reqVO.setBlockChainBlockIdentity(null);
       reqVO.setBlockState(null);
       reqVO.setBlockChainId(null);

       // 调用
       List<HarvestInfoDO> list = harvestInfoService.getHarvestInfoList(reqVO);
       // 断言
       assertEquals(1, list.size());
       assertPojoEquals(dbHarvestInfo, list.get(0));
    }

}
