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 javax.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 javax.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 testCreateGoods_success() {
//        // 准备参数
//        GoodsSaveReqVO createReqVO = randomPojo(GoodsSaveReqVO.class).setId(null);
//
//        // 调用
//        Long goodsId = goodsService.createCorrespondsGoods(createReqVO);
//        // 断言
//        assertNotNull(goodsId);
//        // 校验记录的属性是否正确
//        GoodsDO goods = goodsMapper.selectById(goodsId);
//        assertPojoEquals(createReqVO, goods, "id");
    }

    @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);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetGoodsPage() {
      /* // mock 数据
       GoodsDO dbGoods = randomPojo(GoodsDO.class, o -> { // 等会查询到
           o.setOrderId(null);
           o.setGoodsId(null);
           o.setGoodsName(null);
           o.setMaterial(null);
           o.setColor(null);
           o.setWidth(null);
           o.setHeight(null);
           o.setThickness(null);
           o.setPrice(null);
           o.setBrand(null);
           o.setSpec(null);
           o.setRemark(null);
           o.setCreateTime(null);
       });
       goodsMapper.insert(dbGoods);
       // 测试 orderNo 不匹配
       goodsMapper.insert(cloneIgnoreId(dbGoods, o -> o.setOrderId(null)));
       // 测试 goodsId 不匹配
       goodsMapper.insert(cloneIgnoreId(dbGoods, o -> o.setGoodsId(null)));
       // 测试 goodsName 不匹配
       goodsMapper.insert(cloneIgnoreId(dbGoods, o -> o.setGoodsName(null)));
       // 测试 material 不匹配
       goodsMapper.insert(cloneIgnoreId(dbGoods, o -> o.setMaterial(null)));
       // 测试 color 不匹配
       goodsMapper.insert(cloneIgnoreId(dbGoods, o -> o.setColor(null)));
       // 测试 width 不匹配
       goodsMapper.insert(cloneIgnoreId(dbGoods, o -> o.setWidth(null)));
       // 测试 height 不匹配
       goodsMapper.insert(cloneIgnoreId(dbGoods, o -> o.setHeight(null)));
       // 测试 thickness 不匹配
       goodsMapper.insert(cloneIgnoreId(dbGoods, o -> o.setThickness(null)));
       // 测试 price 不匹配
       goodsMapper.insert(cloneIgnoreId(dbGoods, o -> o.setPrice(null)));
       // 测试 brand 不匹配
       goodsMapper.insert(cloneIgnoreId(dbGoods, o -> o.setBrand(null)));
       // 测试 spec 不匹配
       goodsMapper.insert(cloneIgnoreId(dbGoods, o -> o.setSpec(null)));
       // 测试 remark 不匹配
       goodsMapper.insert(cloneIgnoreId(dbGoods, o -> o.setRemark(null)));
       // 测试 createTime 不匹配
       goodsMapper.insert(cloneIgnoreId(dbGoods, o -> o.setCreateTime(null)));
       // 准备参数
       GoodsPageReqVO reqVO = new GoodsPageReqVO();
       reqVO.setOrderId(null);
       reqVO.setGoodsId(null);
       reqVO.setGoodsName(null);
       reqVO.setMaterial(null);
       reqVO.setColor(null);
       reqVO.setWidth(null);
       reqVO.setHeight(null);
       reqVO.setThickness(null);
       reqVO.setPrice(null);
       reqVO.setBrand(null);
       reqVO.setSpec(null);
       reqVO.setRemark(null);
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<GoodsDO> pageResult = goodsService.getGoodsPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbGoods, pageResult.getList().get(0));*/
    }

}