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

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;


import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;

import cn.iocoder.yudao.module.mes.controller.admin.organization.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.organization.OrganizationDO;
import cn.iocoder.yudao.module.mes.dal.mysql.organization.OrganizationMapper;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import org.springframework.context.annotation.Import;

import javax.annotation.Resource;
import java.util.*;
import java.time.LocalDateTime;

import static cn.hutool.core.util.RandomUtil.*;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.*;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.*;
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.*;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

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

    @Resource
    private OrganizationServiceImpl organizationService;

    @Resource
    private OrganizationMapper organizationMapper;

    @Test
    public void testCreateOrganization_success() {
        // 准备参数
        OrganizationSaveReqVO createReqVO = randomPojo(OrganizationSaveReqVO.class).setId(null);
        createReqVO.setParentId(0L);
        // 调用
        Long organizationId = organizationService.createOrganization(createReqVO);
        // 断言
        assertNotNull(organizationId);
        // 校验记录的属性是否正确
        OrganizationDO organization = organizationMapper.selectById(organizationId);
        assertPojoEquals(createReqVO, organization, "id");
    }

    @Test
    public void testUpdateOrganization_success() {
        // mock 数据
        OrganizationDO dbOrganization = randomPojo(OrganizationDO.class);
        organizationMapper.insert(dbOrganization);// @Sql: 先插入出一条存在的数据
        // 准备参数
        OrganizationSaveReqVO updateReqVO = randomPojo(OrganizationSaveReqVO.class, o -> {
            o.setId(dbOrganization.getId()); // 设置更新的 ID
        });
        updateReqVO.setParentId(0L);
        // 调用
        organizationService.updateOrganization(updateReqVO);
        // 校验是否更新正确
        OrganizationDO organization = organizationMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, organization);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> organizationService.updateOrganization(updateReqVO), ORGANIZATION_NOT_EXISTS);
    }

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

        // 调用
        organizationService.deleteOrganization(id);
       // 校验数据不存在了
       assertNull(organizationMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> organizationService.deleteOrganization(id), ORGANIZATION_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetOrganizationList() {
       // mock 数据
       OrganizationDO dbOrganization = randomPojo(OrganizationDO.class, o -> { // 等会查询到
           o.setName(null);
           o.setParentId(null);
           o.setSort(null);
           o.setWorkerUserId(null);
           o.setMachineId(null);
           o.setPhone(null);
           o.setEmail(null);
           o.setIsEnable(null);
           o.setStatus(null);
           o.setOrgClass(null);
           o.setOrgType(null);
           o.setCreateTime(null);
       });
       organizationMapper.insert(dbOrganization);
       // 测试 name 不匹配
       organizationMapper.insert(cloneIgnoreId(dbOrganization, o -> o.setName(null)));
       // 测试 parentId 不匹配
       organizationMapper.insert(cloneIgnoreId(dbOrganization, o -> o.setParentId(null)));
       // 测试 sort 不匹配
       organizationMapper.insert(cloneIgnoreId(dbOrganization, o -> o.setSort(null)));
       // 测试 workerUserId 不匹配
       organizationMapper.insert(cloneIgnoreId(dbOrganization, o -> o.setWorkerUserId(null)));
       // 测试 machineId 不匹配
       organizationMapper.insert(cloneIgnoreId(dbOrganization, o -> o.setMachineId(null)));
       // 测试 phone 不匹配
       organizationMapper.insert(cloneIgnoreId(dbOrganization, o -> o.setPhone(null)));
       // 测试 email 不匹配
       organizationMapper.insert(cloneIgnoreId(dbOrganization, o -> o.setEmail(null)));
       // 测试 isEnable 不匹配
       organizationMapper.insert(cloneIgnoreId(dbOrganization, o -> o.setIsEnable(null)));
       // 测试 status 不匹配
       organizationMapper.insert(cloneIgnoreId(dbOrganization, o -> o.setStatus(null)));
       // 测试 orgClass 不匹配
       organizationMapper.insert(cloneIgnoreId(dbOrganization, o -> o.setOrgClass(null)));
       // 测试 orgType 不匹配
       organizationMapper.insert(cloneIgnoreId(dbOrganization, o -> o.setOrgType(null)));
       // 测试 createTime 不匹配
       organizationMapper.insert(cloneIgnoreId(dbOrganization, o -> o.setCreateTime(null)));
       // 准备参数
       OrganizationListReqVO reqVO = new OrganizationListReqVO();
       reqVO.setName(null);
       reqVO.setParentId(null);
       reqVO.setSort(null);
       reqVO.setWorkerUserId(null);
       reqVO.setMachineId(null);
       reqVO.setPhone(null);
       reqVO.setEmail(null);
       reqVO.setIsEnable(null);
       reqVO.setStatus(null);
       reqVO.setOrgClass(null);
       reqVO.setOrgType(null);
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       List<OrganizationDO> list = organizationService.getOrganizationList(reqVO);
       // 断言
       assertEquals(1, list.size());
       assertPojoEquals(dbOrganization, list.get(0));
    }

}