package com.nokia.domainb.capability;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.http.HttpHeaders;

import com.alibaba.fastjson.JSONObject;

import cn.chinaunicom.open.common.utils.MD5Util;

public abstract class AbstractQuery {
    // 河北联通移网投诉管理系统
    protected static final String appId = "ENWaB7YdUD";
    // 测试环境密钥
    protected static final String appSecretTest = "MUwA8ZVBprY83mAZrtOfrMfjdiJ9RFXr";
    // 生产环境密钥
    protected static final String appSecret = "oz4OgKBaMNwi4LWfLPbhrPbbuCS8T0Rb";

    protected static HttpHeaders getHeaders() {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        return headers;
    }

    protected static JSONObject getUniBSSHead(boolean test) {
        Date date = new Date();
        String timeStamp = getTimeStamp(date);
        String transId = getTransId(date);

        // UNI_BSS_HEAD
        JSONObject uniBssHead = new JSONObject();
        uniBssHead.put("APP_ID", appId);
        uniBssHead.put("TIMESTAMP", timeStamp);
        uniBssHead.put("TRANS_ID", transId);
        uniBssHead.put("TOKEN", getToken(timeStamp, transId, test));
        return uniBssHead;
    }

    protected static String getTimeStamp(Date date) {
        return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS")).format(date);
    }

    protected static String getTransId(Date date) {
        return (new SimpleDateFormat("yyyyMMddHHmmssSSS" + getRandomNumber())).format(date);
    }

    protected static Object getToken(String timeStamp, String transId, boolean test) {
        String realAppSecret = test ? appSecretTest : appSecret;
        String beforeEncode = "APP_ID" + appId + "TIMESTAMP" + timeStamp + "TRANS_ID" + transId + realAppSecret;
        return MD5Util.md5encode(beforeEncode);
    }

    private static int getRandomNumber() {
        return (int) ((Math.random() * 9.0D + 1.0D) * 100000.0D);
    }

}
