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

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

    @Resource
    private UserTagRefServiceImpl userTagRefService;

    @Resource
    private UserTagRefMapper userTagRefMapper;

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

        // 调用
        Long userTagRefId = userTagRefService.createUserTagRef(reqVO);
        // 断言
        assertNotNull(userTagRefId);
        // 校验记录的属性是否正确
        UserTagRefDO userTagRef = userTagRefMapper.selectById(userTagRefId);
        assertPojoEquals(reqVO, userTagRef);
    }

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

        // 调用
        userTagRefService.updateUserTagRef(reqVO);
        // 校验是否更新正确
        UserTagRefDO userTagRef = userTagRefMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, userTagRef);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> userTagRefService.updateUserTagRef(reqVO), USER_TAG_REF_NOT_EXISTS);
    }

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

        // 调用
        userTagRefService.deleteUserTagRef(id);
        // 校验数据不存在了
        assertNull(userTagRefMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> userTagRefService.deleteUserTagRef(id), USER_TAG_REF_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetUserTagRefPage() {
        // mock 数据
        UserTagRefDO dbUserTagRef = randomPojo(UserTagRefDO.class, o -> { // 等会查询到
            o.setUserId(null);
            o.setTagId(null);
            o.setCreateTime(null);
        });
        userTagRefMapper.insert(dbUserTagRef);
        // 测试 userId 不匹配
        userTagRefMapper.insert(cloneIgnoreId(dbUserTagRef, o -> o.setUserId(null)));
        // 测试 tagId 不匹配
        userTagRefMapper.insert(cloneIgnoreId(dbUserTagRef, o -> o.setTagId(null)));
        // 测试 createTime 不匹配
        userTagRefMapper.insert(cloneIgnoreId(dbUserTagRef, o -> o.setCreateTime(null)));
        // 准备参数
        UserTagRefPageReqVO reqVO = new UserTagRefPageReqVO();
        reqVO.setUserId(null);
        reqVO.setTagId(null);
        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

        // 调用
        PageResult<UserTagRefDO> pageResult = userTagRefService.getUserTagRefPage(reqVO);
        // 断言
        assertEquals(1, pageResult.getTotal());
        assertEquals(1, pageResult.getList().size());
        assertPojoEquals(dbUserTagRef, pageResult.getList().get(0));
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetUserTagRefList() {
        // mock 数据
        UserTagRefDO dbUserTagRef = randomPojo(UserTagRefDO.class, o -> { // 等会查询到
            o.setUserId(null);
            o.setTagId(null);
            o.setCreateTime(null);
        });
        userTagRefMapper.insert(dbUserTagRef);
        // 测试 userId 不匹配
        userTagRefMapper.insert(cloneIgnoreId(dbUserTagRef, o -> o.setUserId(null)));
        // 测试 tagId 不匹配
        userTagRefMapper.insert(cloneIgnoreId(dbUserTagRef, o -> o.setTagId(null)));
        // 测试 createTime 不匹配
        userTagRefMapper.insert(cloneIgnoreId(dbUserTagRef, o -> o.setCreateTime(null)));
        // 准备参数
        UserTagRefExportReqVO reqVO = UserTagRefExportReqVO.builder().createTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28)).build();
        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

        // 调用
        List<UserTagRefDO> list = userTagRefService.getUserTagRefList(reqVO);
        // 断言
        assertEquals(1, list.size());
        assertPojoEquals(dbUserTagRef, list.get(0));
    }

}
