package co.yixiang.yshop.module.member.service.userlevelgrowthvalueconfig;

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 co.yixiang.yshop.framework.test.core.ut.BaseDbUnitTest;

import co.yixiang.yshop.module.member.controller.admin.userlevelgrowthvalueconfig.vo.*;
import co.yixiang.yshop.module.member.dal.dataobject.userlevelgrowthvalueconfig.UserLevelGrowthValueConfigDO;
import co.yixiang.yshop.module.member.dal.mysql.userlevelgrowthvalueconfig.UserLevelGrowthValueConfigMapper;
import co.yixiang.yshop.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 co.yixiang.yshop.module.member.enums.ErrorCodeConstants.*;
import static co.yixiang.yshop.framework.test.core.util.AssertUtils.*;
import static co.yixiang.yshop.framework.test.core.util.RandomUtils.*;
import static co.yixiang.yshop.framework.common.util.date.LocalDateTimeUtils.*;
import static co.yixiang.yshop.framework.common.util.object.ObjectUtils.*;
import static co.yixiang.yshop.framework.common.util.date.DateUtils.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
 * {@link UserLevelGrowthValueConfigServiceImpl} 的单元测试类
 *
 * @author admin
 */
@Import(UserLevelGrowthValueConfigServiceImpl.class)
public class UserLevelGrowthValueConfigServiceImplTest extends BaseDbUnitTest {

    @Resource
    private UserLevelGrowthValueConfigServiceImpl userLevelGrowthValueConfigService;

    @Resource
    private UserLevelGrowthValueConfigMapper userLevelGrowthValueConfigMapper;

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

        // 调用
        Integer userLevelGrowthValueConfigId = userLevelGrowthValueConfigService.createUserLevelGrowthValueConfig(reqVO);
        // 断言
        assertNotNull(userLevelGrowthValueConfigId);
        // 校验记录的属性是否正确
        UserLevelGrowthValueConfigDO userLevelGrowthValueConfig = userLevelGrowthValueConfigMapper.selectById(userLevelGrowthValueConfigId);
        assertPojoEquals(reqVO, userLevelGrowthValueConfig);
    }

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

        // 调用
        userLevelGrowthValueConfigService.updateUserLevelGrowthValueConfig(reqVO);
        // 校验是否更新正确
        UserLevelGrowthValueConfigDO userLevelGrowthValueConfig = userLevelGrowthValueConfigMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, userLevelGrowthValueConfig);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> userLevelGrowthValueConfigService.updateUserLevelGrowthValueConfig(reqVO), USER_LEVEL_GROWTH_VALUE_CONFIG_NOT_EXISTS);
    }

    @Test
    public void testDeleteUserLevelGrowthValueConfig_success() {
        // mock 数据
        UserLevelGrowthValueConfigDO dbUserLevelGrowthValueConfig = randomPojo(UserLevelGrowthValueConfigDO.class);
        userLevelGrowthValueConfigMapper.insert(dbUserLevelGrowthValueConfig);// @Sql: 先插入出一条存在的数据
        // 准备参数
        Integer id = dbUserLevelGrowthValueConfig.getId();

        // 调用
        userLevelGrowthValueConfigService.deleteUserLevelGrowthValueConfig(id);
        // 校验数据不存在了
        assertNull(userLevelGrowthValueConfigMapper.selectById(id));
    }

    @Test
    public void testDeleteUserLevelGrowthValueConfig_notExists() {
        // 准备参数
        Integer id = randomIntegerId();

        // 调用, 并断言异常
        assertServiceException(() -> userLevelGrowthValueConfigService.deleteUserLevelGrowthValueConfig(id), USER_LEVEL_GROWTH_VALUE_CONFIG_NOT_EXISTS);
    }

    private Integer randomIntegerId() {
        return 1;
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetUserLevelGrowthValueConfigPage() {
        // mock 数据
        UserLevelGrowthValueConfigDO dbUserLevelGrowthValueConfig = randomPojo(UserLevelGrowthValueConfigDO.class, o -> { // 等会查询到
            o.setType(null);
            o.setTypeName(null);
            o.setGrowthValue(null);
            o.setCreateTime(null);
        });
        userLevelGrowthValueConfigMapper.insert(dbUserLevelGrowthValueConfig);
        // 测试 type 不匹配
        userLevelGrowthValueConfigMapper.insert(cloneIgnoreId(dbUserLevelGrowthValueConfig, o -> o.setType(null)));
        // 测试 typeName 不匹配
        userLevelGrowthValueConfigMapper.insert(cloneIgnoreId(dbUserLevelGrowthValueConfig, o -> o.setTypeName(null)));
        // 测试 growthValue 不匹配
        userLevelGrowthValueConfigMapper.insert(cloneIgnoreId(dbUserLevelGrowthValueConfig, o -> o.setGrowthValue(null)));
        // 测试 createTime 不匹配
        userLevelGrowthValueConfigMapper.insert(cloneIgnoreId(dbUserLevelGrowthValueConfig, o -> o.setCreateTime(null)));
        // 准备参数
        UserLevelGrowthValueConfigPageReqVO reqVO = new UserLevelGrowthValueConfigPageReqVO();
        reqVO.setType(null);
        reqVO.setTypeName(null);
        reqVO.setGrowthValue(null);
        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

        // 调用
        PageResult<UserLevelGrowthValueConfigDO> pageResult = userLevelGrowthValueConfigService.getUserLevelGrowthValueConfigPage(reqVO);
        // 断言
        assertEquals(1, pageResult.getTotal());
        assertEquals(1, pageResult.getList().size());
        assertPojoEquals(dbUserLevelGrowthValueConfig, pageResult.getList().get(0));
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetUserLevelGrowthValueConfigList() {
        // mock 数据
        UserLevelGrowthValueConfigDO dbUserLevelGrowthValueConfig = randomPojo(UserLevelGrowthValueConfigDO.class, o -> { // 等会查询到
            o.setType(null);
            o.setTypeName(null);
            o.setGrowthValue(null);
            o.setCreateTime(null);
        });
        userLevelGrowthValueConfigMapper.insert(dbUserLevelGrowthValueConfig);
        // 测试 type 不匹配
        userLevelGrowthValueConfigMapper.insert(cloneIgnoreId(dbUserLevelGrowthValueConfig, o -> o.setType(null)));
        // 测试 typeName 不匹配
        userLevelGrowthValueConfigMapper.insert(cloneIgnoreId(dbUserLevelGrowthValueConfig, o -> o.setTypeName(null)));
        // 测试 growthValue 不匹配
        userLevelGrowthValueConfigMapper.insert(cloneIgnoreId(dbUserLevelGrowthValueConfig, o -> o.setGrowthValue(null)));
        // 测试 createTime 不匹配
        userLevelGrowthValueConfigMapper.insert(cloneIgnoreId(dbUserLevelGrowthValueConfig, o -> o.setCreateTime(null)));
        // 准备参数
        UserLevelGrowthValueConfigExportReqVO reqVO = new UserLevelGrowthValueConfigExportReqVO();
        reqVO.setType(null);
        reqVO.setTypeName(null);
        reqVO.setGrowthValue(null);
        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

        // 调用
        List<UserLevelGrowthValueConfigDO> list = userLevelGrowthValueConfigService.getUserLevelGrowthValueConfigList(reqVO);
        // 断言
        assertEquals(1, list.size());
        assertPojoEquals(dbUserLevelGrowthValueConfig, list.get(0));
    }

}
