package co.yixiang.yshop.module.order.service.rechargepackage;

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.order.controller.admin.rechargepackage.vo.*;
import co.yixiang.yshop.module.order.dal.dataobject.rechargepackage.RechargePackageDO;
import co.yixiang.yshop.module.order.dal.mysql.rechargepackage.RechargePackageMapper;
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.order.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 RechargePackageServiceImpl} 的单元测试类
 *
 * @author admin
 */
@Import(RechargePackageServiceImpl.class)
public class RechargePackageServiceImplTest extends BaseDbUnitTest {

    @Resource
    private RechargePackageServiceImpl rechargePackageService;

    @Resource
    private RechargePackageMapper rechargePackageMapper;

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

        // 调用
        Long rechargePackageId = rechargePackageService.createRechargePackage(reqVO);
        // 断言
        assertNotNull(rechargePackageId);
        // 校验记录的属性是否正确
        RechargePackageDO rechargePackage = rechargePackageMapper.selectById(rechargePackageId);
        assertPojoEquals(reqVO, rechargePackage);
    }

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

        // 调用
        rechargePackageService.updateRechargePackage(reqVO);
        // 校验是否更新正确
        RechargePackageDO rechargePackage = rechargePackageMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, rechargePackage);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> rechargePackageService.updateRechargePackage(reqVO), RECHARGE_PACKAGE_NOT_EXISTS);
    }

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

        // 调用
        rechargePackageService.deleteRechargePackage(id);
        // 校验数据不存在了
        assertNull(rechargePackageMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> rechargePackageService.deleteRechargePackage(id), RECHARGE_PACKAGE_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetRechargePackagePage() {
        // mock 数据
        RechargePackageDO dbRechargePackage = randomPojo(RechargePackageDO.class, o -> { // 等会查询到
            o.setRechargeAmount(null);
            o.setGiftAmount(null);
            o.setStatus(null);
            o.setCreateTime(null);
        });
        rechargePackageMapper.insert(dbRechargePackage);
        // 测试 rechargeAmount 不匹配
        rechargePackageMapper.insert(cloneIgnoreId(dbRechargePackage, o -> o.setRechargeAmount(null)));
        // 测试 giftAmount 不匹配
        rechargePackageMapper.insert(cloneIgnoreId(dbRechargePackage, o -> o.setGiftAmount(null)));
        // 测试 status 不匹配
        rechargePackageMapper.insert(cloneIgnoreId(dbRechargePackage, o -> o.setStatus(null)));
        // 测试 createTime 不匹配
        rechargePackageMapper.insert(cloneIgnoreId(dbRechargePackage, o -> o.setCreateTime(null)));
        // 准备参数
        RechargePackagePageReqVO reqVO = new RechargePackagePageReqVO();
        reqVO.setRechargeAmount(null);
        reqVO.setGiftAmount(null);
        reqVO.setStatus(null);
        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

        // 调用
        PageResult<RechargePackageDO> pageResult = rechargePackageService.getRechargePackagePage(reqVO);
        // 断言
        assertEquals(1, pageResult.getTotal());
        assertEquals(1, pageResult.getList().size());
        assertPojoEquals(dbRechargePackage, pageResult.getList().get(0));
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetRechargePackageList() {
        // mock 数据
        RechargePackageDO dbRechargePackage = randomPojo(RechargePackageDO.class, o -> { // 等会查询到
            o.setRechargeAmount(null);
            o.setGiftAmount(null);
            o.setStatus(null);
            o.setCreateTime(null);
        });
        rechargePackageMapper.insert(dbRechargePackage);
        // 测试 rechargeAmount 不匹配
        rechargePackageMapper.insert(cloneIgnoreId(dbRechargePackage, o -> o.setRechargeAmount(null)));
        // 测试 giftAmount 不匹配
        rechargePackageMapper.insert(cloneIgnoreId(dbRechargePackage, o -> o.setGiftAmount(null)));
        // 测试 status 不匹配
        rechargePackageMapper.insert(cloneIgnoreId(dbRechargePackage, o -> o.setStatus(null)));
        // 测试 createTime 不匹配
        rechargePackageMapper.insert(cloneIgnoreId(dbRechargePackage, o -> o.setCreateTime(null)));
        // 准备参数
        RechargePackageExportReqVO reqVO = new RechargePackageExportReqVO();
        reqVO.setRechargeAmount(null);
        reqVO.setGiftAmount(null);
        reqVO.setStatus(null);
        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

        // 调用
        List<RechargePackageDO> list = rechargePackageService.getRechargePackageList(reqVO);
        // 断言
        assertEquals(1, list.size());
        assertPojoEquals(dbRechargePackage, list.get(0));
    }

}
