package com.ruoyi.project.file.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
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.file.FileUploadUtils;
import com.ruoyi.project.file.domain.TFile;
import com.ruoyi.project.file.service.ITFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 附件Controller
 *
 * @author jiang
 * @date 2023-10-16
 */
@RestController
@RequestMapping("/file/file")
public class TFileController extends BaseController {
    @Autowired
    private ITFileService tFileService;

    /**
     * 查询附件列表
     */
    @PreAuthorize("@ss.hasPermi('file:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(TFile tFile) {
        startPage();
        List<TFile> list = tFileService.selectTFileList(tFile);
        return getDataTable(list);
    }


    /**
     * 获取附件详细信息
     */
    @PreAuthorize("@ss.hasPermi('file:file:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tFileService.selectTFileById(id));
    }

    /**
     * 新增附件
     */
    @PreAuthorize("@ss.hasPermi('file:file:add')")
    @Log(title = "附件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TFile tFile) {
        return toAjax(tFileService.insertTFile(tFile));
    }

    /**
     * 修改附件
     */
    @PreAuthorize("@ss.hasPermi('file:file:edit')")
    @Log(title = "附件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TFile tFile) {
        return toAjax(tFileService.updateTFile(tFile));
    }

    /**
     * 删除附件
     */
    @PreAuthorize("@ss.hasPermi('file:file:remove')")
    @Log(title = "附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tFileService.deleteTFileByIds(ids));
    }


    @PostMapping("/uploadFile")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file, @RequestParam(value = "linkId", required = false) Long linkId, @RequestParam(value = "linkName", required = false) String linkName) throws IOException {
        if (!file.isEmpty()) {
            String avatar = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file);
            TFile tFile = new TFile();
            tFile.setFileName(file.getOriginalFilename());
            tFile.setFileUrl(avatar);
            tFile.setUploader(getNickname());
            tFile.setUploadDate(new Date());
            tFile.setLinkId(linkId);
            tFile.setLinkName(linkName);
            tFileService.insertTFile(tFile);
            return AjaxResult.success(avatar);
        }
        return AjaxResult.error("上传异常，请联系管理员");
    }
}
