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

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.system.service.ISysDeptService;
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.TdFileReceive;
import com.ruoyi.system.service.ITdFileReceiveService;
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;

/**
 * 涉密文件接收Controller
 * 
 * @author ruoyi
 * @date 2024-05-14
 */
@Controller
@RequestMapping("/system/filereceive")
public class TdFileReceiveController extends BaseController
{
    private String prefix = "system/filereceive";

    @Autowired
    private ITdFileReceiveService tdFileReceiveService;
    @Autowired
    private ISysDeptService deptService;

    @RequiresPermissions("system:filereceive:view")
    @GetMapping()
    public String filereceive(ModelMap mmap)
    {
        mmap.put("sysuser",getSysUser());
        return prefix + "/filereceive";
    }

    /**
     * 查询涉密文件接收列表
     */
    @RequiresPermissions("system:filereceive:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TdFileReceive tdFileReceive)
    {
        startPage();
        List<TdFileReceive> list = tdFileReceiveService.selectTdFileReceiveList(tdFileReceive);
        List<TdFileReceive> tdFileReceives = new ArrayList<>();
        for (TdFileReceive FileReceivelist : list){
            if (FileReceivelist.getReceiveDepartid().equals(getSysUser().getDept().getDeptName())){
                tdFileReceives.add(FileReceivelist);
            }
        }
        return getDataTable(tdFileReceives);
    }

    /**
     * 导出涉密文件接收列表
     */
    @RequiresPermissions("system:filereceive:export")
    @Log(title = "涉密文件接收", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export()
    {
        List<TdFileReceive> list = tdFileReceiveService.selectTdFileReceiveByReceiveDepart(deptService.selectDeptById(getSysUser().getDeptId()).getDeptName());
        ExcelUtil<TdFileReceive> util = new ExcelUtil<TdFileReceive>(TdFileReceive.class);
        return util.exportExcel(list, "涉密文件接收数据");
    }

    /**
     * 新增涉密文件接收
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存涉密文件接收
     */
    @RequiresPermissions("system:filereceive:add")
    @Log(title = "涉密文件接收", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TdFileReceive tdFileReceive)
    {
        return toAjax(tdFileReceiveService.insertTdFileReceive(tdFileReceive));
    }

    /**
     * 涉密文件接收
     */
    @RequiresPermissions("system:filereceive:edit")
    @GetMapping("/edit/{receiveId}")
    public String edit(@PathVariable("receiveId") Long receiveId, ModelMap mmap)
    {
        TdFileReceive tdFileReceive = tdFileReceiveService.selectTdFileReceiveByReceiveId(receiveId);
        mmap.put("tdFileReceive", tdFileReceive);
        mmap.put("user",getSysUser());
        return prefix + "/edit";
    }

    /**
     * 保存涉密文件接收
     */
    @RequiresPermissions("system:filereceive:edit")
    @Log(title = "涉密文件接收", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TdFileReceive tdFileReceive)
    {
        return toAjax(tdFileReceiveService.updateTdFileReceive(tdFileReceive));
    }

    /**
     * 删除涉密文件接收
     */
    @RequiresPermissions("system:filereceive:remove")
    @Log(title = "涉密文件接收", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tdFileReceiveService.deleteTdFileReceiveByReceiveIds(ids));
    }


}
