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

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.harvestbatch.vo.HarvestBatchPageReqVO;
import cn.iocoder.yudao.module.product.dal.dataobject.harvestbatch.HarvestBatchDO;
import cn.iocoder.yudao.module.product.dal.mysql.harvestbatch.HarvestBatchMapper;
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_BATCH_NOT_EXISTS;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

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

    @Resource
    private HarvestBatchServiceImpl harvestBatchService;

    @Resource
    private HarvestBatchMapper harvestBatchMapper;

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

        // 调用
        harvestBatchService.deleteHarvestBatch(id);
       // 校验数据不存在了
       assertNull(harvestBatchMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> harvestBatchService.deleteHarvestBatch(id), HARVEST_BATCH_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetHarvestBatchPage() {
       // mock 数据
       HarvestBatchDO dbHarvestBatch = randomPojo(HarvestBatchDO.class, o -> { // 等会查询到
           o.setHarvestBatchId(null);
           o.setHarvestId(null);
           o.setHarvestDate(null);
           o.setGoodsName(null);
           o.setGoodsId(null);
           o.setGoodsNum(null);
           o.setBlockChainId(null);
           o.setBlockChainBlockIdentity(null);
           o.setBlockState(null);
           o.setTemplateName(null);
           o.setTemplateId(null);
           o.setRemarks(null);
           o.setCreateTime(null);
           o.setBelongBusinessId(null);
           o.setBelongBusinessName(null);
       });
       harvestBatchMapper.insert(dbHarvestBatch);
       // 测试 harvestBatchId 不匹配
       harvestBatchMapper.insert(cloneIgnoreId(dbHarvestBatch, o -> o.setHarvestBatchId(null)));
       // 测试 harvestId 不匹配
       harvestBatchMapper.insert(cloneIgnoreId(dbHarvestBatch, o -> o.setHarvestId(null)));
       // 测试 harvestDate 不匹配
       harvestBatchMapper.insert(cloneIgnoreId(dbHarvestBatch, o -> o.setHarvestDate(null)));
       // 测试 goodsName 不匹配
       harvestBatchMapper.insert(cloneIgnoreId(dbHarvestBatch, o -> o.setGoodsName(null)));
       // 测试 goodsId 不匹配
       harvestBatchMapper.insert(cloneIgnoreId(dbHarvestBatch, o -> o.setGoodsId(null)));
       // 测试 goodsNum 不匹配
       harvestBatchMapper.insert(cloneIgnoreId(dbHarvestBatch, o -> o.setGoodsNum(null)));
       // 测试 blockChainId 不匹配
       harvestBatchMapper.insert(cloneIgnoreId(dbHarvestBatch, o -> o.setBlockChainId(null)));
       // 测试 blockChainBlockIdentity 不匹配
       harvestBatchMapper.insert(cloneIgnoreId(dbHarvestBatch, o -> o.setBlockChainBlockIdentity(null)));
       // 测试 blockState 不匹配
       harvestBatchMapper.insert(cloneIgnoreId(dbHarvestBatch, o -> o.setBlockState(null)));
       // 测试 templateName 不匹配
       harvestBatchMapper.insert(cloneIgnoreId(dbHarvestBatch, o -> o.setTemplateName(null)));
       // 测试 templateId 不匹配
       harvestBatchMapper.insert(cloneIgnoreId(dbHarvestBatch, o -> o.setTemplateId(null)));
       // 测试 remarks 不匹配
       harvestBatchMapper.insert(cloneIgnoreId(dbHarvestBatch, o -> o.setRemarks(null)));
       // 测试 createTime 不匹配
       harvestBatchMapper.insert(cloneIgnoreId(dbHarvestBatch, o -> o.setCreateTime(null)));
       // 准备参数
       HarvestBatchPageReqVO reqVO = new HarvestBatchPageReqVO();
        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.setHarvestBatchId(null);
       reqVO.setHarvestId(null);
       reqVO.setHarvestDate(dates);
       reqVO.setGoodsName(null);

       // 调用
       PageResult<HarvestBatchDO> pageResult = harvestBatchService.getHarvestBatchPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbHarvestBatch, pageResult.getList().get(0));
    }

}
