package thyyxxk.webserver.controller.archive;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import thyyxxk.webserver.config.auth.PassToken;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.archive.PatientArchive;
import thyyxxk.webserver.service.archive.TaskPatient;
import thyyxxk.webserver.service.archive.ArchiveServer;
import thyyxxk.webserver.utils.ResultVoUtil;

import java.util.List;

@RestController
@RequestMapping("/thyy/archive")
public class ArchiveController {

    private final ArchiveServer server;

    public ArchiveController(ArchiveServer server) {
        this.server = server;
    }

    @GetMapping("/getPatientArchives")
    public ResultVo<List<PatientArchive>> getPatientArchives(@RequestParam("patNo") String patNo,
                                                             @RequestParam("times") Integer times) {
        return ResultVoUtil.success(server.getPatientArchives(patNo, times));
    }

    @GetMapping("/getTaskNameList")
    public Object getTaskNameList() {
        return server.getTaskNameList();
    }

    @PostMapping("/submitTask")
    public ResultVo<String> submitTask(@RequestBody List<TaskPatient> data) {
        return ResultVoUtil.success(server.submitTask(data));
    }

    @GetMapping("/createMergedPDF")
    public Object createMergedPDF(@RequestParam("patNo") String patNo,
                                  @RequestParam("times") Integer times) {
        return server.createMergedPDF(patNo, times);
    }

    @PostMapping("/upload")
    public Object upload(@RequestParam("file") MultipartFile file,
                         @RequestParam("patNo") String patNo,
                         @RequestParam("times") Integer times,
                         @RequestParam(value = "parent", required = false) String parent) {
        return server.upload(file, patNo, times, parent);
    }

    @PostMapping("/addDir")
    public Object addDir(@RequestBody PatientArchive archive) {
        return server.addDir(archive);
    }

    @PostMapping("/delFile")
    public Object delFile(@RequestBody PatientArchive archive) {
        return server.delFile(archive);
    }

    @PostMapping("/sort")
    public ResultVo<String> sort(@RequestBody TaskPatient data) {
        return server.sort(data);
    }

    @GetMapping("/rename")
    public Object rename(@RequestParam("id") String id, @RequestParam("name") String name) {
        return server.rename(id, name);
    }

    @GetMapping("/getAllLog")
    public Object getAllLog(@RequestParam("patNo") String patNo,
                            @RequestParam("times") Integer times) {
        return server.getAllLog(patNo, times);
    }

    @PostMapping("/uploadFile")
    public ResultVo<ArchiveServer.UploadResult> uploadFile(@RequestParam("file") MultipartFile file,
                                                           @RequestParam("path") String path) {
        return ResultVoUtil.success(server.uploadFile(file, path));
    }


    @PostMapping("/uploadFileByCode")
    @PassToken
    public ResultVo<ArchiveServer.UploadResult> uploadFileByCode(@RequestParam("file") MultipartFile file,
                                                                 @RequestParam("path") String path,
                                                                 @RequestParam("code") String code) {
        return ResultVoUtil.success(server.uploadFileByCode(file, path, code));
    }

    @PostMapping("/delUploadById/{id}")
    public ResultVo<String> delUploadById(@PathVariable("id") String id) {
        server.delUploadById(id);
        return ResultVoUtil.success();
    }


}
