package com.njcn.rdms.module.system.api.user;

import com.njcn.rdms.framework.common.pojo.CommonResult;
import com.njcn.rdms.framework.common.util.object.BeanUtils;
import com.njcn.rdms.module.system.api.user.dto.AdminUserRespDTO;
import com.njcn.rdms.module.system.api.user.dto.UserManagementRelationRespDTO;
import com.njcn.rdms.module.system.dal.dataobject.user.AdminUserDO;
import com.njcn.rdms.module.system.dal.dataobject.user.UserManagementRelationDO;
import com.njcn.rdms.module.system.service.user.UserManagementRelationService;
import io.swagger.v3.oas.annotations.Hidden;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import static com.njcn.rdms.framework.common.pojo.CommonResult.success;

@RestController
@Validated
@Hidden
public class UserManagementRelationApiImpl implements UserManagementRelationApi {

    @Resource
    private UserManagementRelationService userManagementRelationService;

    @Override
    public CommonResult<List<UserManagementRelationRespDTO>> getRelationListByManagerUserId(Long managerUserId) {
        List<UserManagementRelationDO> list = userManagementRelationService.getRelationListByManagerUserId(managerUserId);
        return success(BeanUtils.toBean(list, UserManagementRelationRespDTO.class));
    }

    @Override
    public CommonResult<List<UserManagementRelationRespDTO>> getRelationListBySubordinateUserId(Long subordinateUserId) {
        List<UserManagementRelationDO> list = userManagementRelationService.getRelationListBySubordinateUserId(subordinateUserId);
        return success(BeanUtils.toBean(list, UserManagementRelationRespDTO.class));
    }

    @Override
    public CommonResult<AdminUserRespDTO> getDirectManager(Long userId) {
        AdminUserDO manager = userManagementRelationService.getDirectManager(userId);
        if (manager == null) {
            return success(null);
        }
        return success(BeanUtils.toBean(manager, AdminUserRespDTO.class));
    }

    @Override
    public CommonResult<List<UserManagementRelationRespDTO>> getRelationList(Collection<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return success(Collections.emptyList());
        }
        List<UserManagementRelationDO> list = userManagementRelationService.getRelationList(ids);
        return success(BeanUtils.toBean(list, UserManagementRelationRespDTO.class));
    }

    @Override
    public CommonResult<Set<Long>> getAllSubordinateUserIds(Long managerUserId) {
        return success(userManagementRelationService.getAllSubordinateUserIds(managerUserId));
    }

}
