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

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.distributordynamics.vo.*;
import co.yixiang.yshop.module.distributor.dal.dataobject.distributordynamics.DistributorDynamicsDO;
import co.yixiang.yshop.module.distributor.dal.mysql.distributordynamics.DistributorDynamicsMapper;
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 DistributorDynamicsServiceImpl} 的单元测试类
 *
 * @author admin
 */
@Import(DistributorDynamicsServiceImpl.class)
public class DistributorDynamicsServiceImplTest extends BaseDbUnitTest {

    @Resource
    private DistributorDynamicsServiceImpl dynamicsService;

    @Resource
    private DistributorDynamicsMapper dynamicsMapper;

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

        // 调用
        Long dynamicsId = dynamicsService.createDynamics(reqVO);
        // 断言
        assertNotNull(dynamicsId);
        // 校验记录的属性是否正确
        DistributorDynamicsDO dynamics = dynamicsMapper.selectById(dynamicsId);
        assertPojoEquals(reqVO, dynamics);
    }

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

        // 调用
        dynamicsService.updateDynamics(reqVO);
        // 校验是否更新正确
        DistributorDynamicsDO dynamics = dynamicsMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, dynamics);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> dynamicsService.updateDynamics(reqVO), DYNAMICS_NOT_EXISTS);
    }

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

        // 调用
        dynamicsService.deleteDynamics(id);
        // 校验数据不存在了
        assertNull(dynamicsMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> dynamicsService.deleteDynamics(id), DYNAMICS_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetDynamicsPage() {
        // mock 数据
        DistributorDynamicsDO dbDynamics = randomPojo(DistributorDynamicsDO.class, o -> { // 等会查询到
            o.setType(null);
            o.setDistributorId(null);
            o.setDistributorName(null);
            o.setContent(null);
            o.setCreateTime(null);
        });
        dynamicsMapper.insert(dbDynamics);
        // 测试 type 不匹配
        dynamicsMapper.insert(cloneIgnoreId(dbDynamics, o -> o.setType(null)));
        // 测试 distributorId 不匹配
        dynamicsMapper.insert(cloneIgnoreId(dbDynamics, o -> o.setDistributorId(null)));
        // 测试 distributorName 不匹配
        dynamicsMapper.insert(cloneIgnoreId(dbDynamics, o -> o.setDistributorName(null)));
        // 测试 content 不匹配
        dynamicsMapper.insert(cloneIgnoreId(dbDynamics, o -> o.setContent(null)));
        // 测试 createTime 不匹配
        dynamicsMapper.insert(cloneIgnoreId(dbDynamics, o -> o.setCreateTime(null)));
        // 准备参数
        DistributorDynamicsPageReqVO reqVO = new DistributorDynamicsPageReqVO();
        reqVO.setType(null);
        reqVO.setDistributorId(null);
        reqVO.setDistributorName(null);
        reqVO.setContent(null);
        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

        // 调用
        PageResult<DistributorDynamicsDO> pageResult = dynamicsService.getDynamicsPage(reqVO);
        // 断言
        assertEquals(1, pageResult.getTotal());
        assertEquals(1, pageResult.getList().size());
        assertPojoEquals(dbDynamics, pageResult.getList().get(0));
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetDynamicsList() {
        // mock 数据
        DistributorDynamicsDO dbDynamics = randomPojo(DistributorDynamicsDO.class, o -> { // 等会查询到
            o.setType(null);
            o.setDistributorId(null);
            o.setDistributorName(null);
            o.setContent(null);
            o.setCreateTime(null);
        });
        dynamicsMapper.insert(dbDynamics);
        // 测试 type 不匹配
        dynamicsMapper.insert(cloneIgnoreId(dbDynamics, o -> o.setType(null)));
        // 测试 distributorId 不匹配
        dynamicsMapper.insert(cloneIgnoreId(dbDynamics, o -> o.setDistributorId(null)));
        // 测试 distributorName 不匹配
        dynamicsMapper.insert(cloneIgnoreId(dbDynamics, o -> o.setDistributorName(null)));
        // 测试 content 不匹配
        dynamicsMapper.insert(cloneIgnoreId(dbDynamics, o -> o.setContent(null)));
        // 测试 createTime 不匹配
        dynamicsMapper.insert(cloneIgnoreId(dbDynamics, o -> o.setCreateTime(null)));
        // 准备参数
        DistributorDynamicsExportReqVO reqVO = new DistributorDynamicsExportReqVO();
        reqVO.setType(null);
        reqVO.setDistributorId(null);
        reqVO.setDistributorName(null);
        reqVO.setContent(null);
        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

        // 调用
        List<DistributorDynamicsDO> list = dynamicsService.getDynamicsList(reqVO);
        // 断言
        assertEquals(1, list.size());
        assertPojoEquals(dbDynamics, list.get(0));
    }

}
