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

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

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.sql.SqlUtil;
import com.ruoyi.common.utils.uuid.Seq;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.domain.TdFileReceive;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysPostService;
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.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.TdFileProvide;
import com.ruoyi.system.service.ITdFileProvideService;
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-04-23
 */
@Controller
@RequestMapping("/system/fileprovide")
public class TdFileProvideController extends BaseController
{
    private String prefix = "system/fileprovide";

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

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

    /**
     * 查询文件下发列表
     */
    @RequiresPermissions("system:fileprovide:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TdFileProvide tdFileProvide)
    {
        startPage();
        List<TdFileProvide> list = tdFileProvideService.selectTdFileProvideList(tdFileProvide);
        return getDataTable(list);
    }

    /**
     * 导出文件下发列表
     */
    @RequiresPermissions("system:fileprovide:export")
    @Log(title = "文件下发", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TdFileProvide tdFileProvide)
    {
        List<TdFileProvide> list = tdFileProvideService.selectTdFileProvideList(tdFileProvide);
        ExcelUtil<TdFileProvide> util = new ExcelUtil<TdFileProvide>(TdFileProvide.class);
        return util.exportExcel(list, "文件下发数据");
    }

    /**
     * 新增文件下发
     */
    @GetMapping("/add")
    public String add(SysDept dept, ModelMap mmap)
    {
        mmap.put("dept",deptService.selectDeptList(dept));
        mmap.put("user",getSysUser());
        return prefix + "/add";
    }

    /**
     * 新增保存文件下发
     */
    @RequiresPermissions("system:fileprovide:add")
    @Log(title = "文件下发", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TdFileProvide tdFileProvide)
    {
        tdFileProvide.setFileId("File_"+Seq.getId(Seq.commSeqType));
        String[] tar = tdFileProvide.getTargetDepart().split(",");
        for (String depart : tar) {
            TdFileReceive tdFileReceive = new TdFileReceive();
            tdFileReceive.setFileId(tdFileProvide.getFileId());
            tdFileReceive.setFileName(tdFileProvide.getFileName());
            tdFileReceive.setProvideDepart(tdFileProvide.getProvideDepart());
            tdFileReceive.setProvideDate(tdFileProvide.getProvideDate());
            tdFileReceive.setReceiveDepartid(depart);
            tdFileReceiveService.insertTdFileReceive(tdFileReceive);
        }

        return toAjax(tdFileProvideService.insertTdFileProvide(tdFileProvide));
    }

    /**
     * 修改文件下发
     */
    @RequiresPermissions("system:fileprovide:edit")
    @GetMapping("/edit/{fileId}")
    public String edit(@PathVariable("fileId") String fileId,SysDept dept, ModelMap mmap)
    {
        TdFileProvide tdFileProvide = tdFileProvideService.selectTdFileProvideByFileId(fileId);
        mmap.put("user",getSysUser());
        mmap.put("dept",deptService.selectDeptList(dept));
        mmap.put("tdFileProvide", tdFileProvide);
        return prefix + "/edit";
    }

    /**
     * 修改保存文件下发
     */
    @RequiresPermissions("system:fileprovide:edit")
    @Log(title = "文件下发", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TdFileProvide tdFileProvide)
    {
        return toAjax(tdFileProvideService.updateTdFileProvide(tdFileProvide));
    }

    /**
     * 删除文件下发
     */
    @RequiresPermissions("system:fileprovide:remove")
    @Log(title = "文件下发", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        List<TdFileReceive> tdFileReceives = tdFileReceiveService.selectTdFileReceiveByFileId(ids);
        List<String> statesList = new ArrayList<>();
        for (TdFileReceive tdFileReceive : tdFileReceives) {
            statesList.add(tdFileReceive.getReceiveState());
        }
        if (statesList.contains("1")){
            return AjaxResult.error("下发的已被接收文件不能删除");
        }else {
            for (TdFileReceive tdFileReceive : tdFileReceives){
                tdFileReceiveService.deleteTdFileReceiveByReceiveId(tdFileReceive.getReceiveId());
            }
            return toAjax(tdFileProvideService.deleteTdFileProvideByFileIds(ids));
        }
    }

    /**
     * 文件下发详情
     */
    @RequiresPermissions("system:fileprovide:detail")
    @GetMapping("/detail/{fileId}")
    public String detail(@PathVariable("fileId") String fileId,SysDept dept ,ModelMap mmap)
    {
        TdFileProvide tdFileProvide = tdFileProvideService.selectTdFileProvideByFileId(fileId);
        mmap.put("dept",deptService.selectDeptList(dept));
        mmap.put("tdFileProvide", tdFileProvide);
        return prefix + "/detail";
    }
}
