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

import java.util.List;
import java.util.Objects;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysPostService;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TdLeave;
import com.ruoyi.system.service.ITdLeaveService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import oshi.jna.platform.mac.SystemB;

/**
 * 人员离职Controller
 * 
 * @author ruoyi
 * @date 2024-04-16
 */
@Controller
@RequestMapping("/system/leave")
public class TdLeaveController extends BaseController
{
    private String prefix = "system/leave";

    @Autowired
    private ITdLeaveService tdLeaveService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysPostService postService;


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

    /**
     * 查询人员离职列表
     */
    @RequiresPermissions("system:leave:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TdLeave tdLeave)
    {
        startPage();
        List<TdLeave> list = tdLeaveService.selectTdLeaveList(tdLeave);
        return getDataTable(list);
    }

    /**
     * 导出人员离职列表
     */
    @RequiresPermissions("system:leave:export")
    @Log(title = "人员离职", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TdLeave tdLeave)
    {
        List<TdLeave> list = tdLeaveService.selectTdLeaveList(tdLeave);
        ExcelUtil<TdLeave> util = new ExcelUtil<TdLeave>(TdLeave.class);
        return util.exportExcel(list, "人员离职数据");
    }

    /**
     * 新增人员离职
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("addleave",getSysUser());
        return prefix + "/add";
    }

    /**
     * 新增保存人员离职
     */
    @RequiresPermissions("system:leave:add")
    @Log(title = "人员离职", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TdLeave tdLeave)
    {
        return toAjax(tdLeaveService.insertTdLeave(tdLeave));
    }

    /**
     * 修改人员离职
     */
    @RequiresPermissions("system:leave:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TdLeave tdLeave = tdLeaveService.selectTdLeaveById(id);
        mmap.put("tdLeave", tdLeave);
        return prefix + "/edit";
    }

    /**
     * 修改保存人员离职
     */
    @RequiresPermissions("system:leave:edit")
    @Log(title = "人员离职", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TdLeave tdLeave)
    {
        return toAjax(tdLeaveService.updateTdLeave(tdLeave));
    }

    /**
     * 删除人员离职
     */
    @RequiresPermissions("system:leave:remove")
    @Log(title = "人员离职", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tdLeaveService.deleteTdLeaveByIds(ids));
    }

    /**
     * 审核保存
     */
    @RequiresPermissions("system:leave:examine")
    @Log(title = "人员离职", businessType = BusinessType.EXAMINE)
    @PostMapping("/examine")
    @ResponseBody
    public AjaxResult examinesave(TdLeave tdLeave)
    {
        if (Objects.equals(tdLeave.getLeavestate(),"1")){
            SysUser sysUser = userService.selectUserById(tdLeave.getUserId());
            sysUser.setStatus("1");
            userService.updateUser(sysUser);
        }
        return toAjax(tdLeaveService.updateTdLeave(tdLeave));
    }

    /**
     * 人员离职审核
     */
    @RequiresPermissions("system:leave:examine")
    @Log(title = "人员离职", businessType = BusinessType.EXAMINE)
    @GetMapping( "/examine/{id}")
    public String examine(@PathVariable("id") Long id,ModelMap mmap){
        TdLeave leaveuser = tdLeaveService.selectTdLeaveById(id);
        mmap.put("leaveuser", leaveuser);
        mmap.put("sysuser",getSysUser());
        return prefix + "/leaveexamine";
    }

    /**
     * 离职打印
     */
    @RequiresPermissions("system:leave:print")
    @Log(title = "人员离职", businessType = BusinessType.PRINT)
    @GetMapping("/print/{Id}/{userId}")
    public String paint(@PathVariable("Id") Long Id,@PathVariable("userId") Long userId, ModelMap mmap)
    {
        TdLeave leaveuser = tdLeaveService.selectTdLeaveById(Id);
        mmap.put("leaveuser", leaveuser);
        mmap.put("user",userService.selectUserById(userId));
        mmap.put("posts", postService.selectPostsByUserIds(userId));
        return prefix + "/leaveprint";
    }
}
