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

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.dvsubject.vo.DvSubjectPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.dvsubject.vo.DvSubjectSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.dvsubject.DvSubjectDO;
import cn.iocoder.yudao.module.mes.dal.mysql.dvsubject.DvSubjectMapper;
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.Collections;

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_SUBJECT_NOT_EXISTS;
import static org.junit.jupiter.api.Assertions.*;

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

    @Resource
    private DvSubjectServiceImpl dvSubjectService;

    @Resource
    private DvSubjectMapper dvSubjectMapper;

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

        // 调用
        Long dvSubjectId = dvSubjectService.createDvSubject(createReqVO);
        // 断言
        assertNotNull(dvSubjectId);
        // 校验记录的属性是否正确
        DvSubjectDO dvSubject = dvSubjectMapper.selectById(dvSubjectId);
        assertPojoEquals(createReqVO, dvSubject, "id");
    }

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

        // 调用
        dvSubjectService.updateDvSubject(updateReqVO);
        // 校验是否更新正确
        DvSubjectDO dvSubject = dvSubjectMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, dvSubject);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> dvSubjectService.updateDvSubject(updateReqVO), DV_SUBJECT_NOT_EXISTS);
    }

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

        // 调用
        dvSubjectService.deleteDvSubject(Collections.singletonList(id));
       // 校验数据不存在了
       assertNull(dvSubjectMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> dvSubjectService.deleteDvSubject(Collections.singletonList(id)), DV_SUBJECT_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetDvSubjectPage() {
       // mock 数据
       DvSubjectDO dbDvSubject = randomPojo(DvSubjectDO.class, o -> { // 等会查询到
           o.setSubjectCode(null);
           o.setSubjectName(null);
           o.setSubjectType(null);
           o.setSubjectContent(null);
           o.setSubjectStandard(null);
           o.setIsEnable(null);
           o.setCreateTime(null);
       });
       dvSubjectMapper.insert(dbDvSubject);
       // 测试 subjectCode 不匹配
       dvSubjectMapper.insert(cloneIgnoreId(dbDvSubject, o -> o.setSubjectCode(null)));
       // 测试 subjectName 不匹配
       dvSubjectMapper.insert(cloneIgnoreId(dbDvSubject, o -> o.setSubjectName(null)));
       // 测试 subjectType 不匹配
       dvSubjectMapper.insert(cloneIgnoreId(dbDvSubject, o -> o.setSubjectType(null)));
       // 测试 subjectContent 不匹配
       dvSubjectMapper.insert(cloneIgnoreId(dbDvSubject, o -> o.setSubjectContent(null)));
       // 测试 subjectStandard 不匹配
       dvSubjectMapper.insert(cloneIgnoreId(dbDvSubject, o -> o.setSubjectStandard(null)));
       // 测试 isEnable 不匹配
       dvSubjectMapper.insert(cloneIgnoreId(dbDvSubject, o -> o.setIsEnable(null)));
       // 测试 createTime 不匹配
       dvSubjectMapper.insert(cloneIgnoreId(dbDvSubject, o -> o.setCreateTime(null)));
       // 准备参数
       DvSubjectPageReqVO reqVO = new DvSubjectPageReqVO();
       reqVO.setSubjectCode(null);
       reqVO.setSubjectName(null);
       reqVO.setSubjectType(null);
       reqVO.setSubjectContent(null);
       reqVO.setSubjectStandard(null);
       reqVO.setIsEnable(null);
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<DvSubjectDO> pageResult = dvSubjectService.getDvSubjectPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbDvSubject, pageResult.getList().get(0));
    }

}