/**
 * Copyright (C), 2015-2019
 * Author:   HJL
 * Date:     2019/7/10 17:09
 * Description:
 */
package com.emr.controller;

import com.emr.service.Archive_DetailService;
import org.apache.log4j.Logger;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
@RequestMapping(value = "/Upload")
public class UploadFilesController {

    @Autowired
    private Archive_DetailService archiveDetailService;

    private Logger logger = Logger.getLogger(UploadFilesController.class);


    /**
     * app申请打印图片
     *
     * @param
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
    public void uploadImg(HttpServletResponse resp, HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
        resp.setContentType("text/json");
        resp.setCharacterEncoding("utf-8");

        String masterID = request.getParameter("masterID");
        String assortID = request.getParameter("assortID");
        PrintWriter pw = null;
        pw = resp.getWriter();
        if (!file.isEmpty()) {
            logger.info("成功获取照片");
            String fileName = file.getOriginalFilename();
            String path = null;
            String type = null;
            type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
            logger.info("图片初始名称为：" + fileName + " 类型为：" + type);
            if (type != null) {
                if ("PDF".equals(type.toUpperCase())) {
                    // 项目在容器中实际发布运行的根路径
                    String realPath = request.getSession().getServletContext().getRealPath("/");
                    // 自定义的文件名称
                    String trueFileName = String.valueOf(System.currentTimeMillis()) + fileName;
                    // 设置存放图片文件的路径
                    path = realPath + "/static/img/uploads/" + trueFileName;
                    logger.info("存放图片文件的路径:" + path);
                    file.transferTo(new File(path));
                    logger.info("文件成功上传到指定目录下");
                    //2、根据申请单的标志查询出父级id
//                    int parentId=emrPictureService.selectByTypeflag("applyImg").get(0).getId();
//                    //2、将其路径及文件名类型、申请id以及参数保存图片相关信息到图片表
//                    Emr_Picture obj=new Emr_Picture();
//                    obj.setRelationId(seqId);
//                    obj.setMaxPicture("/static/img/uploads/" + trueFileName);
//                    obj.setPictureSort(seqId);
//                    obj.setEffective(1);
//                    obj.setParentId(parentId);
//                    obj.setCreater(userId);
//                    obj.setUpdater(userId);
//                    SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                    String nowTime = format1.format(new Date());
//                    obj.setCreateTime(nowTime);
//                    obj.setUpdateTime(nowTime);
//                    int bol= emrPictureService.insert(obj);
//                    JSONObject json = JSONObject.fromObject(obj);

//                    if(bol==1){
//                        pw.print(json.toString());
//                    }else{
//                        pw.print("保存图片信息失败");
//                    }
                } else {
                    logger.info("不是PDF文件类型,请按要求重新上传");
                    pw.print("文件类型有误,请重新上传");
                }
            } else {
                logger.info("文件类型为空");
                pw.print("文件类型为空");
            }
        } else {
            logger.info("没有找到相对应的文件");
            pw.print("没有找到相对应的文件");
        }

        pw.flush();

    }

    @ResponseBody
    @RequestMapping("upload")
    public String upload(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
        //System.out.println("执行upload");
        request.setCharacterEncoding("UTF-8");
        logger.info("执行图片上传");
        String userId = request.getParameter("userId");
        logger.info("userId:" + userId);
        if (!file.isEmpty()) {
            logger.info("成功获取照片");
            String fileName = file.getOriginalFilename();
            String path = null;
            String type = null;
            type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
            logger.info("图片初始名称为：" + fileName + " 类型为：" + type);
            if (type != null) {
                if ("GIF".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase()) || "JPG".equals(type.toUpperCase())) {
                    // 项目在容器中实际发布运行的根路径
                    String realPath = request.getSession().getServletContext().getRealPath("/");
                    // 自定义的文件名称
                    String trueFileName = String.valueOf(System.currentTimeMillis()) + fileName;
                    // 设置存放图片文件的路径
                    path = realPath + "/uploads/" + trueFileName;
                    logger.info("存放图片文件的路径:" + path);
                    file.transferTo(new File(path));
                    logger.info("文件成功上传到指定目录下");
                } else {
                    logger.info("不是我们想要的文件类型,请按要求重新上传");
                    return "error";
                }
            } else {
                logger.info("文件类型为空");
                return "error";
            }
        } else {
            logger.info("没有找到相对应的文件");
            return "error";
        }
        return "success";
    }
}
 

