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

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.stockworkshop.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.stockworkshop.StockWorkshopDetailDO;
import cn.iocoder.yudao.module.mes.dal.mysql.stockworkshop.StockWorkshopDetailMapper;
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.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 StockWorkshopDetailServiceImpl} 的单元测试类
 *
 * @author 内蒙必硕
 */
@Import(StockWorkshopDetailServiceImpl.class)
public class StockWorkshopDetailServiceImplTest extends BaseDbUnitTest {

    @Resource
    private StockWorkshopDetailServiceImpl stockWorkshopDetailService;

    @Resource
    private StockWorkshopDetailMapper stockWorkshopDetailMapper;

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

        // 调用
        Long stockWorkshopDetailId = stockWorkshopDetailService.createStockWorkshopDetail(createReqVO);
        // 断言
        assertNotNull(stockWorkshopDetailId);
        // 校验记录的属性是否正确
        StockWorkshopDetailDO stockWorkshopDetail = stockWorkshopDetailMapper.selectById(stockWorkshopDetailId);
        assertPojoEquals(createReqVO, stockWorkshopDetail, "id");
    }

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

        // 调用
        stockWorkshopDetailService.updateStockWorkshopDetail(updateReqVO);
        // 校验是否更新正确
        StockWorkshopDetailDO stockWorkshopDetail = stockWorkshopDetailMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, stockWorkshopDetail);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> stockWorkshopDetailService.updateStockWorkshopDetail(updateReqVO), STOCK_WORKSHOP_DETAIL_NOT_EXISTS);
    }

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

        // 调用
        stockWorkshopDetailService.deleteStockWorkshopDetail(id);
       // 校验数据不存在了
       assertNull(stockWorkshopDetailMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> stockWorkshopDetailService.deleteStockWorkshopDetail(id), STOCK_WORKSHOP_DETAIL_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetStockWorkshopDetailPage() {
       // mock 数据
       StockWorkshopDetailDO dbStockWorkshopDetail = randomPojo(StockWorkshopDetailDO.class, o -> { // 等会查询到
           o.setItemId(null);
           o.setCount(null);
           o.setDetailType(null);
           o.setItemRequisitionCode(null);
           o.setItemRequisitionDetailId(null);
           o.setFeedingRecordCode(null);
           o.setFeedingRecordId(null);
           o.setUserId(null);
           o.setOperateTime(null);
           o.setCreateTime(null);
       });
       stockWorkshopDetailMapper.insert(dbStockWorkshopDetail);
       // 测试 itemId 不匹配
       stockWorkshopDetailMapper.insert(cloneIgnoreId(dbStockWorkshopDetail, o -> o.setItemId(null)));
       // 测试 count 不匹配
       stockWorkshopDetailMapper.insert(cloneIgnoreId(dbStockWorkshopDetail, o -> o.setCount(null)));
       // 测试 detailType 不匹配
       stockWorkshopDetailMapper.insert(cloneIgnoreId(dbStockWorkshopDetail, o -> o.setDetailType(null)));
       // 测试 itemRequisitionCode 不匹配
       stockWorkshopDetailMapper.insert(cloneIgnoreId(dbStockWorkshopDetail, o -> o.setItemRequisitionCode(null)));
       // 测试 itemRequisitionDetailId 不匹配
       stockWorkshopDetailMapper.insert(cloneIgnoreId(dbStockWorkshopDetail, o -> o.setItemRequisitionDetailId(null)));
       // 测试 feedingRecordCode 不匹配
       stockWorkshopDetailMapper.insert(cloneIgnoreId(dbStockWorkshopDetail, o -> o.setFeedingRecordCode(null)));
       // 测试 feedingRecordId 不匹配
       stockWorkshopDetailMapper.insert(cloneIgnoreId(dbStockWorkshopDetail, o -> o.setFeedingRecordId(null)));
       // 测试 userId 不匹配
       stockWorkshopDetailMapper.insert(cloneIgnoreId(dbStockWorkshopDetail, o -> o.setUserId(null)));
       // 测试 operateTime 不匹配
       stockWorkshopDetailMapper.insert(cloneIgnoreId(dbStockWorkshopDetail, o -> o.setOperateTime(null)));
       // 测试 createTime 不匹配
       stockWorkshopDetailMapper.insert(cloneIgnoreId(dbStockWorkshopDetail, o -> o.setCreateTime(null)));
       // 准备参数
       StockWorkshopDetailPageReqVO reqVO = new StockWorkshopDetailPageReqVO();
       reqVO.setItemId(null);
       reqVO.setCount(null);
       reqVO.setDetailType(null);
       reqVO.setItemRequisitionCode(null);
       reqVO.setItemRequisitionDetailId(null);
       reqVO.setFeedingRecordCode(null);
       reqVO.setFeedingRecordId(null);
       reqVO.setUserId(null);
       reqVO.setOperateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<StockWorkshopDetailDO> pageResult = stockWorkshopDetailService.getStockWorkshopDetailPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbStockWorkshopDetail, pageResult.getList().get(0));
    }

}