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

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.producereport.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.producereport.ProduceReportDO;
import cn.iocoder.yudao.module.mes.dal.mysql.producereport.ProduceReportMapper;
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 ProduceReportServiceImpl} 的单元测试类
 *
 * @author 内蒙必硕
 */
@Import(ProduceReportServiceImpl.class)
public class ProduceReportServiceImplTest extends BaseDbUnitTest {

    @Resource
    private ProduceReportServiceImpl produceReportService;

    @Resource
    private ProduceReportMapper produceReportMapper;

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

        // 调用
        Long produceReportId = produceReportService.createProduceReport(createReqVO);
        // 断言
        assertNotNull(produceReportId);
        // 校验记录的属性是否正确
        ProduceReportDO produceReport = produceReportMapper.selectById(produceReportId);
        assertPojoEquals(createReqVO, produceReport, "id");
    }

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

        // 调用
        produceReportService.updateProduceReport(updateReqVO);
        // 校验是否更新正确
        ProduceReportDO produceReport = produceReportMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, produceReport);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> produceReportService.updateProduceReport(updateReqVO), PRODUCE_REPORT_NOT_EXISTS);
    }

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

        // 调用
        produceReportService.deleteProduceReport(id);
       // 校验数据不存在了
       assertNull(produceReportMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> produceReportService.deleteProduceReport(id), PRODUCE_REPORT_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetProduceReportPage() {
       // mock 数据
       ProduceReportDO dbProduceReport = randomPojo(ProduceReportDO.class, o -> { // 等会查询到
           o.setProcessInstanceId(null);
           o.setReportCode(null);
           o.setUserId(null);
           o.setOrgId(null);
           o.setOrgType(null);
           o.setReportTime(null);
           o.setTotalTime(null);
           o.setReportStatus(null);
           o.setRemark(null);
           o.setCreateTime(null);
           o.setGroupType(null);
           o.setReportType(null);
           o.setReportDate(null);
       });
       produceReportMapper.insert(dbProduceReport);
       // 测试 processInstanceId 不匹配
       produceReportMapper.insert(cloneIgnoreId(dbProduceReport, o -> o.setProcessInstanceId(null)));
       // 测试 reportCode 不匹配
       produceReportMapper.insert(cloneIgnoreId(dbProduceReport, o -> o.setReportCode(null)));
       // 测试 userId 不匹配
       produceReportMapper.insert(cloneIgnoreId(dbProduceReport, o -> o.setUserId(null)));
       // 测试 orgId 不匹配
       produceReportMapper.insert(cloneIgnoreId(dbProduceReport, o -> o.setOrgId(null)));
       // 测试 orgType 不匹配
       produceReportMapper.insert(cloneIgnoreId(dbProduceReport, o -> o.setOrgType(null)));
       // 测试 reportTime 不匹配
       produceReportMapper.insert(cloneIgnoreId(dbProduceReport, o -> o.setReportTime(null)));
       // 测试 totalTime 不匹配
       produceReportMapper.insert(cloneIgnoreId(dbProduceReport, o -> o.setTotalTime(null)));
       // 测试 reportStatus 不匹配
       produceReportMapper.insert(cloneIgnoreId(dbProduceReport, o -> o.setReportStatus(null)));
       // 测试 remark 不匹配
       produceReportMapper.insert(cloneIgnoreId(dbProduceReport, o -> o.setRemark(null)));
       // 测试 createTime 不匹配
       produceReportMapper.insert(cloneIgnoreId(dbProduceReport, o -> o.setCreateTime(null)));
       // 测试 groupType 不匹配
       produceReportMapper.insert(cloneIgnoreId(dbProduceReport, o -> o.setGroupType(null)));
       // 测试 reportType 不匹配
       produceReportMapper.insert(cloneIgnoreId(dbProduceReport, o -> o.setReportType(null)));
       // 测试 reportDate 不匹配
       produceReportMapper.insert(cloneIgnoreId(dbProduceReport, o -> o.setReportDate(null)));
       // 准备参数
       ProduceReportPageReqVO reqVO = new ProduceReportPageReqVO();
       reqVO.setProcessInstanceId(null);
       reqVO.setReportCode(null);
       reqVO.setUserId(null);
       reqVO.setOrgId(null);
       reqVO.setOrgType(null);
       //reqVO.setReportTime(buildBetweenTimebeijian (2023, 2, 1, 2023, 2, 28));
       //reqVO.setTotalTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setReportStatus(null);
       reqVO.setRemark(null);
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setGroupType(null);
       reqVO.setReportType(null);
       reqVO.setReportDate(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<ProduceReportDO> pageResult = produceReportService.getProduceReportPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbProduceReport, pageResult.getList().get(0));
    }

}