package com.cf.imes.module.executor.service.goods;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;

import jakarta.annotation.Resource;

import com.cf.imes.framework.test.core.ut.BaseDbUnitTest;

import com.cf.imes.module.executor.controller.admin.goods.vo.*;
import com.cf.imes.module.executor.dal.dataobject.goods.GoodsDO;
import com.cf.imes.module.executor.dal.mysql.goods.GoodsMapper;
import com.cf.imes.framework.common.pojo.PageResult;

import jakarta.annotation.Resource;
import org.springframework.context.annotation.Import;
import java.util.*;
import java.time.LocalDateTime;

import static cn.hutool.core.util.RandomUtil.*;
import static com.cf.imes.module.executor.enums.ErrorCodeConstants.*;
import static com.cf.imes.framework.test.core.util.AssertUtils.*;
import static com.cf.imes.framework.test.core.util.RandomUtils.*;
import static com.cf.imes.framework.common.util.date.LocalDateTimeUtils.*;
import static com.cf.imes.framework.common.util.object.ObjectUtils.*;
import static com.cf.imes.framework.common.util.date.DateUtils.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
 * {@link GoodsServiceImpl} 的单元测试类
 *
 * @author 晨丰科技
 */
@Import(GoodsServiceImpl.class)
public class GoodsServiceImplTest extends BaseDbUnitTest {

    @Resource
    private GoodsServiceImpl goodsService;

    @Resource
    private GoodsMapper goodsMapper;

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

        // 调用
        goodsService.updateGoods(updateReqVO);
        // 校验是否更新正确
        GoodsDO goods = goodsMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, goods);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> goodsService.updateGoods(updateReqVO), GOODS_NOT_EXISTS);
    }

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

        // 调用
        goodsService.deleteGoods(id);
       // 校验数据不存在了
       assertNull(goodsMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> goodsService.deleteGoods(id), GOODS_NOT_EXISTS);
    }

}