package com.ssj.sys.controller;

import com.ssj.bean.common.files.domain.FileInfo;
import com.ssj.framework.basic.utils.StringUtils;
import com.ssj.framework.core.common.controller.BaseController;
import com.ssj.framework.core.util.ImageTools;
import com.ssj.framework.core.util.ResponseConstant;
import com.ssj.framework.core.util.ResponseEntity;
import com.ssj.framework.files.service.FileInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



@Controller
@RequestMapping("/file")
public class FileController extends BaseController {
	@Autowired
	private FileInfoService fileInfoService;
	
	
	 /*
     * 附件上传
     */
    @RequestMapping("/uploads")
    @ResponseBody
    public ResponseEntity uploadfile(HttpServletRequest request, HttpServletResponse response) throws Exception{
    	ResponseEntity responseEntity = new ResponseEntity();
    	MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    	 Map<String, MultipartFile> fileList = multipartRequest.getFileMap();
        if (fileList !=null && fileList.size() > 0) {
            //文件信息入库
            List<FileInfo> fileInfos = fileInfoService.uploadFilePicture(loginName(),fileList);
            if (fileList.size() == fileInfos.size()) {
                Map<String, Object> data = new HashMap<String, Object>();
                data.put("pics", fileInfos);
                responseEntity.success(data, "上传图片成功 ");
            } else {
                responseEntity.failure(ResponseConstant.CODE_000, "上传图片失败");
            }

        } else {
            responseEntity.failure(ResponseConstant.CODE_000, "上传图片失败");
        }

        return responseEntity;
    }


    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity upload4Base64( @RequestParam(name = "fileName") String fileName, @RequestParam(name = "pic") String pic) throws Exception {
        ResponseEntity responseEntity = new ResponseEntity();
        if (StringUtils.isBlank(pic)) {
            responseEntity.failure(ResponseConstant.CODE_000, "上传图片失败");
            return responseEntity;
        }
        String savePath = ImageTools.savePic4Base64(pic);
        if (StringUtils.isNotBlank(savePath)) {
        	FileInfo fileInfo = fileInfoService.saveFileInfo(loginName(),savePath, fileName);
            if (fileInfo != null && StringUtils.isNotBlank(fileInfo.getId())) {
            	Map<String, Object> data = new HashMap<String, Object>();
            	 data.put("pics", fileInfo);
                responseEntity.success(data,"上传图片成功 ");
            } else {
                responseEntity.failure(ResponseConstant.CODE_000, "上传图片失败");
            }
        } else {
            responseEntity.failure(ResponseConstant.CODE_000, "上传图片失败");
        }

        return responseEntity;
    }

}
