package com.goldenwater.slgc.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.goldenwater.common.annotation.Log;
import com.goldenwater.common.config.RuoYiConfig;
import com.goldenwater.common.core.controller.BaseController;
import com.goldenwater.common.core.domain.AjaxResult;
import com.goldenwater.common.core.page.TableDataInfo;
import com.goldenwater.common.enums.BusinessType;
import com.goldenwater.common.utils.file.FileUploadUtils;
import com.goldenwater.common.utils.uuid.IdUtils;
import org.apache.commons.io.FilenameUtils;
import com.goldenwater.slgc.domain.DocFile;
import com.goldenwater.slgc.service.DocFileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "16. 文档文件管理")
@RestController
@RequestMapping("/slgc/doc-file")
public class DocFileController extends BaseController {
    @Autowired
    private DocFileService docFileService;

    @Operation(summary = "16.01 查询文档文件列表")
    @GetMapping("/list")
    public TableDataInfo list(DocFile docFile) {
        startPage();
        List<DocFile> list = docFileService.selectDocFileList(docFile);
        return getDataTable(list);
    }

    @Operation(summary = "16.02 获取文档文件详细信息")
    @GetMapping(value = "/{fileId}")
    public AjaxResult getInfo(@PathVariable String fileId) {
        return success(docFileService.selectDocFileById(fileId));
    }

    @Operation(summary = "16.03 新增文档文件")
    @Log(title = "文档文件管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DocFile docFile) {
        return toAjax(docFileService.insertDocFile(docFile));
    }

    @Operation(summary = "16.04 修改文档文件")
    @Log(title = "文档文件管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DocFile docFile) {
        return toAjax(docFileService.updateDocFile(docFile));
    }

    @Operation(summary = "16.05 删除文档文件")
    @Log(title = "文档文件管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fileIds}")
    public AjaxResult remove(@PathVariable String[] fileIds) {
        return toAjax(docFileService.deleteDocFileByIds(fileIds));
    }

    @Operation(summary = "16.06 移动文档文件到指定分类")
    @Log(title = "文档文件管理", businessType = BusinessType.UPDATE)
    @PostMapping("/move")
    public AjaxResult move(@RequestParam("fileIds") String[] fileIds, @RequestParam("fileCate") String fileCate) {
        return toAjax(docFileService.moveDocFile(fileIds, fileCate));
    }

    @Operation(summary = "16.06 重命名文档文件")
    @Log(title = "文档文件管理", businessType = BusinessType.UPDATE)
    @PutMapping("/rename")
    public AjaxResult rename(@RequestParam("fileId") String fileId,
                             @RequestParam("fileName") String fileName) {
        return toAjax(docFileService.renameDocFile(fileId, fileName));
    }

    @Operation(summary = "16.07 上传文档文件")
    @Log(title = "文档文件管理", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile file,
                             @RequestParam(value = "fileCate", required = false) String fileCate,
                             @RequestParam(value = "fileDesc", required = false) String fileDesc) {
        try {
            String filePath = RuoYiConfig.getUploadPath();
            String fileName = FileUploadUtils.upload(filePath, file);

            DocFile docFile = new DocFile();
            docFile.setFileId(IdUtils.fastSimpleUUID());
            docFile.setFileName(file.getOriginalFilename());
            docFile.setFileType(FilenameUtils.getExtension(file.getOriginalFilename()));
            docFile.setFileSize(BigDecimal.valueOf(file.getSize()));
            docFile.setFilePath(fileName);
            docFile.setFileCate(fileCate != null ? fileCate : "0");
            docFile.setFileDesc(fileDesc);
            docFile.setFileTime(new Date());
            docFile.setDownTimes(0);

            docFileService.insertDocFile(docFile);

            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", fileName);
            ajax.put("fileName", file.getOriginalFilename());
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error("上传失败: " + e.getMessage());
        }
    }
}
