package com.chushang.system.remote;

import cn.hutool.core.collection.CollectionUtil;
import com.chushang.common.core.util.StringUtils;
import com.chushang.common.core.web.AjaxResult;
import com.chushang.common.core.web.Result;
import com.chushang.security.annotation.InnerAuth;
import com.chushang.security.entity.po.DataScopeEntity;
import com.chushang.security.entity.po.SysUser;
import com.chushang.security.entity.vo.LoginUser;
import com.chushang.system.entity.dto.ListUserDTO;
import com.chushang.system.feign.RemoteUserService;
import com.chushang.system.service.ISysPermissionService;
import com.chushang.system.service.ISysPostService;
import com.chushang.system.service.ISysRoleService;
import com.chushang.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/user/remote")
public class RemoteUserController implements RemoteUserService {

    @Autowired
    ISysUserService sysUserService;
    @Autowired
    ISysRoleService sysRoleService;
    @Autowired
    ISysPostService sysPostService;
    @Autowired
    ISysPermissionService permissionService;

    @InnerAuth
    @GetMapping("/info/{username}")
    public Result<LoginUser> getUserInfo(@PathVariable("username") String username, String source)
    {
        SysUser sysUser = sysUserService.selectUserByUserName(username);
        if (StringUtils.isNull(sysUser)) {
            return Result.failed("用户名或密码错误");
        }

        // 角色集合
        Set<String> roles = permissionService.getRolePermission(sysUser);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(sysUser);
        // 数据权限集合
        List<DataScopeEntity> dataScopes = permissionService.getDataScopes(sysUser);

        LoginUser sysUserVo = new LoginUser();
        sysUserVo.setUserId(sysUser.getUserId());
        sysUserVo.setUsername(sysUser.getUsername());
        sysUserVo.setSysUser(sysUser);
        sysUserVo.setRoles(roles);
        sysUserVo.setScopes(dataScopes);
        sysUserVo.setPermissions(permissions);
        // 默认传递一个空Map --> 拥有管理员
        sysUserVo.setAuthDataMap(new HashMap<>());
        return Result.ok(sysUserVo);
    }

    @Override
    @GetMapping("/getInfoById/{userId}")
    @InnerAuth
    public Result<SysUser> getInfoById(@PathVariable(value = "userId") Long userId, String source) {
        return Result.ok(sysUserService.selectByUserId(userId));
    }

    /**
     * 根据账户获取对应的userId
     */
    @Override
    @InnerAuth
    @GetMapping("/username")
    public Result<Map<String, Long>> getIdByUsernames(Set<String> userNames, String source) {
        return  Result.ok(sysUserService.getIdByUsernames(userNames));
    }

    @Override
    @InnerAuth
    @GetMapping("/nickname")
    public Result<Map<String, Long>> getIdByNicknames(Set<String> nickNames, String source) {
        return  Result.ok(sysUserService.getIdByNicknames(nickNames));
    }

    @Override
    @GetMapping(value = "/querynamebyid")
    @InnerAuth
    public Map<Long, SysUser> getUserNameByIds(Set<Long> userIds, String source) {
        if (CollectionUtil.isEmpty(userIds)) return Map.of();
        List<SysUser> sysUsers = sysUserService.listByIds(userIds);
        if (CollectionUtil.isNotEmpty(sysUsers)){
            return sysUsers.stream().collect(Collectors.toMap(SysUser::getUserId, Function.identity() ));
        }
        return Map.of();
    }

    @PostMapping(value = "/user/list")
    public Result<List<SysUser>> getSysUsers(ListUserDTO listUser) {
        List<SysUser> list = sysUserService.getSysUsers(listUser);
        return list == null ? Result.failed("未查询到数据") : Result.ok(list);
    }


    @GetMapping(value = "/empData")
    @InnerAuth
    public AjaxResult empData(ListUserDTO listUser) {
        return sysUserService.listUser(listUser);
    }

}
