package cn.iocoder.yudao.module.configure.service.inspectiontemplate;

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.configure.controller.admin.inspectiontemplate.vo.InspectionTemplateCreateReqVO;
import cn.iocoder.yudao.module.configure.controller.admin.inspectiontemplate.vo.InspectionTemplatePageReqVO;
import cn.iocoder.yudao.module.configure.controller.admin.inspectiontemplate.vo.InspectionTemplateUpdateReqVO;
import cn.iocoder.yudao.module.configure.dal.dataobject.inspectiontemplate.InspectionTemplateDO;
import cn.iocoder.yudao.module.configure.dal.mysql.inspectiontemplate.InspectionTemplateMapper;
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 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.configure.enums.ErrorCodeConstants.INSPECTION_TEMPLATE_NOT_EXISTS;
import static org.junit.jupiter.api.Assertions.*;

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

    @Resource
    private InspectionTemplateServiceImpl inspectionTemplateService;

    @Resource
    private InspectionTemplateMapper inspectionTemplateMapper;

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

        // 调用
        Long inspectionTemplateId = inspectionTemplateService.createInspectionTemplate(reqVO);
        // 断言
        assertNotNull(inspectionTemplateId);
        // 校验记录的属性是否正确
        InspectionTemplateDO inspectionTemplate = inspectionTemplateMapper.selectById(inspectionTemplateId);
        assertPojoEquals(reqVO, inspectionTemplate);
    }

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

        // 调用
        inspectionTemplateService.updateInspectionTemplate(reqVO);
        // 校验是否更新正确
        InspectionTemplateDO inspectionTemplate = inspectionTemplateMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, inspectionTemplate);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> inspectionTemplateService.updateInspectionTemplate(reqVO), INSPECTION_TEMPLATE_NOT_EXISTS);
    }

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

        // 调用
        inspectionTemplateService.deleteInspectionTemplate(id);
       // 校验数据不存在了
       assertNull(inspectionTemplateMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> inspectionTemplateService.deleteInspectionTemplate(id), INSPECTION_TEMPLATE_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetInspectionTemplatePage() {
       // mock 数据
       InspectionTemplateDO dbInspectionTemplate = randomPojo(InspectionTemplateDO.class, o -> { // 等会查询到
           o.setTemplateName(null);
           o.setTemplateId(null);
           o.setBelongBusinessId(null);
           o.setBelongBusinessName(null);
           o.setCreateTime(null);
       });
       inspectionTemplateMapper.insert(dbInspectionTemplate);
       // 测试 templateName 不匹配
       inspectionTemplateMapper.insert(cloneIgnoreId(dbInspectionTemplate, o -> o.setTemplateName(null)));
       // 测试 templateId 不匹配
       inspectionTemplateMapper.insert(cloneIgnoreId(dbInspectionTemplate, o -> o.setTemplateId(null)));
       // 测试 belongBusinessId 不匹配
       inspectionTemplateMapper.insert(cloneIgnoreId(dbInspectionTemplate, o -> o.setBelongBusinessId(null)));
       // 测试 belongBusinessName 不匹配
       inspectionTemplateMapper.insert(cloneIgnoreId(dbInspectionTemplate, o -> o.setBelongBusinessName(null)));
       // 测试 createTime 不匹配
       inspectionTemplateMapper.insert(cloneIgnoreId(dbInspectionTemplate, o -> o.setCreateTime(null)));
       // 准备参数
       InspectionTemplatePageReqVO reqVO = new InspectionTemplatePageReqVO();
        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.setTemplateName(null);
       reqVO.setTemplateId(null);
       reqVO.setBelongBusinessId(null);
       reqVO.setBelongBusinessName(null);
       reqVO.setCreateTime(dates);

       // 调用
       PageResult<InspectionTemplateDO> pageResult = inspectionTemplateService.getInspectionTemplatePage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbInspectionTemplate, pageResult.getList().get(0));
    }

}
