package com.ruoyi.web.controller.system;


import com.ruoyi.common.core.controller.BaseController;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;

import com.ruoyi.common.utils.StringUtils;

import com.ruoyi.system.domain.ApproveUser;
import com.ruoyi.system.service.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;

import org.springframework.web.bind.annotation.*;


import java.util.*;

/**
 * 审批用户信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/approve")
public class ApproveUserController extends BaseController
{
    @Autowired
    private IApproveUserService approveUserService;

    @Autowired
    private ISysUserService sysUserService;


    /**
     * 获取审批用户列表
     */
    /*@PreAuthorize(`@ss.hasRole('admin')`)*/
    @GetMapping("/list")
    public TableDataInfo list(ApproveUser user)
    {
        startPage();
        List<ApproveUser> list = approveUserService.selectApproveUserList(user);
        return getDataTable(list);
    }
    @GetMapping("/findApproveUser")
    public TableDataInfo findApproveUser()
    {
        startPage();
        List<Long> list = approveUserService.findApproveUser();
        return getDataTable(list);
    }


    /**
     * 保存指定审批人员
     * @param approveUser
     * @return
     */
    //@PreAuthorize("@ss.hasRole('admin')")
    @RequestMapping("saveApproveUser")
    @ResponseBody
    public Map<String,Object> saveApproveUser(@RequestBody ApproveUser approveUser){
        Map<String, Object> map = new HashMap<String, Object>();
        try{
            if(approveUser.getUserIds()!=null && approveUser.getUserIds().size()>0 ) {
                //删除
                approveUserService.deleteApproveUser();
                List<Long> aUser=approveUser.getUserIds();
                List<ApproveUser> aus = new ArrayList<>();
                for (Long s : aUser) {
                    SysUser sysUser=sysUserService.selectUserById(s);
                    ApproveUser au = new ApproveUser();
                    au.setUserId(s);
                    au.setLoginId(sysUser.getUserName());
                    au.setDeptName(sysUser.getDept().getDeptName());
                    au.setDeptId(sysUser.getDeptId());
                    aus.add(au);
                }
                if(aus.size()>0){
                    approveUserService.insertApproveUsers(aus);
                }
                map.put("status", true);
                map.put("msg", "保存成功！");
            }else{
                map.put("status", false);
                map.put("msg", "保存失败！");
            }
        }catch(Exception e){
            e.printStackTrace();
            map.put("status", false);
            map.put("msg", "保存异常！");
        }

        return map;
    }

    /**
     * 无权限控制
     * 无分页
     * 财务和法务全部人员
     * 获取用户列表
     */
    @GetMapping("/allUserList")
    public TableDataInfo allUserList(SysUser user)
    {
        List<SysUser> list = sysUserService.selectUserByDeptNameAndDeptName("财务部","法务部");
        return getDataTable(list);
    }

}
