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

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.feedingrecord.vo.FeedingRecordPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.feedingrecord.vo.FeedingRecordRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.feedingrecord.vo.FeedingRecordSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.feedingrecord.FeedingRecordDO;
import cn.iocoder.yudao.module.mes.dal.mysql.feedingrecord.FeedingRecordMapper;
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.FEEDING_RECORD_NOT_EXISTS;
import static org.junit.jupiter.api.Assertions.*;

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

    @Resource
    private FeedingRecordServiceImpl feedingRecordService;

    @Resource
    private FeedingRecordMapper feedingRecordMapper;

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

        // 调用
        Long feedingRecordId = feedingRecordService.createFeedingRecord(createReqVO);
        // 断言
        assertNotNull(feedingRecordId);
        // 校验记录的属性是否正确
        FeedingRecordDO feedingRecord = feedingRecordMapper.selectById(feedingRecordId);
        assertPojoEquals(createReqVO, feedingRecord, "id");
    }

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

        // 调用
        feedingRecordService.updateFeedingRecord(updateReqVO);
        // 校验是否更新正确
        FeedingRecordDO feedingRecord = feedingRecordMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, feedingRecord);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> feedingRecordService.updateFeedingRecord(updateReqVO), FEEDING_RECORD_NOT_EXISTS);
    }

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

        // 调用
        feedingRecordService.deleteFeedingRecord(id);
       // 校验数据不存在了
       assertNull(feedingRecordMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> feedingRecordService.deleteFeedingRecord(id), FEEDING_RECORD_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetFeedingRecordPage() {
       // mock 数据
       FeedingRecordDO dbFeedingRecord = randomPojo(FeedingRecordDO.class, o -> { // 等会查询到
           o.setFeedingRecordCode(null);
           o.setProductId(null);
           o.setPlanId(null);
           o.setItemId(null);
           o.setFeedingPipeline(null);
           o.setFeedingType(null);
           o.setWeight(null);
           o.setFeedingTime(null);
           o.setUserId(null);
           o.setRemark(null);
           o.setRecordStatus(null);
           o.setCreateTime(null);
       });
       feedingRecordMapper.insert(dbFeedingRecord);
       // 测试 feedingRecordCode 不匹配
       feedingRecordMapper.insert(cloneIgnoreId(dbFeedingRecord, o -> o.setFeedingRecordCode(null)));
       // 测试 productId 不匹配
       feedingRecordMapper.insert(cloneIgnoreId(dbFeedingRecord, o -> o.setProductId(null)));
       // 测试 planId 不匹配
       feedingRecordMapper.insert(cloneIgnoreId(dbFeedingRecord, o -> o.setPlanId(null)));
       // 测试 itemId 不匹配
       feedingRecordMapper.insert(cloneIgnoreId(dbFeedingRecord, o -> o.setItemId(null)));
       // 测试 feedingPipeline 不匹配
       feedingRecordMapper.insert(cloneIgnoreId(dbFeedingRecord, o -> o.setFeedingPipeline(null)));
       // 测试 feedingType 不匹配
       feedingRecordMapper.insert(cloneIgnoreId(dbFeedingRecord, o -> o.setFeedingType(null)));
       // 测试 weight 不匹配
       feedingRecordMapper.insert(cloneIgnoreId(dbFeedingRecord, o -> o.setWeight(null)));
       // 测试 feedingTime 不匹配
       feedingRecordMapper.insert(cloneIgnoreId(dbFeedingRecord, o -> o.setFeedingTime(null)));
       // 测试 userId 不匹配
       feedingRecordMapper.insert(cloneIgnoreId(dbFeedingRecord, o -> o.setUserId(null)));
       // 测试 remark 不匹配
       feedingRecordMapper.insert(cloneIgnoreId(dbFeedingRecord, o -> o.setRemark(null)));
       // 测试 recordStatus 不匹配
       feedingRecordMapper.insert(cloneIgnoreId(dbFeedingRecord, o -> o.setRecordStatus(null)));
       // 测试 createTime 不匹配
       feedingRecordMapper.insert(cloneIgnoreId(dbFeedingRecord, o -> o.setCreateTime(null)));
       // 准备参数
       FeedingRecordPageReqVO reqVO = new FeedingRecordPageReqVO();
       reqVO.setFeedingRecordCode(null);
       reqVO.setProductId(null);
       reqVO.setPlanId(null);
       reqVO.setItemId(null);
       reqVO.setFeedingPipeline(null);
       reqVO.setFeedingType(null);
       reqVO.setWeight(null);
       reqVO.setFeedingTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setUserId(null);
       reqVO.setRemark(null);
       reqVO.setRecordStatus(null);
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<FeedingRecordRespVO> pageResult = feedingRecordService.getFeedingRecordPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbFeedingRecord, pageResult.getList().get(0));
    }

}