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

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.orgworker.vo.OrgWorkerPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.orgworker.vo.OrgWorkerRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.orgworker.vo.OrgWorkerSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.orgworker.OrgWorkerDO;
import cn.iocoder.yudao.module.mes.dal.mysql.orgworker.OrgWorkerMapper;
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.ORG_WORKER_NOT_EXISTS;
import static org.junit.jupiter.api.Assertions.*;

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

    @Resource
    private OrgWorkerServiceImpl orgWorkerService;

    @Resource
    private OrgWorkerMapper orgWorkerMapper;

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

        // 调用
        Long orgWorkerId = orgWorkerService.createOrgWorker(createReqVO);
        // 断言
        assertNotNull(orgWorkerId);
        // 校验记录的属性是否正确
        OrgWorkerDO orgWorker = orgWorkerMapper.selectById(orgWorkerId);
        assertPojoEquals(createReqVO, orgWorker, "id");
    }

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

        // 调用
        orgWorkerService.updateOrgWorker(updateReqVO);
        // 校验是否更新正确
        OrgWorkerDO orgWorker = orgWorkerMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, orgWorker);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> orgWorkerService.updateOrgWorker(updateReqVO), ORG_WORKER_NOT_EXISTS);
    }

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

        // 调用
        orgWorkerService.deleteOrgWorker(id);
       // 校验数据不存在了
       assertNull(orgWorkerMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> orgWorkerService.deleteOrgWorker(id), ORG_WORKER_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetOrgWorkerPage() {
       // mock 数据
       OrgWorkerDO dbOrgWorker = randomPojo(OrgWorkerDO.class, o -> { // 等会查询到
           o.setOrgId(null);
           o.setWorkDate(null);
           o.setGroupType(null);
           o.setWorkerId(null);
           o.setReportId(null);
           o.setCreateTime(null);
       });
       orgWorkerMapper.insert(dbOrgWorker);
       // 测试 orgId 不匹配
       orgWorkerMapper.insert(cloneIgnoreId(dbOrgWorker, o -> o.setOrgId(null)));
       // 测试 workDate 不匹配
       orgWorkerMapper.insert(cloneIgnoreId(dbOrgWorker, o -> o.setWorkDate(null)));
       // 测试 groupType 不匹配
       orgWorkerMapper.insert(cloneIgnoreId(dbOrgWorker, o -> o.setGroupType(null)));
       // 测试 workerId 不匹配
       orgWorkerMapper.insert(cloneIgnoreId(dbOrgWorker, o -> o.setWorkerId(null)));
       // 测试 reportId 不匹配
       orgWorkerMapper.insert(cloneIgnoreId(dbOrgWorker, o -> o.setReportId(null)));
       // 测试 createTime 不匹配
       orgWorkerMapper.insert(cloneIgnoreId(dbOrgWorker, o -> o.setCreateTime(null)));
       // 准备参数
       OrgWorkerPageReqVO reqVO = new OrgWorkerPageReqVO();
       reqVO.setOrgId(null);
       reqVO.setWorkDate(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setGroupType(null);
       reqVO.setWorkerId(null);
       reqVO.setReportId(null);
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<OrgWorkerRespVO> pageResult = orgWorkerService.getOrgWorkerPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbOrgWorker, pageResult.getList().get(0));
    }

}