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

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.userlevelequity.vo.*;
import co.yixiang.yshop.module.member.dal.dataobject.userlevelequity.UserLevelEquityDO;
import co.yixiang.yshop.module.member.dal.mysql.userlevelequity.UserLevelEquityMapper;
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 UserLevelEquityServiceImpl} 的单元测试类
 *
 * @author admin
 */
@Import(UserLevelEquityServiceImpl.class)
public class UserLevelEquityServiceImplTest extends BaseDbUnitTest {

    @Resource
    private UserLevelEquityServiceImpl userLevelEquityService;

    @Resource
    private UserLevelEquityMapper userLevelEquityMapper;

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

        // 调用
        Long userLevelEquityId = userLevelEquityService.createUserLevelEquity(reqVO);
        // 断言
        assertNotNull(userLevelEquityId);
        // 校验记录的属性是否正确
        UserLevelEquityDO userLevelEquity = userLevelEquityMapper.selectById(userLevelEquityId);
        assertPojoEquals(reqVO, userLevelEquity);
    }

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

        // 调用
        userLevelEquityService.updateUserLevelEquity(reqVO);
        // 校验是否更新正确
        UserLevelEquityDO userLevelEquity = userLevelEquityMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, userLevelEquity);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> userLevelEquityService.updateUserLevelEquity(reqVO), USER_LEVEL_EQUITY_NOT_EXISTS);
    }

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

        // 调用
        userLevelEquityService.deleteUserLevelEquity(id);
        // 校验数据不存在了
        assertNull(userLevelEquityMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> userLevelEquityService.deleteUserLevelEquity(id), USER_LEVEL_EQUITY_NOT_EXISTS);
    }

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

        // 调用
        PageResult<UserLevelEquityDO> pageResult = userLevelEquityService.getUserLevelEquityPage(reqVO);
        // 断言
        assertEquals(1, pageResult.getTotal());
        assertEquals(1, pageResult.getList().size());
        assertPojoEquals(dbUserLevelEquity, pageResult.getList().get(0));
    }

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

        // 调用
        List<UserLevelEquityDO> list = userLevelEquityService.getUserLevelEquityList(reqVO);
        // 断言
        assertEquals(1, list.size());
        assertPojoEquals(dbUserLevelEquity, list.get(0));
    }

}
