package com.jiayue.insu.incloud.utils;

import cn.hutool.core.date.DateUtil;

import cn.hutool.crypto.digest.HMac;
import cn.hutool.crypto.digest.HmacAlgorithm;
import com.jiayue.insu.incloud.constants.CommonStant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.Properties;

/**
 * @description:
 * @author: yuanhao
 * @createDate: 2021/9/27
 * @version: 1.0
 */
@Slf4j
public class SystermUtils {

    public static Properties props = System.getProperties();
    public static String osName = props.getProperty("os.name");

    /**
     * 功能描述: 判断文件夹是否存在
     *
     * @param:
     * @return:
     * @auther: bizy
     * @date: 2018/7/18 15:47 下午
     */
    public static String judeDirExists(String path) {
        try {

            if (osName.indexOf("Windows") > -1) {
                path = path.replaceAll("/", "\\\\");
            } else {
                path = path.replaceAll("\\\\", "/");
                path = "/" + path;
            }

            File file = new File(path);

            if (file.exists()) {
                if (file.isDirectory()) {
                    log.debug("dir exists");
                } else {
                    log.debug("the same name file exists, can not create dir");
                }
            } else {
                log.debug("dir not exists, create it ...");
                File parent = file.getParentFile(); // 获取父文件
                if (!parent.exists()) {
                    parent.mkdirs(); //创建所有父文件夹
                }
                file.mkdir();

                if (osName.indexOf("Windows") <= -1) {
                    Runtime.getRuntime().exec("chmod 777 -R " + path);//ftp服务器在linux环境中，生成的文件涉及到读写权限问题，直接给777权限
                }
            }
            log.info("path:" + path);
        } catch (IOException e) {
            log.error("系统错误：" + e.getMessage(), e);
        } finally {
            return path;
        }
    }


    public static String getResourceBasePath() {
        // 获取跟目录
        File path = null;
        try {
            path = new File(ResourceUtils.getURL("classpath:").getPath());

        } catch (FileNotFoundException e) {
            // nothing to do
        }
        if (path == null || !path.exists()) {
            path = new File("");
        }

        String pathStr = path.getAbsolutePath();
        try {
            pathStr = URLDecoder.decode(pathStr, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        // 如果是在eclipse中运行，则和target同级目录,如果是jar部署到服务器，则默认和jar包同级
//    pathStr = pathStr.replace("\\target\\classes", "");

        return pathStr;
    }


    public static String AMPM() {
        String ampm = CommonStant.AM_DAY;
        boolean am = DateUtil.isAM(new Date());
        if (!am) {
            ampm = CommonStant.PM_DAY;
        }

        return ampm;
    }

    /**
     * 获取签名
     * @param signOrg
     * @return
     */
    public static String caluMd5Sign(String signOrg) {
        //签名密钥
        byte[] key = "123456789".getBytes();
        HMac mac = new HMac(HmacAlgorithm.HmacMD5, key);

        String macHexSign = mac.digestHex(signOrg);
        return macHexSign;
    }

}
