package com.nmgs.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nmgs.config.ResultData;
import com.nmgs.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.springframework.scheduling.annotation.Async;
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 java.io.File;
import java.util.Base64;
import java.util.Properties;

@Controller
@RequestMapping(value = "/fileUpload")
@Api(tags = "附件上传")
public class FileUploadController {
    @RequestMapping(value = "/uploadAllFile", method = {RequestMethod.POST})
    @ApiOperation(value = "上传附件")
    @ResponseBody
    public ResultData<String> uploadAllFile(@RequestParam("filedata") MultipartFile filedata, @RequestParam("fileType") String fileType, @RequestParam("uuid") String uuid) {
        String fileName = "";
        try {
            LogUtil.WriteLog_Info("附件上传接口执行开始===========" + PubTools.getCurrentDate(), "FileUploadController");
            Properties props = System.getProperties();
            String os = props.getProperty("os.name").toLowerCase();
            String fileAddressLocal = PathUtil.uploadPath + "/" + PubTools.getCurrentDateYMD();
            if (os.startsWith("win")) {
                //    fileAddressLocal = PropertiesUtil.getValue("fileAddressWin");
                fileAddressLocal = fileAddressLocal.replace("/", "\\");
            } else {
                //   fileAddressLocal = PropertiesUtil.getValue("fileAddressLinux");
            }
            LogUtil.WriteLog_Info("附件上传接口====fileAddressLocal====" + fileAddressLocal, "FileUploadController");
            if (!PubTools.isNull(fileAddressLocal)) {
                File filePath = new File(fileAddressLocal);
                if (!filePath.exists()) {
                    filePath.mkdir();
                }
                if ("NUMBERPIC".equals(fileType)) {//人员身份证
                    fileName = uuid + "NUMPIC" + ".jpg";
                } else if ("LICENSEPIC".equals(fileType)) {//车辆驾驶证
                    fileName = uuid + "-LICPIC" + ".jpg";
                } else if ("PERMITPIC".equals(fileType)) {//车辆行驶证
                    fileName = uuid + "-PERPIC" + ".jpg";
                } else if ("VEHICLEPIC".equals(fileType)) {//车辆照片
                    fileName = uuid + "-VEHPIC" + ".jpg";
                } else {
                    return ResultData.fail("上传附件失败:附件类型不匹配");
                }

               /* File dest = new File(PropertiesUtil.getValue("fileTemp"), fileName);
                System.out.println(filedata.getOriginalFilename());
                // 使用下面的jar包
                FileUtils.copyInputStreamToFile(filedata.getInputStream(), dest);
                File fileTemp = new File(PropertiesUtil.getValue("fileTemp") + "/" + fileName);
                if (!fileTemp.exists()) {
                    return ResultData.success(2, "附件上传失败");
                }
                String imageBase64 = ImageUtils.getImgFileToBase64(PropertiesUtil.getValue("fileTemp") + "/" + fileName);
                if (fileTemp.exists()) {
                    fileTemp.delete();
                }*/
                byte[] bytes = filedata.getBytes();
                String imageBase64 = ImageUtils.byteToBase64(bytes);
                if (PubTools.isNull(imageBase64)) {
                    return ResultData.success(2, "附件解析失败");
                }
                String addWaterImage = ImageUtils.addImageWaterMark(imageBase64, "仅用于内蒙古交通货运车辆积分会员认证", fileName);
                if (!PubTools.isNull(addWaterImage)) {
                    /*// ImageUtils.delFile(fileName, false);
                    byte[] imageBytes = Base64.getDecoder().decode(addWaterImage);
                    // 创建文件
                    File file = new File(fileAddressLocal, fileName); // 指定上传位置
                    if (file.exists()) {
                        file.delete();
                    }
                    // 保存文件
                    LogUtil.WriteLog_Info("附件开始上传到服务器===========" + PubTools.getCurrentDate(), "FileUploadController");
                    FileUtils.writeByteArrayToFile(file, imageBytes);*/
                    LogUtil.WriteLog_Info("附件上传到服务器结束===========" + PubTools.getCurrentDate(), "FileUploadController");
                } else if ("-9999".equals(addWaterImage)) {
                    throw new Exception("附件添加水印失败");
                }
            }
            LogUtil.WriteLog_Info("附件上传接口执行结束===========" + PubTools.getCurrentDate(), "FileUploadController");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.fail("上传附件失败:" + e.getMessage());
        }
        return ResultData.success("上传成功", fileName, 1);
    }

    @RequestMapping(value = "/uploadAllFileTest", method = {RequestMethod.POST})
    @ApiOperation(value = "上传附件测试")
    @ResponseBody
    public ResultData<String> uploadAllFileTest() {
        String fileName = "测试返回结果";
        return ResultData.success("上传成功", fileName, 1);
    }
}
