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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.mes.controller.admin.dvcheck.vo.DvCheckPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.dvcheck.vo.DvCheckSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.dvcheck.DvCheckDO;
import cn.iocoder.yudao.module.mes.dal.mysql.dvcheck.DvCheckMapper;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Import;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
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.mes.enums.ErrorCodeConstants.DV_CHECK_NOT_EXISTS;
import static org.junit.jupiter.api.Assertions.*;

/**
 * {@link DvCheckServiceImpl} 的单元测试类
 *
 * @author 内蒙必硕
 */
@Import(DvCheckServiceImpl.class)
public class DvCheckServiceImplTest extends BaseDbUnitTest {

    @Resource
    private DvCheckServiceImpl dvCheckService;

    @Resource
    private DvCheckMapper dvCheckMapper;

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

        // 调用
        Long dvCheckId = dvCheckService.createDvCheck(createReqVO);
        // 断言
        assertNotNull(dvCheckId);
        // 校验记录的属性是否正确
        DvCheckDO dvCheck = dvCheckMapper.selectById(dvCheckId);
        assertPojoEquals(createReqVO, dvCheck, "id");
    }

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

        // 调用
        dvCheckService.updateDvCheck(updateReqVO);
        // 校验是否更新正确
        DvCheckDO dvCheck = dvCheckMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, dvCheck);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> dvCheckService.updateDvCheck(updateReqVO), DV_CHECK_NOT_EXISTS);
    }

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

        // 调用
        dvCheckService.deleteDvCheck(id);
       // 校验数据不存在了
       assertNull(dvCheckMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> dvCheckService.deleteDvCheck(id), DV_CHECK_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetDvCheckPage() {
       // mock 数据
       DvCheckDO dbDvCheck = randomPojo(DvCheckDO.class, o -> { // 等会查询到
           o.setPlanCode(null);
           o.setPlanName(null);
           o.setPlanType(null);
           o.setStartDate(null);
           o.setEndDate(null);
           o.setStatus(null);
           o.setRemark(null);
           o.setIsEnable(null);
           o.setCreateTime(null);
           o.setCycleUnit(null);
           o.setCycleCount(null);
           o.setPlanLevel(null);
           o.setLastTime(null);
           o.setFormulaCode(null);
       });
       dvCheckMapper.insert(dbDvCheck);
       // 测试 planCode 不匹配
       dvCheckMapper.insert(cloneIgnoreId(dbDvCheck, o -> o.setPlanCode(null)));
       // 测试 planName 不匹配
       dvCheckMapper.insert(cloneIgnoreId(dbDvCheck, o -> o.setPlanName(null)));
       // 测试 planType 不匹配
       dvCheckMapper.insert(cloneIgnoreId(dbDvCheck, o -> o.setPlanType(null)));
       // 测试 startDate 不匹配
       dvCheckMapper.insert(cloneIgnoreId(dbDvCheck, o -> o.setStartDate(null)));
       // 测试 endDate 不匹配
       dvCheckMapper.insert(cloneIgnoreId(dbDvCheck, o -> o.setEndDate(null)));
       // 测试 status 不匹配
       dvCheckMapper.insert(cloneIgnoreId(dbDvCheck, o -> o.setStatus(null)));
       // 测试 remark 不匹配
       dvCheckMapper.insert(cloneIgnoreId(dbDvCheck, o -> o.setRemark(null)));
       // 测试 isEnable 不匹配
       dvCheckMapper.insert(cloneIgnoreId(dbDvCheck, o -> o.setIsEnable(null)));
       // 测试 createTime 不匹配
       dvCheckMapper.insert(cloneIgnoreId(dbDvCheck, o -> o.setCreateTime(null)));
       // 测试 cycleUnit 不匹配
       dvCheckMapper.insert(cloneIgnoreId(dbDvCheck, o -> o.setCycleUnit(null)));
       // 测试 cycleCount 不匹配
       dvCheckMapper.insert(cloneIgnoreId(dbDvCheck, o -> o.setCycleCount(null)));
       // 测试 planLevel 不匹配
       dvCheckMapper.insert(cloneIgnoreId(dbDvCheck, o -> o.setPlanLevel(null)));
       // 测试 lastTime 不匹配
       dvCheckMapper.insert(cloneIgnoreId(dbDvCheck, o -> o.setLastTime(null)));
       // 测试 formulaCode 不匹配
       dvCheckMapper.insert(cloneIgnoreId(dbDvCheck, o -> o.setFormulaCode(null)));
       // 准备参数
       DvCheckPageReqVO reqVO = new DvCheckPageReqVO();
       reqVO.setPlanCode(null);
       reqVO.setPlanName(null);
       reqVO.setPlanType(null);
       reqVO.setStartDate(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setEndDate(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setStatus(null);
       reqVO.setRemark(null);
       reqVO.setIsEnable(null);
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setCycleUnit(null);
       reqVO.setCycleCount(null);
       reqVO.setPlanLevel(null);
       reqVO.setLastTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setFormulaCode(null);

       // 调用
       PageResult<DvCheckDO> pageResult = dvCheckService.getDvCheckPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbDvCheck, pageResult.getList().get(0));
    }

}