package com.foreverwin.mesnac.common.service.impl;

import com.foreverwin.mesnac.common.enums.HandleEnum;
import com.foreverwin.mesnac.common.ftp.CappFtpClient;
import com.foreverwin.mesnac.common.service.FileService;
import com.foreverwin.mesnac.common.util.ExceptionUtil;
import com.foreverwin.mesnac.common.util.StringUtil;
import com.foreverwin.mesnac.meapi.model.Sfc;
import com.foreverwin.mesnac.meapi.service.SfcService;
import com.foreverwin.modular.core.exception.BaseException;
import com.foreverwin.modular.core.util.CommonMethods;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service
public class FileServiceImpl implements FileService {
    @Autowired
    private SfcService sfcService;
    @Autowired
    private CappFtpClient cappFtpClient;
    @Override
    public Map<String, String> getFilePaths(String sfc) throws IOException {

        Sfc sfcServiceById = sfcService.getById(HandleEnum.SFC.getHandle(CommonMethods.getSite(), sfc));
        if (sfcServiceById==null){
            throw new BaseException("未找到产品条码"+sfc);
        }
        String itemBo = sfcServiceById.getItemBo();
        String path="/"+ StringUtil.trimHandle(itemBo)+"_"+StringUtil.trimRevision(itemBo)+"/";
        FTPClient connect = null;
        Map<String,String> pathMap = new HashMap<>();
        try {
            connect = cappFtpClient.login();
            connect.changeWorkingDirectory(path);
            connect.enterLocalPassiveMode();
            FTPFile[] files = connect.listFiles();
            if(files!=null){
                for (int i = 0; i < files.length; i++) {
                    if(files[i].isFile()){
                        String filename=files[i].getName();
                        pathMap.put(filename,path+filename);
                    }
                }
            }
            connect.disconnect();
        }catch (Exception e){
            ExceptionUtil.throwException(e);
        }finally {
            if (connect!=null&&connect.isConnected()){
                connect.logout();
                connect.disconnect();
            }
        }

        return pathMap;
    }

    @Override
    public Map<String, String> getFilePathsByItemBo(String itemBo) throws IOException {
        String path="/"+ StringUtil.trimHandle(itemBo)+"_"+StringUtil.trimRevision(itemBo)+"/";
        FTPClient connect = null;
        Map<String,String> pathMap = new HashMap<>();
        try {
            connect = cappFtpClient.login();
            connect.changeWorkingDirectory(path);
            connect.enterLocalPassiveMode();
            FTPFile[] files = connect.listFiles();
            if(files!=null){
                for (int i = 0; i < files.length; i++) {
                    if(files[i].isFile()){
                        String filename=files[i].getName();
                        pathMap.put(filename,path+filename);
                    }
                }
            }
            connect.disconnect();
        }catch (Exception e){
            ExceptionUtil.throwException(e);
        }finally {
            if (connect!=null&&connect.isConnected()){
                connect.logout();
                connect.disconnect();
            }
        }

        return pathMap;
    }

}
