package cn.iocoder.yudao.module.system.api.user;

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.module.system.api.user.dto.*;
import cn.iocoder.yudao.module.system.api.user.vo.AdminUserRpcVO;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
@DataPermission(enable = false)
public class AdminUserApiImpl implements AdminUserApi {

    @Resource
    private AdminUserService userService;

    @Resource
    private DeptService deptService;

    @Override
    public CommonResult<AdminUserRespDTO> getUser(Long id) {
        AdminUserDO user = userService.getUser(id);
        return success(BeanUtils.toBean(user, AdminUserRespDTO.class));
    }

    @Override
    public CommonResult<List<AdminUserRespDTO>> getUserList(Collection<Long> ids) {
        List<AdminUserDO> users = userService.getUserList(ids);
        return success(BeanUtils.toBean(users, AdminUserRespDTO.class));
    }

    @Override
    public CommonResult<List<AdminUserApiVO>> getUserList(AdminUserApiDTO dto) {
        List<AdminUserDO> users = userService.getUserList(dto);
        List<AdminUserApiVO> list = BeanUtil.copyToList(users, AdminUserApiVO.class);
        return success(list);
    }

    @Override
    public CommonResult<List<AdminUserRespDTO>> getUserListByDeptIds(Collection<Long> deptIds) {
        List<AdminUserDO> users = userService.getUserListByDeptIds(deptIds, null);
        return success(BeanUtils.toBean(users, AdminUserRespDTO.class));
    }

    @Override
    @DataPermission(enable = false)
    public CommonResult<List<AdminUserRespDTO>> getUserByDeptIdAll(Long deptId) {

        List<DeptDO> deptDOS = deptService.getChildDept(deptId);

        List<AdminUserDO> user = userService.getUserListByDeptIds(convertList(deptDOS, DeptDO::getId), null);
        user = user.stream().filter(data -> !data.getId().equals(getLoginUserId())).collect(Collectors.toList());

        return success(BeanUtils.toBean(user, AdminUserRespDTO.class));
    }

    @Override
    public CommonResult<List<AdminUserRespDTO>> getUserListByPostIds(Collection<Long> postIds) {
        List<AdminUserDO> users = userService.getUserListByPostIds(postIds);
        return success(BeanUtils.toBean(users, AdminUserRespDTO.class));
    }

    @Override
    public CommonResult<Boolean> validateUserList(Set<Long> ids) {
        userService.validateUserList(ids);
        return success(true);
    }

    @Override
    public CommonResult<List<Long>> getUserIdsByUserIdGroupByDept(Long userId) {
        AdminUserDO user = userService.getUser(userId);
        List<Long> userIds = userService.getUserIdsByDeptId(user.getDeptId());
        return success(userIds);
    }

    @Override
    public CommonResult<Long> createUser(UserSaveRespDTO saveRespDTO) {

        return success(userService.createUser(BeanUtils.toBean(saveRespDTO, UserSaveReqVO.class)));
    }

    @Override
    public void updateFieldworkType(Collection<Long> userId, Integer fieldworkFlag) {

        userService.updateFieldworkType(userId, fieldworkFlag);
    }

    @Override
    public void updateUserStaffing(Long userId, Integer userStaffing) {

        userService.updateUserStaffing(userId, userStaffing);
    }

    @Override
    public CommonResult<List<Long>> getUserIdsByUserNature(Integer userNature) {

        List<AdminUserDO> userDOS = userService.getListByNature(userNature);
        return success(convertList(userDOS, AdminUserDO::getId));
    }

    @Override
    public CommonResult<List<AdminUserRpcVO>> getFactoryUsers(Long factoryId) {
        List<AdminUserDO> factoryUsers = userService.getFactoryUsers(factoryId);
        return success(BeanUtils.toBean(factoryUsers, AdminUserRpcVO.class));
    }

    @Override
    public CommonResult updateUserEntryDate(UserSaveRespDTO dto) {
        userService.updateUserEntryDate(dto.getId(), dto.getEntryDate());
        return success(true);
    }

    @Override
    public CommonResult<PageResult<AdminUserApiVO>> getUserPage(AdminUserPageApiDTO dto) {
        PageResult<AdminUserApiVO> vo = new PageResult<>();
        UserPageReqVO userPageReqVO = BeanUtil.copyProperties(dto, UserPageReqVO.class);
        PageResult<AdminUserDO> pageResult = userService.getUserPage(userPageReqVO);
        BeanUtil.copyProperties(pageResult, vo);
        return success(vo);
    }

    @Override
    public CommonResult<List<Long>> getUserListBySubordinateIds(Long adminId) {
        List<Long> ids = userService.getUserListBySubordinateIds(adminId);
        return success(ids);
    }

    @Override
    public CommonResult<List<AdminUserRespDTO>> getUserListAll(Integer userType, Long deptId, Integer status, String roleCodes) {
        List<AdminUserDO> userList = userService.getUserListByStatus(userType, deptId, status, roleCodes, null);
        return success(BeanUtils.toBean(userList, AdminUserRespDTO.class));
    }

    @Override
    public CommonResult<PageResult<AdminUserApiVO>> getUserPageByRole(AdminUserPageApiDTO dto) {

        return success(BeanUtils.toBean(userService.getUserPageByRole(dto), AdminUserApiVO.class));
    }

    @Override
    public CommonResult<PageResult<AdminUserApiVO>> getUserBringDeptPage(AdminUserPageApiDTO dto) {
        PageResult<AdminUserApiVO> vo = new PageResult<>();
        UserPageReqVO userPageReqVO = BeanUtil.copyProperties(dto, UserPageReqVO.class);
        PageResult<AdminUserDO> pageResult = userService.getUserBringDeptPage(userPageReqVO);
        BeanUtil.copyProperties(pageResult, vo);
        return success(vo);
    }

}
