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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.SysDept;
import com.ruoyi.common.core.domain.entity.SysPostVO;
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.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.domain.SysUserPost;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.service.*;
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.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 涉密人员统计
 */
@Controller
@RequestMapping("/system/usernum")
public class SysUsernumController extends BaseController {
    private String prefix = "system/usernum";

    @Autowired
    private ISysUserService userService;

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

    @Autowired
    private SysUserPostService sysUserPostService;

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

    /**
     * 用户列表
     * @param user
     * @return
     */
    @RequiresPermissions("system:usernum:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysUser user,SysDept dept)
    {
        startPage();
        List<SysUser> sysuser = new ArrayList<>();
        List<SysUser> list = userService.selectUserGroupBydept(user);
        List<SysDept> sysDepts = deptService.selectDeptList(dept);
        for (SysDept Dept : sysDepts){
            for ( SysUser user1 : list){
                if (Dept.getDeptName().equals(user1.getDept().getDeptName())){
                    sysuser.add(user1);
                }
            }
        }
        return getDataTable(sysuser);
    }

    /**
     * 导出用户
     * @param user
     * @return
     */
    @Log(title = "用户统计", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:usernum:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysUser user)
    {
        List<SysUser> list = userService.selectUserList(user);
        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
        return util.exportExcel(list, "用户统计数据");
    }

    /**
     * 打印
     */
    @RequiresPermissions("system:usernum:detail")
    @GetMapping("/print/{deptId}")
    public String print(@PathVariable("deptId")Long deptId, SysUser user,ModelMap mmap)

    {
        List<SysUser> list = userService.lambdaQuery().eq(SysUser::getDeptId, deptId)
                .eq(SysUser::getDelFlag,0)
                .ne(SysUser::getUserId,1)
                .list();
        list.forEach(sysuser -> {
            List<SysUserPost> postList = sysUserPostService.lambdaQuery().
                    eq(SysUserPost::getUserId, sysuser.getUserId()).list();
            List<Long> postIds = postList.stream().map(SysUserPost::getPostId).collect(Collectors.toList());
            if(CollUtil.isNotEmpty(postIds)){
                List<SysPost> sysPostList = postService.lambdaQuery().in(SysPost::getPostId, postIds).list();
                sysuser.setPosts( Convert.toList(SysPostVO.class, sysPostList));
            }
        });
        mmap.put("list", list);
        mmap.put("dept",deptService.selectDeptById(deptId));
        mmap.put("sysuser",getSysUser());
        return prefix + "/print";
    }
}
