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

import cn.iocoder.yudao.module.basic.dal.dataobject.goodscategory.GoodsCategoryDO;
import cn.iocoder.yudao.module.basic.dal.mysql.goodscategory.GoodsCategoryMapper;
import cn.iocoder.yudao.module.basic.enums.ErrorCodeConstants;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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 cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;

import cn.iocoder.yudao.module.basic.controller.admin.businessinfo.vo.*;
import cn.iocoder.yudao.module.basic.dal.dataobject.businessinfo.BusinessInfoDO;
import cn.iocoder.yudao.module.basic.dal.mysql.businessinfo.BusinessInfoMapper;
import cn.iocoder.yudao.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 cn.iocoder.yudao.framework.common.exception.ErrorCode.*;
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.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 BusinessInfoServiceImpl} 的单元测试类
*
* @author 芋道源码
*/
@Import(BusinessInfoServiceImpl.class)
public class BusinessInfoServiceImplTest extends BaseDbUnitTest {

    @Resource
    private BusinessInfoServiceImpl businessInfoService;

    @Resource
    private BusinessInfoMapper businessInfoMapper;
    @Resource
    private GoodsCategoryMapper goodsCategoryMapper;

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

        // 调用
        Long businessInfoId = businessInfoService.createBusinessInfo(reqVO);
        // 断言
        assertNotNull(businessInfoId);
        // 校验记录的属性是否正确
        BusinessInfoDO businessInfo = businessInfoMapper.selectById(businessInfoId);
        assertPojoEquals(reqVO, businessInfo);
    }

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

        // 调用
        businessInfoService.updateBusinessInfo(reqVO);
        // 校验是否更新正确
        BusinessInfoDO businessInfo = businessInfoMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, businessInfo);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> businessInfoService.updateBusinessInfo(reqVO), ErrorCodeConstants.BUSINESS_INFO_NOT_EXISTS);
    }

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

        // 调用
        businessInfoService.deleteBusinessInfo(id);
       // 校验数据不存在了
       assertNull(businessInfoMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> businessInfoService.deleteBusinessInfo(id), ErrorCodeConstants.BUSINESS_INFO_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetBusinessInfoPage() {
       // mock 数据
       BusinessInfoDO dbBusinessInfo = randomPojo(BusinessInfoDO.class, o -> { // 等会查询到
           o.setBusinessName(null);
       });
       businessInfoMapper.insert(dbBusinessInfo);
       // 测试 businessName 不匹配
       businessInfoMapper.insert(cloneIgnoreId(dbBusinessInfo, o -> o.setBusinessName(null)));
       // 准备参数
       BusinessInfoPageReqVO reqVO = new BusinessInfoPageReqVO();
       reqVO.setBusinessName(null);

       // 调用
       PageResult<BusinessInfoDO> pageResult = businessInfoService.getBusinessInfoPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbBusinessInfo, pageResult.getList().get(0));
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetBusinessInfoList1() {
        // mock 数据
        List<GoodsCategoryDO> level1CategorieList =
                goodsCategoryMapper.selectList(new QueryWrapper<GoodsCategoryDO>().eq("level", 1));
        System.out.println("level1CategorieList = " + level1CategorieList);
    }

}
