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

import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import javax.annotation.Resource;

import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;

import cn.iocoder.yudao.module.product.controller.admin.inspectionitem.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.inspectionitem.InspectionItemDO;
import cn.iocoder.yudao.module.product.dal.mysql.inspectionitem.InspectionItemMapper;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import org.springframework.context.annotation.Import;

import java.time.ZoneId;
import java.util.*;
import java.time.LocalDateTime;

import static cn.iocoder.yudao.module.product.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 org.junit.jupiter.api.Assertions.*;

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

    @Resource
    private InspectionItemServiceImpl inspectionItemService;

    @Resource
    private InspectionItemMapper inspectionItemMapper;

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

        // 调用
        inspectionItemService.updateInspectionItem(reqVO);
        // 校验是否更新正确
        InspectionItemDO inspectionItem = inspectionItemMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, inspectionItem);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> inspectionItemService.updateInspectionItem(reqVO), INSPECTION_ITEM_NOT_EXISTS);
    }

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

        // 调用
        inspectionItemService.deleteInspectionItem(id);
       // 校验数据不存在了
       assertNull(inspectionItemMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> inspectionItemService.deleteInspectionItem(id), INSPECTION_ITEM_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetInspectionItemPage() {
       // mock 数据
       InspectionItemDO dbInspectionItem = randomPojo(InspectionItemDO.class, o -> { // 等会查询到
           o.setInspectionId(null);
           o.setInspectionItemId(null);
           o.setInspectionItem(null);
           o.setInspectionUnit(null);
           o.setInspectionRequire(null);
           o.setInspectionResult(null);
           o.setInspectionConclusion(null);
           o.setBelongBusinessId(null);
           o.setBelongBusinessName(null);
           o.setCreateTime(null);
       });
       inspectionItemMapper.insert(dbInspectionItem);
       // 测试 inspectionId 不匹配
       inspectionItemMapper.insert(cloneIgnoreId(dbInspectionItem, o -> o.setInspectionId(null)));
       // 测试 inspectionItemId 不匹配
       inspectionItemMapper.insert(cloneIgnoreId(dbInspectionItem, o -> o.setInspectionItemId(null)));
       // 测试 inspectionItem 不匹配
       inspectionItemMapper.insert(cloneIgnoreId(dbInspectionItem, o -> o.setInspectionItem(null)));
       // 测试 inspectionUnit 不匹配
       inspectionItemMapper.insert(cloneIgnoreId(dbInspectionItem, o -> o.setInspectionUnit(null)));
       // 测试 inspectionRequire 不匹配
       inspectionItemMapper.insert(cloneIgnoreId(dbInspectionItem, o -> o.setInspectionRequire(null)));
       // 测试 inspectionResult 不匹配
       inspectionItemMapper.insert(cloneIgnoreId(dbInspectionItem, o -> o.setInspectionResult(null)));
       // 测试 inspectionConclusion 不匹配
       inspectionItemMapper.insert(cloneIgnoreId(dbInspectionItem, o -> o.setInspectionConclusion(null)));
       // 测试 belongBusinessId 不匹配
       inspectionItemMapper.insert(cloneIgnoreId(dbInspectionItem, o -> o.setBelongBusinessId(null)));
       // 测试 belongBusinessName 不匹配
       inspectionItemMapper.insert(cloneIgnoreId(dbInspectionItem, o -> o.setBelongBusinessName(null)));
       // 测试 createTime 不匹配
       inspectionItemMapper.insert(cloneIgnoreId(dbInspectionItem, o -> o.setCreateTime(null)));
       // 准备参数
       InspectionItemPageReqVO reqVO = new InspectionItemPageReqVO();
        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.setInspectionItemId(null);
       reqVO.setInspectionItem(null);
       reqVO.setInspectionUnit(null);
       reqVO.setInspectionRequire(null);
       reqVO.setInspectionResult(null);
       reqVO.setInspectionConclusion(null);
       reqVO.setBelongBusinessId(null);
       reqVO.setBelongBusinessName(null);
       reqVO.setCreateTime(dates);

       // 调用
       PageResult<InspectionItemDO> pageResult = inspectionItemService.getInspectionItemPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbInspectionItem, pageResult.getList().get(0));
    }

}
