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

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.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.TdFileReceive;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ITdFileReceiveService;
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.List;
import java.util.Objects;

/**
 * 涉密文件提取Controller
 */
@Controller
@RequestMapping("/system/fileextract")
public class TdFileExtractController extends BaseController {
    private String prefix = "system/fileextract";
    @Autowired
    private ITdFileReceiveService tdFileReceiveService;
    @Autowired
    private ISysDeptService deptService;

    @RequiresPermissions("system:fileextract:view")
    @GetMapping()
    public String fileextract(ModelMap mmap)
    {
        mmap.put("sysuser",getSysUser());
        return prefix + "/fileextract";
    }
    /**
     * 查询涉密文件接收列表
     */
    @RequiresPermissions("system:fileextract: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 (Objects.equals(FileReceivelist.getReceiveState(),"1") && FileReceivelist.getReceiveDepartid().equals(getSysUser().getDept().getDeptName())){
                tdFileReceives.add(FileReceivelist);
            }
        }
        return getDataTable(tdFileReceives);
    }
    /**
     * 导出涉密文件接收列表
     */
    @RequiresPermissions("system:fileextract:export")
    @Log(title = "涉密文件接收", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TdFileReceive tdFileReceive)
    {
        List<TdFileReceive> list = tdFileReceiveService.selectTdFileReceiveList(tdFileReceive);
        ExcelUtil<TdFileReceive> util = new ExcelUtil<TdFileReceive>(TdFileReceive.class);
        return util.exportExcel(list, "涉密文件接收数据");
    }
    /**
     * 涉密文件提取
     */
    @RequiresPermissions("system:fileextract: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:fileextract:edit")
    @Log(title = "涉密文件接收", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TdFileReceive tdFileReceive)
    {
        return toAjax(tdFileReceiveService.updateTdFileReceive(tdFileReceive));
    }

}
