package com.tofly.xrtymis.custom.utils;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.tofly.common.ftp.config.FtpUtil;
import com.tofly.xrtymis.config.FilePathBean;
import com.tofly.xrtymis.custom.commons.CommonConstants;
import com.tofly.xrtymis.custom.commons.Constate;
import com.tofly.xrtymis.custom.service.FileService;
import com.tofly.xrtymis.entity.File;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author fcl
 * @date 2021/4/21 11:52
 */
@Component
public class SaveFile {

    @Autowired
    private FileService fileService;

    @Autowired
    private FilePathBean filePathBean;

    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(Constate.POINT));
                FtpUtil.uploadFile2Ftp(file, filePath);
                File localFile = new File();
                localFile.setFilePath(filePath);
                localFile.setFileName(fileName);
                localFile.setOtherName(IdWorker.getIdStr() + fileName.substring(fileName.lastIndexOf(Constate.POINT)));
                //关联表名称
                localFile.setTableName(tableName);
                //获取后缀
                String suffix = fileName.substring(fileName.lastIndexOf(Constate.POINT));
                localFile.setFileFormat(suffix);
                localFile.setFileSize(file.getSize() + Constate.KB);
                fileService.save(localFile);
                //获得文件ID
                fileIds.append(localFile.getId()).append(Constate.CSV);
            }
        });

        return fileIds;
    }

    public boolean removeFileById(String ids){
        if (StringUtils.isNotBlank(ids)) {
            List<Long> idList = Arrays.stream(ids.split(Constate.CSV)).map(s -> Long.parseLong(s.trim()))
                    .collect(Collectors.toList());
            return fileService.removeFileList(idList);
        }
        return false;
    }


    /**
     * 存储文件
     * @param fileList
     * @param fileIdsdb
     * @param fileIds
     * @return
     */
    public StringBuilder saveFiles(List<MultipartFile> fileList, String fileIdsdb, String fileIds) {
        StringBuilder fileIdList = null;
        if (CollectionUtils.isNotEmpty(fileList)) {
            fileIdList = this.saveFileList(fileList, filePathBean.getSbxxcj(), CommonConstants.WATERREP_RECORD_TABLE_NAME);
        }

        if (StringUtils.isNotEmpty(fileIdsdb) && !fileIdsdb.equals(fileIds)) {
            List<Long> removeFileId = new ArrayList<>();
            if (StringUtils.isEmpty(fileIds)) {
                removeFileId = Arrays.stream(fileIdsdb.split(Constate.CSV)).map(s -> Long.parseLong(s.trim()))
                        .collect(Collectors.toList());
            } else {
                removeFileId = Arrays.stream(fileIdsdb.split(Constate.CSV)).filter(st -> !fileIds.contains(st)).map(st -> Long.parseLong(st.trim())).collect(Collectors.toList());
            }
            // 删除对应文件数据
            if (!removeFileId.isEmpty()) {
                // 删除远程文件
                this.removeFileById(org.apache.commons.lang.StringUtils.join(removeFileId,","));
            }
        }
        StringBuilder files = new StringBuilder();
        if (StringUtils.isEmpty(fileIds) || StringUtils.isEmpty(fileIdList)) {
            if(StringUtils.isNotEmpty(fileIds)){
                files.append(fileIds);
            }else if(StringUtils.isNotEmpty(fileIdList)){
                files.append(fileIdList);
            }
        } else {
            files.append(fileIds).append(Constate.CSV).append(fileIdList);
        }
        return files;
    }


}
