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

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

    @Resource
    private DistributorWagesLogServiceImpl wagesLogService;

    @Resource
    private DistributorWagesLogMapper wagesLogMapper;

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

        // 调用
        Long wagesLogId = wagesLogService.createWagesLog(reqVO);
        // 断言
        assertNotNull(wagesLogId);
        // 校验记录的属性是否正确
        DistributorWagesLogDO wagesLog = wagesLogMapper.selectById(wagesLogId);
        assertPojoEquals(reqVO, wagesLog);
    }

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

        // 调用
        wagesLogService.updateWagesLog(reqVO);
        // 校验是否更新正确
        DistributorWagesLogDO wagesLog = wagesLogMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, wagesLog);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> wagesLogService.updateWagesLog(reqVO), WAGES_LOG_NOT_EXISTS);
    }

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

        // 调用
        wagesLogService.deleteWagesLog(id);
        // 校验数据不存在了
        assertNull(wagesLogMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> wagesLogService.deleteWagesLog(id), WAGES_LOG_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetWagesLogPage() {
        // mock 数据
        DistributorWagesLogDO dbWagesLog = randomPojo(DistributorWagesLogDO.class, o -> { // 等会查询到
            o.setWagesId(null);
            o.setUserId(null);
            o.setType(null);
            o.setAmount(null);
            o.setCreateTime(null);
            o.setDistributorId(null);
            o.setStatus(null);
            o.setUserName(null);
            o.setDistributorName(null);
        });
        wagesLogMapper.insert(dbWagesLog);
        // 测试 wagesId 不匹配
        wagesLogMapper.insert(cloneIgnoreId(dbWagesLog, o -> o.setWagesId(null)));
        // 测试 userId 不匹配
        wagesLogMapper.insert(cloneIgnoreId(dbWagesLog, o -> o.setUserId(null)));
        // 测试 type 不匹配
        wagesLogMapper.insert(cloneIgnoreId(dbWagesLog, o -> o.setType(null)));
        // 测试 amount 不匹配
        wagesLogMapper.insert(cloneIgnoreId(dbWagesLog, o -> o.setAmount(null)));
        // 测试 createTime 不匹配
        wagesLogMapper.insert(cloneIgnoreId(dbWagesLog, o -> o.setCreateTime(null)));
        // 测试 distributorId 不匹配
        wagesLogMapper.insert(cloneIgnoreId(dbWagesLog, o -> o.setDistributorId(null)));
        // 测试 status 不匹配
        wagesLogMapper.insert(cloneIgnoreId(dbWagesLog, o -> o.setStatus(null)));
        // 测试 userName 不匹配
        wagesLogMapper.insert(cloneIgnoreId(dbWagesLog, o -> o.setUserName(null)));
        // 测试 distributorName 不匹配
        wagesLogMapper.insert(cloneIgnoreId(dbWagesLog, o -> o.setDistributorName(null)));
        // 准备参数
        DistributorWagesLogPageReqVO reqVO = new DistributorWagesLogPageReqVO();
        reqVO.setWagesId(null);
        reqVO.setUserId(null);
        reqVO.setType(null);
        reqVO.setAmount(null);
        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
        reqVO.setDistributorId(null);
        reqVO.setStatus(null);
        reqVO.setUserName(null);
        reqVO.setDistributorName(null);

        // 调用
        PageResult<DistributorWagesLogDO> pageResult = wagesLogService.getWagesLogPage(reqVO);
        // 断言
        assertEquals(1, pageResult.getTotal());
        assertEquals(1, pageResult.getList().size());
        assertPojoEquals(dbWagesLog, pageResult.getList().get(0));
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetWagesLogList() {
        // mock 数据
        DistributorWagesLogDO dbWagesLog = randomPojo(DistributorWagesLogDO.class, o -> { // 等会查询到
            o.setWagesId(null);
            o.setUserId(null);
            o.setType(null);
            o.setAmount(null);
            o.setCreateTime(null);
            o.setDistributorId(null);
            o.setStatus(null);
            o.setUserName(null);
            o.setDistributorName(null);
        });
        wagesLogMapper.insert(dbWagesLog);
        // 测试 wagesId 不匹配
        wagesLogMapper.insert(cloneIgnoreId(dbWagesLog, o -> o.setWagesId(null)));
        // 测试 userId 不匹配
        wagesLogMapper.insert(cloneIgnoreId(dbWagesLog, o -> o.setUserId(null)));
        // 测试 type 不匹配
        wagesLogMapper.insert(cloneIgnoreId(dbWagesLog, o -> o.setType(null)));
        // 测试 amount 不匹配
        wagesLogMapper.insert(cloneIgnoreId(dbWagesLog, o -> o.setAmount(null)));
        // 测试 createTime 不匹配
        wagesLogMapper.insert(cloneIgnoreId(dbWagesLog, o -> o.setCreateTime(null)));
        // 测试 distributorId 不匹配
        wagesLogMapper.insert(cloneIgnoreId(dbWagesLog, o -> o.setDistributorId(null)));
        // 测试 status 不匹配
        wagesLogMapper.insert(cloneIgnoreId(dbWagesLog, o -> o.setStatus(null)));
        // 测试 userName 不匹配
        wagesLogMapper.insert(cloneIgnoreId(dbWagesLog, o -> o.setUserName(null)));
        // 测试 distributorName 不匹配
        wagesLogMapper.insert(cloneIgnoreId(dbWagesLog, o -> o.setDistributorName(null)));
        // 准备参数
        DistributorWagesLogExportReqVO reqVO = new DistributorWagesLogExportReqVO();
        reqVO.setWagesId(null);
        reqVO.setUserId(null);
        reqVO.setType(null);
        reqVO.setAmount(null);
        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
        reqVO.setDistributorId(null);
        reqVO.setStatus(null);
        reqVO.setUserName(null);
        reqVO.setDistributorName(null);

        // 调用
        List<DistributorWagesLogDO> list = wagesLogService.getWagesLogList(reqVO);
        // 断言
        assertEquals(1, list.size());
        assertPojoEquals(dbWagesLog, list.get(0));
    }

}
