package com.cool.bang.modules.system.controller;

import com.cool.bang.common.base.BaseController;
import com.cool.bang.common.constant.ControllerConstant;
import com.cool.bang.common.web.response.Result;
import com.cool.bang.modules.system.domain.po.Resource;
import com.cool.bang.modules.upload.service.IUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * system upload controller
 *
 * @author PeiZhi Guo
 * 2022-6-7 9:54
 */
@RestController
@RequestMapping(ControllerConstant.SYSTEM + "upload/v1")
public class UploadController extends BaseController {


    private IUploadService uploadService;

    @Autowired
    public UploadController(IUploadService uploadService) {
        this.uploadService = uploadService;
    }

    @PostMapping
    public Result upload(MultipartFile file, Resource resource) {
        Map<String, Object> map = new HashMap<>();
        try {
            String path = uploadService.upload(file,"avatar");
            map.put("path", path);
            map.put("type", resource.getType());
            map.put("wjlx", resource.getWjlx());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure("服务器上传异常：" + e.getMessage());
        }
        return success(map);
    }

}
