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

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

    @Resource
    private DistributorWagesServiceImpl wagesService;

    @Resource
    private DistributorWagesMapper wagesMapper;

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

        // 调用
        Long wagesId = wagesService.createWages(reqVO);
        // 断言
        assertNotNull(wagesId);
        // 校验记录的属性是否正确
        DistributorWagesDO wages = wagesMapper.selectById(wagesId);
        assertPojoEquals(reqVO, wages);
    }

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

        // 调用
        wagesService.updateWages(reqVO);
        // 校验是否更新正确
        DistributorWagesDO wages = wagesMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, wages);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> wagesService.updateWages(reqVO), WAGES_NOT_EXISTS);
    }

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

        // 调用
        wagesService.deleteWages(id);
        // 校验数据不存在了
        assertNull(wagesMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> wagesService.deleteWages(id), WAGES_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetWagesPage() {
        // mock 数据
        DistributorWagesDO dbWages = randomPojo(DistributorWagesDO.class, o -> { // 等会查询到
            o.setUserId(null);
            o.setCreateTime(null);
            o.setAmount(null);
            o.setRefuseAmount(null);
            o.setDistributorId(null);
        });
        wagesMapper.insert(dbWages);
        // 测试 userId 不匹配
        wagesMapper.insert(cloneIgnoreId(dbWages, o -> o.setUserId(null)));
        // 测试 createTime 不匹配
        wagesMapper.insert(cloneIgnoreId(dbWages, o -> o.setCreateTime(null)));
        // 测试 amount 不匹配
        wagesMapper.insert(cloneIgnoreId(dbWages, o -> o.setAmount(null)));
        // 测试 refuseAmount 不匹配
        wagesMapper.insert(cloneIgnoreId(dbWages, o -> o.setRefuseAmount(null)));
        // 测试 distributorId 不匹配
        wagesMapper.insert(cloneIgnoreId(dbWages, o -> o.setDistributorId(null)));
        // 准备参数
        DistributorWagesPageReqVO reqVO = new DistributorWagesPageReqVO();
        reqVO.setUserId(null);
        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
        reqVO.setAmount(null);
        reqVO.setRefuseAmount(null);
        reqVO.setDistributorId(null);

        // 调用
        PageResult<DistributorWagesDO> pageResult = wagesService.getWagesPage(reqVO);
        // 断言
        assertEquals(1, pageResult.getTotal());
        assertEquals(1, pageResult.getList().size());
        assertPojoEquals(dbWages, pageResult.getList().get(0));
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetWagesList() {
        // mock 数据
        DistributorWagesDO dbWages = randomPojo(DistributorWagesDO.class, o -> { // 等会查询到
            o.setUserId(null);
            o.setCreateTime(null);
            o.setAmount(null);
            o.setRefuseAmount(null);
            o.setDistributorId(null);
        });
        wagesMapper.insert(dbWages);
        // 测试 userId 不匹配
        wagesMapper.insert(cloneIgnoreId(dbWages, o -> o.setUserId(null)));
        // 测试 createTime 不匹配
        wagesMapper.insert(cloneIgnoreId(dbWages, o -> o.setCreateTime(null)));
        // 测试 amount 不匹配
        wagesMapper.insert(cloneIgnoreId(dbWages, o -> o.setAmount(null)));
        // 测试 refuseAmount 不匹配
        wagesMapper.insert(cloneIgnoreId(dbWages, o -> o.setRefuseAmount(null)));
        // 测试 distributorId 不匹配
        wagesMapper.insert(cloneIgnoreId(dbWages, o -> o.setDistributorId(null)));
        // 准备参数
        DistributorWagesExportReqVO reqVO = new DistributorWagesExportReqVO();
        reqVO.setUserId(null);
        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
        reqVO.setAmount(null);
        reqVO.setRefuseAmount(null);
        reqVO.setDistributorId(null);

        // 调用
        List<DistributorWagesDO> list = wagesService.getWagesList(reqVO);
        // 断言
        assertEquals(1, list.size());
        assertPojoEquals(dbWages, list.get(0));
    }

}
