package cn.iocoder.yudao.module.iot.service.formula;

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.iot.controller.admin.formula.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.formula.FormulaRecordDO;
import cn.iocoder.yudao.module.iot.dal.mysql.formula.FormulaRecordMapper;
import cn.iocoder.yudao.framework.common.pojo.PageResult;


import org.springframework.context.annotation.Import;

import javax.annotation.Resource;
import java.util.*;
import java.time.LocalDateTime;

import static cn.hutool.core.util.RandomUtil.*;
import static cn.iocoder.yudao.module.iot.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 FormulaRecordServiceImpl} 的单元测试类
 *
 * @author 内蒙必硕
 */
@Import(FormulaRecordServiceImpl.class)
public class FormulaRecordServiceImplTest extends BaseDbUnitTest {

    @Resource
    private FormulaRecordServiceImpl formulaRecordService;

    @Resource
    private FormulaRecordMapper formulaRecordMapper;

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

        // 调用
        Long formulaRecordId = formulaRecordService.createFormulaRecord(createReqVO);
        // 断言
        assertNotNull(formulaRecordId);
        // 校验记录的属性是否正确
        FormulaRecordDO formulaRecord = formulaRecordMapper.selectById(formulaRecordId);
        assertPojoEquals(createReqVO, formulaRecord, "id");
    }

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

        // 调用
        formulaRecordService.updateFormulaRecord(updateReqVO);
        // 校验是否更新正确
        FormulaRecordDO formulaRecord = formulaRecordMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, formulaRecord);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> formulaRecordService.updateFormulaRecord(updateReqVO), FORMULA_RECORD_NOT_EXISTS);
    }

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

        // 调用
        formulaRecordService.deleteFormulaRecord(id);
       // 校验数据不存在了
       assertNull(formulaRecordMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> formulaRecordService.deleteFormulaRecord(id), FORMULA_RECORD_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetFormulaRecordPage() {
       // mock 数据
       FormulaRecordDO dbFormulaRecord = randomPojo(FormulaRecordDO.class, o -> { // 等会查询到
           o.setMachineName(null);
           o.setWorkerName(null);
           o.setResultType(null);
           o.setFormulaCode(null);
           o.setCreateTime(null);
       });
       formulaRecordMapper.insert(dbFormulaRecord);
       // 测试 machineName 不匹配
       formulaRecordMapper.insert(cloneIgnoreId(dbFormulaRecord, o -> o.setMachineName(null)));
       // 测试 workerName 不匹配
       formulaRecordMapper.insert(cloneIgnoreId(dbFormulaRecord, o -> o.setWorkerName(null)));
       // 测试 resultType 不匹配
       formulaRecordMapper.insert(cloneIgnoreId(dbFormulaRecord, o -> o.setResultType(null)));
       // 测试 formulaCode 不匹配
       formulaRecordMapper.insert(cloneIgnoreId(dbFormulaRecord, o -> o.setFormulaCode(null)));
       // 测试 createTime 不匹配
       formulaRecordMapper.insert(cloneIgnoreId(dbFormulaRecord, o -> o.setCreateTime(null)));
       // 准备参数
       FormulaRecordPageReqVO reqVO = new FormulaRecordPageReqVO();
       reqVO.setMachineName(null);
       reqVO.setWorkerName(null);
       reqVO.setResultType(null);
       reqVO.setFormulaCode(null);
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<FormulaRecordDO> pageResult = formulaRecordService.getFormulaRecordPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbFormulaRecord, pageResult.getList().get(0));
    }

}