package com.ruoyi.project.pssr.controller;

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.pssr.domain.TPssrFile;
import com.ruoyi.project.pssr.service.ITPssrFileService;
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.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * PSSR附件Controller
 *
 * @author ssy
 * @date 2024-10-30
 */
@RestController
@RequestMapping("/pssr/file")
public class TPssrFileController extends BaseController {
    @Autowired
    private ITPssrFileService tPssrFileService;

    /**
     * 查询PSSR附件列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TPssrFile tPssrFile) {
        startPage();
        List<TPssrFile> list = tPssrFileService.selectTPssrFileList(tPssrFile);
        return getDataTable(list);
    }

    /**
     * 导出PSSR附件列表
     */
    @Log(title = "PSSR附件", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TPssrFile tPssrFile) {
        List<TPssrFile> list = tPssrFileService.selectTPssrFileList(tPssrFile);
        ExcelUtil<TPssrFile> util = new ExcelUtil<TPssrFile>(TPssrFile.class);
        return util.exportExcel(list, "file");
    }

    /**
     * 获取PSSR附件详细信息
     */
    @GetMapping(value = "/{fileId}")
    public AjaxResult getInfo(@PathVariable("fileId") Long fileId) {
        return AjaxResult.success(tPssrFileService.selectTPssrFileById(fileId));
    }

    /**
     * 新增PSSR附件
     */
    @PreAuthorize("@ss.hasPermi('pssr:file:add')")
    @Log(title = "PSSR附件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TPssrFile tPssrFile) {
        return toAjax(tPssrFileService.insertTPssrFile(tPssrFile));
    }

    /**
     * 修改PSSR附件
     */
    @Log(title = "PSSR附件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TPssrFile tPssrFile) {
        return toAjax(tPssrFileService.updateTPssrFile(tPssrFile));
    }

    /**
     * 删除PSSR附件
     */
    @Log(title = "PSSR附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fileIds}")
    public AjaxResult remove(@PathVariable Long[] fileIds) {
        return toAjax(tPssrFileService.deleteTPssrFileByIds(fileIds));
    }


    @Log(title = "pssr附件上传", businessType = BusinessType.UPDATE)
    @PostMapping("/uploadFileM")
    public AjaxResult uploadFileM(@RequestParam("file") MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            String url = FileUploadUtils.upload(RuoYiConfig.getFilePath("/pssr/picture"), file);
            TPssrFile pssrFile = new TPssrFile();
            pssrFile.setFileUrl(url);
            pssrFile.setFileName(file.getOriginalFilename());
            pssrFile.setCreatedate(new Date());
            pssrFile.setCreaterCode(getUserId().toString());
            tPssrFileService.insertTPssrFile(pssrFile);
            return AjaxResult.success(pssrFile.getFileId());
        }
        return AjaxResult.error("上传失败，请联系管理员");
    }

    @Log(title = "pssr附件上传", businessType = BusinessType.UPDATE)
    @PostMapping("/uploadFile")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file, Long subId, Long itemId, String forShort) throws IOException {
        if (!file.isEmpty()) {
            String url = FileUploadUtils.upload(RuoYiConfig.getFilePath("/pssr/pictures"), file);
            TPssrFile pssrFile = new TPssrFile();
            pssrFile.setItemId(itemId);
            pssrFile.setSubId(subId);
            pssrFile.setForShort(forShort);
            pssrFile.setFileUrl(url);
            pssrFile.setFileName(file.getOriginalFilename());
            pssrFile.setCreatedate(new Date());
            pssrFile.setCreaterCode(getUserId().toString());
            tPssrFileService.insertTPssrFile(pssrFile);
            return AjaxResult.success(pssrFile.getFileId());
        }
        return AjaxResult.error("上传失败，请联系管理员");
    }
}
