package com.hzya.frame.sys.file.download.service.impl;

import com.hzya.frame.basedao.service.impl.BaseService;
import com.hzya.frame.sys.file.upload.entity.FileUploadDto;
import com.hzya.frame.web.exception.BaseSystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hzya.frame.sys.file.download.entity.FileDownloadEntity;
import com.hzya.frame.sys.file.download.service.IFileDownloadService;
import com.hzya.frame.sys.file.download.dao.IFileDownloadDao;

import java.io.File;

/**
 * @description: 系统文件下载  service
 * @tableName:   sys_file
 * @entityName:  FileDownloadEntity
 * @author: 	 gjh
 * @history:	 1.0
 */
@Service(value="sys_fileDownloadService")
public class  FileDownloadServiceImpl extends BaseService< FileDownloadEntity,Long> implements IFileDownloadService{

	protected IFileDownloadDao  filedownloadDao;

	@Autowired
	public void setFileDownloadDao(IFileDownloadDao dao) {

		this.filedownloadDao = dao;
		this.dao=dao;
	}

	@Override
	public FileDownloadEntity fileDownload(String fileSaveName)throws Exception {
		/** 如果fileSaveName直接为空，或者null直接抛出异常*/
		if(null == fileSaveName || "".equals(fileSaveName)){
            throw new BaseSystemException("请传入fileSaveName参数!");
		}

		/** 根据唯一名称获取文件信息*/
		FileDownloadEntity queryEntity = new FileDownloadEntity();
		queryEntity.setFilesavename(fileSaveName);
		/** 查询出文件信息*/
		FileDownloadEntity fileDownloadEntity =filedownloadDao.queryOne(queryEntity);
		/** 查询不到文件的时候，直接抛出异常*/
		if(fileDownloadEntity == null){
            throw new BaseSystemException("文件不存在!");
		}
		/** 拼接文件路径*/
		String filePath = fileDownloadEntity.getFilepath()+"/"+fileSaveName;
		/** 获取file文件*/
		File file = new File(filePath);
		/** 设置file*/
		fileDownloadEntity.setFile(file);

		return fileDownloadEntity;
	}

	@Override
	public FileDownloadEntity fileDownload(FileUploadDto fileUploadDto) {
		/** 如果fileSaveName直接为空，或者null直接抛出异常*/
		if(fileUploadDto == null || fileUploadDto.getId() == null || "".equals(fileUploadDto.getId())){
			throw new BaseSystemException("请传入参数!");
		}

		/** 根据唯一名称获取文件信息*/
		FileDownloadEntity queryEntity = new FileDownloadEntity();
		queryEntity.setId(fileUploadDto.getId());
		queryEntity.setSts("Y");
		/** 查询出文件信息*/
		FileDownloadEntity fileDownloadEntity =filedownloadDao.queryOne(queryEntity);
		/** 查询不到文件的时候，直接抛出异常*/
		if(fileDownloadEntity == null){
            throw new BaseSystemException("文件不存在!");
		}
		/** 拼接文件路径*/
		String filePath = fileDownloadEntity.getFilepath();
		/** 获取file文件*/
		File file = new File(filePath);
		/** 设置file*/
		fileDownloadEntity.setFile(file);
		return fileDownloadEntity;
	}
}
