package cc.smtweb.system.bpm.spring.file.attach;


import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.util.DateUtil;
import cc.smtweb.framework.core.util.StringUtil;
import cc.smtweb.system.bpm.spring.config.FileConfigProperties;
import cc.smtweb.system.bpm.spring.dataCacheInit.AttachFolderEntityBuffer;
import cc.smtweb.system.bpm.web.sys.base.attach.AttachInfo;
import cc.smtweb.system.bpm.web.sys.base.attachFolder.AttachFolder;
import cc.smtweb.system.bpm.web.sys.base.attachFolder.pojo.AttachFolderPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.nio.file.Path;
import java.util.List;

/**
 * Created by AKmm at 2011-2-23 15:06:56
 * 附件存储工具，按存储方式决定保存在
 */
@Component
public final class AttachUtil {
    //一个文件夹下最多允许多少文件
    private final static int MAX_FILE_COUNT = 1000;
    private static IAttachWorkIntf worker;
    @Autowired
    private FileConfigProperties properties;
    @Autowired
    private AttachFolderEntityBuffer attachFolderEntityBuffer;
    @Autowired
    private DbEngine dbEngine;
    @Autowired
    private AttachFtpWork attachFtpWork;
    @Autowired
    private AttachFileWork attachFileWork;

    static {
//      init();
    }

    public void init() {
        final String at = StringUtil.checkNull(properties.getAttachType()).toLowerCase();
        if ("ftp".equals(at) || "sftp".equals(at))
            worker = attachFtpWork;
        else worker = attachFileWork;
    }

    private void checkAttachFolder(final AttachInfo attach) throws Exception {
        final String period = DateUtil.getNowYm();
        AttachFolderPojo folder = attachFolderEntityBuffer.getByPath(attach.getPath(), period);

        AttachFolder entity = new AttachFolder();
        boolean isNew = false;
        if (folder == null) {
            isNew = true;
            folder = new AttachFolderPojo();
            folder.setFolderId(dbEngine.nextId());
            folder.setFolderAttachPath(attach.getPath());
            folder.setFolderPeriod(period);
            folder.setFolderMaxSeq(1);
            folder.setFolderFileCount(0L);

            entity.setPath(attach.getPath());
            entity.setPeriod(period);
            entity.setMaxSeq(1);
        }
        if (folder.getFolderFileCount() >= MAX_FILE_COUNT) {
            folder.setFolderMaxSeq(folder.getFolderMaxSeq() + 1);
            folder.setFolderFileCount(0l);

            entity.setMaxSeq(folder.getFolderMaxSeq() + 1);
            entity.setFileCount(0);
        }
        folder.setFolderFileCount(folder.getFolderFileCount() + 1);
        entity.setFileCount(folder.getFolderFileCount() + 1);
        entity.setId(folder.getFolderId());
        //检查并确定远程目录
        if (isNew) {
            dbEngine.insertEntity(entity, "folder_id,folder_path,folder_period,folder_max_seq,folder_file_count");
        } else {
            dbEngine.updateEntity(entity, "folder_max_seq,folder_file_count");
        }
        attach.setPath(attach.getPath() + "/" + period + "/" + folder.getFolderMaxSeq());
        attachFolderEntityBuffer.putOneData(folder);
    }

    public void saveAttach(final AttachInfo attach, String localAttach, boolean isDelLocal) throws Exception {
        synchronized (attach.getPath()) {
            checkAttachFolder(attach);
            saveAttach(attach.getName(), attach.getPath(), localAttach, false);
        }
    }

    public void saveAttach(String remoteAttachName, String remoteFolderPath, String localAttach, boolean isDelLocal) throws Exception {
        worker.saveAttach(remoteAttachName, remoteFolderPath, localAttach, isDelLocal);
    }

    public void copyAttach(String srcFileName, String desFileName, String remoteFolderPath, boolean isDelSrc) throws Exception {
        worker.copyAttach(srcFileName, desFileName, remoteFolderPath, isDelSrc);
    }

    public void deletePath(String remotePath) throws Exception {
        worker.deletePath(remotePath);
    }

    public void deleteAttach(String remotePath, String remoteName) throws Exception {
        worker.deleteAttach(remotePath, remoteName);
    }

    public void downloadAttach(String remotePath, String remoteName, String fileName, HttpServletResponse response) throws Exception {
        worker.downloadAttach(remotePath, remoteName, fileName, response);
    }

    public void downloadAttach(String remotePath, String remoteName, String localPath) throws Exception {
        worker.downloadAttach(remotePath, remoteName, localPath);
    }

    public void downloadAttach(String remotePath, String remoteName, String localPath, String localName) throws Exception {
        worker.downloadAttach(remotePath, remoteName, localPath, localName);
    }

    public List<String> getAttachListFromFtp(final List<AttachInfo> attachlist, final String ownerType, final String localRoot) throws Exception {
        return worker.getAttachListFromFtp(attachlist, ownerType, localRoot);
    }

    public void makeRemoteDir(String path) throws Exception {
        worker.makeRemoteDir(path);
    }

    public Path getAttachPath(String remotePath, String remoteName) throws Exception {
        return worker.getAttachPath(remotePath, remoteName);
    }

    public void showImg(String remotePath, String remoteName, HttpServletResponse response) throws Exception {
        worker.showAttachImg(remotePath, remoteName, response);
    }
}
