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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.pojo.Pms;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.framework.datapermission.core.util.DataPermissionUtils;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
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.stereotype.Service;

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

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

/**
 * Admin 用户 API 实现类
 *
 * @author 芋道源码
 */
@Service
public class AdminUserApiImpl implements AdminUserApi {

    @Resource
    private AdminUserService userService;
    @Resource
    private DeptService deptService;


    @Override
    @DataPermission(enable = false)
    public AdminUserRespDTO getUser(Long id) {
        AdminUserDO user = userService.getUser(id);
        return BeanUtils.toBean(user, AdminUserRespDTO.class);
    }

    @Override
    public List<AdminUserRespDTO> getUserListBySubordinate(Long id) {
//        // 1.1 获取用户负责的部门
//        List<DeptDO> depts = deptService.getDeptListByLeaderUserId(id);
//        if (CollUtil.isEmpty(depts)) {
//            return Collections.emptyList();
//        }
//        // 1.2 获取所有子部门
//        Set<Long> deptIds = convertSet(depts, DeptDO::getId);
//        List<DeptDO> childDeptList = deptService.getChildDeptList(deptIds);
//        if (CollUtil.isNotEmpty(childDeptList)) {
//            deptIds.addAll(convertSet(childDeptList, DeptDO::getId));
//        }
//
//        // 2. 获取部门对应的用户信息
        AdminUserDO user = userService.getUser(id);
        Set<Long> childDeptIdListFromCache = deptService.getChildDeptIdListFromCache(user.getDeptId());
        List<AdminUserDO> users = userService.getUserListByDeptIds(childDeptIdListFromCache);
        users.removeIf(item -> ObjUtil.equal(item.getId(), id)); // 排除自己
        return BeanUtils.toBean(users, AdminUserRespDTO.class);
    }

    @Override
    public List<AdminUserRespDTO> getUserListByDeptId(Long id) {
        DeptDO dept = deptService.getDeptNoPermission(id);
        if (Objects.isNull(dept)) {
            return Collections.emptyList();
        }
        // 1.2 获取所有子部门
        Set<Long> deptIds = new HashSet<>();
        deptIds.add(dept.getId());
        List<DeptDO> childDeptList = deptService.getChildDeptList(deptIds);
        if (CollUtil.isNotEmpty(childDeptList)) {
            deptIds.addAll(convertSet(childDeptList, DeptDO::getId));
        }

        // 2. 获取部门对应的用户信息
        List<AdminUserDO> users = userService.getUserListByDeptIds(deptIds);
        users.removeIf(item -> ObjUtil.equal(item.getId(), id)); // 排除自己
        return BeanUtils.toBean(users, AdminUserRespDTO.class);
    }

    @Override
    public List<AdminUserRespDTO> getUserListByDept(Long id) {
        return BeanUtils.toBean(userService.selectListByDept(id), AdminUserRespDTO.class);
    }

    @Override
    public List<AdminUserRespDTO> getUserList(Collection<Long> ids) {
        return DataPermissionUtils.executeIgnore(() -> { // 禁用数据权限。原因是，一般基于指定 id 的 API 查询，都是数据拼接为主
            List<AdminUserDO> users = userService.getUserList(ids);
            return BeanUtils.toBean(users, AdminUserRespDTO.class);
        });
    }

    @Override
    public List<AdminUserRespDTO> getUserListByDeptIds(Collection<Long> deptIds) {
        List<AdminUserDO> users = userService.getUserListByDeptIds(deptIds);
        return BeanUtils.toBean(users, AdminUserRespDTO.class);
    }
    @Override
    public List<AdminUserRespDTO> getUserListByDeptIdsNew(Collection<Long> deptIds) {
        List<AdminUserDO> users = userService.getUserListByDeptIdsNew(deptIds);
        return BeanUtils.toBean(users, AdminUserRespDTO.class);
    }
    @Override
    @DataPermission(enable = false)
    public List<Pms.deptStat> getUserCountByDeptId(Long deptId) {
        return userService.selectCountByDeptId(deptId);

    }

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

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

    @Override
    public List<AdminUserRespDTO> getAllUser() {
        List<AdminUserDO> allUsers = userService.getAllUsers();
        return BeanUtils.toBean(allUsers, AdminUserRespDTO.class);
    }

    @Override
    public List<AdminUserRespDTO> getUserListByUsername(String username) {
        List<AdminUserDO> users = userService.getUserListByUsername(username);
        return BeanUtils.toBean(users, AdminUserRespDTO.class);
    }

    @Override
    public List<AdminUserRespDTO> getUserListByUsernames(Set<String> usernames) {
        List<AdminUserDO> users = userService.getUsers(usernames);
        return BeanUtils.toBean(users, AdminUserRespDTO.class);
    }

    @Override
    public void validateUserList(Collection<Long> ids) {
        userService.validateUserList(ids);
    }

}
