package cn.iocoder.yudao.module.identity.service.diversionrecord;

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.identity.controller.admin.diversionrecord.vo.*;
import cn.iocoder.yudao.module.identity.dal.dataobject.diversionrecord.DiversionRecordDO;
import cn.iocoder.yudao.module.identity.dal.mysql.diversionrecord.DiversionRecordMapper;
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.identity.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 DiversionRecordServiceImpl} 的单元测试类
*
* @author 芋道源码
*/
@Import(DiversionRecordServiceImpl.class)
public class DiversionRecordServiceImplTest extends BaseDbUnitTest {

    @Resource
    private DiversionRecordServiceImpl diversionRecordService;

    @Resource
    private DiversionRecordMapper diversionRecordMapper;


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

        // 调用
        diversionRecordService.updateDiversionRecord(reqVO);
        // 校验是否更新正确
        DiversionRecordDO diversionRecord = diversionRecordMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, diversionRecord);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> diversionRecordService.updateDiversionRecord(reqVO), DIVERSION_RECORD_NOT_EXISTS);
    }

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

        // 调用
        diversionRecordService.deleteDiversionRecord(id);
       // 校验数据不存在了
       assertNull(diversionRecordMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> diversionRecordService.deleteDiversionRecord(id), DIVERSION_RECORD_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetDiversionRecordPage() {
       // mock 数据
       DiversionRecordDO dbDiversionRecord = randomPojo(DiversionRecordDO.class, o -> { // 等会查询到
           o.setDealerId(null);
           o.setDealerName(null);
           o.setBatchId(null);
           o.setGoodsId(null);
           o.setGoodsName(null);
           o.setScanLocation(null);
           o.setScanTime(null);
           o.setDiversionState(null);
           o.setDiversionLevel(null);
           o.setStatus(null);
           o.setRemarks(null);
           o.setBelongBusinessId(null);
           o.setBelongBusinessName(null);
           o.setCreateTime(null);
       });
       diversionRecordMapper.insert(dbDiversionRecord);
       // 测试 dealerId 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setDealerId(null)));
       // 测试 dealerName 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setDealerName(null)));
       // 测试 batchId 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setBatchId(null)));
       // 测试 goodsId 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setGoodsId(null)));
       // 测试 goodsName 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setGoodsName(null)));
       // 测试 scanLocation 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setScanLocation(null)));
       // 测试 scanTime 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setScanTime(null)));
       // 测试 diversionState 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setDiversionState(null)));
       // 测试 diversionLevel 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setDiversionLevel(null)));
       // 测试 status 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setStatus(null)));
       // 测试 remarks 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setRemarks(null)));
       // 测试 belongBusinessId 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setBelongBusinessId(null)));
       // 测试 belongBusinessName 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setBelongBusinessName(null)));
       // 测试 createTime 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setCreateTime(null)));
       // 准备参数
       DiversionRecordPageReqVO reqVO = new DiversionRecordPageReqVO();
       reqVO.setDealerId(null);
       reqVO.setDealerName(null);
       reqVO.setBatchId(null);
       reqVO.setGoodsId(null);
       reqVO.setGoodsName(null);
       reqVO.setScanLocation(null);
//       reqVO.setScanTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setDiversionState(null);
       reqVO.setDiversionLevel(null);
       reqVO.setStatus(null);
       reqVO.setRemarks(null);
       reqVO.setBelongBusinessId(null);
       reqVO.setBelongBusinessName(null);
//       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<DiversionRecordDO> pageResult = diversionRecordService.getDiversionRecordPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbDiversionRecord, pageResult.getList().get(0));
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetDiversionRecordList() {
       // mock 数据
       DiversionRecordDO dbDiversionRecord = randomPojo(DiversionRecordDO.class, o -> { // 等会查询到
           o.setDealerId(null);
           o.setDealerName(null);
           o.setBatchId(null);
           o.setGoodsId(null);
           o.setGoodsName(null);
           o.setScanLocation(null);
           o.setScanTime(null);
           o.setDiversionState(null);
           o.setDiversionLevel(null);
           o.setStatus(null);
           o.setRemarks(null);
           o.setBelongBusinessId(null);
           o.setBelongBusinessName(null);
           o.setCreateTime(null);
       });
       diversionRecordMapper.insert(dbDiversionRecord);
       // 测试 dealerId 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setDealerId(null)));
       // 测试 dealerName 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setDealerName(null)));
       // 测试 batchId 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setBatchId(null)));
       // 测试 goodsId 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setGoodsId(null)));
       // 测试 goodsName 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setGoodsName(null)));
       // 测试 scanLocation 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setScanLocation(null)));
       // 测试 scanTime 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setScanTime(null)));
       // 测试 diversionState 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setDiversionState(null)));
       // 测试 diversionLevel 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setDiversionLevel(null)));
       // 测试 status 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setStatus(null)));
       // 测试 remarks 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setRemarks(null)));
       // 测试 belongBusinessId 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setBelongBusinessId(null)));
       // 测试 belongBusinessName 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setBelongBusinessName(null)));
       // 测试 createTime 不匹配
       diversionRecordMapper.insert(cloneIgnoreId(dbDiversionRecord, o -> o.setCreateTime(null)));
       // 准备参数
       DiversionRecordExportReqVO reqVO = new DiversionRecordExportReqVO();
       reqVO.setDealerId(null);
       reqVO.setDealerName(null);
       reqVO.setBatchId(null);
       reqVO.setGoodsId(null);
       reqVO.setGoodsName(null);
       reqVO.setScanLocation(null);
//       reqVO.setScanTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setDiversionState(null);
       reqVO.setDiversionLevel(null);
       reqVO.setStatus(null);
       reqVO.setRemarks(null);
       reqVO.setBelongBusinessId(null);
       reqVO.setBelongBusinessName(null);
//       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       List<DiversionRecordDO> list = diversionRecordService.getDiversionRecordList(reqVO);
       // 断言
       assertEquals(1, list.size());
       assertPojoEquals(dbDiversionRecord, list.get(0));
    }

}
