package com.fdkankan.ucenter.common;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fdkankan.common.util.FileUtils;
import com.fdkankan.ucenter.common.constants.ConstantFilePath;

import java.util.HashMap;
import java.util.Map;

/**
 * 百度统计
 * Created by Hb_zzZ on 2020/8/24.
 */
public class StatisticsUtil {

    private static final String BAIDU_USERNAME = "四维时代";

    private static final String BAIDU_PASSWORD = "4DAge2018";

    private static final String BAIDU_TOKEN = "17a1e010f1be38abead0066b2b02cb4c";

    private static final String SITE_ID = "12325722";

    private static final String METRICS = "pv_count,visitor_count,ip_count,visit1_count,outward_count,exit_count,average_stay_time,exit_ratio";

    private static final String METHOD = "visit/toppage/a";

    private static final String BAIDU_URL = "https://api.baidu.com/json/tongji/v1/ReportService/getData";

    public static Map<String, Map<String, Double>> findStatisticsData(String dateStr) throws Exception{

        JSONObject param = new JSONObject();
        JSONObject header = new JSONObject();
        JSONObject body = new JSONObject();
        header.put("username", BAIDU_USERNAME);
        header.put("password", BAIDU_PASSWORD);
        header.put("token", BAIDU_TOKEN);
        header.put("account_type", 1);
        body.put("site_id", SITE_ID);
        body.put("start_date", dateStr);
        body.put("end_date", dateStr);
        body.put("metrics", METRICS);
        body.put("method", METHOD);
        param.put("header", header);
        param.put("body", body);
        String baiduData = HttpRequestorUtil.postJson(BAIDU_URL, param.toString(), "POST");

        JSONObject data = JSONObject.parseObject(baiduData);

        Map<String, Map<String, Double>> map = new HashMap<>();
        Map<String, Double> numMap = null;
        String url = "";
        String sceneNum = "";
        if(data.getJSONObject("header").getIntValue("status") == 0){

            JSONArray items = data.getJSONObject("body").getJSONArray("data").getJSONObject(0)
                    .getJSONObject("result").getJSONArray("items");

            JSONArray urlArray = items.getJSONArray(0);
            JSONArray numArray = items.getJSONArray(1);

            for(int i = 0, len = urlArray.size(); i < len; i++){
                url = urlArray.getJSONArray(i).getJSONObject(0).getString("name");
                if((url.contains("www.4dkankan.com") || url.contains("test.4dkankan.com")) && url.contains("?m=") && !url.contains("&type=event")){
                    if(url.contains("&")){
                        url = url.substring(url.indexOf("?m=") + 3);
                        if(url.contains("#")){
                            url = url.substring(0, url.indexOf("#"));
                        }
                        sceneNum = url.substring(0, url.indexOf("&"));
                    }else {
                        sceneNum = url.substring(url.indexOf("?m=") + 3);
                    }
                    numMap = map.get(sceneNum);
                    numMap = getNumMap(numMap, numArray, i);
                    map.put(sceneNum, numMap);
                }
                if((url.contains("www.4dkankan.com") || url.contains("test.4dkankan.com")) && url.contains("&type=event&name=show_tag")){
                    if(url.contains("&")){
                        url = url.substring(url.indexOf("?m=") + 3);
                        if(url.contains("#")){
                            url = url.substring(0, url.indexOf("#"));
                        }
                        sceneNum = url.substring(0, url.indexOf("&"));
                    }else {
                        sceneNum = url.substring(url.indexOf("?m=") + 3);
                    }
                    numMap = map.get(sceneNum + "_tag");
                    numMap = getNumMap(numMap, numArray, i);
                    map.put(sceneNum + "_tag", numMap);
                }
                if((url.contains("www.4dkankan.com") || url.contains("test.4dkankan.com")) && url.contains("&type=event&name=show_video")){
                    if(url.contains("&")){
                        url = url.substring(url.indexOf("?m=") + 3);
                        if(url.contains("#")){
                            url = url.substring(0, url.indexOf("#"));
                        }
                        sceneNum = url.substring(0, url.indexOf("&"));
                    }else {
                        sceneNum = url.substring(url.indexOf("?m=") + 3);
                    }
                    numMap = map.get(sceneNum + "_video");
                    numMap = getNumMap(numMap, numArray, i);
                    map.put(sceneNum + "_video", numMap);
                }
            }
        }
        String filePath = ConstantFilePath.BASE_PATH + "/statistics/" + dateStr + ".json";
        FileUtils.writeFile(filePath, baiduData);
//        System.out.println(JSONArray.toJSON(map).toString());
        return map;
    }

    public static void main(String[] args) {
        try{
            StatisticsUtil.findStatisticsData("20200822");
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private static Map<String, Double> getNumMap(Map<String, Double> numMap, JSONArray numArray, int index){
        if(numMap == null){
            numMap = new HashMap<>();
            numMap.put("pv_count", numArray.getJSONArray(index).getDouble(0));
            numMap.put("visitor_count", numArray.getJSONArray(index).getDouble(1));
            numMap.put("ip_count", numArray.getJSONArray(index).getDouble(2));
            numMap.put("visit1_count", numArray.getJSONArray(index).getDouble(3));
            numMap.put("outward_count", numArray.getJSONArray(index).getDouble(4));
            numMap.put("exit_count", numArray.getJSONArray(index).getDouble(5));
            numMap.put("average_stay_time", numArray.getJSONArray(index).getDouble(6));
            numMap.put("exit_ratio", numArray.getJSONArray(index).getDouble(7));
        }else {
            numMap.put("pv_count", numMap.get("pv_count") + numArray.getJSONArray(index).getDouble(0));
            numMap.put("visitor_count", numMap.get("visitor_count") + numArray.getJSONArray(index).getDouble(1));
            numMap.put("ip_count", numMap.get("ip_count") + numArray.getJSONArray(index).getDouble(2));
            numMap.put("visit1_count", numMap.get("visit1_count") + numArray.getJSONArray(index).getDouble(3));
            numMap.put("outward_count", numMap.get("outward_count") + numArray.getJSONArray(index).getDouble(4));
            numMap.put("exit_count", numMap.get("exit_count") + numArray.getJSONArray(index).getDouble(5));
            numMap.put("average_stay_time", numMap.get("average_stay_time") + numArray.getJSONArray(index).getDouble(6));
            numMap.put("exit_ratio", numMap.get("exit_ratio") + numArray.getJSONArray(index).getDouble(7));
        }
        return numMap;
    }
}
