package com.tofly.qualitycontrol.service.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.qualitycontrol.common.QualityControlConstant;
import com.tofly.qualitycontrol.entity.QualityControlFile;
import com.tofly.qualitycontrol.mapper.QualityControlFileMapper;
import com.tofly.qualitycontrol.service.IQualityControlFileService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

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

import static com.tofly.qualitycontrol.util.QualityControlUtil.isNotNull;

@Service
public class QualityControlFileServiceImpl extends ServiceImpl<QualityControlFileMapper,QualityControlFile> implements IQualityControlFileService, QualityControlConstant {

    @Override
    public StringBuilder saveFileList(List<MultipartFile> fileList,String filePathBean,String tableName) {
        StringBuilder fileIds = new StringBuilder("");
        for (MultipartFile file:fileList) {
            String fileName = file.getOriginalFilename();
            if (isNotNull(fileName)) {
                String suffix = fileName.substring(fileName.lastIndexOf(POINT));
                String filePath = filePathBean + IdWorker.getIdStr() + fileName.substring(fileName.lastIndexOf(POINT));
                if(FtpUtil.uploadFile2Ftp(file,filePath)) {

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

    @Override
    public Boolean removeFileList(List<Long> fileIdList) {
        List<QualityControlFile> 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) {
                QualityControlFile file = getById(fileId);
                if (file != null){
                    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
     */
    @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 (isNotNull(fileName)) {
                String suffix = fileName.substring(fileName.lastIndexOf(POINT));
                String filePath = filePathBean + IdWorker.getIdStr() + fileName.substring(fileName.lastIndexOf(POINT));
                FtpUtil.uploadFile2Ftp(file,filePath);

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

}
