package cn.iocoder.yudao.module.product.service.inspection;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.product.controller.admin.inspection.vo.InspectionCreateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.inspection.vo.InspectionExportReqVO;
import cn.iocoder.yudao.module.product.controller.admin.inspection.vo.InspectionPageReqVO;
import cn.iocoder.yudao.module.product.controller.admin.inspection.vo.InspectionUpdateReqVO;
import cn.iocoder.yudao.module.product.dal.dataobject.inspection.InspectionDO;
import cn.iocoder.yudao.module.product.dal.mysql.inspection.InspectionMapper;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Import;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.INSPECTION_NOT_EXISTS;
import static org.junit.jupiter.api.Assertions.*;

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

    @Resource
    private InspectionServiceImpl inspectionService;

    @Resource
    private InspectionMapper inspectionMapper;

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

        // 调用
        Long inspectionId = inspectionService.createInspection(reqVO);
        // 断言
        assertNotNull(inspectionId);
        // 校验记录的属性是否正确
        InspectionDO inspection = inspectionMapper.selectById(inspectionId);
        assertPojoEquals(reqVO, inspection);
    }

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

        // 调用
        inspectionService.updateInspection(reqVO);
        // 校验是否更新正确
        InspectionDO inspection = inspectionMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, inspection);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> inspectionService.updateInspection(reqVO), INSPECTION_NOT_EXISTS);
    }

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

        // 调用
        inspectionService.deleteInspection(id);
       // 校验数据不存在了
       assertNull(inspectionMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> inspectionService.deleteInspection(id), INSPECTION_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetInspectionPage() {
       // mock 数据
       InspectionDO dbInspection = randomPojo(InspectionDO.class, o -> { // 等会查询到
           o.setInspectionId(null);
           o.setGoodsId(null);
           o.setGoodsName(null);
           o.setHarvestBatchId(null);
           o.setHarvestTime(null);
           o.setInspectionStandard(null);
           o.setInspectionResult(null);
           o.setInspectionOwnItem(null);
           o.setInspectionUser(null);
           o.setInspectionEntrustItem(null);
           o.setInspectionOrganization(null);
           o.setInspectionDate(null);
           o.setInspectionLevel(null);
           o.setIssuanceDate(null);
           o.setBlockState(null);
           o.setBlockChainId(null);
           o.setBlockChainBlockIdentity(null);
           o.setBelongBusinessId(null);
           o.setBelongBusinessName(null);
           o.setCreateTime(null);
       });
       inspectionMapper.insert(dbInspection);
       // 测试 inspectionId 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setInspectionId(null)));
       // 测试 goodsId 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setGoodsId(null)));
       // 测试 goodsName 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setGoodsName(null)));
       // 测试 HarvestBatchId 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setHarvestBatchId(null)));
       // 测试 harvestTime 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setHarvestTime(null)));
       // 测试 inspectionStandard 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setInspectionStandard(null)));
       // 测试 inspectionResult 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setInspectionResult(null)));
       // 测试 inspectionOwnItem 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setInspectionOwnItem(null)));
       // 测试 inspectionUser 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setInspectionUser(null)));
       // 测试 inspectionEntrustItem 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setInspectionEntrustItem(null)));
       // 测试 InspectionOrganization 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setInspectionOrganization(null)));
       // 测试 inspectionDate 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setInspectionDate(null)));
       // 测试 inspectionLevel 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setInspectionLevel(null)));
       // 测试 issuanceDate 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setIssuanceDate(null)));
       // 测试 blockState 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setBlockState(null)));
       // 测试 blockChainId 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setBlockChainId(null)));
       // 测试 blockChainBlockIdentity 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setBlockChainBlockIdentity(null)));
       // 测试 belongBusinessId 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setBelongBusinessId(null)));
       // 测试 belongBusinessName 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setBelongBusinessName(null)));
       // 测试 createTime 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setCreateTime(null)));
       // 准备参数
       InspectionPageReqVO reqVO = new InspectionPageReqVO();
        LocalDateTime[] localDateTimes = LocalDateTimeUtils.buildBetweenTime(2023, 2, 1, 2023, 2, 28);
        Date[] dates = new Date[localDateTimes.length];
        for(int i=0; i<localDateTimes.length; i++) {
            dates[i] = Date.from(localDateTimes[i].atZone(ZoneId.systemDefault()).toInstant());
        }
       reqVO.setInspectionId(null);
       reqVO.setGoodsId(null);
       reqVO.setGoodsName(null);
       reqVO.setHarvestBatchId(null);
       reqVO.setHarvestTime(dates);
       reqVO.setInspectionStandard(null);
       reqVO.setInspectionResult(null);
       reqVO.setInspectionOwnItem(null);
       reqVO.setInspectionUser(null);
       reqVO.setInspectionEntrustItem(null);
       reqVO.setInspectionOrganization(null);
       reqVO.setInspectionDate(dates);
       reqVO.setInspectionLevel(null);
       reqVO.setIssuanceDate(dates);
       reqVO.setBlockState(null);
       reqVO.setBlockChainId(null);
       reqVO.setBlockChainBlockIdentity(null);
       reqVO.setBelongBusinessId(null);
       reqVO.setBelongBusinessName(null);
       reqVO.setCreateTime(dates);

       // 调用
       PageResult<InspectionDO> pageResult = inspectionService.getInspectionPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbInspection, pageResult.getList().get(0));
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetInspectionList() {
       // mock 数据
       InspectionDO dbInspection = randomPojo(InspectionDO.class, o -> { // 等会查询到
           o.setInspectionId(null);
           o.setGoodsId(null);
           o.setGoodsName(null);
           o.setHarvestBatchId(null);
           o.setHarvestTime(null);
           o.setInspectionStandard(null);
           o.setInspectionResult(null);
           o.setInspectionOwnItem(null);
           o.setInspectionUser(null);
           o.setInspectionEntrustItem(null);
           o.setInspectionOrganization(null);
           o.setInspectionDate(null);
           o.setInspectionLevel(null);
           o.setIssuanceDate(null);
           o.setBlockState(null);
           o.setBlockChainId(null);
           o.setBlockChainBlockIdentity(null);
           o.setBelongBusinessId(null);
           o.setBelongBusinessName(null);
           o.setCreateTime(null);
       });
       inspectionMapper.insert(dbInspection);
       // 测试 inspectionId 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setInspectionId(null)));
       // 测试 goodsId 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setGoodsId(null)));
       // 测试 goodsName 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setGoodsName(null)));
       // 测试 HarvestBatchId 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setHarvestBatchId(null)));
       // 测试 harvestTime 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setHarvestTime(null)));
       // 测试 inspectionStandard 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setInspectionStandard(null)));
       // 测试 inspectionResult 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setInspectionResult(null)));
       // 测试 inspectionOwnItem 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setInspectionOwnItem(null)));
       // 测试 inspectionUser 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setInspectionUser(null)));
       // 测试 inspectionEntrustItem 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setInspectionEntrustItem(null)));
       // 测试 InspectionOrganization 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setInspectionOrganization(null)));
       // 测试 inspectionDate 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setInspectionDate(null)));
       // 测试 inspectionLevel 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setInspectionLevel(null)));
       // 测试 issuanceDate 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setIssuanceDate(null)));
       // 测试 blockState 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setBlockState(null)));
       // 测试 blockChainId 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setBlockChainId(null)));
       // 测试 blockChainBlockIdentity 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setBlockChainBlockIdentity(null)));
       // 测试 belongBusinessId 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setBelongBusinessId(null)));
       // 测试 belongBusinessName 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setBelongBusinessName(null)));
       // 测试 createTime 不匹配
       inspectionMapper.insert(cloneIgnoreId(dbInspection, o -> o.setCreateTime(null)));
       // 准备参数
       InspectionExportReqVO reqVO = new InspectionExportReqVO();
        LocalDateTime[] localDateTimes = LocalDateTimeUtils.buildBetweenTime(2023, 2, 1, 2023, 2, 28);
        Date[] dates = new Date[localDateTimes.length];
        for(int i=0; i<localDateTimes.length; i++) {
            dates[i] = Date.from(localDateTimes[i].atZone(ZoneId.systemDefault()).toInstant());
        }
       reqVO.setInspectionId(null);
       reqVO.setGoodsId(null);
       reqVO.setGoodsName(null);
       reqVO.setHarvestBatchId(null);
       reqVO.setHarvestTime(dates);
       reqVO.setInspectionStandard(null);
       reqVO.setInspectionResult(null);
       reqVO.setInspectionOwnItem(null);
       reqVO.setInspectionUser(null);
       reqVO.setInspectionEntrustItem(null);
       reqVO.setInspectionOrganization(null);
       reqVO.setInspectionDate(dates);
       reqVO.setInspectionLevel(null);
       reqVO.setIssuanceDate(dates);
       reqVO.setBlockState(null);
       reqVO.setBlockChainId(null);
       reqVO.setBlockChainBlockIdentity(null);
       reqVO.setBelongBusinessId(null);
       reqVO.setBelongBusinessName(null);
       reqVO.setCreateTime(dates);

       // 调用
       List<InspectionDO> list = inspectionService.getInspectionList(reqVO);
       // 断言
       assertEquals(1, list.size());
       assertPojoEquals(dbInspection, list.get(0));
    }

}
