package com.cf.imes.module.executor.service.plan;

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 com.cf.imes.framework.test.core.ut.BaseDbUnitTest;

import com.cf.imes.module.executor.controller.admin.plan.vo.*;
import com.cf.imes.module.executor.dal.dataobject.plan.PlanDO;
import com.cf.imes.module.executor.dal.mysql.plan.PlanMapper;
import com.cf.imes.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 com.cf.imes.module.executor.enums.ErrorCodeConstants.*;
import static com.cf.imes.framework.test.core.util.AssertUtils.*;
import static com.cf.imes.framework.test.core.util.RandomUtils.*;
import static com.cf.imes.framework.common.util.date.LocalDateTimeUtils.*;
import static com.cf.imes.framework.common.util.object.ObjectUtils.*;
import static com.cf.imes.framework.common.util.date.DateUtils.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
 * {@link PlanServiceImpl} 的单元测试类
 *
 * @author 晨丰科技
 */
@Import(PlanServiceImpl.class)
public class PlanServiceImplTest extends BaseDbUnitTest {

    @Resource
    private PlanServiceImpl planService;

    @Resource
    private PlanMapper planMapper;

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

        // 调用
        Long planId = planService.createPlan(createReqVO);
        // 断言
        assertNotNull(planId);
        // 校验记录的属性是否正确
        PlanDO plan = planMapper.selectById(planId);
        assertPojoEquals(createReqVO, plan, "id");
    }

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

        // 调用
        planService.updatePlan(updateReqVO);
        // 校验是否更新正确
        PlanDO plan = planMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, plan);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> planService.updatePlan(updateReqVO), PLAN_NOT_EXISTS);
    }

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

        // 调用
        planService.deletePlan(id);
       // 校验数据不存在了
       assertNull(planMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> planService.deletePlan(id), PLAN_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetPlanPage() {
       // mock 数据
       PlanDO dbPlan = randomPojo(PlanDO.class, o -> { // 等会查询到
           o.setPlanNo(null);
           o.setSort(null);
           o.setType(null);
           o.setStatus(null);
           o.setIsPay(null);
           o.setMachineId(null);
           o.setPlanTime(null);
           o.setOrderNos(null);
           o.setRemark(null);
           o.setCreateTime(null);
           o.setOperator(null);
           o.setProduceTime(null);
       });
       planMapper.insert(dbPlan);
       // 测试 planNo 不匹配
       planMapper.insert(cloneIgnoreId(dbPlan, o -> o.setPlanNo(null)));
       // 测试 sort 不匹配
       planMapper.insert(cloneIgnoreId(dbPlan, o -> o.setSort(null)));
       // 测试 type 不匹配
       planMapper.insert(cloneIgnoreId(dbPlan, o -> o.setType(null)));
       // 测试 status 不匹配
       planMapper.insert(cloneIgnoreId(dbPlan, o -> o.setStatus(null)));
       // 测试 isPay 不匹配
       planMapper.insert(cloneIgnoreId(dbPlan, o -> o.setIsPay(null)));
       // 测试 machineId 不匹配
       planMapper.insert(cloneIgnoreId(dbPlan, o -> o.setMachineId(null)));
       // 测试 planTime 不匹配
       planMapper.insert(cloneIgnoreId(dbPlan, o -> o.setPlanTime(null)));
       // 测试 orderNos 不匹配
       planMapper.insert(cloneIgnoreId(dbPlan, o -> o.setOrderNos(null)));
       // 测试 remark 不匹配
       planMapper.insert(cloneIgnoreId(dbPlan, o -> o.setRemark(null)));
       // 测试 createTime 不匹配
       planMapper.insert(cloneIgnoreId(dbPlan, o -> o.setCreateTime(null)));
       // 测试 operator 不匹配
       planMapper.insert(cloneIgnoreId(dbPlan, o -> o.setOperator(null)));
       // 测试 produceTime 不匹配
       planMapper.insert(cloneIgnoreId(dbPlan, o -> o.setProduceTime(null)));
       // 准备参数
       PlanPageReqVO reqVO = new PlanPageReqVO();
       reqVO.setPlanNo(null);
       reqVO.setSort(null);
       reqVO.setType(null);
       reqVO.setStatus(null);
       reqVO.setIsPay(null);
       reqVO.setMachineId(null);
       reqVO.setPlanTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setOrderNos(null);
       reqVO.setRemark(null);
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setOperator(null);
       reqVO.setProduceTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<PlanRespVO> pageResult = planService.getPlanPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbPlan, pageResult.getList().get(0));
    }

}