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

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

    @Resource
    private DistributorWithdrawalMethodServiceImpl withdrawalMethodService;

    @Resource
    private DistributorWithdrawalMethodMapper withdrawalMethodMapper;

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

        // 调用
        Long withdrawalMethodId = withdrawalMethodService.createWithdrawalMethod(reqVO);
        // 断言
        assertNotNull(withdrawalMethodId);
        // 校验记录的属性是否正确
        DistributorWithdrawalMethodDO withdrawalMethod = withdrawalMethodMapper.selectById(withdrawalMethodId);
        assertPojoEquals(reqVO, withdrawalMethod);
    }

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

        // 调用
        withdrawalMethodService.updateWithdrawalMethod(reqVO);
        // 校验是否更新正确
        DistributorWithdrawalMethodDO withdrawalMethod = withdrawalMethodMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, withdrawalMethod);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> withdrawalMethodService.updateWithdrawalMethod(reqVO), WITHDRAWAL_METHOD_NOT_EXISTS);
    }

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

        // 调用
        withdrawalMethodService.deleteWithdrawalMethod(id);
        // 校验数据不存在了
        assertNull(withdrawalMethodMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> withdrawalMethodService.deleteWithdrawalMethod(id), WITHDRAWAL_METHOD_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetWithdrawalMethodPage() {
        // mock 数据
        DistributorWithdrawalMethodDO dbWithdrawalMethod = randomPojo(DistributorWithdrawalMethodDO.class, o -> { // 等会查询到
            o.setUserId(null);
            o.setDistributorId(null);
            o.setPaymentMethod(null);
            o.setAccountNum(null);
            o.setRealName(null);
            o.setCreateTime(null);
        });
        withdrawalMethodMapper.insert(dbWithdrawalMethod);
        // 测试 userId 不匹配
        withdrawalMethodMapper.insert(cloneIgnoreId(dbWithdrawalMethod, o -> o.setUserId(null)));
        // 测试 distributorId 不匹配
        withdrawalMethodMapper.insert(cloneIgnoreId(dbWithdrawalMethod, o -> o.setDistributorId(null)));
        // 测试 paymentMethod 不匹配
        withdrawalMethodMapper.insert(cloneIgnoreId(dbWithdrawalMethod, o -> o.setPaymentMethod(null)));
        // 测试 acountNum 不匹配
        withdrawalMethodMapper.insert(cloneIgnoreId(dbWithdrawalMethod, o -> o.setAccountNum(null)));
        // 测试 realName 不匹配
        withdrawalMethodMapper.insert(cloneIgnoreId(dbWithdrawalMethod, o -> o.setRealName(null)));
        // 测试 createTime 不匹配
        withdrawalMethodMapper.insert(cloneIgnoreId(dbWithdrawalMethod, o -> o.setCreateTime(null)));
        // 准备参数
        DistributorWithdrawalMethodPageReqVO reqVO = new DistributorWithdrawalMethodPageReqVO();
        reqVO.setUserId(null);
        reqVO.setDistributorId(null);
        reqVO.setPaymentMethod(null);
        reqVO.setAcountNum(null);
        reqVO.setRealName(null);
        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

        // 调用
        PageResult<DistributorWithdrawalMethodDO> pageResult = withdrawalMethodService.getWithdrawalMethodPage(reqVO);
        // 断言
        assertEquals(1, pageResult.getTotal());
        assertEquals(1, pageResult.getList().size());
        assertPojoEquals(dbWithdrawalMethod, pageResult.getList().get(0));
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetWithdrawalMethodList() {
        // mock 数据
        DistributorWithdrawalMethodDO dbWithdrawalMethod = randomPojo(DistributorWithdrawalMethodDO.class, o -> { // 等会查询到
            o.setUserId(null);
            o.setDistributorId(null);
            o.setPaymentMethod(null);
            o.setAccountNum(null);
            o.setRealName(null);
            o.setCreateTime(null);
        });
        withdrawalMethodMapper.insert(dbWithdrawalMethod);
        // 测试 userId 不匹配
        withdrawalMethodMapper.insert(cloneIgnoreId(dbWithdrawalMethod, o -> o.setUserId(null)));
        // 测试 distributorId 不匹配
        withdrawalMethodMapper.insert(cloneIgnoreId(dbWithdrawalMethod, o -> o.setDistributorId(null)));
        // 测试 paymentMethod 不匹配
        withdrawalMethodMapper.insert(cloneIgnoreId(dbWithdrawalMethod, o -> o.setPaymentMethod(null)));
        // 测试 acountNum 不匹配
        withdrawalMethodMapper.insert(cloneIgnoreId(dbWithdrawalMethod, o -> o.setAccountNum(null)));
        // 测试 realName 不匹配
        withdrawalMethodMapper.insert(cloneIgnoreId(dbWithdrawalMethod, o -> o.setRealName(null)));
        // 测试 createTime 不匹配
        withdrawalMethodMapper.insert(cloneIgnoreId(dbWithdrawalMethod, o -> o.setCreateTime(null)));
        // 准备参数
        DistributorWithdrawalMethodExportReqVO reqVO = new DistributorWithdrawalMethodExportReqVO();
        reqVO.setUserId(null);
        reqVO.setDistributorId(null);
        reqVO.setPaymentMethod(null);
        reqVO.setAcountNum(null);
        reqVO.setRealName(null);
        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

        // 调用
        List<DistributorWithdrawalMethodDO> list = withdrawalMethodService.getWithdrawalMethodList(reqVO);
        // 断言
        assertEquals(1, list.size());
        assertPojoEquals(dbWithdrawalMethod, list.get(0));
    }

}
