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

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;



import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;

import cn.iocoder.yudao.module.mes.controller.admin.machine.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.machine.MachineComponentDO;
import cn.iocoder.yudao.module.mes.dal.mysql.machine.MachineComponentMapper;
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 MachineComponentServiceImpl} 的单元测试类
 *
 * @author 内蒙必硕
 */
@Import(MachineComponentServiceImpl.class)
public class MachineComponentServiceImplTest extends BaseDbUnitTest {

    @Resource
    private MachineComponentServiceImpl machineComponentService;

    @Resource
    private MachineComponentMapper machineComponentMapper;

    @Test
    public void testCreateMachineComponent_success() {
        // 准备参数
        MachineComponentSaveReqVO createReqVO = randomPojo(MachineComponentSaveReqVO.class).setId(null);
        createReqVO.setParentId(0L);
        // 调用
        Long machineComponentId = machineComponentService.createMachineComponent(createReqVO);
        // 断言
        assertNotNull(machineComponentId);
        // 校验记录的属性是否正确
        MachineComponentDO machineComponent = machineComponentMapper.selectById(machineComponentId);
        assertPojoEquals(createReqVO, machineComponent, "id");
    }

    @Test
    public void testUpdateMachineComponent_success() {
        // mock 数据
        MachineComponentDO dbMachineComponent = randomPojo(MachineComponentDO.class);
        machineComponentMapper.insert(dbMachineComponent);// @Sql: 先插入出一条存在的数据
        // 准备参数
        MachineComponentSaveReqVO updateReqVO = randomPojo(MachineComponentSaveReqVO.class, o -> {
            o.setId(dbMachineComponent.getId()); // 设置更新的 ID
        });
        updateReqVO.setParentId(0L);
        // 调用
        machineComponentService.updateMachineComponent(updateReqVO);
        // 校验是否更新正确
        MachineComponentDO machineComponent = machineComponentMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, machineComponent);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> machineComponentService.updateMachineComponent(updateReqVO), MACHINE_COMPONENT_NOT_EXISTS);
    }

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

        // 调用
        machineComponentService.deleteMachineComponent(id);
       // 校验数据不存在了
       assertNull(machineComponentMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> machineComponentService.deleteMachineComponent(id), MACHINE_COMPONENT_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetMachineComponentList() {
       // mock 数据
       MachineComponentDO dbMachineComponent = randomPojo(MachineComponentDO.class, o -> { // 等会查询到
           o.setName(null);
           o.setParentId(null);
           o.setSort(null);
           o.setOrgId(null);
           o.setSerialCode(null);
           o.setOutgoingTime(null);
           o.setOutgoingReport(null);
           o.setPosition(null);
           o.setStandard(null);
           o.setRemark(null);
           o.setCreateTime(null);
           o.setStatus(null);
           o.setComponentType(null);
           o.setMachineType(null);
       });
       machineComponentMapper.insert(dbMachineComponent);
       // 测试 name 不匹配
       machineComponentMapper.insert(cloneIgnoreId(dbMachineComponent, o -> o.setName(null)));
       // 测试 parentId 不匹配
       machineComponentMapper.insert(cloneIgnoreId(dbMachineComponent, o -> o.setParentId(null)));
       // 测试 sort 不匹配
       machineComponentMapper.insert(cloneIgnoreId(dbMachineComponent, o -> o.setSort(null)));
       // 测试 orgId 不匹配
       machineComponentMapper.insert(cloneIgnoreId(dbMachineComponent, o -> o.setOrgId(null)));
       // 测试 serialCode 不匹配
       machineComponentMapper.insert(cloneIgnoreId(dbMachineComponent, o -> o.setSerialCode(null)));
       // 测试 outgoingTime 不匹配
       machineComponentMapper.insert(cloneIgnoreId(dbMachineComponent, o -> o.setOutgoingTime(null)));
       // 测试 outgoingReport 不匹配
       machineComponentMapper.insert(cloneIgnoreId(dbMachineComponent, o -> o.setOutgoingReport(null)));
       // 测试 position 不匹配
       machineComponentMapper.insert(cloneIgnoreId(dbMachineComponent, o -> o.setPosition(null)));
       // 测试 standard 不匹配
       machineComponentMapper.insert(cloneIgnoreId(dbMachineComponent, o -> o.setStandard(null)));
       // 测试 remark 不匹配
       machineComponentMapper.insert(cloneIgnoreId(dbMachineComponent, o -> o.setRemark(null)));
       // 测试 createTime 不匹配
       machineComponentMapper.insert(cloneIgnoreId(dbMachineComponent, o -> o.setCreateTime(null)));
       // 测试 status 不匹配
       machineComponentMapper.insert(cloneIgnoreId(dbMachineComponent, o -> o.setStatus(null)));
       // 测试 componentType 不匹配
       machineComponentMapper.insert(cloneIgnoreId(dbMachineComponent, o -> o.setComponentType(null)));
       // 测试 machineType 不匹配
       machineComponentMapper.insert(cloneIgnoreId(dbMachineComponent, o -> o.setMachineType(null)));
       // 准备参数
       MachineComponentListReqVO reqVO = new MachineComponentListReqVO();
       reqVO.setName(null);
       reqVO.setParentId(null);
       reqVO.setSort(null);
       reqVO.setOrgId(null);
       reqVO.setSerialCode(null);
       reqVO.setOutgoingTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setOutgoingReport(null);
       reqVO.setPosition(null);
       reqVO.setStandard(null);
       reqVO.setRemark(null);
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setStatus(null);
       reqVO.setComponentType(null);
       reqVO.setMachineType(null);

       // 调用
       List<MachineComponentDO> list = machineComponentService.getMachineComponentList(reqVO);
       // 断言
       assertEquals(1, list.size());
       assertPojoEquals(dbMachineComponent, list.get(0));
    }

}