package com.happy.action;

import com.happy.Model.AdminManager;
import com.happy.Model.FileInfo;
import com.happy.Until.ResUtil;
import com.happy.Until.ResponseUtil;
import com.happy.Until.UUIDUtil;
import com.happy.service.FileService;
import com.opensymphony.xwork2.ActionSupport;
import net.sf.json.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.UUID;

public class uploadAction extends ActionSupport implements ServletRequestAware {

    private HttpServletRequest request;
    public HttpServletResponse response;

    private File myFile;
    private String myFileFileName;
    public String url_name;
    public String url;

    public String linkId;

    @Resource
    public FileService fileService;

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    public File getMyFile() {
        return myFile;
    }

    public void setMyFile(File myFile) {
        this.myFile = myFile;
    }

    public String getMyFileFileName() {
        return myFileFileName;
    }

    public void setMyFileFileName(String myFileFileName) {
        this.myFileFileName = myFileFileName;
    }

    public String getUrl_name() {
        return url_name;
    }

    public void setUrl_name(String url_name) {
        this.url_name = url_name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    // 上传图片
    public String himage(){
        JSONObject resultJson = new JSONObject();

        if(1==1){
            resultJson.put("code", 500);
            resultJson.put("message", "该上传接口停用！");
            ResponseUtil.writeJson(ServletActionContext.getResponse(),
                    resultJson.toString());
            return null;
        }

        String filepath=request.getServletContext().getRealPath("/download/");
        if (myFile==null){
            resultJson.put("code", 500);
            resultJson.put("message", "请上传图片！");
            ResponseUtil.writeJson(ServletActionContext.getResponse(),
                    resultJson.toString());
            return null;
        }
        try{
            if (!myFileFileName.endsWith("jpg") && !myFileFileName.endsWith("png")){
                resultJson.put("code", 500);
                resultJson.put("message", "请上传jpg或png格式文件！");
                ResponseUtil.writeJson(ServletActionContext.getResponse(),
                        resultJson.toString());
                return null;
            }
            Long uuid = UUIDUtil.generateID();

            File destFile = new File(filepath, uuid+myFileFileName);
            // 获取图片大小
            long size = myFile.length();
            // 获取图片尺寸
            BufferedImage sourceImg = ImageIO.read(new FileInputStream(myFile));
//            System.out.println(String.format("%.1f",myFile.length()/1024.0));// 源图大小
//            System.out.println(sourceImg.getWidth()); // 源图宽度
//            System.out.println(sourceImg.getHeight()); // 源图高度
            // 保存图片到指定位置
            FileUtils.copyFile(myFile, destFile);
            // 返回图片url路径
            String url = "https://chtech.ncjti.edu.cn/hotelReservation" + request.getSession().getServletContext().getContextPath();
            String fileurl = url + "/download/" + uuid + myFileFileName;
            FileInfo fileInfo = new FileInfo();
            fileInfo.setName(myFileFileName);
            fileInfo.setUrl(fileurl);
            fileInfo.setId(UUID.randomUUID().toString());
            /* 保存入库  */
            int m = fileService.insertFile(fileInfo);
            if(m>0){
                resultJson.put("code", 200);
                resultJson.put("message", "上传成功");
                resultJson.put("data", fileInfo);
                ResponseUtil.writeJson(ServletActionContext.getResponse(),resultJson.toString());
            }else{
                resultJson.put("code", 502);
                resultJson.put("message", "上传失败");
                ResponseUtil.writeJson(ServletActionContext.getResponse(),resultJson.toString());
            }
        }catch (Exception e){
            resultJson.put("code", 502);
            resultJson.put("message", "操作异常");
            ResponseUtil.writeJson(ServletActionContext.getResponse(),
                    resultJson.toString());
            return null;
        }
        resultJson.put("code", 500);
        resultJson.put("message", "上传失败");
        ResponseUtil.writeJson(ServletActionContext.getResponse(),
                resultJson.toString());
        return null;
    }

    /**
     * 描述:根据关联Id查询附件
     * @return
     */
    public String getByLinkId(){
        JSONObject resultJson = new JSONObject();
        if (linkId == null) {
            resultJson.put("message", "请传入关联id");
            resultJson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        List<FileInfo> fileInfoList = fileService.queryList("and link_id ='"+linkId+"'");
        if (fileInfoList != null) {
            resultJson.put("message", "查询成功");
            resultJson.put("code", 200);
            resultJson.put("data", fileInfoList);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }else{
            resultJson.put("message", "未查到存在关联附件");
            resultJson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
    }

}
