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.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Base64;
import java.util.Properties;

@Controller
@RequestMapping(value = "/fileSolve")
@Api(tags = "附件处理")
@Async("integralManagerHttpThreadPool")
public class FileController {
    @RequestMapping(value = "/delFile", method = {RequestMethod.POST})
    @ApiOperation(value = "删除附件")
    @ResponseBody
    public ResultData<String> delFile(@RequestParam(value = "fileName") String fileName, @RequestParam(value = "canDel") String canDel) {
        boolean isXunhuan = false;
        if ("Y".equals(canDel)) {
            isXunhuan = true;
        }
        LogUtil.WriteLog_Info("附件删除接口执行开始===========" + PubTools.getCurrentDate() + "==参数fileName====" + fileName, "FileController");
        ImageUtils.delFile(fileName, isXunhuan);
        LogUtil.WriteLog_Info("附件删除接口执行结束===========" + PubTools.getCurrentDate(), "FileController");
        return ResultData.success(1, "删除成功");
    }

    @RequestMapping(value = "/uploadFile", method = {RequestMethod.POST})
    @ApiOperation(value = "上传附件到本项目和远程指定目录")
    @ResponseBody
    public ResultData<String> uploadFile(@RequestParam("jsonArray") String jsonArray) {
        try {
            LogUtil.WriteLog_Info("附件上传接口执行开始===========" + PubTools.getCurrentDate(), "FileController");
            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, "FileController");
            if (!PubTools.isNull(fileAddressLocal)) {
                File filePath = new File(fileAddressLocal);
                if (!filePath.exists()) {
                    filePath.mkdir();
                }
                JSONArray from = JSONObject.parseArray(jsonArray);
                if (from.size() > 0) {
                    for (int k = 0; k < from.size(); k++) {
                        JSONObject map = (JSONObject) from.get(k);
                        String fileName = map.get("fileName").toString();
                        String imageBase64 = map.get("imageBase64").toString();
                        if (PubTools.isNull(fileName) && PubTools.isNull(imageBase64)) {
                            continue;
                        }
                        if (!PubTools.isNull(imageBase64)) {
                            // ImageUtils.delFile(fileName, false);
                            byte[] imageBytes = Base64.getDecoder().decode(imageBase64);
                            // 创建文件
                            File file = new File(fileAddressLocal, fileName); // 指定上传位置
                            if (file.exists()) {
                                file.delete();
                            }
                            // 保存文件
                            LogUtil.WriteLog_Info("附件开始上传到服务器===========" + PubTools.getCurrentDate(), "FileController");
                            org.apache.commons.io.FileUtils.writeByteArrayToFile(file, imageBytes);
                            LogUtil.WriteLog_Info("附件上传到服务器结束===========" + PubTools.getCurrentDate(), "FileController");
                        }
                    }
                }

            }
            LogUtil.WriteLog_Info("附件上传接口执行结束===========" + PubTools.getCurrentDate(), "FileController");
        } catch (Exception e) {
            return ResultData.fail("上传附件失败====" + e.getMessage());
        }
        return ResultData.success(1, "上传成功");
    }


}
