package com.ruoyi.project.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.core.domain.saas.entity.SysUser;
import com.ruoyi.project.utils.DocumentUtil;
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.project.domain.TrsFileInfo;
import com.ruoyi.project.service.ITrsFileInfoService;
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 org.springframework.web.multipart.MultipartFile;

/**
 * 文件校对Controller
 * 
 * @author ruoyi
 * @date 2025-03-27
 */
@Controller
@RequestMapping("/project/filejiaodui")
public class TrsFileInfoController extends BaseController
{
    private String prefix = "project/filejiaodui";

    @Autowired
    private ITrsFileInfoService trsFileInfoService;

    @Autowired
    private DocumentUtil documentUtil;

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

    /**
     * 查询文件校对列表
     */
    @RequiresPermissions("project:filejiaodui:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TrsFileInfo trsFileInfo)
    {
        startPage();
        List<TrsFileInfo> list = trsFileInfoService.selectTrsFileInfoList(trsFileInfo);
        return getDataTable(list);
    }

    /**
     * 导出文件校对列表
     */
    @RequiresPermissions("project:filejiaodui:export")
    @Log(title = "文件校对", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TrsFileInfo trsFileInfo)
    {
        List<TrsFileInfo> list = trsFileInfoService.selectTrsFileInfoList(trsFileInfo);
        ExcelUtil<TrsFileInfo> util = new ExcelUtil<TrsFileInfo>(TrsFileInfo.class);
        return util.exportExcel(list, "文件校对数据");
    }

    /**
     * 新增文件校对
     */
/*    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }*/

    @Log(title = "文件导入", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:filejiaodui:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult add(MultipartFile file, boolean updateSupport) throws Exception
    {
        try (InputStream inputStream = file.getInputStream()) {
            String originalFilename = file.getOriginalFilename();
            byte[] bytes = new byte[10];
            if (inputStream.markSupported()) {
                inputStream.mark(bytes.length);  // 标记当前位置
            }
            inputStream.read(bytes, 0, bytes.length);
            if (inputStream.markSupported()) {
                inputStream.reset();  // 重置流位置到标记点
            }

            String fileType = documentUtil.getFileTypeByMagicNumber(bytes);
            if ("zip or office (docx, xlsx, pptx)".equals(fileType)) {
                fileType = documentUtil.checkZipOrOfficeFile(file);
            } else if ("office (doc, xls, ppt)".equals(fileType)) {
                fileType = documentUtil.checkLegacyOfficeFile(file);
            }

            // 提取文件内容
            String content;
            try (InputStream contentInputStream = file.getInputStream()) {
                content = documentUtil.extractContent(fileType, contentInputStream);
            }
            TrsFileInfo trsFileInfo = new TrsFileInfo();
            trsFileInfo.setDoccontent(content);
            trsFileInfo.setDocname(originalFilename);
            trsFileInfo.setCreatetime(new Date());
            trsFileInfo.setDoctype(fileType);
            return toAjax(trsFileInfoService.insertTrsFileInfo(trsFileInfo));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return AjaxResult.success();
    }

    /**
     * 新增保存文件校对
     */
/*    @RequiresPermissions("project:filejiaodui:add")
    @Log(title = "文件校对", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TrsFileInfo trsFileInfo)
    {
        return toAjax(trsFileInfoService.insertTrsFileInfo(trsFileInfo));
    }*/

    /**
     * 修改文件校对
     */
    @RequiresPermissions("project:filejiaodui:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TrsFileInfo trsFileInfo = trsFileInfoService.selectTrsFileInfoById(id);
        mmap.put("trsFileInfo", trsFileInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存文件校对
     */
    @RequiresPermissions("project:filejiaodui:edit")
    @Log(title = "文件校对", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TrsFileInfo trsFileInfo)
    {
        return toAjax(trsFileInfoService.updateTrsFileInfo(trsFileInfo));
    }

    /**
     * 删除文件校对
     */
    @RequiresPermissions("project:filejiaodui:remove")
    @Log(title = "文件校对", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(trsFileInfoService.deleteTrsFileInfoByIds(ids));
    }

    /**
     * 查看文件校对
     */
    @RequiresPermissions("project:filejiaodui:edit")
    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") Long id, ModelMap mmap)
    {
        TrsFileInfo trsFileInfo = trsFileInfoService.selectTrsFileInfoById(id);
        mmap.put("trsFileInfo", trsFileInfo);
        return prefix + "/view";
    }
}
