package com.example.system.controller;

import com.example.system.model.InformationTable;
import com.example.system.repository.InformationRepository;
import com.example.system.utils.OssUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/oss")
@CrossOrigin
public class OssController {

    @Autowired
    OssUtils ossUtils;

    @Autowired
    InformationRepository informationRepository; // 注入信息表的数据库操作接口

    @PostMapping("uploadOneFile")
    public String uploadFile(@RequestPart("file") MultipartFile file,
                             @RequestParam("userId") Long userId,
                             @RequestParam("competitionId") Long competitionId,
                             @RequestParam("relatedData") String relatedData) {
        log.info("上传文件");
        // 上传文件到oss并获取URL
        String url = ossUtils.uploadOneFile(file);

        // 存储URL、userId、competitionId和relatedData到数据库
        InformationTable information = new InformationTable();
        information.setPersonalInfo(url);
        information.setUserId(userId);
        information.setCompetitionId(competitionId);
        information.setRelatedData(relatedData);
        informationRepository.save(information);

        return url;
    }

    @PostMapping("uploadArrayFile")
    public List<String> uploadArrayFile(@RequestBody UploadArrayRequest uploadArrayRequest) {
        // 上传多个文件到oss并获取URL列表
        List<String> urls = ossUtils.uploadArrayFile(uploadArrayRequest.getFiles());
        // 存储URL、userId、competitionId和relatedData到数据库
        for (String url : urls) {
            InformationTable information = new InformationTable();
            information.setPersonalInfo(url);
            information.setUserId(uploadArrayRequest.getUserId());
            information.setCompetitionId(uploadArrayRequest.getCompetitionId());
            information.setRelatedData(uploadArrayRequest.getRelatedData());
            informationRepository.save(information);
        }

        return urls;
    }

    @PostMapping("deleteFile")
    public boolean deleteFile(@RequestBody DeleteRequest deleteRequest) {
        // 删除oss上的文件
        boolean deleted = ossUtils.deleteFile(deleteRequest.getFileUrl());

        // 如果文件删除成功，则从数据库中删除相应的记录
        if (deleted) {
            InformationTable information = informationRepository.findByPersonalInfo(deleteRequest.getFileUrl());
            if (information != null) {
                informationRepository.delete(information);
            }
        }

        return deleted;
    }

    // 内部类用于接收上传多个文件请求的参数
    static class UploadArrayRequest {
        private MultipartFile[] files;
        private Long userId;
        private Long competitionId;
        private String relatedData;

        public MultipartFile[] getFiles() {
            return files;
        }

        public void setFiles(MultipartFile[] files) {
            this.files = files;
        }

        public Long getUserId() {
            return userId;
        }

        public void setUserId(Long userId) {
            this.userId = userId;
        }

        public Long getCompetitionId() {
            return competitionId;
        }

        public void setCompetitionId(Long competitionId) {
            this.competitionId = competitionId;
        }

        public String getRelatedData() {
            return relatedData;
        }

        public void setRelatedData(String relatedData) {
            this.relatedData = relatedData;
        }
    }

    // 内部类用于接收删除文件请求的参数
    static class DeleteRequest {
        private String fileUrl;

        public String getFileUrl() {
            return fileUrl;
        }

        public void setFileUrl(String fileUrl) {
            this.fileUrl = fileUrl;
        }
    }
}
