package com.mortals.xhx.base.system.upload.service.impl;

import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.IUser;
import com.mortals.framework.util.StringUtils;
import com.mortals.xhx.base.system.idgenerator.service.IdgeneratorService;
import com.mortals.xhx.base.system.idgenerator.service.impl.IdgeneratorServiceImpl;
import com.mortals.xhx.base.system.upload.service.UploadService;
import com.mortals.xhx.common.code.UploadFileType;

import com.mortals.xhx.tools.uid.ISeqGeneratorService;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * <p>Title: 上传文件</p>
 * <p>Description: UploadServiceImpl service接口 </p>
 * <p>Copyright: Copyright &reg;  </p>
 * <p>Company: </p>
 *
 * @author
 * @version 1.0.0
 */
@Service("uploadfileService")
public class UploadServiceImpl implements UploadService {

    private static Log log = LogFactory.getLog(UploadServiceImpl.class);

    @Value("${upload.path}")
    private String filePath;

    @Autowired
    private ISeqGeneratorService seqGeneratorService;
    

    @Override
    public String saveFileUpload(MultipartFile tempFile, String prePath, IUser user) {
        if (tempFile == null || tempFile.getSize() == 0) {
            throw new AppException("没有要上传的文件！");
        }

        String fileName = tempFile.getOriginalFilename();
        String extension = FilenameUtils.getExtension(fileName);
        UploadFileType type = UploadFileType.getFileType(extension);
        if (type.getMaxSize() != 0 && tempFile.getSize() > type.getMaxSize()) {
            log.error("文件上传大小超过限制，当前文件---" + tempFile.getSize() + "，允许大小----------" + type.getMaxSize());
            throw new AppException("文件上传大小超过限制！");
        }
        if (null != fileName && fileName.length() > 50) {
            throw new AppException("文件名称过长，无法上传！");
        }

        String rootPath = this.filePath.endsWith("/") ? this.filePath : this.filePath + "/";
        String filePath = rootPath + (StringUtils.isEmpty(prePath) ? "" : prePath + "/");
        File pathDir = new File(filePath);
        if (!pathDir.exists()) {
            pathDir.mkdirs();
        }


        String newName = seqGeneratorService.nextOrderId(1024) + "." + extension;
        String filePathAll = filePath + newName;

        File uploadFile = new File(filePathAll);
        try {
            log.info("文件正在储存");
            tempFile.transferTo(uploadFile);
        } catch (Exception e) {
            throw new AppException(e.getMessage());
        }

        return (StringUtils.isEmpty(prePath) ? "" : prePath + "/") + newName;
    }

    @Override
    public String getFilePath(String path) {
        String filePath = this.filePath.endsWith("/") ? this.filePath : this.filePath + "/";
        return filePath + path;
    }

}