package com.novel.clean.util;

import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class CleanUtil {
    public static  QQWryIPUtil qqWryIPUtil;
    public static IPZone ipZone;
    static{
            try {
                qqWryIPUtil = new QQWryIPUtil();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
/**
 * This method is used for incoming IP to resolve provinces and service providers
 * 此方法用于传入ip解析出省份和服务商
 * @author maliang
 * */
    public Map<String,String> getIp(String ip){
        Map<String,String> analysis=new HashMap<>();
        try {
            ipZone= qqWryIPUtil.findIP(ip);
            Map<String, String> zone = QQWryIPUtil.addressResolution(ipZone.getMainInfo());
            analysis.put("country",ipZone.getMainInfo());
            analysis.put("detail_area",ipZone.getSubInfo());
            analysis.put("province",zone.get("province") ==""?"-999":zone.get("province"));
            analysis.put("city",zone.get("city") ==""?"-999":zone.get("city"));
        }catch (Exception e){
            analysis.put("country","-999");
            analysis.put("detail_area","-999");
            analysis.put("province","-999");
            analysis.put("city","-999");
        }
        return analysis;
    }
/**
 * This method resolves action and action_detail fields by requesting addresses
 *这个方法可以通过请求地址把action和action_detail字段解析出来
 * @author maliang
* */
    public Map<String,String> getAction(String requrlstr){
        Map<String,String> analysis=new HashMap<>();
        analysis.put("action","-999");
        analysis.put("action_detail","-999");
        String[] split = requrlstr.split("/");
        try {
            analysis.put("action",split[2]+"_"+split[3]);
            analysis.put("action_detail",split[2]+"_"+split[3]);
        }catch (Exception e){
        }

        return analysis;
    }
/**
 * This method can parse the bookid and chapternum fields clicked by users in reqParam
 *这个方法可以把reqParam里用户点击的bookid和chapternum字段解析出来
 * @author maliang
* */
    public Map<String,String> getBook(String reqParam){
        Map<String,String> analysis=new HashMap<>();
        try{
            JSONObject jsonObject = JSONObject.parseObject(reqParam);
            analysis.put("bookId",(jsonObject.getString("bookId")==null)?"-999":jsonObject.getString("bookId"));
            analysis.put("chapterNum",jsonObject.getString("chapterNum")==null?"-999":jsonObject.getString("chapterNum"));
        }catch (Exception e){
        }

        return analysis;
    }

}
