package com.ruoyi.web.controller.system.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * packageName com.ruoyi.web.controller.system.system
 *
 * @author wangxy
 * @version JDK 8
 * @className SysUserApplyController
 * @date 2024/8/5
 * @description 人员申请
 */
@Controller
@RequestMapping("/system/userApply")
public class SysUserApplyController extends BaseController {


    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private ISysPostService postService;


    private String prefix = "system/user/userApply";


    @RequiresPermissions("system:userApply:view")
    @GetMapping()
    public String apply() {
        return prefix + "/userApply";
    }

    /**
     * 人员申请列表
     *
     * @param user
     * @return
     */
    @RequiresPermissions("system:userApply:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysUser user) {
        startPage();
        user.setExamine("2");
        List<SysUser> list = userService.selectUserList(user);
        return getDataTable(list);
    }


    /**
     * 人员审核
     */
    @RequiresPermissions("system:userApply:apply")
    @GetMapping("/apply/{userId}")
    public String apply(@PathVariable("userId") Long userId, ModelMap mmap) {
        List<SysRole> roles = roleService.selectRolesByUserId(userId);
        mmap.put("sysuser", getSysUser());
        mmap.put("user", userService.selectUserById(userId));
        mmap.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
        mmap.put("posts", postService.selectPostsByUserId(userId));
        return prefix + "/applyDetail";
    }


    @GetMapping("/submit/{userId}")
    @ResponseBody
    public AjaxResult submit(@PathVariable("userId") Long userId) {
        boolean update = userService.lambdaUpdate()
                .eq(SysUser::getUserId, userId)
                .set(SysUser::getExamine, "3").update();
        return toAjax(update);

    }


}
