package com.xkrs.controller;

import com.xkrs.model.vo.FileServerResultVo;
import com.xkrs.service.FileServerService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 文件服务器管理服务
 * @author tajochen
 */
@RestController
@RequestMapping("/api/file")
public class FileServerController {

    @Resource
    private FileServerService fileServerService;

    /**
     * 文件统计
     * @return
     */
    @RequestMapping(value="/stat",method = RequestMethod.GET)
    @PreAuthorize("hasAnyAuthority('auth_system_manager','auth_administor')")
    @CrossOrigin
    public FileServerResultVo getFileStat() {

        return fileServerService.getFileStat();
    }

    /**
     * 文件删除
     * @param md5   信息摘要
     * @param path  路径
     * @return
     */
    @RequestMapping(value="/delete",method = RequestMethod.DELETE)
    @PreAuthorize("hasAnyAuthority('auth_system_manager','role_administor')")
    @CrossOrigin
    public FileServerResultVo deleteFile(@RequestParam(value = "md5",required = false) String md5,
                                         @RequestParam(value = "path",required = false) String path) {
        FileServerResultVo fileServerResultVo = new FileServerResultVo();
        Boolean existed = (md5==null||md5.isEmpty())&&(path==null||path.isEmpty());
        if(existed){
            fileServerResultVo.setStatus("fail");
            fileServerResultVo.setData("");
            fileServerResultVo.setMessage("参数为空");
            return fileServerResultVo;
        }
        return fileServerService.deleteFile(md5,path);
    }

    /**
     * 获取文件信息
     * @param md5   信息摘要
     * @param path  路径
     * @return
     */
    @RequestMapping(value="/get/info",method = RequestMethod.DELETE)
    @PreAuthorize("hasAnyAuthority('auth_system_manager','role_administor')")
    @CrossOrigin
    public FileServerResultVo getFileInfo(@RequestParam(value = "md5",required = false) String md5,
                                          @RequestParam(value = "path",required = false) String path) {

        FileServerResultVo fileServerResultVo = new FileServerResultVo();
        Boolean existed = (md5==null||md5.isEmpty())&&(path==null||path.isEmpty());
        if(existed){
            fileServerResultVo.setStatus("fail");
            fileServerResultVo.setData("");
            fileServerResultVo.setMessage("参数为空");
            return fileServerResultVo;
        }
        return fileServerService.getFileInfo(md5,path);
    }

    /**
     * 获取文件列表
     * @param dir   文件夹
     * @return
     */
    @RequestMapping(value="/get/list",method = RequestMethod.DELETE)
    @PreAuthorize("hasAnyAuthority('auth_system_manager','role_administor')")
    @CrossOrigin
    public FileServerResultVo getFileList(@RequestParam(value = "dir") String dir) {
        return fileServerService.getFileList(dir);
    }

    /**
     * 修复统计信息
     * @param date 要修复的日期，格式如：20190725
     * @return
     */
    @RequestMapping(value="/get/repair_stat",method = RequestMethod.DELETE)
    @PreAuthorize("hasAnyAuthority('auth_system_manager','role_administor')")
    @CrossOrigin
    public FileServerResultVo getFileRepairStat(@RequestParam(value = "date") String date) {
        return fileServerService.getFileRepairStat(date);
    }

    /**
     * 同步失败修复
     * @param force 是否强行修复(0|1)
     * @return
     */
    @RequestMapping(value="/get/repair",method = RequestMethod.DELETE)
    @PreAuthorize("hasAnyAuthority('auth_system_manager','role_administor')")
    @CrossOrigin
    public FileServerResultVo getFileRepair(@RequestParam(value = "force") String force) {
        return fileServerService.getFileRepair(force);
    }

}
