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

    @Resource
    private StockWorkshopServiceImpl stockWorkshopService;

    @Resource
    private StockWorkshopMapper stockWorkshopMapper;

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

        // 调用
        Long stockWorkshopId = stockWorkshopService.createStockWorkshop(createReqVO);
        // 断言
        assertNotNull(stockWorkshopId);
        // 校验记录的属性是否正确
        StockWorkshopDO stockWorkshop = stockWorkshopMapper.selectById(stockWorkshopId);
        assertPojoEquals(createReqVO, stockWorkshop, "id");
    }

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

        // 调用
        stockWorkshopService.updateStockWorkshop(updateReqVO);
        // 校验是否更新正确
        StockWorkshopDO stockWorkshop = stockWorkshopMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, stockWorkshop);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> stockWorkshopService.updateStockWorkshop(updateReqVO), STOCK_WORKSHOP_NOT_EXISTS);
    }

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

        // 调用
        stockWorkshopService.deleteStockWorkshop(id);
       // 校验数据不存在了
       assertNull(stockWorkshopMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> stockWorkshopService.deleteStockWorkshop(id), STOCK_WORKSHOP_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetStockWorkshopPage() {
       // mock 数据
       StockWorkshopDO dbStockWorkshop = randomPojo(StockWorkshopDO.class, o -> { // 等会查询到
           o.setItemId(null);
           o.setCount(null);
           o.setCategoryId(null);
           o.setCreateTime(null);
       });
       stockWorkshopMapper.insert(dbStockWorkshop);
       // 测试 itemId 不匹配
       stockWorkshopMapper.insert(cloneIgnoreId(dbStockWorkshop, o -> o.setItemId(null)));
       // 测试 count 不匹配
       stockWorkshopMapper.insert(cloneIgnoreId(dbStockWorkshop, o -> o.setCount(null)));
       // 测试 categoryId 不匹配
       stockWorkshopMapper.insert(cloneIgnoreId(dbStockWorkshop, o -> o.setCategoryId(null)));
       // 测试 createTime 不匹配
       stockWorkshopMapper.insert(cloneIgnoreId(dbStockWorkshop, o -> o.setCreateTime(null)));
       // 准备参数
       StockWorkshopPageReqVO reqVO = new StockWorkshopPageReqVO();
       reqVO.setItemId(null);
       reqVO.setCount(null);
       reqVO.setCategoryId(null);
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<StockWorkshopDO> pageResult = stockWorkshopService.getStockWorkshopPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbStockWorkshop, pageResult.getList().get(0));
    }

}