package com.jeeplus.common.bos;

import com.jeeplus.common.config.FileGlobal;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.ossutil.OssClient;
import com.jeeplus.common.utils.FileUtils;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.security.SystemAuthorizingRealm;
import com.jeeplus.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "${adminPath}/bos/")
public class BosController {
    private final String url = "http://jsjyoa.oss-cn-hangzhou.aliyuncs.com/";
    private final  String upLoadUrl = Global.getConfig("upLoadUrl");
    @RequestMapping("upload")
    @ResponseBody
    public Map upload(HttpServletRequest request, MultipartFile file) throws IOException {
        Map map = new HashMap();
        /*AjaxJson webupload = this.webupload(request, file);
        map.put("msg",webupload.getMsg());
        map.put("code","1");
        map.put("url",webupload.getBody().get("url"));*/
        String storeAs = request.getParameter("storeAs");
        try {
            String path = storeAs;
            OssClient ossClient = new OssClient();
            InputStream inputStream = file.getInputStream();
            String st = ossClient.uploadFileToOss(inputStream,path);
            System.out.println(st);
            map.put("msg","上传成功");
            map.put("code","1");
            map.put("url",url+path);
        }catch (Exception e){
            map.put("msg","上传失败");
            map.put("code","0");
        }
        return map;
    }

    @RequestMapping("uploadPic")
    @ResponseBody
    public Map uploadPic(MultipartFile uploadFile){
        Map map = new HashMap();
        try {
            String name = uploadFile.getOriginalFilename();
            String path = "picture/"+System.currentTimeMillis()+"/"+name;
            OssClient ossClient = new OssClient();
            InputStream inputStream = uploadFile.getInputStream();
            ossClient.uploadFileToOss(inputStream,path);
            map.put("msg","上传成功");
            map.put("code","1");
            map.put("url",url+path);
        }catch (Exception e){
            map.put("msg","上传失败");
            map.put("code","0");
        }
        return map;
    }

    @RequestMapping("toUpload")
    @ResponseBody
    public Map toUpload(MultipartFile uploadFile) {
        Map map = new HashMap();
        try {
            String name = uploadFile.getOriginalFilename();
            String path = upLoadUrl+System.currentTimeMillis()+"/"+name;
            File destFile = new File(path);
            if (!destFile.exists()){
                destFile.mkdirs();
            }
            uploadFile.transferTo(destFile);
            map.put("msg","上传成功");
            map.put("code","1");
            map.put("url",url+path);
        }catch (Exception e){
            map.put("msg","上传失败");
            map.put("code","0");
        }
        return map;
    }

    /**
     * 上传文件到本地
     * @return
     * @throws IOException
     * @throws IllegalStateException
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "webupload/upload")
    @ResponseBody
    public AjaxJson webupload(HttpServletRequest request, MultipartFile file) throws IllegalStateException, IOException {
        AjaxJson j = new AjaxJson();

        User currentUser = UserUtils.getUser();
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH )+1;
        SystemAuthorizingRealm.Principal principal = (SystemAuthorizingRealm.Principal) UserUtils.getPrincipal();
        String fileUrl = FileGlobal.getAttachmentUrl2()+year+"/"+month+"/"+System.currentTimeMillis()+"/";
        String fileDir = FileGlobal.getAttachmentDir2()+year+"/"+month+"/"+System.currentTimeMillis()+"/";
        // 判断文件是否为空
        if (!file.isEmpty()) {
            // 文件保存路径
            // 转存文件
            FileUtils.createDirectory(fileDir);
            String name = file.getOriginalFilename();
            String filePath = fileDir +  name;
            //File newFile = FileUtils.getAvailableFile(filePath,0);
            File newFile = new File(filePath);
            file.transferTo(newFile);
            j.put("id", newFile.getAbsolutePath());
            j.put("url", fileUrl+ newFile.getName());
        }
        return j;
    }

}
