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

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

import com.ruoyi.system.domain.TdFileProvide;
import com.ruoyi.system.domain.TdFileReceive;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ITdFileProvideService;
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 com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
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-15
 */
@Controller
@RequestMapping("/system/filedestory")
public class TdFileDestoryController extends BaseController
{
    private String prefix = "system/filedestory";

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

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

    /**
     * 查询涉密文件销毁列表
     */
    @RequiresPermissions("system:filedestory: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.getExtractState(),"1") && FileReceivelist.getReceiveDepartid().equals(deptService.selectDeptById(getSysUser().getDeptId()).getDeptName())){
                tdFileReceives.add(FileReceivelist);
            }
        }

        return getDataTable(tdFileReceives);
    }

    /**
     * 导出涉密文件销毁列表
     */
    @RequiresPermissions("system:filedestory: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:filedestory:edit")
    @GetMapping("/edit/{receiveId}")
    public String edit(@PathVariable("receiveId") Long receiveId, ModelMap mmap)
    {
        TdFileReceive tdFileReceive = tdFileReceiveService.selectTdFileReceiveByReceiveId(receiveId);
        String fileId = tdFileReceive.getFileId();
        TdFileProvide tdFileProvide = tdFileProvideService.selectTdFileProvideByFileId(fileId);
        mmap.put("tdFileProvide", tdFileProvide);
        mmap.put("tdFileReceive", tdFileReceive);
        mmap.put("user",getSysUser());
        return prefix + "/edit";
    }

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

    /**
     * 涉密文件销毁详情
     */
    @RequiresPermissions("system:filedestory:detail")
    @GetMapping("/detail/{receiveId}")
    public String detail(@PathVariable("receiveId") Long receiveId, ModelMap mmap)
    {
        TdFileReceive tdFileReceive = tdFileReceiveService.selectTdFileReceiveByReceiveId(receiveId);
        String fileId = tdFileReceive.getFileId();
        TdFileProvide tdFileProvide = tdFileProvideService.selectTdFileProvideByFileId(fileId);
        mmap.put("tdFileProvide", tdFileProvide);
        mmap.put("tdFileReceive", tdFileReceive);
        return prefix + "/detail";
    }

}
