package com.cf.imes.module.system.service.process;

import com.cf.imes.module.system.controller.admin.process.vo.process.ProcessPageReqVO;
import com.cf.imes.module.system.controller.admin.process.vo.process.ProcessSaveReqVO;
import com.cf.imes.module.system.controller.admin.process.vo.process.ProcessUserSaveReqVO;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import javax.annotation.Resource;

import com.cf.imes.framework.test.core.ut.BaseDbUnitTest;

import com.cf.imes.module.system.dal.dataobject.process.ProcessDO;
import com.cf.imes.module.system.dal.mysql.process.ProcessMapper;
import com.cf.imes.framework.common.pojo.PageResult;

import org.springframework.context.annotation.Import;

import static com.cf.imes.module.system.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 org.junit.jupiter.api.Assertions.*;

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

    @Resource
    private ProcessServiceImpl processService;

    @Resource
    private ProcessMapper processMapper;

    @Test
    public void testCreateProcess_success() {
        // 准备参数
        ProcessUserSaveReqVO createReqVO = randomPojo(ProcessUserSaveReqVO.class).setId(null);
        System.out.println(createReqVO);

        // 调用
        Long processId = processService.createProcess(createReqVO);
        // 断言
        assertNotNull(processId);
        // 校验记录的属性是否正确
        ProcessDO process = processMapper.selectById(processId);
        assertPojoEquals(createReqVO, process, "id");
    }

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

        // 调用
        processService.updateProcess(updateReqVO);
        // 校验是否更新正确
        ProcessDO process = processMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, process);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> processService.updateProcess(updateReqVO), PROCESS_NOT_EXISTS);
    }

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

        // 调用
        processService.deleteProcess(id);
       // 校验数据不存在了
       assertNull(processMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> processService.deleteProcess(id), PROCESS_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetProcessPage() {
       // mock 数据
       ProcessDO dbProcess = randomPojo(ProcessDO.class, o -> { // 等会查询到
           o.setName(null);
           o.setPieceRate(null);
           o.setPieceType(null);
           o.setType(null);
           o.setIsCustom(null);
           o.setIsDealer(null);
           o.setSort(null);
           o.setHourCapacity(null);
           o.setUnit(null);
           o.setIsEnabled(null);
           o.setPrepareTime(null);
           o.setDescription(null);
           o.setCreateTime(null);
       });
       processMapper.insert(dbProcess);
       // 测试 name 不匹配
       processMapper.insert(cloneIgnoreId(dbProcess, o -> o.setName(null)));
       // 测试 pieceRate 不匹配
       processMapper.insert(cloneIgnoreId(dbProcess, o -> o.setPieceRate(null)));
       // 测试 pieceType 不匹配
       processMapper.insert(cloneIgnoreId(dbProcess, o -> o.setPieceType(null)));
       // 测试 type 不匹配
       processMapper.insert(cloneIgnoreId(dbProcess, o -> o.setType(null)));
       // 测试 isCustom 不匹配
       processMapper.insert(cloneIgnoreId(dbProcess, o -> o.setIsCustom(null)));
       // 测试 isDealer 不匹配
       processMapper.insert(cloneIgnoreId(dbProcess, o -> o.setIsDealer(null)));
       // 测试 sort 不匹配
       processMapper.insert(cloneIgnoreId(dbProcess, o -> o.setSort(null)));
       // 测试 hourCapacity 不匹配
       processMapper.insert(cloneIgnoreId(dbProcess, o -> o.setHourCapacity(null)));
       // 测试 unit 不匹配
       processMapper.insert(cloneIgnoreId(dbProcess, o -> o.setUnit(null)));
       // 测试 isEnabled 不匹配
       processMapper.insert(cloneIgnoreId(dbProcess, o -> o.setIsEnabled(null)));
       // 测试 prepareTime 不匹配
       processMapper.insert(cloneIgnoreId(dbProcess, o -> o.setPrepareTime(null)));
       // 测试 description 不匹配
       processMapper.insert(cloneIgnoreId(dbProcess, o -> o.setDescription(null)));
       // 测试 createTime 不匹配
       processMapper.insert(cloneIgnoreId(dbProcess, o -> o.setCreateTime(null)));
       // 准备参数
       ProcessPageReqVO reqVO = new ProcessPageReqVO();
       reqVO.setName(null);
       reqVO.setPieceRate(null);
       reqVO.setPieceType(null);
       reqVO.setType(null);
       reqVO.setIsCustom(null);
       reqVO.setIsDealer(null);
       reqVO.setSort(null);
       reqVO.setHourCapacity(null);
       reqVO.setUnit(null);
       reqVO.setIsEnabled(null);
//       reqVO.setPrepareTime();
       reqVO.setDescription(null);
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<ProcessDO> pageResult = processService.getProcessPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbProcess, pageResult.getList().get(0));
    }

}