package com.xinelu.manage.service.signpatientinformed.impl;

import com.xinelu.common.config.SystemBusinessConfig;
import com.xinelu.common.constant.FileUploadPathConstants;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.file.FileUploadUtils;
import com.xinelu.common.utils.file.ImageResize;
import com.xinelu.common.utils.file.MimeTypeUtils;
import com.xinelu.manage.service.signpatientinformed.IFileUploadService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @description: 文件上传Service业务层处理
 * @author: haown
 * @create: 2024-03-01 10:36
 **/
@Service
public class FileUplooadServiceImpl implements IFileUploadService {

	@Resource
	private SystemBusinessConfig systemBusinessConfig;

	@Override public AjaxResult uploadFile(MultipartFile multipartFile, String type) throws Exception {
		//获取不同的文件路径
		String uploadPathUrl = "";
		switch (type) {
			// 签约知情书
			case FileUploadPathConstants.SIGN_INFORMED_TYPE:
				uploadPathUrl = SystemBusinessConfig.getProfile() + systemBusinessConfig.getSignInformedFileUrl();
				break;
			//宣教库封面路径上传
			case FileUploadPathConstants.PROPAGANDA_COVER_TYPE:
				uploadPathUrl = SystemBusinessConfig.getProfile() + systemBusinessConfig.getPropagandaCoverUrl();
				break;
			//宣教库视频路径上传
			case FileUploadPathConstants.COMMON_VIDEO_TYPE:
				uploadPathUrl = SystemBusinessConfig.getProfile() + systemBusinessConfig.getCommonVideoUrl();
				break;
			//素材库封面图片路径上传
			case FileUploadPathConstants.MATERIALS_COVER_TYPE:
				uploadPathUrl = SystemBusinessConfig.getProfile() + systemBusinessConfig.getMaterialsCoverUrl();
				break;
			//素材库视频路径上传
			case FileUploadPathConstants.MATERIALS_VIDEO_TYPE:
				uploadPathUrl = SystemBusinessConfig.getProfile() + systemBusinessConfig.getMaterialsVideoUrl();
				break;
			default:
				break;
		}
		if (StringUtils.isBlank(uploadPathUrl)) {
			return AjaxResult.success();
		}
		//上传
		String pictureName = "";
		if (StringUtils.equals(type, FileUploadPathConstants.MATERIALS_VIDEO_TYPE) || StringUtils.equals(type, FileUploadPathConstants.COMMON_VIDEO_TYPE)) {
			pictureName = FileUploadUtils.upload(uploadPathUrl, multipartFile, MimeTypeUtils.VIDEO_EXTENSION);
		} else {
			pictureName = FileUploadUtils.upload(uploadPathUrl, multipartFile, MimeTypeUtils.IMAGE_EXTENSION);
		}
		if (StringUtils.isBlank(pictureName)) {
			throw new ServiceException("文件上传失败，请联系管理员！");
		}
        if (type.equals(FileUploadPathConstants.MATERIALS_COVER_TYPE)){
            //裁剪图片
            ImageResize.scaledDown(pictureName);
        }
		AjaxResult ajax = AjaxResult.success("上传成功！");
		ajax.put("imgUrl", pictureName);
		return ajax;
	}
}
