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

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.moldrecord.vo.MoldRecordPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.moldrecord.vo.MoldRecordSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.moldrecord.MoldRecordDO;
import cn.iocoder.yudao.module.mes.dal.mysql.moldrecord.MoldRecordMapper;
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.MOLD_RECORD_NOT_EXISTS;
import static org.junit.jupiter.api.Assertions.*;

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

    @Resource
    private MoldRecordServiceImpl moldRecordService;

    @Resource
    private MoldRecordMapper moldRecordMapper;

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

        // 调用
        Long moldRecordId = moldRecordService.createMoldRecord(createReqVO);
        // 断言
        assertNotNull(moldRecordId);
        // 校验记录的属性是否正确
        MoldRecordDO moldRecord = moldRecordMapper.selectById(moldRecordId);
        assertPojoEquals(createReqVO, moldRecord, "id");
    }

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

        // 调用
        moldRecordService.updateMoldRecord(updateReqVO);
        // 校验是否更新正确
        MoldRecordDO moldRecord = moldRecordMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, moldRecord);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> moldRecordService.updateMoldRecord(updateReqVO), MOLD_RECORD_NOT_EXISTS);
    }

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

        // 调用
        moldRecordService.deleteMoldRecord(id);
        // 校验数据不存在了
        assertNull(moldRecordMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> moldRecordService.deleteMoldRecord(id), MOLD_RECORD_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetMoldRecordPage() {
        // mock 数据
        MoldRecordDO dbMoldRecord = randomPojo(MoldRecordDO.class, o -> { // 等会查询到
            o.setFlowCode(null);
            o.setBrandId(null);
            o.setMoldId(null);
            o.setOldMoldId(null);
            o.setMachineId(null);
            o.setUserId(null);
            o.setRecordType(null);
            o.setRecordStatus(null);
            o.setRemark(null);
            o.setCreateTime(null);
        });
        moldRecordMapper.insert(dbMoldRecord);
        // 测试 flowCode 不匹配
        moldRecordMapper.insert(cloneIgnoreId(dbMoldRecord, o -> o.setFlowCode(null)));
        // 测试 brandId 不匹配
        moldRecordMapper.insert(cloneIgnoreId(dbMoldRecord, o -> o.setBrandId(null)));
        // 测试 moldId 不匹配
        moldRecordMapper.insert(cloneIgnoreId(dbMoldRecord, o -> o.setMoldId(null)));
        // 测试 oldMoldId 不匹配
        moldRecordMapper.insert(cloneIgnoreId(dbMoldRecord, o -> o.setOldMoldId(null)));
        // 测试 machineId 不匹配
        moldRecordMapper.insert(cloneIgnoreId(dbMoldRecord, o -> o.setMachineId(null)));
        // 测试 userId 不匹配
        moldRecordMapper.insert(cloneIgnoreId(dbMoldRecord, o -> o.setUserId(null)));
        // 测试 recordType 不匹配
        moldRecordMapper.insert(cloneIgnoreId(dbMoldRecord, o -> o.setRecordType(null)));
        // 测试 recordStatus 不匹配
        moldRecordMapper.insert(cloneIgnoreId(dbMoldRecord, o -> o.setRecordStatus(null)));
        // 测试 remark 不匹配
        moldRecordMapper.insert(cloneIgnoreId(dbMoldRecord, o -> o.setRemark(null)));
        // 测试 createTime 不匹配
        moldRecordMapper.insert(cloneIgnoreId(dbMoldRecord, o -> o.setCreateTime(null)));
        // 准备参数
        MoldRecordPageReqVO reqVO = new MoldRecordPageReqVO();
        reqVO.setFlowCode(null);
        reqVO.setBrandId(null);
        reqVO.setMoldId(null);
        reqVO.setOldMoldId(null);
        reqVO.setMachineId(null);
        reqVO.setUserId(null);
        reqVO.setRecordType(null);
        reqVO.setRecordStatus(null);
        reqVO.setRemark(null);
        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

        // 调用
        PageResult<MoldRecordDO> pageResult = moldRecordService.getMoldRecordPage(reqVO);
        // 断言
        assertEquals(1, pageResult.getTotal());
        assertEquals(1, pageResult.getList().size());
        assertPojoEquals(dbMoldRecord, pageResult.getList().get(0));
    }

}