package org.dromara.zhishu.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.constant.SystemConstants;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.StreamUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.bo.SysPostBo;
import org.dromara.system.domain.bo.SysRoleBo;
import org.dromara.system.domain.vo.SysRoleVo;
import org.dromara.system.domain.vo.SysUserInfoVo;
import org.dromara.system.domain.vo.SysUserVo;
import org.dromara.system.service.ISysPostService;
import org.dromara.system.service.ISysRoleService;
import org.dromara.system.service.ISysUserService;
import org.dromara.zhishu.domain.TInviteRelations;
import org.dromara.zhishu.domain.bo.TInviteRelationsBo;
import org.dromara.zhishu.domain.vo.ShopVo;
import org.dromara.zhishu.domain.vo.TInviteRelationsVo;
import org.dromara.zhishu.service.ITInviteRelationsService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;

import java.util.List;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zhishu/inviteRelations")
public class TInviteRelationsController {

    private final ITInviteRelationsService tInviteRelationsService;
    private final ISysUserService userService;
    private final ISysRoleService roleService;
    private final ISysPostService postService;

    @SaIgnore
    @GetMapping("/list")
    public R<Map<String, Object>> list() {
        try {
            List<TInviteRelations> data = tInviteRelationsService.selectByInviterId(LoginHelper.getUserId());
            if (data == null) {
                return R.fail(400, "no data");
            }
            Map<String, Object> row = new HashMap<>();
            row.put("rows", data);
            return R.ok(row);
        } catch (Exception e) {
            return R.fail(400, e.getMessage());
        }
    }


    @SaIgnore
    @GetMapping("/userShopList")
    public R<Map<String, Object>> userShopList() {
        try {
            List<ShopVo> data = tInviteRelationsService.selectUserShopList(LoginHelper.getUserId());
            if (data == null) {
                return R.fail(400, "no data");
            }
            Map<String, Object> row = new HashMap<>();
            row.put("rows", data);
            return R.ok(row);
        } catch (Exception e) {
            return R.fail(400, e.getMessage());
        }
    }

    /**
     * 查询用户信息
     * @param userId
     * @return
     */
    @SaCheckPermission("inviteRelations:user:query")
    @GetMapping("/{userId}")
    public R<SysUserInfoVo> getInfo(@PathVariable Long userId) {
        SysUserInfoVo userInfoVo = new SysUserInfoVo();

        // 获取用户基本信息
        SysUserVo sysUser = userService.selectUserById(userId);
        userInfoVo.setUser(sysUser);

        // 获取用户角色ID列表
        userInfoVo.setRoleIds(roleService.selectRoleListByUserId(userId));

        // 获取用户岗位信息（如果有部门）
        if (ObjectUtil.isNotNull(sysUser.getDeptId())) {
            SysPostBo postBo = new SysPostBo();
            postBo.setDeptId(sysUser.getDeptId());
            userInfoVo.setPosts(postService.selectPostList(postBo));
            userInfoVo.setPostIds(postService.selectPostListByUserId(userId));
        }

        // 获取所有角色列表（过滤掉超级管理员）
        SysRoleBo roleBo = new SysRoleBo();
        roleBo.setStatus(SystemConstants.NORMAL);
        List<SysRoleVo> roles = roleService.selectRoleList(roleBo);
        userInfoVo.setRoles(StreamUtils.filter(roles, r -> !r.isSuperAdmin()));

        return R.ok(userInfoVo);
    }
}
