package com.vctgo.file.controller;

import com.vctgo.common.core.domain.R;
import com.vctgo.common.core.web.controller.BaseController;
import com.vctgo.common.mybatisplus.aop.BaseInfoAnnotation;
import com.vctgo.common.mybatisplus.aop.OperateTypeEnum;
import com.vctgo.common.mybatisplus.aop.TemporaryIgnoreTables;
import com.vctgo.file.service.IFileInfoService;
import com.vctgo.system.api.domain.FileInfo;
import com.vctgo.system.api.domain.dto.FileInfoDto;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 文件请求处理
 *
 * @author vctgo
 */
@Api(tags = "文件服务基础业务")
@RestController
public class FileInfoController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(FileInfoController.class);
    @Autowired
    private IFileInfoService fileInfoService;

    /**
     * 业务文件上传请求
     */
    @BaseInfoAnnotation(OperateTypeEnum.ADD)
    @PostMapping("/upload")
    public R<FileInfoDto> upload(MultipartFile file) {
        return fileInfoService.upload(file);
    }

    /**
     * 带着上传标签 文件上传请求
     */
    @BaseInfoAnnotation(OperateTypeEnum.ADD)
    @PostMapping("/uploadByUploadTag")
    public R<FileInfo> uploadByUploadTag(String uploadTag, MultipartFile file) {
        return fileInfoService.uploadByUploadTag(uploadTag, file);
    }

    /**
     * 更新文件状态信息
     *
     * @param serviceName 业务名称
     * @param status      状态 0/1
     * @param serviceId   服务对应的记录Id
     * @return
     */
    @PostMapping("/updateFileStatusByServiceIdAndServiceName")
    public R updateFileStatusByServiceIdAndServiceName(String serviceName, String status, String serviceId) {
        return 0 == fileInfoService.updateFileStatusByServiceIdAndServiceName(serviceName, status, serviceId) ? R.fail() : R.ok();
    }

    /**
     * 更新文件服务Id信息
     *
     * @param serviceName 业务名称
     * @param serviceId   业务Id
     * @param ids         记录文件的id数组
     * @return
     */
    @PostMapping("/updateFileServiceIdAndServiceNameByIds")
    public R updateFileServiceIdAndServiceNameByIds(String serviceName, String serviceId, String ids) {
        return 0 == fileInfoService.updateFileServiceIdAndServiceNameByIds(serviceName, serviceId, ids) ? R.fail() : R.ok();
    }

    /**
     * 更新文件服务Id信息
     *
     * @param serviceName 业务名称
     * @param serviceId   业务Id
     * @param status      状态 0/1
     * @return
     */
    @PostMapping("/updateFileServiceIdAndServiceNameAndStatusByIds")
    public R updateFileServiceIdAndServiceNameAndStatusByIds(String serviceName, String serviceId, String status, String ids) {
        return 0 == fileInfoService.updateFileServiceIdAndServiceNameAndStatusByIds(serviceName, serviceId, status, ids) ? R.fail() : R.ok();
    }

    /**
     * 通过文件Ids获取文件信息
     *
     * @param ids 文件Ids
     * @return
     */
    @TemporaryIgnoreTables(temporaryIgnoreTables = {"file_info"})
    @GetMapping("/selectUrlById/{ids}")
    public R<List<FileInfo>> selectUrlByIds(@PathVariable String ids) {
        return R.ok(fileInfoService.selectUrlByIds(ids));
    }

    /**
     * 通过文件id的集合获取文件集信息
     *
     * @param ids 文件Ids
     * @return
     */
    @TemporaryIgnoreTables(temporaryIgnoreTables = {"file_info"})
    @GetMapping("/url-list/{tenantId}/{ids}")
    public R<List<FileInfo>> selectPointTenantUrlByIds(@PathVariable Long tenantId,
                                                      @PathVariable String ids) {
        return R.ok(fileInfoService.selectPointTenantUrlByIds(tenantId, ids));
    }

    /**
     * fastdfs下载文件
     *
     * @param filePath 文件路径
     * @return
     */
    @GetMapping("/fastdfs/download")
    public R fastdfsDownLoad(String filePath) {
        fileInfoService.fastdfsDownLoad(filePath);
        return R.ok();
    }


    /**
     * 通过文件Id逻辑删除文件、文件夹
     * @param fileIds 文件、文件夹ID集合
     * @return
     */
    @GetMapping("/logicalDelete/{fileIds}")
    public R logicalDeleteByFileIds(@PathVariable("fileIds") String fileIds) {
        fileInfoService.logicalDeleteByFileIds(fileIds);
        return R.ok();
    }
}
