package cn.iocoder.yudao.module.mes.service.workreportplan;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.mes.controller.admin.producereportplan.vo.WorkReportPlanPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.producereportplan.vo.WorkReportPlanSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.workreportplan.WorkReportPlanDO;
import cn.iocoder.yudao.module.mes.dal.mysql.workreportplan.WorkReportPlanMapper;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Import;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
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.mes.enums.ErrorCodeConstants.WORK_REPORT_PLAN_NOT_EXISTS;
import static org.junit.jupiter.api.Assertions.*;

/**
 * {@link WorkReportPlanServiceImpl} 的单元测试类
 *
 * @author 内蒙必硕
 */
@Import(WorkReportPlanServiceImpl.class)
public class WorkReportPlanServiceImplTest extends BaseDbUnitTest {

    @Resource
    private WorkReportPlanServiceImpl workReportPlanService;

    @Resource
    private WorkReportPlanMapper workReportPlanMapper;

    @Test
    public void testCreateWorkReportPlan_success() {
        // 准备参数
        WorkReportPlanSaveReqVO createReqVO = randomPojo(WorkReportPlanSaveReqVO.class).setId(null);

        // 调用
        Long workReportPlanId = workReportPlanService.createWorkReportPlan(createReqVO);
        // 断言
        assertNotNull(workReportPlanId);
        // 校验记录的属性是否正确
        WorkReportPlanDO workReportPlan = workReportPlanMapper.selectById(workReportPlanId);
        assertPojoEquals(createReqVO, workReportPlan, "id");
    }

    @Test
    public void testUpdateWorkReportPlan_success() {
        // mock 数据
        WorkReportPlanDO dbWorkReportPlan = randomPojo(WorkReportPlanDO.class);
        workReportPlanMapper.insert(dbWorkReportPlan);// @Sql: 先插入出一条存在的数据
        // 准备参数
        WorkReportPlanSaveReqVO updateReqVO = randomPojo(WorkReportPlanSaveReqVO.class, o -> {
            o.setId(dbWorkReportPlan.getId()); // 设置更新的 ID
        });

        // 调用
        workReportPlanService.updateWorkReportPlan(updateReqVO);
        // 校验是否更新正确
        WorkReportPlanDO workReportPlan = workReportPlanMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, workReportPlan);
    }

    @Test
    public void testUpdateWorkReportPlan_notExists() {
        // 准备参数
        WorkReportPlanSaveReqVO updateReqVO = randomPojo(WorkReportPlanSaveReqVO.class);

        // 调用, 并断言异常
        assertServiceException(() -> workReportPlanService.updateWorkReportPlan(updateReqVO), WORK_REPORT_PLAN_NOT_EXISTS);
    }

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

        // 调用
        workReportPlanService.deleteWorkReportPlan(id);
       // 校验数据不存在了
       assertNull(workReportPlanMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> workReportPlanService.deleteWorkReportPlan(id), WORK_REPORT_PLAN_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetWorkReportPlanPage() {
       // mock 数据
       WorkReportPlanDO dbWorkReportPlan = randomPojo(WorkReportPlanDO.class, o -> { // 等会查询到
           o.setReportId(null);
           o.setPlanId(null);
           o.setProductId(null);
           o.setQualityNumberPlan(null);
           o.setWasteNumberPlan(null);
           o.setReportTimePlan(null);
           o.setTotalTimePlan(null);
           o.setCreateTime(null);
       });
       workReportPlanMapper.insert(dbWorkReportPlan);
       // 测试 reportId 不匹配
       workReportPlanMapper.insert(cloneIgnoreId(dbWorkReportPlan, o -> o.setReportId(null)));
       // 测试 planId 不匹配
       workReportPlanMapper.insert(cloneIgnoreId(dbWorkReportPlan, o -> o.setPlanId(null)));
       // 测试 productId 不匹配
       workReportPlanMapper.insert(cloneIgnoreId(dbWorkReportPlan, o -> o.setProductId(null)));
       // 测试 qualityNumberPlan 不匹配
       workReportPlanMapper.insert(cloneIgnoreId(dbWorkReportPlan, o -> o.setQualityNumberPlan(null)));
       // 测试 wasteNumberPlan 不匹配
       workReportPlanMapper.insert(cloneIgnoreId(dbWorkReportPlan, o -> o.setWasteNumberPlan(null)));
       // 测试 reportTimePlan 不匹配
       workReportPlanMapper.insert(cloneIgnoreId(dbWorkReportPlan, o -> o.setReportTimePlan(null)));
       // 测试 totalTimePlan 不匹配
       workReportPlanMapper.insert(cloneIgnoreId(dbWorkReportPlan, o -> o.setTotalTimePlan(null)));
       // 测试 createTime 不匹配
       workReportPlanMapper.insert(cloneIgnoreId(dbWorkReportPlan, o -> o.setCreateTime(null)));
       // 准备参数
       WorkReportPlanPageReqVO reqVO = new WorkReportPlanPageReqVO();
       reqVO.setReportId(null);
       reqVO.setPlanId(null);
       reqVO.setProductId(null);
       reqVO.setQualityNumberPlan(null);
       reqVO.setWasteNumberPlan(null);
       reqVO.setReportTimePlan(null);
       reqVO.setTotalTimePlan(null);
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<WorkReportPlanDO> pageResult = workReportPlanService.getWorkReportPlanPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbWorkReportPlan, pageResult.getList().get(0));
    }

}