package com.ruoyi.klk.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Component;
import org.springframework.util.StreamUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class CommonService {


    /**
     * 1-身份证正面 2-身份证反面 3-银行卡 4-营业执照 5-合作协议-纸质版 6-合作协议-电子版
     * 获取附件类型
     *
     * @param attType
     * @return
     */
    public String getAttType(String attType) {
        switch (attType) {
            case "1":
                return "FR_ID_CARD_FRONT";
            case "2":
                return "FR_ID_CARD_BEHIND";
            case "3":
                return "BANK_CARD";
            case "4":
                return "BUSINESS_LICENCE";
            case "5":
                return "XY";
            case "6":
                return "NETWORK_XY";
            default:
                return "";
        }
    }

    /*读取网络文件*/
    public  InputStream getFileInputStream(String path) {
        URL url = null;
        try {
            url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //设置超时间为3秒
            conn.setConnectTimeout(3 * 1000);
            //防止屏蔽程序抓取而返回403错误
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            //得到输入流
            return conn.getInputStream();
        } catch (Exception e) {

        }
        return null;
    }

    public  String fileToBase64(String path) {
        String base64 = null;
        InputStream in = null;
        try {
            in = getFileInputStream(path);
            byte[] bytes = StreamUtils.copyToByteArray(in);
            in.read(bytes);
            base64 = new String(Base64.encodeBase64(bytes),"UTF-8");
            System.out.println("将文件["+path+"]转base64字符串:"+base64);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return base64;
    }

    public  String fileToBase64(InputStream is) {
        String base64 = null;
        try {
            byte[] bytes = StreamUtils.copyToByteArray(is);
            is.read(bytes);
            base64 = new String(Base64.encodeBase64(bytes),"UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return base64;
    }

    /**
     * 卡拉卡回调统一响应
     * {
     *     "code":"SUCCESS",
     *     "message":"执行成功"
     * }
     */
    public Map<String, String> getKlkCommonResp() {
        Map<String, String> map = new HashMap<>();
        map.put("code", "SUCCESS");
        map.put("message", "执行成功");
        return map;
    }

}
