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

import com.ruoyi.common.annotation.Log;
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.common.enums.BusinessType;
import com.ruoyi.framework.shiro.util.AuthorizationUtils;
import com.ruoyi.system.domain.SysPost;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 涉密人员审核
 */
@Controller
@RequestMapping("/system/userexamine")
public class SysUserExamineController extends BaseController {
    private String prefix = "system/user/userexamine";
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private ISysPostService postService;
    @RequiresPermissions("system:userexamine:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/userexamine";
    }

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

    /**
     * 人员审核
     */
    @RequiresPermissions("system:userexamine:examine")
    @GetMapping("/examine/{userId}")
    public String userExamine(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        userService.checkUserDataScope(userId);
        SysUser sysuser = getSysUser();
        List<SysRole> roles = roleService.selectRolesByUserId(userId);
        mmap.put("sysuser",sysuser);
        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 + "/examinedetail";
    }

    /**
     * 审核保存
     * @param userId
     * @return
     */
    @RequiresPermissions("system:userexamine:examine")
    @Log(title = "用户审核", businessType = BusinessType.EXAMINE)
    @GetMapping("/saveexamine/{userId}/{examine}/{examinedate}/{examineuser}")
    @ResponseBody
    public AjaxResult editSave(@PathVariable("userId") Long userId,@PathVariable("examine")
    String examine,@PathVariable("examinedate") String examinedate,@PathVariable("examineuser") String examineuser)
    {
        SysUser sysUser = userService.selectUserById(userId);
        sysUser.setExamine(examine);
        sysUser.setExaminedate(examinedate);
        sysUser.setExamineuser(examineuser);
        return toAjax(userService.updateUser(sysUser));
    }

    /**
     * 打印界面
     */
    @RequiresPermissions("system:userexamine:examineprint")
    @Log(title = "用户审核", businessType = BusinessType.PRINT)
    @GetMapping("/examineprint/{userId}")
    public String paint(@PathVariable("userId") Long userId,ModelMap mmap)
    {
        mmap.put("user",userService.selectUserById(userId));
        mmap.put("roles", roleService.selectRolesByUserIds(userId));
        mmap.put("posts", postService.selectPostsByUserIds(userId));
        return prefix + "/examineprint";
    }




}
