package cn.iocoder.yudao.module.identity.service.diversionstats;

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 cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;

import cn.iocoder.yudao.module.identity.controller.admin.diversionstats.vo.*;
import cn.iocoder.yudao.module.identity.dal.dataobject.diversionstats.DiversionStatsDO;
import cn.iocoder.yudao.module.identity.dal.mysql.diversionstats.DiversionStatsMapper;
import cn.iocoder.yudao.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 cn.iocoder.yudao.module.identity.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.*;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.*;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
* {@link DiversionStatsServiceImpl} 的单元测试类
*
* @author 芋道源码
*/
@Import(DiversionStatsServiceImpl.class)
public class DiversionStatsServiceImplTest extends BaseDbUnitTest {

    @Resource
    private DiversionStatsServiceImpl diversionStatsService;

    @Resource
    private DiversionStatsMapper diversionStatsMapper;

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

        // 调用
        Long diversionStatsId = diversionStatsService.createDiversionStats(reqVO);
        // 断言
        assertNotNull(diversionStatsId);
        // 校验记录的属性是否正确
        DiversionStatsDO diversionStats = diversionStatsMapper.selectById(diversionStatsId);
        assertPojoEquals(reqVO, diversionStats);
    }

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

        // 调用
        diversionStatsService.updateDiversionStats(reqVO);
        // 校验是否更新正确
        DiversionStatsDO diversionStats = diversionStatsMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, diversionStats);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> diversionStatsService.updateDiversionStats(reqVO), DIVERSION_STATS_NOT_EXISTS);
    }

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

        // 调用
        diversionStatsService.deleteDiversionStats(id);
       // 校验数据不存在了
       assertNull(diversionStatsMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> diversionStatsService.deleteDiversionStats(id), DIVERSION_STATS_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetDiversionStatsPage() {
       // mock 数据
       DiversionStatsDO dbDiversionStats = randomPojo(DiversionStatsDO.class, o -> { // 等会查询到
           o.setDealerId(null);
           o.setDealerName(null);
           o.setBatchId(null);
           o.setGoodsId(null);
           o.setGoodsName(null);
           o.setNum(null);
           o.setBelongBusinessId(null);
           o.setBelongBusinessName(null);
           o.setCreateTime(null);
       });
       diversionStatsMapper.insert(dbDiversionStats);
       // 测试 dealerId 不匹配
       diversionStatsMapper.insert(cloneIgnoreId(dbDiversionStats, o -> o.setDealerId(null)));
       // 测试 dealerName 不匹配
       diversionStatsMapper.insert(cloneIgnoreId(dbDiversionStats, o -> o.setDealerName(null)));
       // 测试 batchId 不匹配
       diversionStatsMapper.insert(cloneIgnoreId(dbDiversionStats, o -> o.setBatchId(null)));
       // 测试 goodsId 不匹配
       diversionStatsMapper.insert(cloneIgnoreId(dbDiversionStats, o -> o.setGoodsId(null)));
       // 测试 goodsName 不匹配
       diversionStatsMapper.insert(cloneIgnoreId(dbDiversionStats, o -> o.setGoodsName(null)));
       // 测试 num 不匹配
       diversionStatsMapper.insert(cloneIgnoreId(dbDiversionStats, o -> o.setNum(null)));
       // 测试 belongBusinessId 不匹配
       diversionStatsMapper.insert(cloneIgnoreId(dbDiversionStats, o -> o.setBelongBusinessId(null)));
       // 测试 belongBusinessName 不匹配
       diversionStatsMapper.insert(cloneIgnoreId(dbDiversionStats, o -> o.setBelongBusinessName(null)));
       // 测试 createTime 不匹配
       diversionStatsMapper.insert(cloneIgnoreId(dbDiversionStats, o -> o.setCreateTime(null)));
       // 准备参数
       DiversionStatsPageReqVO reqVO = new DiversionStatsPageReqVO();
       reqVO.setDealerId(null);
       reqVO.setDealerName(null);
       reqVO.setBatchId(null);
       reqVO.setGoodsId(null);
       reqVO.setGoodsName(null);
       reqVO.setNum(null);
       reqVO.setBelongBusinessId(null);
       reqVO.setBelongBusinessName(null);
//       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<DiversionStatsDO> pageResult = diversionStatsService.getDiversionStatsPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbDiversionStats, pageResult.getList().get(0));
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetDiversionStatsList() {
       // mock 数据
       DiversionStatsDO dbDiversionStats = randomPojo(DiversionStatsDO.class, o -> { // 等会查询到
           o.setDealerId(null);
           o.setDealerName(null);
           o.setBatchId(null);
           o.setGoodsId(null);
           o.setGoodsName(null);
           o.setNum(null);
           o.setBelongBusinessId(null);
           o.setBelongBusinessName(null);
           o.setCreateTime(null);
       });
       diversionStatsMapper.insert(dbDiversionStats);
       // 测试 dealerId 不匹配
       diversionStatsMapper.insert(cloneIgnoreId(dbDiversionStats, o -> o.setDealerId(null)));
       // 测试 dealerName 不匹配
       diversionStatsMapper.insert(cloneIgnoreId(dbDiversionStats, o -> o.setDealerName(null)));
       // 测试 batchId 不匹配
       diversionStatsMapper.insert(cloneIgnoreId(dbDiversionStats, o -> o.setBatchId(null)));
       // 测试 goodsId 不匹配
       diversionStatsMapper.insert(cloneIgnoreId(dbDiversionStats, o -> o.setGoodsId(null)));
       // 测试 goodsName 不匹配
       diversionStatsMapper.insert(cloneIgnoreId(dbDiversionStats, o -> o.setGoodsName(null)));
       // 测试 num 不匹配
       diversionStatsMapper.insert(cloneIgnoreId(dbDiversionStats, o -> o.setNum(null)));
       // 测试 belongBusinessId 不匹配
       diversionStatsMapper.insert(cloneIgnoreId(dbDiversionStats, o -> o.setBelongBusinessId(null)));
       // 测试 belongBusinessName 不匹配
       diversionStatsMapper.insert(cloneIgnoreId(dbDiversionStats, o -> o.setBelongBusinessName(null)));
       // 测试 createTime 不匹配
       diversionStatsMapper.insert(cloneIgnoreId(dbDiversionStats, o -> o.setCreateTime(null)));
       // 准备参数
       DiversionStatsExportReqVO reqVO = new DiversionStatsExportReqVO();
       reqVO.setDealerId(null);
       reqVO.setDealerName(null);
       reqVO.setBatchId(null);
       reqVO.setGoodsId(null);
       reqVO.setGoodsName(null);
       reqVO.setNum(null);
       reqVO.setBelongBusinessId(null);
       reqVO.setBelongBusinessName(null);
//       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       List<DiversionStatsDO> list = diversionStatsService.getDiversionStatsList(reqVO);
       // 断言
       assertEquals(1, list.size());
       assertPojoEquals(dbDiversionStats, list.get(0));
    }

}
