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

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.workteam.vo.WorkTeamPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.workteam.vo.WorkTeamRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.workteam.vo.WorkTeamSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.workteam.WorkTeamDO;
import cn.iocoder.yudao.module.mes.dal.mysql.workteam.WorkTeamMapper;
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_TEAM_NOT_EXISTS;
import static org.junit.jupiter.api.Assertions.*;

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

    @Resource
    private WorkTeamServiceImpl workTeamService;

    @Resource
    private WorkTeamMapper workTeamMapper;

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

        // 调用
        Long workTeamId = workTeamService.createWorkTeam(createReqVO);
        // 断言
        assertNotNull(workTeamId);
        // 校验记录的属性是否正确
        WorkTeamDO workTeam = workTeamMapper.selectById(workTeamId);
        assertPojoEquals(createReqVO, workTeam, "id");
    }

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

        // 调用
        workTeamService.updateWorkTeam(updateReqVO);
        // 校验是否更新正确
        WorkTeamDO workTeam = workTeamMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, workTeam);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> workTeamService.updateWorkTeam(updateReqVO), WORK_TEAM_NOT_EXISTS);
    }

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

        // 调用
        workTeamService.deleteWorkTeam(id);
       // 校验数据不存在了
       assertNull(workTeamMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> workTeamService.deleteWorkTeam(id), WORK_TEAM_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetWorkTeamPage() {
       // mock 数据
       WorkTeamDO dbWorkTeam = randomPojo(WorkTeamDO.class, o -> { // 等会查询到
           o.setTeamName(null);
           o.setTeamLeaderId(null);
           o.setTeamType(null);
           o.setRemark(null);
           o.setIsEnable(null);
           o.setCreateTime(null);
       });
       workTeamMapper.insert(dbWorkTeam);
       // 测试 teamName 不匹配
       workTeamMapper.insert(cloneIgnoreId(dbWorkTeam, o -> o.setTeamName(null)));
       // 测试 teamLeaderId 不匹配
       workTeamMapper.insert(cloneIgnoreId(dbWorkTeam, o -> o.setTeamLeaderId(null)));
       // 测试 teamType 不匹配
       workTeamMapper.insert(cloneIgnoreId(dbWorkTeam, o -> o.setTeamType(null)));
       // 测试 remark 不匹配
       workTeamMapper.insert(cloneIgnoreId(dbWorkTeam, o -> o.setRemark(null)));
       // 测试 isEnable 不匹配
       workTeamMapper.insert(cloneIgnoreId(dbWorkTeam, o -> o.setIsEnable(null)));
       // 测试 createTime 不匹配
       workTeamMapper.insert(cloneIgnoreId(dbWorkTeam, o -> o.setCreateTime(null)));
       // 准备参数
       WorkTeamPageReqVO reqVO = new WorkTeamPageReqVO();
       reqVO.setTeamName(null);
       reqVO.setTeamLeaderId(null);
       reqVO.setTeamType(null);
       reqVO.setRemark(null);
       reqVO.setIsEnable(null);
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<WorkTeamRespVO> pageResult = workTeamService.getWorkTeamPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbWorkTeam, pageResult.getList().get(0));
    }

}