package com.tofly.gpsboot.service.comm.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.ftp.config.FtpUtil;
import com.tofly.gpsboot.common.Constant;
import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.gpsboot.mapper.comm.FileMapper;
import com.tofly.gpsboot.service.comm.IFileInfoService;

import com.tofly.pojo.comm.File;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;


@Service("FileInfoService")
public class FileInfoServiceImpl extends ServiceImpl<FileMapper, File> implements IFileInfoService {

    @Override
    public StringBuilder saveFileList(List<MultipartFile> fileList, String filePathBean, String tableName) {
        StringBuilder fileIds = new StringBuilder("");

        //保存照片
        fileList.forEach(file -> {
            //文件名
            String fileName = file.getOriginalFilename();
            if (!"".equals(fileName)) {
                //文件路径
                assert fileName != null;
                String filePath = filePathBean + IdWorker.getIdStr() + fileName.substring(fileName.lastIndexOf(Constant.POINT));
                boolean b = FtpUtil.uploadFile2Ftp(file, filePath);
                if (b) {
                    File localFile = new File();
                    localFile.setFilePath(filePath);
                    localFile.setFileName(fileName);
                    localFile.setOtherName(IdWorker.getIdStr() + fileName.substring(fileName.lastIndexOf(Constant.POINT)));
                    //关联表名称
                    localFile.setTableName(tableName);
                    //获取后缀
                    String suffix = fileName.substring(fileName.lastIndexOf(Constant.POINT));
                    localFile.setFileFormat(suffix);
                    localFile.setFileSize(file.getSize() + Constant.KB);
                    this.save(localFile);
                    //获得文件ID
                    fileIds.append(localFile.getId()).append(Constant.CSV);
                } else {
                    throw new BusinessException(Constant.FILE_UPLOAD_ERROR_CODE, Constant.FILE_UPLOAD_ERROR);
                }

            }
        });
        if (fileIds.length() > 0) {
            return fileIds.deleteCharAt(fileIds.length() - 1);
        }
        return fileIds;
    }

    @Override
    public Boolean removeFileList(List<Long> fileIdList) {
        List<File> files = this.listByIds(fileIdList);
        // 删除远程文件
        files.forEach(k -> FtpUtil.deleteFile(k.getFilePath()));
        return true;
    }

    @Override
    public List<String> getFilePathList(List<Long> fileIdList) {
        List<String> filePathList = new ArrayList<>();
        fileIdList.forEach(fileId -> {
                    filePathList.add(this.getById(fileId).getFilePath());
                }

        );
        return filePathList;
    }


}
