package com.xinelu.applet.controller.uploadfile;

import com.xinelu.applet.service.uploadfile.IUploadFileService;
import com.xinelu.common.constant.Constants;
import com.xinelu.common.core.domain.AjaxResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author ljh
 * @version 1.0
 * Create by 2022/11/2 10:38
 */
@RestController
@RequestMapping("/nurseApplet/uploadFile")
public class UploadFileController {

    @Resource
    private IUploadFileService uploadFileService;

    /**
     * 会员App头像图片上传
     *
     * @param patientId     会员主键id
     * @param multipartFile 文件
     * @return 结果
     */
    @PostMapping("/uploadHeadPictureUrl")
    public AjaxResult uploadHeadPictureUrl(@RequestParam("file") MultipartFile multipartFile, @RequestParam(value = "patientId") Long patientId) throws Exception {
        if (Objects.isNull(patientId)) {
            return AjaxResult.error("会员头像信息不能为空！");
        }
        if (Objects.isNull(multipartFile) || StringUtils.isBlank(multipartFile.getOriginalFilename())) {
            return AjaxResult.error("当前文件视频不存在，无法上传！");
        }
        if (multipartFile.getOriginalFilename().contains(Constants.EMPTY)) {
            return AjaxResult.error("当前视频文件名含有空格，请先去除空格在上传！");
        }
        return uploadFileService.uploadHeadPictureUrl(multipartFile, patientId);
    }

    /**
     * 护理员App确认任务完成图片上传
     *
     * @param orderNo       编号
     * @param multipartFile 文件
     * @return 结果
     */
    @PostMapping("/uploadPictureUrl")
    public AjaxResult uploadPictureUrl(@RequestParam("file") MultipartFile multipartFile, @RequestParam(value = "orderNo") String orderNo) throws Exception {
        if (StringUtils.isBlank(orderNo)) {
            return AjaxResult.error("订单信息不能为空!");
        }
        if (Objects.isNull(multipartFile) || StringUtils.isBlank(multipartFile.getOriginalFilename())) {
            return AjaxResult.error("当前文件不存在，无法上传！");
        }
        if (multipartFile.getOriginalFilename().contains(Constants.EMPTY)) {
            return AjaxResult.error("当前文件名含有空格，请先去除空格在上传！");
        }
        return uploadFileService.uploadPictureUrl(multipartFile, orderNo);
    }
}