/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.quality.service.comm.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.tofly.common.ftp.config.FtpUtil;
import com.tofly.quality.common.FlowConstant;
import com.tofly.quality.entity.comm.File;
import com.tofly.quality.mapper.comm.FileMapper;
import com.tofly.quality.service.comm.FileService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 公共文件表： TF_YWPN_FILE_W
 *
 * @author xqf
 * @date Fri Mar 11 00:00:00 CST 2022
 */
@Service("fileService")
public class FileServiceImpl extends ServiceImpl<FileMapper, File> implements FileService {

    /**
     *  上传文件,并获取文件id 用逗号分隔
     * @param fileList 文件集合
     * @param filePathBean 文件路径
     * @param tableName 应用表名称
     * @return
     */
    @Override
    public StringBuilder saveFileList(List<MultipartFile> fileList, String filePathBean, String tableName) {
        StringBuilder fileIds = new StringBuilder("");
        for (MultipartFile file:fileList) {
            String fileName = file.getOriginalFilename();
            if (StringUtils.isNotEmpty(fileName)) {
                String suffix = fileName.substring(fileName.lastIndexOf(FlowConstant.POINT));
                String filePath = filePathBean + IdWorker.getIdStr() + fileName.substring(fileName.lastIndexOf(FlowConstant.POINT));
                if(FtpUtil.uploadFile2Ftp(file,filePath)) {

                    File localFile = File.builder()
                            .filePath(filePath)
                            .fileName(fileName)
                            .otherName(IdWorker.getIdStr() + fileName.substring(fileName.lastIndexOf(FlowConstant.POINT)))
                            .tableName(tableName)
                            .fileFormat(suffix)
                            .fileSize(file.getSize() + FlowConstant.KB).build();
                    save(localFile);
                    fileIds.append(localFile.getId()).append(FlowConstant.CSV);
                }else {
                    return null;
                }
            }
        }
        if (fileIds.length() > 0) {
            return fileIds.deleteCharAt(fileIds.length() - 1);
        }
        return fileIds;
    }

    @Override
    public Boolean removeFileList(List<Long> fileIdList) {
        List<File> files = listByIds(fileIdList);
        try {
            files.forEach(k -> FtpUtil.deleteFile(k.getFilePath()));
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public List<Map<String,String>> getFileInfo(List<Long> fileIdList) {
        List<Map<String,String>> fileInfoList = Lists.newArrayList();

        fileIdList.forEach(fileId -> {
            Map<String,String> map = Maps.newHashMap();
            if(fileId!=null) {
                File file = getById(fileId);
                map.put("id", file.getId() + "");
                map.put("url", file.getFilePath());
                map.put("fileName", file.getFileName());

                fileInfoList.add(map);
            }
        });

        return fileInfoList;
    }

    /**
     * 上传文件,并获取文件id, 返回Map 文件名，id
     * @param fileList 文件集合
     * @param filePathBean 文件路径
     * @param tableName 表名称
     * @return Map<String, Long> 文件名称，id
     */
    @Override
    public Map<String, Long> saveFileMap(List<MultipartFile> fileList,String filePathBean,String tableName) {
        Map<String,Long> fileMap = new HashMap<>();
        for (MultipartFile file:fileList) {
            String fileName = file.getOriginalFilename();
            if (StringUtils.isNotEmpty(fileName)) {
                String suffix = fileName.substring(fileName.lastIndexOf(FlowConstant.POINT));
                String filePath = filePathBean + IdWorker.getIdStr() + fileName.substring(fileName.lastIndexOf(FlowConstant.POINT));
                FtpUtil.uploadFile2Ftp(file,filePath);

                File localFile = File.builder()
                        .filePath(filePath)
                        .fileName(fileName)
                        .otherName(IdWorker.getIdStr() + fileName.substring(fileName.lastIndexOf(FlowConstant.POINT)))
                        .tableName(tableName)
                        .fileFormat(suffix)
                        .fileSize(file.getSize() + FlowConstant.KB).build();
                save(localFile);
                fileMap.put(fileName,localFile.getId());
            }
        }
        return fileMap;
    }

}
