package com.winsun.utils;

import com.winsun.constant.FilePath;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.Calendar;

/**
 * 二维码海报工具类
 * @Author: chancy
 * @Date: 2020/3/12 14:33
 */
@Slf4j
public class PicturesUtil {

    /**
     * 上传图片
     * <p>
     * 前端传来图片必须为jpeg格式
     * @param file 图片base64字符串
     * @param partner 用户ID做为文件夹名
     * @param pathType 图片上一级文件夹名
     * @param
     * @return
     */
    public static String uploadPictures(String file, String partner,String pathType){
        if(StringUtils.isBlank(partner)){
            partner = "temp";
        }
        Calendar now = Calendar.getInstance();
        Integer yyyy = now.get(Calendar.YEAR);
        Integer MM = (now.get(Calendar.MONTH) + 1);
        Integer DD = now.get(Calendar.DAY_OF_MONTH);
        String yymmdd = yyyy+"/"+MM+"/"+DD;
        /*System.out.println("年: " + now.get(Calendar.YEAR));
        System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");
        System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));
        System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));
        System.out.println("分: " + now.get(Calendar.MINUTE));*/
        //blog.csdn.net/kookob/java/article/details/6885383
        String filePath = null;
        String[] multipartFiles = new String[]{file};
        for (int i = 0; i < multipartFiles.length; i++) {
            if (!multipartFiles[i].isEmpty()) {
                String base64Data = multipartFiles[i].split(",")[1];
                String houzhui = multipartFiles[i].split(",")[0].split("/")[1].split(";")[0];
                String  filePath1 = FilePath.PATH.getValue()+ pathType+ "/"  + partner + "/" + yymmdd;
                File dir = new File(filePath1);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                //enclosure/images/idCard/temp/年/月/日/name.jpeg
                filePath = FilePath.PATH.getValue()+ pathType+ "/"  + partner + "/" + yymmdd + "." + houzhui;

                //byte[] bytes = Base64.getUrlDecoder().decode(base64Data);
                //byte[] bytes =new BASE64Decoder().decodeBuffer(base64Data);
                //byte[] bytes = Base64.getDecoder().decode(base64Data);
                byte[] bytes = Base64.getMimeDecoder().decode(base64Data.replace(" ","+"));
                        FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(filePath);
                    fos.write(bytes);
                } catch (IOException e) {
                    log.error("图片上传异常",e.getMessage());
                    e.printStackTrace();
                } finally {
                    if (fos != null) {
                        try {
                            fos.close();
                        } catch (IOException e) {
                            log.error("图片上传异常",e.getMessage());
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        String enclosure = filePath.replace("enclosure", "manager/ciop");
        return enclosure;
    }


    /**
     * 海报删除图片
     * @param fileName 删除图片地址
     * @return
     */
    public static boolean deletePictures(String fileName){
        File file = new File(fileName.replace("manager/ciop","enclosure"));
        Boolean flag = false;
        //判断文件是否存在
        if (file.exists() == true){
            flag = file.delete();
            if (flag){
                log.info("成功删除海报背景图片"+file.getName());
            }else {
                log.info("海报背景图片删除失败");
            }
        }else {
            log.info("图片不存在，终止操作");
        }
        return flag;
    }
}
