package cn.iocoder.yudao.module.flow.service.outbound;

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 cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;

import cn.iocoder.yudao.module.flow.controller.admin.outbound.vo.*;
import cn.iocoder.yudao.module.flow.dal.dataobject.outbound.OutboundDO;
import cn.iocoder.yudao.module.flow.dal.mysql.outbound.OutboundMapper;
import cn.iocoder.yudao.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 cn.iocoder.yudao.module.flow.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.*;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.*;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
* {@link OutboundServiceImpl} 的单元测试类
*
* @author 芋道源码
*/
@Import(OutboundServiceImpl.class)
public class OutboundServiceImplTest extends BaseDbUnitTest {

    @Resource
    private OutboundServiceImpl outboundService;

    @Resource
    private OutboundMapper outboundMapper;

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

        // 调用
        Long outboundId = outboundService.createOutbound(reqVO);
        // 断言
        assertNotNull(outboundId);
        // 校验记录的属性是否正确
        OutboundDO outbound = outboundMapper.selectById(outboundId);
        assertPojoEquals(reqVO, outbound);
    }

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

        // 调用
        outboundService.updateOutbound(reqVO);
        // 校验是否更新正确
        OutboundDO outbound = outboundMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, outbound);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> outboundService.updateOutbound(reqVO), OUTBOUND_NOT_EXISTS);
    }

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

        // 调用
        outboundService.deleteOutbound(id);
       // 校验数据不存在了
       assertNull(outboundMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> outboundService.deleteOutbound(id), OUTBOUND_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetOutboundPage() {
       // mock 数据
       OutboundDO dbOutbound = randomPojo(OutboundDO.class, o -> { // 等会查询到
           o.setVendorId(null);
           o.setVendorName(null);
           o.setOutBoundId(null);
           o.setOutBoundTime(null);
           o.setCarrierVehicles(null);
           o.setCarrier(null);
           o.setCarrierContact(null);
           o.setBlockState(null);
           o.setBlockChainId(null);
           o.setBlockChainBlockIdentity(null);
           o.setBelongBusinessId(null);
           o.setBelongBusinessName(null);
           o.setCreateTime(null);
       });
       outboundMapper.insert(dbOutbound);
       // 测试 vendorId 不匹配
       outboundMapper.insert(cloneIgnoreId(dbOutbound, o -> o.setVendorId(null)));
       // 测试 vendorName 不匹配
       outboundMapper.insert(cloneIgnoreId(dbOutbound, o -> o.setVendorName(null)));
       // 测试 outBoundId 不匹配
       outboundMapper.insert(cloneIgnoreId(dbOutbound, o -> o.setOutBoundId(null)));
       // 测试 outBoundTime 不匹配
       outboundMapper.insert(cloneIgnoreId(dbOutbound, o -> o.setOutBoundTime(null)));
       // 测试 carrierVehicles 不匹配
       outboundMapper.insert(cloneIgnoreId(dbOutbound, o -> o.setCarrierVehicles(null)));
       // 测试 carrier 不匹配
       outboundMapper.insert(cloneIgnoreId(dbOutbound, o -> o.setCarrier(null)));
       // 测试 carrierContact 不匹配
       outboundMapper.insert(cloneIgnoreId(dbOutbound, o -> o.setCarrierContact(null)));
       // 测试 blockState 不匹配
       outboundMapper.insert(cloneIgnoreId(dbOutbound, o -> o.setBlockState(null)));
       // 测试 blockChainId 不匹配
       outboundMapper.insert(cloneIgnoreId(dbOutbound, o -> o.setBlockChainId(null)));
       // 测试 blockChainBlockIdentity 不匹配
       outboundMapper.insert(cloneIgnoreId(dbOutbound, o -> o.setBlockChainBlockIdentity(null)));
       // 测试 belongBusinessId 不匹配
       outboundMapper.insert(cloneIgnoreId(dbOutbound, o -> o.setBelongBusinessId(null)));
       // 测试 belongBusinessName 不匹配
       outboundMapper.insert(cloneIgnoreId(dbOutbound, o -> o.setBelongBusinessName(null)));
       // 测试 createTime 不匹配
       outboundMapper.insert(cloneIgnoreId(dbOutbound, o -> o.setCreateTime(null)));
       // 准备参数
       OutboundPageReqVO reqVO = new OutboundPageReqVO();
       reqVO.setVendorId(null);
       reqVO.setVendorName(null);
       reqVO.setOutBoundId(null);
//       reqVO.setOutBoundTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setCarrierVehicles(null);
       reqVO.setCarrier(null);
       reqVO.setCarrierContact(null);
       reqVO.setBlockState(null);
       reqVO.setBlockChainId(null);
       reqVO.setBlockChainBlockIdentity(null);
       reqVO.setBelongBusinessId(null);
       reqVO.setBelongBusinessName(null);
//       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<OutboundDO> pageResult = outboundService.getOutboundPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbOutbound, pageResult.getList().get(0));
    }


}
