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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import cn.iocoder.yudao.module.system.api.user.dto.UserDTO;
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 com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

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;

    /**
     * 创建用户
     *
     * @param userDTO 用户DTO
     * @return java.lang.Long
     * @author vinjor-M
     * @date 15:27 2024/8/3
     **/
    @Override
    public Long createUser(UserDTO userDTO) {
        UserSaveReqVO userSaveReqVO = BeanUtils.toBean(userDTO, UserSaveReqVO.class);
        return userService.createUser(userSaveReqVO);
    }

    /**
     * 删除用户
     *
     * @param id 用户编号
     */
    @Override
    public void deleteUser(Long id) {
        userService.deleteUser(id);
    }

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

    /**
     * 根据openid和租户id查询用户
     *
     * @param openId   微信id
     * @param tenantId 租户id，可能为null
     * @return cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO
     * @author vinjor-M
     * @date 11:29 2024/9/24
     **/
    @Override
    public AdminUserRespDTO getUserByOpenId(String openId, String tenantId) {
        AdminUserDO user = userService.selectUserByOpenId(openId,tenantId);
        return BeanUtils.toBean(user, AdminUserRespDTO.class);
    }

    /**
     * 通过用户 登录账户 查询用户---自动拼租户
     *
     * @param username 用户登录账户
     * @return 用户对象信息
     */
    @Override
    public AdminUserRespDTO getUserByUsername(String username) {
        AdminUserDO user = userService.getUserByUsername(username);
        return BeanUtils.toBean(user, AdminUserRespDTO.class);
    }
    /**
     * 通过用户 登录账户 查询用户---不带租户
     *
     * @param username 用户登录账户
     * @return 用户对象信息
     */
    @Override
    @TenantIgnore
    public AdminUserRespDTO getUserByUsernameNoTenantId(String username) {
        AdminUserDO user = userService.getUserByUsernameNoTenantId(username);
        return BeanUtils.toBean(user, AdminUserRespDTO.class);
    }

    @Override
    public List<AdminUserRespDTO> getUserListBySubordinate(Long id) {
        // 1.1 获取用户负责的部门
        AdminUserDO user = userService.getUser(id);
        if (user == null) {
            return Collections.emptyList();
        }
        ArrayList<Long> deptIds = new ArrayList<>();
        DeptDO dept = deptService.getDept(user.getDeptId());
        if (dept == null) {
            return Collections.emptyList();
        }
        if (ObjUtil.notEqual(dept.getLeaderUserId(), id)) { // 校验为负责人
            return Collections.emptyList();
        }
        deptIds.add(dept.getId());
        // 1.2 获取所有子部门
        List<DeptDO> childDeptList = deptService.getChildDeptList(dept.getId());
        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> getUserList(Collection<Long> ids) {
        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> getUserListByPostIds(Collection<Long> postIds) {
        List<AdminUserDO> users = userService.getUserListByPostIds(postIds);
        return BeanUtils.toBean(users, AdminUserRespDTO.class);
    }

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

    /**
     * 重置密码
     * @author 小李
     * @date 16:39 2024/8/9
     * @param id 用户ID
     * @param password 新密码
     **/
    @Override
    public void resetPassword(Long id, String password){
        userService.updateUserPassword(id, password);
    }

    @Override
    public List<AdminUserRespDTO> getUsersByRoleRescue(String roleCode, String searchValue, List<String> deptIds) {
        List<AdminUserRespDTO> res =new ArrayList<>();
        List<AdminUserDO> users = userService.getUsersByRoleRescue(roleCode, searchValue, deptIds);
        BeanUtil.copyProperties(users,res);
        return res;
    }

    @Override
    public AdminUserRespDTO getUserByMobile(String mobile) {
        AdminUserDO user = userService.getUserByMobile(mobile);
        return BeanUtils.toBean(user, AdminUserRespDTO.class);
    }

    /**
     * 设置用户openId
     *
     * @param userId 用户id
     * @param openId 微信id
     * @author vinjor-M
     * @date 15:45 2024/9/24
     **/
    @Override
    public void setOpenId(Long userId, String openId) {
        userService.setOpenId(userId, openId);
    }

}
