package cn.iocoder.yudao.module.basic.service.goodsinfo;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.basic.controller.admin.goodsinfo.vo.GoodsInfoCreateReqVO;
import cn.iocoder.yudao.module.basic.controller.admin.goodsinfo.vo.GoodsInfoPageReqVO;
import cn.iocoder.yudao.module.basic.controller.admin.goodsinfo.vo.GoodsInfoUpdateReqVO;
import cn.iocoder.yudao.module.basic.dal.dataobject.goodsinfo.GoodsInfoDO;
import cn.iocoder.yudao.module.basic.dal.mysql.goodsinfo.GoodsInfoMapper;
import cn.iocoder.yudao.module.basic.enums.ErrorCodeConstants;
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.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.randomPojo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* {@link GoodsInfoServiceImpl} 的单元测试类
*
* @author 芋道源码
*/
@Import(GoodsInfoServiceImpl.class)
public class GoodsInfoServiceImplTest extends BaseDbUnitTest {

    @Resource
    private GoodsInfoServiceImpl goodsInfoService;

    @Resource
    private GoodsInfoMapper goodsInfoMapper;

    @Test
    public void testCreateGoodsInfo_success() {
        // 准备参数
        GoodsInfoCreateReqVO reqVO = randomPojo(GoodsInfoCreateReqVO.class);

        // 调用
        Long goodsInfoId = goodsInfoService.createGoodsInfo(reqVO);
        // 断言
        assertNotNull(goodsInfoId);
        // 校验记录的属性是否正确
        GoodsInfoDO goodsInfo = goodsInfoMapper.selectById(goodsInfoId);
        assertPojoEquals(reqVO, goodsInfo);
    }

    @Test
    public void testUpdateGoodsInfo_success() {
        // mock 数据
        GoodsInfoDO dbGoodsInfo = randomPojo(GoodsInfoDO.class);
        goodsInfoMapper.insert(dbGoodsInfo);// @Sql: 先插入出一条存在的数据
        // 准备参数
        GoodsInfoUpdateReqVO reqVO = randomPojo(GoodsInfoUpdateReqVO.class, o -> {
            o.setId(dbGoodsInfo.getId()); // 设置更新的 ID
        });

        // 调用
        goodsInfoService.updateGoodsInfo(reqVO);
        // 校验是否更新正确
        GoodsInfoDO goodsInfo = goodsInfoMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, goodsInfo);
    }

    @Test
    public void testUpdateGoodsInfo_notExists() {
        // 准备参数
        GoodsInfoUpdateReqVO reqVO = randomPojo(GoodsInfoUpdateReqVO.class);

        // 调用, 并断言异常
        assertServiceException(() -> goodsInfoService.updateGoodsInfo(reqVO), ErrorCodeConstants.GOODS_INFO_NOT_EXISTS);
    }

//    @Test
//    public void testDeleteGoodsInfo_success() {
//        // mock 数据
//        GoodsInfoDO dbGoodsInfo = randomPojo(GoodsInfoDO.class);
//        goodsInfoMapper.insert(dbGoodsInfo);// @Sql: 先插入出一条存在的数据
//        // 准备参数
//        Long id = dbGoodsInfo.getId();
//
//        // 调用
//        goodsInfoService.deleteGoodsInfo(id);
//       // 校验数据不存在了
//       assertNull(goodsInfoMapper.selectById(id));
//    }

//    @Test
//    public void testDeleteGoodsInfo_notExists() {
//        // 准备参数
//        Long id = randomLongId();
//
//        // 调用, 并断言异常
//        assertServiceException(() -> goodsInfoService.deleteGoodsInfo(id), ErrorCodeConstants.GOODS_INFO_NOT_EXISTS);
//    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetGoodsInfoPage() {
       // mock 数据
       GoodsInfoDO dbGoodsInfo = randomPojo(GoodsInfoDO.class, o -> { // 等会查询到
           o.setGoodsName(null);
           o.setGoodsId(null);
           o.setGoodsType(null);
           o.setGoodsTypeId(null);
           o.setGoodsUnitId(null);
           o.setIsBaseUnit(null);
           o.setConversionValue(null);
           o.setGoodsSpecs(null);
           o.setGoodsRemarks(null);
           o.setGoodsImgUrl(null);
           o.setShelfLife(null);
           o.setBelongBusinessId(null);
           o.setBelongBusinessName(null);
           o.setCreateTime(null);
       });
       goodsInfoMapper.insert(dbGoodsInfo);
       // 测试 goodsName 不匹配
       goodsInfoMapper.insert(cloneIgnoreId(dbGoodsInfo, o -> o.setGoodsName(null)));
       // 测试 goodsId 不匹配
       goodsInfoMapper.insert(cloneIgnoreId(dbGoodsInfo, o -> o.setGoodsId(null)));
       // 测试 goodsType 不匹配
       goodsInfoMapper.insert(cloneIgnoreId(dbGoodsInfo, o -> o.setGoodsType(null)));
       // 测试 goodsTypeId 不匹配
       goodsInfoMapper.insert(cloneIgnoreId(dbGoodsInfo, o -> o.setGoodsTypeId(null)));
       // 测试 goodsUnit 不匹配
       goodsInfoMapper.insert(cloneIgnoreId(dbGoodsInfo, o -> o.setGoodsUnitId(null)));
       // 测试 goodsUnit 不匹配
        goodsInfoMapper.insert(cloneIgnoreId(dbGoodsInfo, o -> o.setIsBaseUnit(null)));
        // 测试 goodsUnit 不匹配
        goodsInfoMapper.insert(cloneIgnoreId(dbGoodsInfo, o -> o.setConversionValue(null)));
       // 测试 goodsSpecs 不匹配
       goodsInfoMapper.insert(cloneIgnoreId(dbGoodsInfo, o -> o.setGoodsSpecs(null)));
       // 测试 goodsRemarks 不匹配
       goodsInfoMapper.insert(cloneIgnoreId(dbGoodsInfo, o -> o.setGoodsRemarks(null)));
       // 测试 goodsImgUrl 不匹配
       goodsInfoMapper.insert(cloneIgnoreId(dbGoodsInfo, o -> o.setGoodsImgUrl(null)));
       // 测试 shelfLife 不匹配
       goodsInfoMapper.insert(cloneIgnoreId(dbGoodsInfo, o -> o.setShelfLife(null)));
       // 测试 belongBusinessId 不匹配
       goodsInfoMapper.insert(cloneIgnoreId(dbGoodsInfo, o -> o.setBelongBusinessId(null)));
       // 测试 belongBusinessName 不匹配
       goodsInfoMapper.insert(cloneIgnoreId(dbGoodsInfo, o -> o.setBelongBusinessName(null)));
       // 测试 createTime 不匹配
       goodsInfoMapper.insert(cloneIgnoreId(dbGoodsInfo, o -> o.setCreateTime(null)));
       // 准备参数
       GoodsInfoPageReqVO reqVO = new GoodsInfoPageReqVO();
       reqVO.setGoodsName(null);
       reqVO.setGoodsId(null);
       reqVO.setGoodsType(null);
       reqVO.setGoodsTypeId(null);
       reqVO.setGoodsUnitId(null);
       reqVO.setIsBaseUnit(null);
       reqVO.setConversionValue(null);
       reqVO.setGoodsSpecs(null);
       reqVO.setGoodsImgUrl(null);
       reqVO.setShelfLife(null);
       reqVO.setBelongBusinessId(null);
       reqVO.setBelongBusinessName(null);
       //reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<GoodsInfoDO> pageResult = goodsInfoService.getGoodsInfoPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbGoodsInfo, pageResult.getList().get(0));
    }

}
