package co.yixiang.yshop.module.distributor.service.distributoruser;

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.distributor.controller.admin.distributoruser.vo.*;
import co.yixiang.yshop.module.distributor.dal.dataobject.distributoruser.DistributorUserDO;
import co.yixiang.yshop.module.distributor.dal.mysql.distributoruser.DistributorUserMapper;
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.distributor.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 DistributorUserServiceImpl} 的单元测试类
 *
 * @author admin
 */
@Import(DistributorUserServiceImpl.class)
public class DistributorUserServiceImplTest extends BaseDbUnitTest {

    @Resource
    private DistributorUserServiceImpl userService;

    @Resource
    private DistributorUserMapper userMapper;

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

        // 调用
        Long userId = userService.createUser(reqVO);
        // 断言
        assertNotNull(userId);
        // 校验记录的属性是否正确
        DistributorUserDO user = userMapper.selectById(userId);
        assertPojoEquals(reqVO, user);
    }

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

        // 调用
        userService.updateUser(reqVO);
        // 校验是否更新正确
        DistributorUserDO user = userMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, user);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> userService.updateUser(reqVO), USER_NOT_EXISTS);
    }

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

        // 调用
        userService.deleteUser(id);
        // 校验数据不存在了
        assertNull(userMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> userService.deleteUser(id), USER_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetUserPage() {
        // mock 数据
        DistributorUserDO dbUser = randomPojo(DistributorUserDO.class, o -> { // 等会查询到
            o.setUserId(null);
            o.setCreateTime(null);
            o.setSuperiorIdP1(null);
            o.setSuperiorNameP1(null);
            o.setSuperiorIdP2(null);
            o.setSuperiorNameP2(null);
            o.setExpireTime(null);
            o.setNickname(null);
            o.setAvatar(null);
            o.setProductCount(null);
            o.setOrderCount(null);
            o.setWages(null);
        });
        userMapper.insert(dbUser);
        // 测试 userId 不匹配
        userMapper.insert(cloneIgnoreId(dbUser, o -> o.setUserId(null)));
        // 测试 createTime 不匹配
        userMapper.insert(cloneIgnoreId(dbUser, o -> o.setCreateTime(null)));
        // 测试 superiorIdP1 不匹配
        userMapper.insert(cloneIgnoreId(dbUser, o -> o.setSuperiorIdP1(null)));
        // 测试 superiorNameP1 不匹配
        userMapper.insert(cloneIgnoreId(dbUser, o -> o.setSuperiorNameP1(null)));
        // 测试 superiorIdP2 不匹配
        userMapper.insert(cloneIgnoreId(dbUser, o -> o.setSuperiorIdP2(null)));
        // 测试 superiorNameP2 不匹配
        userMapper.insert(cloneIgnoreId(dbUser, o -> o.setSuperiorNameP2(null)));
        // 测试 expireTime 不匹配
        userMapper.insert(cloneIgnoreId(dbUser, o -> o.setExpireTime(null)));
        // 测试 nickname 不匹配
        userMapper.insert(cloneIgnoreId(dbUser, o -> o.setNickname(null)));
        // 测试 avatar 不匹配
        userMapper.insert(cloneIgnoreId(dbUser, o -> o.setAvatar(null)));
        // 测试 productCount 不匹配
        userMapper.insert(cloneIgnoreId(dbUser, o -> o.setProductCount(null)));
        // 测试 orderCount 不匹配
        userMapper.insert(cloneIgnoreId(dbUser, o -> o.setOrderCount(null)));
        // 测试 wages 不匹配
        userMapper.insert(cloneIgnoreId(dbUser, o -> o.setWages(null)));
        // 准备参数
        DistributorUserPageReqVO reqVO = new DistributorUserPageReqVO();
        reqVO.setUserId(null);
        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
        reqVO.setSuperiorIdP1(null);
        reqVO.setSuperiorNameP1(null);
        reqVO.setSuperiorIdP2(null);
        reqVO.setSuperiorNameP2(null);
        reqVO.setExpireTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
        reqVO.setNickname(null);
        reqVO.setAvatar(null);
        reqVO.setProductCount(null);
        reqVO.setOrderCount(null);
        reqVO.setWages(null);

        // 调用
        PageResult<DistributorUserDO> pageResult = userService.getUserPage(reqVO);
        // 断言
        assertEquals(1, pageResult.getTotal());
        assertEquals(1, pageResult.getList().size());
        assertPojoEquals(dbUser, pageResult.getList().get(0));
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetUserList() {
        // mock 数据
        DistributorUserDO dbUser = randomPojo(DistributorUserDO.class, o -> { // 等会查询到
            o.setUserId(null);
            o.setCreateTime(null);
            o.setSuperiorIdP1(null);
            o.setSuperiorNameP1(null);
            o.setSuperiorIdP2(null);
            o.setSuperiorNameP2(null);
            o.setExpireTime(null);
            o.setNickname(null);
            o.setAvatar(null);
            o.setProductCount(null);
            o.setOrderCount(null);
            o.setWages(null);
        });
        userMapper.insert(dbUser);
        // 测试 userId 不匹配
        userMapper.insert(cloneIgnoreId(dbUser, o -> o.setUserId(null)));
        // 测试 createTime 不匹配
        userMapper.insert(cloneIgnoreId(dbUser, o -> o.setCreateTime(null)));
        // 测试 superiorIdP1 不匹配
        userMapper.insert(cloneIgnoreId(dbUser, o -> o.setSuperiorIdP1(null)));
        // 测试 superiorNameP1 不匹配
        userMapper.insert(cloneIgnoreId(dbUser, o -> o.setSuperiorNameP1(null)));
        // 测试 superiorIdP2 不匹配
        userMapper.insert(cloneIgnoreId(dbUser, o -> o.setSuperiorIdP2(null)));
        // 测试 superiorNameP2 不匹配
        userMapper.insert(cloneIgnoreId(dbUser, o -> o.setSuperiorNameP2(null)));
        // 测试 expireTime 不匹配
        userMapper.insert(cloneIgnoreId(dbUser, o -> o.setExpireTime(null)));
        // 测试 nickname 不匹配
        userMapper.insert(cloneIgnoreId(dbUser, o -> o.setNickname(null)));
        // 测试 avatar 不匹配
        userMapper.insert(cloneIgnoreId(dbUser, o -> o.setAvatar(null)));
        // 测试 productCount 不匹配
        userMapper.insert(cloneIgnoreId(dbUser, o -> o.setProductCount(null)));
        // 测试 orderCount 不匹配
        userMapper.insert(cloneIgnoreId(dbUser, o -> o.setOrderCount(null)));
        // 测试 wages 不匹配
        userMapper.insert(cloneIgnoreId(dbUser, o -> o.setWages(null)));
        // 准备参数
        DistributorUserExportReqVO reqVO = new DistributorUserExportReqVO();
        reqVO.setUserId(null);
        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
        reqVO.setSuperiorIdP1(null);
        reqVO.setSuperiorNameP1(null);
        reqVO.setSuperiorIdP2(null);
        reqVO.setSuperiorNameP2(null);
        reqVO.setExpireTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
        reqVO.setNickname(null);
        reqVO.setAvatar(null);
        reqVO.setProductCount(null);
        reqVO.setOrderCount(null);
        reqVO.setWages(null);

        // 调用
        List<DistributorUserDO> list = userService.getUserList(reqVO);
        // 断言
        assertEquals(1, list.size());
        assertPojoEquals(dbUser, list.get(0));
    }

}
