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

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.scanrecord.vo.*;
import cn.iocoder.yudao.module.identity.dal.dataobject.scanrecord.ScanRecordDO;
import cn.iocoder.yudao.module.identity.dal.mysql.scanrecord.ScanRecordMapper;
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 ScanRecordServiceImpl} 的单元测试类
*
* @author 芋道源码
*/
@Import(ScanRecordServiceImpl.class)
public class ScanRecordServiceImplTest extends BaseDbUnitTest {

    @Resource
    private ScanRecordServiceImpl scanRecordService;

    @Resource
    private ScanRecordMapper scanRecordMapper;

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

        // 调用
        Long scanRecordId = scanRecordService.createScanRecord(reqVO);
        // 断言
        assertNotNull(scanRecordId);
        // 校验记录的属性是否正确
        ScanRecordDO scanRecord = scanRecordMapper.selectById(scanRecordId);
        assertPojoEquals(reqVO, scanRecord);
    }

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

        // 调用
        scanRecordService.updateScanRecord(reqVO);
        // 校验是否更新正确
        ScanRecordDO scanRecord = scanRecordMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, scanRecord);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> scanRecordService.updateScanRecord(reqVO), SCAN_RECORD_NOT_EXISTS);
    }

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

        // 调用
        scanRecordService.deleteScanRecord(id);
       // 校验数据不存在了
       assertNull(scanRecordMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> scanRecordService.deleteScanRecord(id), SCAN_RECORD_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetScanRecordPage() {
       // mock 数据
       ScanRecordDO dbScanRecord = randomPojo(ScanRecordDO.class, o -> { // 等会查询到
           o.setGenerateRecordId(null);
           o.setIdentityUrl(null);
           o.setBatchId(null);
           o.setGoodsId(null);
           o.setGoodsName(null);
           o.setScanIp(null);
           o.setBatchType(null);
           o.setScanArea(null);
           o.setScanTime(null);
           o.setScanLocation(null);
           o.setBelongBusinessId(null);
           o.setBelongBusinessName(null);
       });
       scanRecordMapper.insert(dbScanRecord);
       // 测试 generateRecordId 不匹配
       scanRecordMapper.insert(cloneIgnoreId(dbScanRecord, o -> o.setGenerateRecordId(null)));
       // 测试 identityUrl 不匹配
       scanRecordMapper.insert(cloneIgnoreId(dbScanRecord, o -> o.setIdentityUrl(null)));
       // 测试 batchId 不匹配
       scanRecordMapper.insert(cloneIgnoreId(dbScanRecord, o -> o.setBatchId(null)));
       // 测试 goodsId 不匹配
       scanRecordMapper.insert(cloneIgnoreId(dbScanRecord, o -> o.setGoodsId(null)));
       // 测试 goodsName 不匹配
       scanRecordMapper.insert(cloneIgnoreId(dbScanRecord, o -> o.setGoodsName(null)));
       // 测试 scanIp 不匹配
       scanRecordMapper.insert(cloneIgnoreId(dbScanRecord, o -> o.setScanIp(null)));
       // 测试 batchType 不匹配
       scanRecordMapper.insert(cloneIgnoreId(dbScanRecord, o -> o.setBatchType(null)));
       // 测试 scanArea 不匹配
       scanRecordMapper.insert(cloneIgnoreId(dbScanRecord, o -> o.setScanArea(null)));
       // 测试 scanTime 不匹配
       scanRecordMapper.insert(cloneIgnoreId(dbScanRecord, o -> o.setScanTime(null)));
       // 测试 scanLocation 不匹配
       scanRecordMapper.insert(cloneIgnoreId(dbScanRecord, o -> o.setScanLocation(null)));
       // 测试 belongBusinessId 不匹配
       scanRecordMapper.insert(cloneIgnoreId(dbScanRecord, o -> o.setBelongBusinessId(null)));
       // 测试 belongBusinessName 不匹配
       scanRecordMapper.insert(cloneIgnoreId(dbScanRecord, o -> o.setBelongBusinessName(null)));
       // 准备参数
       ScanRecordPageReqVO reqVO = new ScanRecordPageReqVO();
       reqVO.setGenerateRecordId(null);
       reqVO.setIdentityUrl(null);
       reqVO.setBatchId(null);
       reqVO.setGoodsId(null);
       reqVO.setGoodsName(null);
       reqVO.setScanIp(null);
       reqVO.setBatchType(null);
       reqVO.setScanArea(null);
//       reqVO.setScanTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setScanLocation(null);
       reqVO.setBelongBusinessId(null);
       reqVO.setBelongBusinessName(null);

       // 调用
       PageResult<ScanRecordDO> pageResult = scanRecordService.getScanRecordPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbScanRecord, pageResult.getList().get(0));
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetScanRecordList() {
       // mock 数据
       ScanRecordDO dbScanRecord = randomPojo(ScanRecordDO.class, o -> { // 等会查询到
           o.setGenerateRecordId(null);
           o.setIdentityUrl(null);
           o.setBatchId(null);
           o.setGoodsId(null);
           o.setGoodsName(null);
           o.setScanIp(null);
           o.setBatchType(null);
           o.setScanArea(null);
           o.setScanTime(null);
           o.setScanLocation(null);
           o.setBelongBusinessId(null);
           o.setBelongBusinessName(null);
       });
       scanRecordMapper.insert(dbScanRecord);
       // 测试 generateRecordId 不匹配
       scanRecordMapper.insert(cloneIgnoreId(dbScanRecord, o -> o.setGenerateRecordId(null)));
       // 测试 identityUrl 不匹配
       scanRecordMapper.insert(cloneIgnoreId(dbScanRecord, o -> o.setIdentityUrl(null)));
       // 测试 batchId 不匹配
       scanRecordMapper.insert(cloneIgnoreId(dbScanRecord, o -> o.setBatchId(null)));
       // 测试 goodsId 不匹配
       scanRecordMapper.insert(cloneIgnoreId(dbScanRecord, o -> o.setGoodsId(null)));
       // 测试 goodsName 不匹配
       scanRecordMapper.insert(cloneIgnoreId(dbScanRecord, o -> o.setGoodsName(null)));
       // 测试 scanIp 不匹配
       scanRecordMapper.insert(cloneIgnoreId(dbScanRecord, o -> o.setScanIp(null)));
       // 测试 batchType 不匹配
       scanRecordMapper.insert(cloneIgnoreId(dbScanRecord, o -> o.setBatchType(null)));
       // 测试 scanArea 不匹配
       scanRecordMapper.insert(cloneIgnoreId(dbScanRecord, o -> o.setScanArea(null)));
       // 测试 scanTime 不匹配
       scanRecordMapper.insert(cloneIgnoreId(dbScanRecord, o -> o.setScanTime(null)));
       // 测试 scanLocation 不匹配
       scanRecordMapper.insert(cloneIgnoreId(dbScanRecord, o -> o.setScanLocation(null)));
       // 测试 belongBusinessId 不匹配
       scanRecordMapper.insert(cloneIgnoreId(dbScanRecord, o -> o.setBelongBusinessId(null)));
       // 测试 belongBusinessName 不匹配
       scanRecordMapper.insert(cloneIgnoreId(dbScanRecord, o -> o.setBelongBusinessName(null)));
       // 准备参数
       ScanRecordExportReqVO reqVO = new ScanRecordExportReqVO();
       reqVO.setGenerateRecordId(null);
       reqVO.setIdentityUrl(null);
       reqVO.setBatchId(null);
       reqVO.setGoodsId(null);
       reqVO.setGoodsName(null);
       reqVO.setScanIp(null);
       reqVO.setBatchType(null);
       reqVO.setScanArea(null);
//       reqVO.setScanTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setScanLocation(null);
       reqVO.setBelongBusinessId(null);
       reqVO.setBelongBusinessName(null);

       // 调用
       List<ScanRecordDO> list = scanRecordService.getScanRecordList(reqVO);
       // 断言
       assertEquals(1, list.size());
       assertPojoEquals(dbScanRecord, list.get(0));
    }

}
