package cc.smtweb.system.bpm.spring.dao;

import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.system.bpm.spring.entity.AttachPathPO;
import cc.smtweb.system.bpm.util.FilePathGenerator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.util.UriEncoder;

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

@Service
public class SysAttachDao {
    @Autowired
    private FilePathGenerator filePathGenerator;

    @Autowired
    private DbEngine dbEngine;

    /**
     * 获取文件本地文件路径
     *
     * @param filePath 相对路径
     * @return 本地文件全路径
     */
    public String getDiskPath(String filePath) {
        return filePathGenerator.getFileDiskPath(filePath);
    }

    /**
     * 获取访问文件的URL地址
     *
     * @param filePath 文件相对路径
     * @return 文件URL地址
     */
    public String getFileUrl(String filePath) {
        return filePathGenerator.getFileUrl(filePath);
    }

    /**
     * 获取访问文件的URL地址
     *
     * @param filePath 文件相对路径
     * @param filePath 文件名
     * @return 文件URL地址
     */
    public String getFileUrl(String filePath, String fileName) {
        return "/fs/download?path=" + UriEncoder.encode(filePath) + "&name=" + UriEncoder.encode(fileName);
    }

    public AttachPathPO get(Long id) {
        if (id != null) {
            return dbEngine.queryEntity("select attach_id, attach_name, attach_path, attach_content_type, attach_size, attach_create_time from sw_user.sys_attach where attach_id=?",
                    AttachPathPO.class, id);
        }

        return null;
    }

    // 删除文件记录和文件
    public void remove(Long fileId) {
//    if (id != null) {
//      return dbEngine.queryEntity("select attach_id, attach_name, attach_path, attach_content_type, attach_size, attach_create_time from sw_user.sys_attach where attach_id=?",
//              AttachPathPO.class, id);
//    }
//
//    return null;
    }

    // 删除文件
    public void remove(String filePath) {
//    if (id != null) {
//      return dbEngine.queryEntity("select attach_id, attach_name, attach_path, attach_content_type, attach_size, attach_create_time from sw_user.sys_attach where attach_id=?",
//              AttachPathPO.class, id);
//    }
//
//    return null;
    }

    public List<AttachPathPO> list(Long[] ids) {
        if (ids != null && ids.length > 0) {
            return dbEngine.query("select attach_id, attach_name, attach_path, attach_content_type, attach_size, attach_create_time from sw_user.sys_attach where attach_id in( "
                            + StringUtils.join(ids, ",") + ")",
                    AttachPathPO.class);
        }

        return null;
    }

    public Map<Long, AttachPathPO> map(Long[] ids) {
        List<AttachPathPO> list = list(ids);
        if (list != null && !list.isEmpty()) {
            Map<Long, AttachPathPO> map = new HashMap<>(list.size());
            list.forEach((item) -> map.put(item.getAttachId(), item));

            return map;
        }

        return null;
    }

    // 保持文件，删除临时文件记录，避免被定时删除
    public void retain(String filePath) {

    }

    // 保持文件，删除临时文件记录，避免被定时删除
    public void retain(Long fileId) {

    }
}
