package com.gz.service.archive.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gz.config.FileUploadConfig;
import com.gz.core.exception.BusinessException;
import com.gz.core.exception.CustomExceptionEnum;
import com.gz.dto.archive.ArchiveDTO;
import com.gz.dto.system.ArchiveFileDTO;
import com.gz.mapper.archive.ArchiveFileMapper;
import com.gz.mapper.archive.ArchiveMapper;
import com.gz.service.archive.ArchiveFileService;
import com.gz.utils.JwtUtils;
import com.gz.vo.archive.SearchArchiveFileVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

/**
 * @author LiuchangLan
 * @date 2020/9/9 11:24
 */
@Service
@Slf4j
public class ArchiveFileServiceImpl implements ArchiveFileService {
    @Resource
    private ArchiveFileMapper archiveFileMapper;

    @Resource
    private ArchiveMapper archiveMapper;


    @Override
    public ArchiveFileDTO upload(MultipartFile file, Integer archiveId) throws BusinessException, IOException {

        if (file.isEmpty()) {
            throw new BusinessException(CustomExceptionEnum.File_NOT_EXISTS);
        }
        // 上传文件名称
        String fileName = file.getOriginalFilename();
        // 文件格式类型 大写
        String fileType = fileName.substring(fileName.lastIndexOf(".") + 1).toUpperCase();
        // 查询需要上传的档案信息
        ArchiveDTO archiveDTO = archiveMapper.selectByPrimaryKey(archiveId);
        // 获取服务器文件存储路径
        String serverFileUploadPath = "";
        // 保存到服务器的文件名
        String fn = "";
        switch (fileType) {
            case "PDF":
                serverFileUploadPath = String.format(FileUploadConfig.ARCHIVE_UPLOAD_PATH_PDF_FOMAT, archiveDTO.getGdnd(), archiveDTO.getQzh(), archiveDTO.getBgqx(), archiveDTO.getGdnd(), fileType);
                // pdf文件名为档号
                fn = String.format("%s.%s", archiveDTO.getDh(), fileType.toLowerCase());
                break;
            case "TIF":
                // tif文件名为页码 无法通过后台生成 读取上传文件的名称
                fn = fileName;
                serverFileUploadPath = String.format(FileUploadConfig.ARCHIVE_UPLOAD_PATH_PDF_FOMAT, archiveDTO.getGdnd(), archiveDTO.getQzh(), archiveDTO.getBgqx(), archiveDTO.getGdnd(), fileType, archiveDTO.getDh());
                break;
            default:
                // 其他文件直接抛错
                throw new BusinessException(CustomExceptionEnum.UPLOAD_ARCHIVE_FILE_TYPE_ERROR);
        }
        // 上传路径
        File serverFilePath = new File(FileUploadConfig.ARCHIVE_FILE_ROOT_DIRECTORY + serverFileUploadPath);
        // 判断上传路径是否存在
        if (!serverFilePath.exists()) {
            serverFilePath.mkdirs();
        }
        // 创建文件
        File dest = new File(serverFilePath.getPath() + "/" + fn);
        // 上传
        file.transferTo(dest);
        // 数据库插入
        ArchiveFileDTO archiveFileDTO = new ArchiveFileDTO();
        archiveFileDTO.setArchiveId(archiveId);
        archiveFileDTO.setOriginalFileName(fileName);
        archiveFileDTO.setFileName(fn);
        archiveFileDTO.setFileSize(file.getSize());
        archiveFileDTO.setFilePath(serverFileUploadPath);
        archiveFileDTO.setCreated(JwtUtils.getCurrentUserJwtPayload().getId());
        archiveFileMapper.insertSelective(archiveFileDTO);

        return archiveFileDTO;

    }

    @Override
    public Integer delete(Integer id) {
        ArchiveFileDTO archiveFileDTO = archiveFileMapper.selectByPrimaryKey(id);
        // 文件路径
        String filePath = String.format("%s%s/%s", FileUploadConfig.ARCHIVE_FILE_ROOT_DIRECTORY, archiveFileDTO.getFilePath(), archiveFileDTO.getFileName());
        // 删除该文件
        File file = new File(filePath);
        if (file.exists()) {
            log.info("删除文件:{}",filePath);
            file.delete();
        }
        return archiveFileMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<ArchiveFileDTO> selectByPage(SearchArchiveFileVO vo) {
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        ArchiveFileDTO archiveFileDTO = BeanUtil.copyProperties(vo, ArchiveFileDTO.class);
        return new PageInfo<>(archiveFileMapper.select(archiveFileDTO));
    }
}
