package com.xinelu.applet.service.uploadfile.Impl;

import com.xinelu.applet.service.uploadfile.IUploadFileService;
import com.xinelu.common.config.XinELuConfig;
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.MimeTypeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @Description 头像上传
 * @Author ZH
 * @Date 2022-10-28
 */
@Service
public class UploadFileServiceImpl implements IUploadFileService {

    @Resource
    private XinELuConfig xinELuConfig;

    /**
     * 会员App头像图片上传
     *
     * @param patientId     会员主键id
     * @param multipartFile 文件
     * @return 结果
     */
    @Override
    public AjaxResult uploadHeadPictureUrl(MultipartFile multipartFile, Long patientId) throws Exception {
        //获取路径名称
        String uploadPathUrl = XinELuConfig.getProfile() + xinELuConfig.getHeadPictureUrl() + "/" + patientId;
        //上传图片
        String pictureName = FileUploadUtils.uploadPictureUrlPath(uploadPathUrl, multipartFile, MimeTypeUtils.IMAGE_EXTENSION);
        if (StringUtils.isBlank(pictureName)) {
            throw new ServiceException("头像上传失败，请联系管理员！");
        }
        //获取返回值
        AjaxResult ajax = AjaxResult.success("上传成功！");
        ajax.put("imgUrl", pictureName);
        return ajax;
    }

    /**
     * 护理员App确认任务完成图片上传
     *
     * @param orderNo       订单编号
     * @param multipartFile 文件
     * @return 结果
     * @throws Exception 异常信息
     **/
    @Override
    public AjaxResult uploadPictureUrl(MultipartFile multipartFile, String orderNo) throws Exception {
        //获取路径名称
        String uploadPathUrl = XinELuConfig.getProfile() + xinELuConfig.getAppointmentOrderDetailsUrl() + "/" + orderNo;
        //上传图片
        String pictureName = FileUploadUtils.uploadPictureUrlPath(uploadPathUrl, multipartFile, MimeTypeUtils.IMAGE_EXTENSION);
        if (StringUtils.isBlank(pictureName)) {
            throw new ServiceException("图片上传失败，请联系管理员！");
        }
        //获取返回值
        AjaxResult ajax = AjaxResult.success("上传成功！");
        ajax.put("imgUrl", pictureName);
        return ajax;
    }
}