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

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.dvrepair.vo.DvRepairPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.dvrepair.vo.DvRepairSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.dvrepair.DvRepairDO;
import cn.iocoder.yudao.module.mes.dal.mysql.dvrepair.DvRepairMapper;
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_REPAIR_NOT_EXISTS;
import static org.junit.jupiter.api.Assertions.*;

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

    @Resource
    private DvRepairServiceImpl dvRepairService;

    @Resource
    private DvRepairMapper dvRepairMapper;

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

        // 调用
        Long dvRepairId = dvRepairService.createDvRepair(createReqVO);
        // 断言
        assertNotNull(dvRepairId);
        // 校验记录的属性是否正确
        DvRepairDO dvRepair = dvRepairMapper.selectById(dvRepairId);
        assertPojoEquals(createReqVO, dvRepair, "id");
    }

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

        // 调用
        dvRepairService.updateDvRepair(updateReqVO);
        // 校验是否更新正确
        DvRepairDO dvRepair = dvRepairMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, dvRepair);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> dvRepairService.updateDvRepair(updateReqVO), DV_REPAIR_NOT_EXISTS);
    }

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

        // 调用
        dvRepairService.deleteDvRepair(Collections.singletonList(id));
       // 校验数据不存在了
       assertNull(dvRepairMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> dvRepairService.deleteDvRepair(Collections.singletonList(id)), DV_REPAIR_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetDvRepairPage() {
       // mock 数据
       DvRepairDO dbDvRepair = randomPojo(DvRepairDO.class, o -> { // 等会查询到
           o.setRepairCode(null);
           o.setRepairName(null);
           o.setMachineryId(null);
           o.setMachineryCode(null);
           o.setMachineryName(null);
           o.setMachineryBrand(null);
           o.setMachinerySpec(null);
           o.setMachineryTypeId(null);
           o.setRequireDate(null);
           o.setFinishDate(null);
           o.setConfirmDate(null);
           o.setRepairResult(null);
           o.setAcceptedBy(null);
           o.setConfirmBy(null);
           o.setStatus(null);
           o.setRemark(null);
           o.setCreateTime(null);
       });
       dvRepairMapper.insert(dbDvRepair);
       // 测试 repairCode 不匹配
       dvRepairMapper.insert(cloneIgnoreId(dbDvRepair, o -> o.setRepairCode(null)));
       // 测试 repairName 不匹配
       dvRepairMapper.insert(cloneIgnoreId(dbDvRepair, o -> o.setRepairName(null)));
       // 测试 machineryId 不匹配
       dvRepairMapper.insert(cloneIgnoreId(dbDvRepair, o -> o.setMachineryId(null)));
       // 测试 machineryCode 不匹配
       dvRepairMapper.insert(cloneIgnoreId(dbDvRepair, o -> o.setMachineryCode(null)));
       // 测试 machineryName 不匹配
       dvRepairMapper.insert(cloneIgnoreId(dbDvRepair, o -> o.setMachineryName(null)));
       // 测试 machineryBrand 不匹配
       dvRepairMapper.insert(cloneIgnoreId(dbDvRepair, o -> o.setMachineryBrand(null)));
       // 测试 machinerySpec 不匹配
       dvRepairMapper.insert(cloneIgnoreId(dbDvRepair, o -> o.setMachinerySpec(null)));
       // 测试 machineryTypeId 不匹配
       dvRepairMapper.insert(cloneIgnoreId(dbDvRepair, o -> o.setMachineryTypeId(null)));
       // 测试 requireDate 不匹配
       dvRepairMapper.insert(cloneIgnoreId(dbDvRepair, o -> o.setRequireDate(null)));
       // 测试 finishDate 不匹配
       dvRepairMapper.insert(cloneIgnoreId(dbDvRepair, o -> o.setFinishDate(null)));
       // 测试 confirmDate 不匹配
       dvRepairMapper.insert(cloneIgnoreId(dbDvRepair, o -> o.setConfirmDate(null)));
       // 测试 repairResult 不匹配
       dvRepairMapper.insert(cloneIgnoreId(dbDvRepair, o -> o.setRepairResult(null)));
       // 测试 acceptedBy 不匹配
       dvRepairMapper.insert(cloneIgnoreId(dbDvRepair, o -> o.setAcceptedBy(null)));
       // 测试 confirmBy 不匹配
       dvRepairMapper.insert(cloneIgnoreId(dbDvRepair, o -> o.setConfirmBy(null)));
       // 测试 status 不匹配
       dvRepairMapper.insert(cloneIgnoreId(dbDvRepair, o -> o.setStatus(null)));
       // 测试 remark 不匹配
       dvRepairMapper.insert(cloneIgnoreId(dbDvRepair, o -> o.setRemark(null)));
       // 测试 createTime 不匹配
       dvRepairMapper.insert(cloneIgnoreId(dbDvRepair, o -> o.setCreateTime(null)));
       // 准备参数
       DvRepairPageReqVO reqVO = new DvRepairPageReqVO();
       reqVO.setRepairCode(null);
       reqVO.setRepairName(null);
       reqVO.setMachineryId(null);
       reqVO.setMachineryCode(null);
       reqVO.setMachineryName(null);
       reqVO.setMachineryBrand(null);
       reqVO.setMachinerySpec(null);
       reqVO.setMachineryTypeId(null);
       reqVO.setRequireDate(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setFinishDate(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setConfirmDate(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setRepairResult(null);
       reqVO.setAcceptedBy(null);
       reqVO.setConfirmBy(null);
       reqVO.setStatus(null);
       reqVO.setRemark(null);
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<DvRepairDO> pageResult = dvRepairService.getDvRepairPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbDvRepair, pageResult.getList().get(0));
    }

}