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

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

import javax.annotation.Resource;

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

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

    @Resource
    private EnergyDeviceServiceImpl energyDeviceService;

    @Resource
    private EnergyDeviceMapper energyDeviceMapper;

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

        // 调用
        Long energyDeviceId = energyDeviceService.createEnergyDevice(createReqVO);
        // 断言
        assertNotNull(energyDeviceId);
        // 校验记录的属性是否正确
        EnergyDeviceDO energyDevice = energyDeviceMapper.selectById(energyDeviceId);
        assertPojoEquals(createReqVO, energyDevice, "id");
    }

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

        // 调用
        energyDeviceService.updateEnergyDevice(updateReqVO);
        // 校验是否更新正确
        EnergyDeviceDO energyDevice = energyDeviceMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, energyDevice);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> energyDeviceService.updateEnergyDevice(updateReqVO), ENERGY_DEVICE_NOT_EXISTS);
    }

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

        // 调用
        energyDeviceService.deleteEnergyDevice(id);
       // 校验数据不存在了
       assertNull(energyDeviceMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> energyDeviceService.deleteEnergyDevice(id), ENERGY_DEVICE_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetEnergyDevicePage() {
       // mock 数据
       EnergyDeviceDO dbEnergyDevice = randomPojo(EnergyDeviceDO.class, o -> { // 等会查询到
           o.setName(null);
           o.setCode(null);
//           o.setDeviceType(null);
           o.setInfo(null);
           o.setCheckCron(null);
           o.setLastCheckTime(null);
           o.setLastCheckValue(null);
           o.setUnitName(null);
           o.setIsEnable(null);
           o.setCreateTime(null);
       });
       energyDeviceMapper.insert(dbEnergyDevice);
       // 测试 name 不匹配
       energyDeviceMapper.insert(cloneIgnoreId(dbEnergyDevice, o -> o.setName(null)));
       // 测试 code 不匹配
       energyDeviceMapper.insert(cloneIgnoreId(dbEnergyDevice, o -> o.setCode(null)));
       // 测试 deviceType 不匹配
//       energyDeviceMapper.insert(cloneIgnoreId(dbEnergyDevice, o -> o.setDeviceType(null)));
       // 测试 info 不匹配
       energyDeviceMapper.insert(cloneIgnoreId(dbEnergyDevice, o -> o.setInfo(null)));
       // 测试 checkCron 不匹配
       energyDeviceMapper.insert(cloneIgnoreId(dbEnergyDevice, o -> o.setCheckCron(null)));
       // 测试 lastCheckTime 不匹配
       energyDeviceMapper.insert(cloneIgnoreId(dbEnergyDevice, o -> o.setLastCheckTime(null)));
       // 测试 lastCheckValue 不匹配
       energyDeviceMapper.insert(cloneIgnoreId(dbEnergyDevice, o -> o.setLastCheckValue(null)));
       // 测试 unitName 不匹配
       energyDeviceMapper.insert(cloneIgnoreId(dbEnergyDevice, o -> o.setUnitName(null)));
       // 测试 isEnable 不匹配
       energyDeviceMapper.insert(cloneIgnoreId(dbEnergyDevice, o -> o.setIsEnable(null)));
       // 测试 createTime 不匹配
       energyDeviceMapper.insert(cloneIgnoreId(dbEnergyDevice, o -> o.setCreateTime(null)));
       // 准备参数
       EnergyDevicePageReqVO reqVO = new EnergyDevicePageReqVO();
       reqVO.setName(null);
       reqVO.setCode(null);
//       reqVO.setDeviceType(null);
       reqVO.setInfo(null);
       reqVO.setCheckCron(null);
       reqVO.setLastCheckTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setLastCheckValue(null);
       reqVO.setUnitName(null);
       reqVO.setIsEnable(null);
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<EnergyDeviceDO> pageResult = energyDeviceService.getEnergyDevicePage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbEnergyDevice, pageResult.getList().get(0));
    }

}