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

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.energydevice.vo.EnergyDeviceCheckRecordPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.energydevice.vo.EnergyDeviceCheckRecordSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.energydevice.EnergyDeviceCheckRecordDO;
import cn.iocoder.yudao.module.mes.dal.mysql.energydevice.EnergyDeviceCheckRecordMapper;
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.ENERGY_DEVICE_CHECK_RECORD_NOT_EXISTS;
import static org.junit.jupiter.api.Assertions.*;

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

    @Resource
    private EnergyDeviceCheckRecordServiceImpl energyDeviceCheckRecordService;

    @Resource
    private EnergyDeviceCheckRecordMapper energyDeviceCheckRecordMapper;

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

        // 调用
        Long energyDeviceCheckRecordId = energyDeviceCheckRecordService.createEnergyDeviceCheckRecord(createReqVO);
        // 断言
        assertNotNull(energyDeviceCheckRecordId);
        // 校验记录的属性是否正确
        EnergyDeviceCheckRecordDO energyDeviceCheckRecord = energyDeviceCheckRecordMapper.selectById(energyDeviceCheckRecordId);
        assertPojoEquals(createReqVO, energyDeviceCheckRecord, "id");
    }

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

        // 调用
        energyDeviceCheckRecordService.updateEnergyDeviceCheckRecord(updateReqVO);
        // 校验是否更新正确
        EnergyDeviceCheckRecordDO energyDeviceCheckRecord = energyDeviceCheckRecordMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, energyDeviceCheckRecord);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> energyDeviceCheckRecordService.updateEnergyDeviceCheckRecord(updateReqVO), ENERGY_DEVICE_CHECK_RECORD_NOT_EXISTS);
    }

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

        // 调用
        energyDeviceCheckRecordService.deleteEnergyDeviceCheckRecord(id);
       // 校验数据不存在了
       assertNull(energyDeviceCheckRecordMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> energyDeviceCheckRecordService.deleteEnergyDeviceCheckRecord(id), ENERGY_DEVICE_CHECK_RECORD_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetEnergyDeviceCheckRecordPage() {
       // mock 数据
       EnergyDeviceCheckRecordDO dbEnergyDeviceCheckRecord = randomPojo(EnergyDeviceCheckRecordDO.class, o -> { // 等会查询到
           o.setDeviceId(null);
           o.setCheckTime(null);
           o.setDiffValue(null);
           o.setRemark(null);
           o.setCreateTime(null);
       });
       energyDeviceCheckRecordMapper.insert(dbEnergyDeviceCheckRecord);
       // 测试 deviceId 不匹配
       energyDeviceCheckRecordMapper.insert(cloneIgnoreId(dbEnergyDeviceCheckRecord, o -> o.setDeviceId(null)));
       // 测试 checkTime 不匹配
       energyDeviceCheckRecordMapper.insert(cloneIgnoreId(dbEnergyDeviceCheckRecord, o -> o.setCheckTime(null)));
       // 测试 diffValue 不匹配
       energyDeviceCheckRecordMapper.insert(cloneIgnoreId(dbEnergyDeviceCheckRecord, o -> o.setDiffValue(null)));
       // 测试 remark 不匹配
       energyDeviceCheckRecordMapper.insert(cloneIgnoreId(dbEnergyDeviceCheckRecord, o -> o.setRemark(null)));
       // 测试 createTime 不匹配
       energyDeviceCheckRecordMapper.insert(cloneIgnoreId(dbEnergyDeviceCheckRecord, o -> o.setCreateTime(null)));
       // 准备参数
       EnergyDeviceCheckRecordPageReqVO reqVO = new EnergyDeviceCheckRecordPageReqVO();
       reqVO.setDeviceId(null);
       reqVO.setCheckTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setDiffValue(null);
       reqVO.setRemark(null);
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<EnergyDeviceCheckRecordDO> pageResult = energyDeviceCheckRecordService.getEnergyDeviceCheckRecordPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbEnergyDeviceCheckRecord, pageResult.getList().get(0));
    }

}