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

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

    @Resource
    private UserTagGroupServiceImpl userTagGroupService;

    @Resource
    private UserTagGroupMapper userTagGroupMapper;

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

        // 调用
        Long userTagGroupId = userTagGroupService.createUserTagGroup(reqVO);
        // 断言
        assertNotNull(userTagGroupId);
        // 校验记录的属性是否正确
        UserTagGroupDO userTagGroup = userTagGroupMapper.selectById(userTagGroupId);
        assertPojoEquals(reqVO, userTagGroup);
    }

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

        // 调用
        userTagGroupService.updateUserTagGroup(reqVO);
        // 校验是否更新正确
        UserTagGroupDO userTagGroup = userTagGroupMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, userTagGroup);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> userTagGroupService.updateUserTagGroup(reqVO), USER_TAG_GROUP_NOT_EXISTS);
    }

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

        // 调用
        userTagGroupService.deleteUserTagGroup(id);
        // 校验数据不存在了
        assertNull(userTagGroupMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> userTagGroupService.deleteUserTagGroup(id), USER_TAG_GROUP_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetUserTagGroupPage() {
        // mock 数据
        UserTagGroupDO dbUserTagGroup = randomPojo(UserTagGroupDO.class, o -> { // 等会查询到
            o.setGroupName(null);
            o.setSorted(null);
            o.setCreateTime(null);
        });
        userTagGroupMapper.insert(dbUserTagGroup);
        // 测试 groupName 不匹配
        userTagGroupMapper.insert(cloneIgnoreId(dbUserTagGroup, o -> o.setGroupName(null)));
        // 测试 sorted 不匹配
        userTagGroupMapper.insert(cloneIgnoreId(dbUserTagGroup, o -> o.setSorted(null)));
        // 测试 createTime 不匹配
        userTagGroupMapper.insert(cloneIgnoreId(dbUserTagGroup, o -> o.setCreateTime(null)));
        // 准备参数
        UserTagGroupPageReqVO reqVO = new UserTagGroupPageReqVO();
        reqVO.setGroupName(null);
        reqVO.setSorted(null);
        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

        // 调用
        PageResult<UserTagGroupDO> pageResult = userTagGroupService.getUserTagGroupPage(reqVO);
        // 断言
        assertEquals(1, pageResult.getTotal());
        assertEquals(1, pageResult.getList().size());
        assertPojoEquals(dbUserTagGroup, pageResult.getList().get(0));
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetUserTagGroupList() {
        // mock 数据
        UserTagGroupDO dbUserTagGroup = randomPojo(UserTagGroupDO.class, o -> { // 等会查询到
            o.setGroupName(null);
            o.setSorted(null);
            o.setCreateTime(null);
        });
        userTagGroupMapper.insert(dbUserTagGroup);
        // 测试 groupName 不匹配
        userTagGroupMapper.insert(cloneIgnoreId(dbUserTagGroup, o -> o.setGroupName(null)));
        // 测试 sorted 不匹配
        userTagGroupMapper.insert(cloneIgnoreId(dbUserTagGroup, o -> o.setSorted(null)));
        // 测试 createTime 不匹配
        userTagGroupMapper.insert(cloneIgnoreId(dbUserTagGroup, o -> o.setCreateTime(null)));
        // 准备参数
        UserTagGroupExportReqVO reqVO = new UserTagGroupExportReqVO();
        reqVO.setGroupName(null);
        reqVO.setSorted(null);
        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

        // 调用
        List<UserTagGroupDO> list = userTagGroupService.getUserTagGroupList(reqVO);
        // 断言
        assertEquals(1, list.size());
        assertPojoEquals(dbUserTagGroup, list.get(0));
    }

}
