/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.API.iim;

import com.alibaba.fastjson.JSONObject;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.oss.OSSClientUtil;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.IdGen;
import com.jeeplus.common.utils.SmackUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.iim.entity.*;
import com.jeeplus.modules.iim.service.LayGroupService;
import com.jeeplus.modules.sys.dao.UserDao;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.service.OfficeService;
import com.jeeplus.modules.sys.service.SystemService;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.sysuseroffice.entity.Useroffice;
import com.jeeplus.modules.sysuseroffice.service.UserofficeService;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 联系人Controller
 * 
 * @author jeeplus(seven修改)
 * @version 2016-6-23
 */
//@Controller
//@RequestMapping(value = "${frontPath}/iim/contact")
public class ImContactController extends BaseController {

//	@ResponseBody
//	@RequestMapping(value = { "uploadImage", "uploadFile" })
	public String uploadImage(HttpServletResponse response,MultipartHttpServletRequest multiRequest)
			throws IllegalStateException, IOException {
		String filepath = "";
		String content = null;
		LayFileJsonData data = new LayFileJsonData();
		String contentType = "";
		// 判断文件是否为空
        Iterator<String> fileNames = multiRequest.getFileNames();
        while(fileNames.hasNext()) {
            //取得上传文件
            MultipartFile file = multiRequest.getFile(fileNames.next());
            if (!file.isEmpty()) {
                contentType = file.getContentType();
                // 文件保存路径
                String realPath ="messages/"+ DateUtils.formatDate(new Date())+"/"+ UserUtils.getPrincipal() + "/";
                OSSClientUtil ossUtil = new OSSClientUtil();
                String newName = System.currentTimeMillis()+"-"+file.getOriginalFilename();

                ossUtil.uploadFile2OSS(file.getInputStream(),realPath,newName);
                filepath = Global.getAliDownloadUrl() + "/" + realPath + newName;

                data.setName(file.getOriginalFilename());
                data.setSrc(filepath);
            }
        }

		ObjectMapper mapper = new ObjectMapper();
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("code", "0");
		map.put("data", data);

		content = mapper.writeValueAsString(map);
 		return content;
	}

}