package cn.iocoder.yudao.module.identity.service.generatebatch;

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.identity.controller.admin.generatebatch.vo.*;
import cn.iocoder.yudao.module.identity.dal.dataobject.generatebatch.GenerateBatchDO;
import cn.iocoder.yudao.module.identity.dal.mysql.generatebatch.GenerateBatchMapper;
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.identity.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 GenerateBatchServiceImpl} 的单元测试类
*
* @author 芋道源码
*/
@Import(GenerateBatchServiceImpl.class)
public class GenerateBatchServiceImplTest extends BaseDbUnitTest {

    @Resource
    private GenerateBatchServiceImpl generateBatchService;

    @Resource
    private GenerateBatchMapper generateBatchMapper;

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

        // 调用
        Long generateBatchId = generateBatchService.createGenerateBatch(reqVO);
        // 断言
        assertNotNull(generateBatchId);
        // 校验记录的属性是否正确
        GenerateBatchDO generateBatch = generateBatchMapper.selectById(generateBatchId);
        assertPojoEquals(reqVO, generateBatch);
    }

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

        // 调用
        generateBatchService.updateGenerateBatch(reqVO);
        // 校验是否更新正确
        GenerateBatchDO generateBatch = generateBatchMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, generateBatch);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> generateBatchService.updateGenerateBatch(reqVO), GENERATE_BATCH_NOT_EXISTS);
    }

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

        // 调用
        generateBatchService.deleteGenerateBatch(id);
       // 校验数据不存在了
       assertNull(generateBatchMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> generateBatchService.deleteGenerateBatch(id), GENERATE_BATCH_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetGenerateBatchPage() {
       // mock 数据
       GenerateBatchDO dbGenerateBatch = randomPojo(GenerateBatchDO.class, o -> { // 等会查询到
           o.setGenerateRecordId(null);
           o.setBatchId(null);
           o.setNum(null);
           o.setBatchType(null);
           o.setBelongBusinessId(null);
           o.setBelongBusinessName(null);
           o.setCreateTime(null);
       });
       generateBatchMapper.insert(dbGenerateBatch);
       // 测试 generateRecordId 不匹配
       generateBatchMapper.insert(cloneIgnoreId(dbGenerateBatch, o -> o.setGenerateRecordId(null)));
       // 测试 batchId 不匹配
       generateBatchMapper.insert(cloneIgnoreId(dbGenerateBatch, o -> o.setBatchId(null)));
       // 测试 num 不匹配
       generateBatchMapper.insert(cloneIgnoreId(dbGenerateBatch, o -> o.setNum(null)));
       // 测试 batchType 不匹配
       generateBatchMapper.insert(cloneIgnoreId(dbGenerateBatch, o -> o.setBatchType(null)));
       // 测试 belongBusinessId 不匹配
       generateBatchMapper.insert(cloneIgnoreId(dbGenerateBatch, o -> o.setBelongBusinessId(null)));
       // 测试 belongBusinessName 不匹配
       generateBatchMapper.insert(cloneIgnoreId(dbGenerateBatch, o -> o.setBelongBusinessName(null)));
       // 测试 createTime 不匹配
       generateBatchMapper.insert(cloneIgnoreId(dbGenerateBatch, o -> o.setCreateTime(null)));
       // 准备参数
       GenerateBatchPageReqVO reqVO = new GenerateBatchPageReqVO();
       reqVO.setGenerateRecordId(null);
       reqVO.setBatchId(null);
       reqVO.setNum(null);
       reqVO.setBatchType(null);
       reqVO.setBelongBusinessId(null);
       reqVO.setBelongBusinessName(null);
       reqVO.setCreateTime(null);

       // 调用
       PageResult<GenerateBatchDO> pageResult = generateBatchService.getGenerateBatchPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbGenerateBatch, pageResult.getList().get(0));
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetGenerateBatchList() {
       // mock 数据
       GenerateBatchDO dbGenerateBatch = randomPojo(GenerateBatchDO.class, o -> { // 等会查询到
           o.setGenerateRecordId(null);
           o.setBatchId(null);
           o.setNum(null);
           o.setBatchType(null);
           o.setBelongBusinessId(null);
           o.setBelongBusinessName(null);
           o.setCreateTime(null);
       });
       generateBatchMapper.insert(dbGenerateBatch);
       // 测试 generateRecordId 不匹配
       generateBatchMapper.insert(cloneIgnoreId(dbGenerateBatch, o -> o.setGenerateRecordId(null)));
       // 测试 batchId 不匹配
       generateBatchMapper.insert(cloneIgnoreId(dbGenerateBatch, o -> o.setBatchId(null)));
       // 测试 num 不匹配
       generateBatchMapper.insert(cloneIgnoreId(dbGenerateBatch, o -> o.setNum(null)));
       // 测试 batchType 不匹配
       generateBatchMapper.insert(cloneIgnoreId(dbGenerateBatch, o -> o.setBatchType(null)));
       // 测试 belongBusinessId 不匹配
       generateBatchMapper.insert(cloneIgnoreId(dbGenerateBatch, o -> o.setBelongBusinessId(null)));
       // 测试 belongBusinessName 不匹配
       generateBatchMapper.insert(cloneIgnoreId(dbGenerateBatch, o -> o.setBelongBusinessName(null)));
       // 测试 createTime 不匹配
       generateBatchMapper.insert(cloneIgnoreId(dbGenerateBatch, o -> o.setCreateTime(null)));
       // 准备参数
       GenerateBatchExportReqVO reqVO = new GenerateBatchExportReqVO();
       reqVO.setGenerateRecordId(null);
       reqVO.setBatchId(null);
       reqVO.setNum(null);
       reqVO.setBatchType(null);
       reqVO.setBelongBusinessId(null);
       reqVO.setBelongBusinessName(null);
       reqVO.setCreateTime(null);

       // 调用
       List<GenerateBatchDO> list = generateBatchService.getGenerateBatchList(reqVO);
       // 断言
       assertEquals(1, list.size());
       assertPojoEquals(dbGenerateBatch, list.get(0));
    }

}
