package cn.iocoder.yudao.module.mes.service.changerecord;

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.mes.controller.admin.changerecord.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.changerecord.ProduceReportChangeRecordDO;
import cn.iocoder.yudao.module.mes.dal.mysql.changerecord.ProduceReportChangeRecordMapper;
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.mes.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.date.LocalDateTimeUtils.*;
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 ProduceReportChangeRecordServiceImpl} 的单元测试类
 *
 * @author 内蒙必硕
 */
@Import(ProduceReportChangeRecordServiceImpl.class)
public class ProduceReportChangeRecordServiceImplTest extends BaseDbUnitTest {

    @Resource
    private ProduceReportChangeRecordServiceImpl produceReportChangeRecordService;

    @Resource
    private ProduceReportChangeRecordMapper produceReportChangeRecordMapper;

    @Test
    public void testCreateProduceReportChangeRecord_success() {
        // 准备参数
        ProduceReportChangeRecordSaveReqVO createReqVO = randomPojo(ProduceReportChangeRecordSaveReqVO.class).setId(null);

        // 调用
        Long produceReportChangeRecordId = produceReportChangeRecordService.createProduceReportChangeRecord(createReqVO);
        // 断言
        assertNotNull(produceReportChangeRecordId);
        // 校验记录的属性是否正确
        ProduceReportChangeRecordDO produceReportChangeRecord = produceReportChangeRecordMapper.selectById(produceReportChangeRecordId);
        assertPojoEquals(createReqVO, produceReportChangeRecord, "id");
    }

    @Test
    public void testUpdateProduceReportChangeRecord_success() {
        // mock 数据
        ProduceReportChangeRecordDO dbProduceReportChangeRecord = randomPojo(ProduceReportChangeRecordDO.class);
        produceReportChangeRecordMapper.insert(dbProduceReportChangeRecord);// @Sql: 先插入出一条存在的数据
        // 准备参数
        ProduceReportChangeRecordSaveReqVO updateReqVO = randomPojo(ProduceReportChangeRecordSaveReqVO.class, o -> {
            o.setId(dbProduceReportChangeRecord.getId()); // 设置更新的 ID
        });

        // 调用
        produceReportChangeRecordService.updateProduceReportChangeRecord(updateReqVO);
        // 校验是否更新正确
        ProduceReportChangeRecordDO produceReportChangeRecord = produceReportChangeRecordMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, produceReportChangeRecord);
    }

    @Test
    public void testUpdateProduceReportChangeRecord_notExists() {
        // 准备参数
        ProduceReportChangeRecordSaveReqVO updateReqVO = randomPojo(ProduceReportChangeRecordSaveReqVO.class);

        // 调用, 并断言异常
        assertServiceException(() -> produceReportChangeRecordService.updateProduceReportChangeRecord(updateReqVO), PRODUCE_REPORT_CHANGE_RECORD_NOT_EXISTS);
    }

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

        // 调用
        produceReportChangeRecordService.deleteProduceReportChangeRecord(id);
       // 校验数据不存在了
       assertNull(produceReportChangeRecordMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> produceReportChangeRecordService.deleteProduceReportChangeRecord(id), PRODUCE_REPORT_CHANGE_RECORD_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetProduceReportChangeRecordPage() {
       // mock 数据
       ProduceReportChangeRecordDO dbProduceReportChangeRecord = randomPojo(ProduceReportChangeRecordDO.class, o -> { // 等会查询到
           o.setReportId(null);
           o.setChangeType(null);
           o.setBeforeChange(null);
           o.setAfterChange(null);
           o.setCreateTime(null);
       });
       produceReportChangeRecordMapper.insert(dbProduceReportChangeRecord);
       // 测试 reportId 不匹配
       produceReportChangeRecordMapper.insert(cloneIgnoreId(dbProduceReportChangeRecord, o -> o.setReportId(null)));
       // 测试 changeType 不匹配
       produceReportChangeRecordMapper.insert(cloneIgnoreId(dbProduceReportChangeRecord, o -> o.setChangeType(null)));
       // 测试 beforeChange 不匹配
       produceReportChangeRecordMapper.insert(cloneIgnoreId(dbProduceReportChangeRecord, o -> o.setBeforeChange(null)));
       // 测试 afterChange 不匹配
       produceReportChangeRecordMapper.insert(cloneIgnoreId(dbProduceReportChangeRecord, o -> o.setAfterChange(null)));
       // 测试 createTime 不匹配
       produceReportChangeRecordMapper.insert(cloneIgnoreId(dbProduceReportChangeRecord, o -> o.setCreateTime(null)));
       // 准备参数
       ProduceReportChangeRecordPageReqVO reqVO = new ProduceReportChangeRecordPageReqVO();
       reqVO.setReportId(null);
       reqVO.setChangeType(null);
       reqVO.setBeforeChange(null);
       reqVO.setAfterChange(null);
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<ProduceReportChangeRecordDO> pageResult = produceReportChangeRecordService.getProduceReportChangeRecordPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbProduceReportChangeRecord, pageResult.getList().get(0));
    }

}