package com.jpsoft.shinestar.modules.common.controller;

import com.jpsoft.shinestar.config.OSSConfig;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.common.utils.OSSUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class fileUploadController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private OSSConfig ossConfig;

    @PostMapping("uploadPicture")
    @ApiOperation(value="人员照片上传")
    @ApiImplicitParams({
            @ApiImplicitParam(name="subFolder",value = "路径",required = true,paramType = "form"),
            @ApiImplicitParam(name = "photoFile",value = "员工照片", required = true,paramType="form", dataType = "__file")
    })
    public MessageResult<String> uploadPicture(String subFolder, MultipartFile photoFile){
        MessageResult<String> messageResult = new MessageResult<>();

        try {
            String retFileUrl = OSSUtil.upload(ossConfig,"/" + subFolder,photoFile.getOriginalFilename(),photoFile.getInputStream());

            messageResult.setResult(true);
            messageResult.setData(retFileUrl);
            messageResult.setCode(200);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);

            messageResult.setResult(false);
            messageResult.setMessage(e.getMessage());
        }

        return messageResult;
    }
}
