package com.xkrs.service.impl;

import com.xkrs.model.vo.FileServerResultVo;
import com.xkrs.service.FileServerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

import static com.xkrs.utils.RequestUtil.postFileManage;

/**
 * 文件服务器接口
 * @author tajochen
 */
@Service
public class FileServerServiceImpl implements FileServerService {

    Logger logger = LoggerFactory.getLogger(FileServerServiceImpl.class);

    @Value("${my.FileServerAdminAdress}")
    private String fileServerAdminAdress;

    /**
     * 获取文件统计信息
     * @return
     */
    @Override
    public FileServerResultVo getFileStat(){
        String requestUrl = fileServerAdminAdress + "/group1/stat";
        Map<String,String> map = new HashMap<String, String>(16);
        FileServerResultVo fileServerResultVo = postFileManage(requestUrl,map);
        return fileServerResultVo;
    }

    /**
     * 删除文件
     * @param md5   信息摘要
     * @param path  路径
     * @return
     */
    @Override
    public FileServerResultVo deleteFile(String md5,String path){
        String requestUrl = fileServerAdminAdress + "/group1/delete";
        Map<String,String> map = new HashMap<String, String>(16);
        if(md5 != null && !md5.isEmpty()){
            map.put("md5",md5);
        }
        if(path != null && !path.isEmpty()){
            map.put("path",path);
        }
        FileServerResultVo fileServerResultVo = postFileManage(requestUrl,map);
        return fileServerResultVo;
    }

    /**
     * 获取文件信息
     * @param md5   信息摘要
     * @param path  路径
     * @return
     */
    @Override
    public FileServerResultVo getFileInfo(String md5,String path){
        String requestUrl = fileServerAdminAdress + "/group1/get_file_info";
        Map<String,String> map = new HashMap<String, String>(16);
        if(md5 != null && !md5.isEmpty()){
            map.put("md5",md5);
        }
        if(path != null && !path.isEmpty()){
            map.put("path",path);
        }
        FileServerResultVo fileServerResultVo = postFileManage(requestUrl,map);
        return fileServerResultVo;
    }

    /**
     * 获取文件列表
     * @param dir 目录名
     * @return
     */
    @Override
    public FileServerResultVo getFileList(String dir){
        String requestUrl = fileServerAdminAdress + "/group1/list_dir";
        Map<String,String> map = new HashMap<String, String>(16);
        map.put("dir",dir);
        FileServerResultVo fileServerResultVo = postFileManage(requestUrl,map);
        return fileServerResultVo;
    }

    /**
     * 修复统计信息
     * @param date 要修复的日期，格式如：20190725
     * @return
     */
    @Override
    public FileServerResultVo getFileRepairStat(String date){
        String requestUrl = fileServerAdminAdress + "/group1/repair_stat";
        Map<String,String> map = new HashMap<String, String>(16);
        map.put("date",date);
        FileServerResultVo fileServerResultVo = postFileManage(requestUrl,map);
        return fileServerResultVo;
    }

    /**
     * 同步失败修复
     * @param force 是否强行修复(0|1)
     * @return
     */
    @Override
    public FileServerResultVo getFileRepair(String force){
        String requestUrl = fileServerAdminAdress + "/group1/repair";
        Map<String,String> map = new HashMap<String, String>(16);
        map.put("force",force);
        FileServerResultVo fileServerResultVo = postFileManage(requestUrl,map);
        return fileServerResultVo;
    }

}
