package com.cico.nest.lcdp.boot.core.system.api.user;

import cn.hutool.core.collection.CollUtil;
import com.cico.nest.lcdp.boot.core.system.dal.dataobject.user.AdminUserDO;
import com.cico.nest.lcdp.boot.core.system.dal.mysql.user.AdminUserMapper;
import com.cico.nest.lcdp.boot.core.system.service.user.AdminUserService;
import com.cico.nest.lcdp.boot.core.system.service.user.dto.AdminUserRoleDeptDTO;
import com.cico.nest.lcdp.framework.api.user.LcdpUserApi;
import com.cico.nest.lcdp.framework.api.user.dto.LcdpUserDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;


/**
 * @author zws
 * @date: 2024/6/19
 */

@Service
@RequiredArgsConstructor
public class LcdpUserApiImpl implements LcdpUserApi {

    private final AdminUserService adminUserService;

    private final AdminUserMapper adminUserMapper;;

    /**
     * 获取登录用户信息
     * @param needRoleAndDept 是否需要角色和部门信息
     * @return 用户信息
     */
    @Override
    public LcdpUserDTO getUserById(String userId, boolean needRoleAndDept) {
        if(needRoleAndDept){
            List<AdminUserRoleDeptDTO> adminUserRoleDeptDTOs = adminUserMapper.selectJoinRoleAndDept(Long.valueOf(userId));
            if(CollUtil.isNotEmpty(adminUserRoleDeptDTOs)){
                return AdminUserRoleDeptDTO.toUserDTO(adminUserRoleDeptDTOs.get(0));
            }else{
                return null;
            }
        }
        AdminUserDO adminUserDO = adminUserMapper.selectById(userId);
        return AdminUserDO.toLoginUserDTO(adminUserDO);
    }

    /**
     * 查询所有用户
     * @param needRoleAndDept 是否需要角色和部门信息
     * @return 用户列表
     */
    @Override
    public List<LcdpUserDTO> listUser(boolean needRoleAndDept) {
        if(needRoleAndDept){
            List<AdminUserRoleDeptDTO> adminUserRoleDeptDTOs = adminUserMapper.selectJoinRoleAndDept(null);
            return AdminUserRoleDeptDTO.toUserDTO(adminUserRoleDeptDTOs);
        }
        List<AdminUserDO> adminUserDOS = adminUserService.listUser();
        return AdminUserDO.toLoginUserDTO(adminUserDOS);
    }

    /**
     * 根据登录名查询用户信息
     * @param userName 用户账号
     * @return 用户信息
     */
    @Override
    public LcdpUserDTO getUserByUsername(String userName) {
        AdminUserDO adminUser = adminUserService.getUserByUsername(userName);
        return AdminUserDO.toLoginUserDTO(adminUser);
    }

    /**
     * 根据用户真实姓名查询用户信息
     */
    @Override
    public List<LcdpUserDTO> listUserByNicknames(Collection<String> nickNames) {
        List<AdminUserDO> adminUserDOS = adminUserService.listUserByUserNickNames(nickNames);
        return AdminUserDO.toLoginUserDTO(adminUserDOS);
    }

    /**
     * 根据用户名查询用户信息
     */
    @Override
    public List<LcdpUserDTO> listUserByUserNames(Collection<String> userNames) {
        List<AdminUserDO> adminUserDOS = adminUserService.listUser(userNames);
        return AdminUserDO.toLoginUserDTO(adminUserDOS);
    }

    @Override
    public List<LcdpUserDTO> listUserByUserIds(Collection<Long> collection) {
        return null;
    }

    /**
     * 根据用户真实姓名模糊查询用户名
     */
    @Override
    public List<String> listUserNamLikeNickname(String nickname) {
        return adminUserService.listUserNamLikeRealName(nickname);
    }

    /**
     * 根据角色id获取用户信息
     * @param roleIds 角色id
     * @return 用户信息列表
     */
    @Override
    public List<LcdpUserDTO> getUserByRoleIds(List<String> roleIds, boolean needRoleAndDept) {
        List<Long> roleLongList = roleIds.stream().map(Long::valueOf).toList();
        if(needRoleAndDept){
            List<AdminUserRoleDeptDTO> adminUserRoleDeptDTOList = adminUserMapper.selectJoinRoleAndDeptByRoleId(null,roleLongList);
            return AdminUserRoleDeptDTO.toUserDTO(adminUserRoleDeptDTOList);
        }
        List<AdminUserRoleDeptDTO> adminUserRoleDeptDTOList = adminUserMapper.selectJoinDeptByRoleId(null,roleLongList);
        return AdminUserRoleDeptDTO.toUserDTO(adminUserRoleDeptDTOList);
    }
}
