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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.identity.controller.admin.generaterecord.vo.GenerateRecordExportReqVO;
import cn.iocoder.yudao.module.identity.controller.admin.generaterecord.vo.GenerateRecordPageReqVO;
import cn.iocoder.yudao.module.identity.controller.admin.generaterecord.vo.GenerateRecordRespVO;
import cn.iocoder.yudao.module.identity.controller.admin.generaterecord.vo.GenerateRecordUpdateReqVO;
import cn.iocoder.yudao.module.identity.dal.dataobject.generaterecord.GenerateRecordDO;
import cn.iocoder.yudao.module.identity.dal.mysql.generaterecord.GenerateRecordMapper;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Import;

import javax.annotation.Resource;
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.identity.enums.ErrorCodeConstants.GENERATE_RECORD_NOT_EXISTS;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

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

    @Resource
    private GenerateRecordServiceImpl generateRecordService;

    @Resource
    private GenerateRecordMapper generateRecordMapper;

    @Test
    public void testCreateGenerateRecord_success() {
//        // 准备参数
//        GenerateRecordCreateReqVO reqVO = randomPojo(GenerateRecordCreateReqVO.class);
//
//        // 调用
//        Long generateRecordId = generateRecordService.createGenerateRecord(reqVO);
//        // 断言
//        assertNotNull(generateRecordId);
//        // 校验记录的属性是否正确
//        GenerateRecordDO generateRecord = generateRecordMapper.selectById(generateRecordId);
//        assertPojoEquals(reqVO, generateRecord);
    }

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

        // 调用
        generateRecordService.updateGenerateRecord(reqVO);
        // 校验是否更新正确
        GenerateRecordDO generateRecord = generateRecordMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, generateRecord);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> generateRecordService.updateGenerateRecord(reqVO), GENERATE_RECORD_NOT_EXISTS);
    }

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

        // 调用
        generateRecordService.deleteGenerateRecord(id);
       // 校验数据不存在了
       assertNull(generateRecordMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> generateRecordService.deleteGenerateRecord(id), GENERATE_RECORD_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetGenerateRecordPage() {
       // mock 数据
       GenerateRecordDO dbGenerateRecord = randomPojo(GenerateRecordDO.class, o -> { // 等会查询到
           o.setGenerateRecordId(null);
           o.setRuleId(null);
           o.setCodeRuleFormat(null);
           o.setCodeType(null);
           o.setCharLength(null);
           o.setCharType(null);
           o.setCodeNum(null);
           o.setOperateUser(null);
           o.setOperateUserId(null);
           o.setState(null);
           o.setBelongBusinessId(null);
           o.setBelongBusinessName(null);
           o.setCreateTime(null);
       });
       generateRecordMapper.insert(dbGenerateRecord);
       // 测试 generateRecordId 不匹配
       generateRecordMapper.insert(cloneIgnoreId(dbGenerateRecord, o -> o.setGenerateRecordId(null)));
       // 测试 ruleId 不匹配
       generateRecordMapper.insert(cloneIgnoreId(dbGenerateRecord, o -> o.setRuleId(null)));
       // 测试 codeRuleFormat 不匹配
       generateRecordMapper.insert(cloneIgnoreId(dbGenerateRecord, o -> o.setCodeRuleFormat(null)));
       // 测试 codeType 不匹配
       generateRecordMapper.insert(cloneIgnoreId(dbGenerateRecord, o -> o.setCodeType(null)));
       // 测试 charLength 不匹配
       generateRecordMapper.insert(cloneIgnoreId(dbGenerateRecord, o -> o.setCharLength(null)));
       // 测试 charType 不匹配
       generateRecordMapper.insert(cloneIgnoreId(dbGenerateRecord, o -> o.setCharType(null)));
       // 测试 codeNum 不匹配
       generateRecordMapper.insert(cloneIgnoreId(dbGenerateRecord, o -> o.setCodeNum(null)));
       // 测试 operateUser 不匹配
       generateRecordMapper.insert(cloneIgnoreId(dbGenerateRecord, o -> o.setOperateUser(null)));
       // 测试 operateUserId 不匹配
       generateRecordMapper.insert(cloneIgnoreId(dbGenerateRecord, o -> o.setOperateUserId(null)));
       // 测试 state 不匹配
       generateRecordMapper.insert(cloneIgnoreId(dbGenerateRecord, o -> o.setState(null)));
       // 测试 belongBusinessId 不匹配
       generateRecordMapper.insert(cloneIgnoreId(dbGenerateRecord, o -> o.setBelongBusinessId(null)));
       // 测试 belongBusinessName 不匹配
       generateRecordMapper.insert(cloneIgnoreId(dbGenerateRecord, o -> o.setBelongBusinessName(null)));
       // 测试 createTime 不匹配
       generateRecordMapper.insert(cloneIgnoreId(dbGenerateRecord, o -> o.setCreateTime(null)));
       // 准备参数
       GenerateRecordPageReqVO reqVO = new GenerateRecordPageReqVO();
       reqVO.setGenerateRecordId(null);
       reqVO.setCodeRuleFormat(null);
       reqVO.setBelongBusinessName(null);
//       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<GenerateRecordRespVO> pageResult = generateRecordService.getGenerateRecordPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbGenerateRecord, pageResult.getList().get(0));
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetGenerateRecordList() {
       // mock 数据
       GenerateRecordDO dbGenerateRecord = randomPojo(GenerateRecordDO.class, o -> { // 等会查询到
           o.setGenerateRecordId(null);
           o.setRuleId(null);
           o.setCodeRuleFormat(null);
           o.setCodeType(null);
           o.setCharLength(null);
           o.setCharType(null);
           o.setCodeNum(null);
           o.setOperateUser(null);
           o.setOperateUserId(null);
           o.setState(null);
           o.setBelongBusinessId(null);
           o.setBelongBusinessName(null);
           o.setCreateTime(null);
       });
       generateRecordMapper.insert(dbGenerateRecord);
       // 测试 generateRecordId 不匹配
       generateRecordMapper.insert(cloneIgnoreId(dbGenerateRecord, o -> o.setGenerateRecordId(null)));
       // 测试 ruleId 不匹配
       generateRecordMapper.insert(cloneIgnoreId(dbGenerateRecord, o -> o.setRuleId(null)));
       // 测试 codeRuleFormat 不匹配
       generateRecordMapper.insert(cloneIgnoreId(dbGenerateRecord, o -> o.setCodeRuleFormat(null)));
       // 测试 codeType 不匹配
       generateRecordMapper.insert(cloneIgnoreId(dbGenerateRecord, o -> o.setCodeType(null)));
       // 测试 charLength 不匹配
       generateRecordMapper.insert(cloneIgnoreId(dbGenerateRecord, o -> o.setCharLength(null)));
       // 测试 charType 不匹配
       generateRecordMapper.insert(cloneIgnoreId(dbGenerateRecord, o -> o.setCharType(null)));
       // 测试 codeNum 不匹配
       generateRecordMapper.insert(cloneIgnoreId(dbGenerateRecord, o -> o.setCodeNum(null)));
       // 测试 operateUser 不匹配
       generateRecordMapper.insert(cloneIgnoreId(dbGenerateRecord, o -> o.setOperateUser(null)));
       // 测试 operateUserId 不匹配
       generateRecordMapper.insert(cloneIgnoreId(dbGenerateRecord, o -> o.setOperateUserId(null)));
       // 测试 state 不匹配
       generateRecordMapper.insert(cloneIgnoreId(dbGenerateRecord, o -> o.setState(null)));
       // 测试 belongBusinessId 不匹配
       generateRecordMapper.insert(cloneIgnoreId(dbGenerateRecord, o -> o.setBelongBusinessId(null)));
       // 测试 belongBusinessName 不匹配
       generateRecordMapper.insert(cloneIgnoreId(dbGenerateRecord, o -> o.setBelongBusinessName(null)));
       // 测试 createTime 不匹配
       generateRecordMapper.insert(cloneIgnoreId(dbGenerateRecord, o -> o.setCreateTime(null)));
       // 准备参数
       GenerateRecordExportReqVO reqVO = new GenerateRecordExportReqVO();
       reqVO.setGenerateRecordId(null);
       reqVO.setRuleId(null);
       reqVO.setCodeRuleFormat(null);
       reqVO.setCodeType(null);
       reqVO.setCharLength(null);
       reqVO.setCharType(null);
       reqVO.setCodeNum(null);
       reqVO.setOperateUser(null);
       reqVO.setOperateUserId(null);
       reqVO.setState(null);
       reqVO.setBelongBusinessId(null);
       reqVO.setBelongBusinessName(null);
//       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       List<GenerateRecordDO> list = generateRecordService.getGenerateRecordList(reqVO);
       // 断言
       assertEquals(1, list.size());
       assertPojoEquals(dbGenerateRecord, list.get(0));
    }

}
