package co.yixiang.yshop.module.member.service.integralrule;

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.member.controller.admin.integralrule.vo.*;
import co.yixiang.yshop.module.member.dal.dataobject.integralrule.IntegralRuleDO;
import co.yixiang.yshop.module.member.dal.mysql.integralrule.IntegralRuleMapper;
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.member.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 IntegralRuleServiceImpl} 的单元测试类
 *
 * @author admin
 */
@Import(IntegralRuleServiceImpl.class)
public class IntegralRuleServiceImplTest extends BaseDbUnitTest {

    @Resource
    private IntegralRuleServiceImpl integralRuleService;

    @Resource
    private IntegralRuleMapper integralRuleMapper;

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

        // 调用
        Long integralRuleId = integralRuleService.createIntegralRule(reqVO);
        // 断言
        assertNotNull(integralRuleId);
        // 校验记录的属性是否正确
        IntegralRuleDO integralRule = integralRuleMapper.selectById(integralRuleId);
        assertPojoEquals(reqVO, integralRule);
    }

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

        // 调用
        integralRuleService.updateIntegralRule(reqVO);
        // 校验是否更新正确
        IntegralRuleDO integralRule = integralRuleMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, integralRule);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> integralRuleService.updateIntegralRule(reqVO), INTEGRAL_RULE_NOT_EXISTS);
    }

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

        // 调用
        integralRuleService.deleteIntegralRule(id);
        // 校验数据不存在了
        assertNull(integralRuleMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> integralRuleService.deleteIntegralRule(id), INTEGRAL_RULE_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetIntegralRulePage() {
        // mock 数据
        IntegralRuleDO dbIntegralRule = randomPojo(IntegralRuleDO.class, o -> { // 等会查询到
            o.setType(null);
            o.setTypeName(null);
            o.setIntegral(null);
            o.setAttribute1(null);
            o.setCreateTime(null);
        });
        integralRuleMapper.insert(dbIntegralRule);
        // 测试 type 不匹配
        integralRuleMapper.insert(cloneIgnoreId(dbIntegralRule, o -> o.setType(null)));
        // 测试 typeName 不匹配
        integralRuleMapper.insert(cloneIgnoreId(dbIntegralRule, o -> o.setTypeName(null)));
        // 测试 integral 不匹配
        integralRuleMapper.insert(cloneIgnoreId(dbIntegralRule, o -> o.setIntegral(null)));
        // 测试 attribute1 不匹配
        integralRuleMapper.insert(cloneIgnoreId(dbIntegralRule, o -> o.setAttribute1(null)));
        // 测试 createTime 不匹配
        integralRuleMapper.insert(cloneIgnoreId(dbIntegralRule, o -> o.setCreateTime(null)));
        // 准备参数
        IntegralRulePageReqVO reqVO = new IntegralRulePageReqVO();
        reqVO.setType(null);
        reqVO.setTypeName(null);
        reqVO.setIntegral(null);
        reqVO.setAttribute1(null);
        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

        // 调用
        PageResult<IntegralRuleDO> pageResult = integralRuleService.getIntegralRulePage(reqVO);
        // 断言
        assertEquals(1, pageResult.getTotal());
        assertEquals(1, pageResult.getList().size());
        assertPojoEquals(dbIntegralRule, pageResult.getList().get(0));
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetIntegralRuleList() {
        // mock 数据
        IntegralRuleDO dbIntegralRule = randomPojo(IntegralRuleDO.class, o -> { // 等会查询到
            o.setType(null);
            o.setTypeName(null);
            o.setIntegral(null);
            o.setAttribute1(null);
            o.setCreateTime(null);
        });
        integralRuleMapper.insert(dbIntegralRule);
        // 测试 type 不匹配
        integralRuleMapper.insert(cloneIgnoreId(dbIntegralRule, o -> o.setType(null)));
        // 测试 typeName 不匹配
        integralRuleMapper.insert(cloneIgnoreId(dbIntegralRule, o -> o.setTypeName(null)));
        // 测试 integral 不匹配
        integralRuleMapper.insert(cloneIgnoreId(dbIntegralRule, o -> o.setIntegral(null)));
        // 测试 attribute1 不匹配
        integralRuleMapper.insert(cloneIgnoreId(dbIntegralRule, o -> o.setAttribute1(null)));
        // 测试 createTime 不匹配
        integralRuleMapper.insert(cloneIgnoreId(dbIntegralRule, o -> o.setCreateTime(null)));
        // 准备参数
        IntegralRuleExportReqVO reqVO = new IntegralRuleExportReqVO();
        reqVO.setType(null);
        reqVO.setTypeName(null);
        reqVO.setIntegral(null);
        reqVO.setAttribute1(null);
        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

        // 调用
        List<IntegralRuleDO> list = integralRuleService.getIntegralRuleList(reqVO);
        // 断言
        assertEquals(1, list.size());
        assertPojoEquals(dbIntegralRule, list.get(0));
    }

}
