package com.ag.base;

import cn.hutool.core.collection.CollectionUtil;
import com.ag.util.UUIDUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

/**
 * Created by Administrator on 2017/4/12 0012.
 */
public abstract class BaseService {
    protected void callExcetion(String msg){
        throw new RuntimeException(msg);
    }

    protected Map<String,String> getRequestMap(HttpServletRequest request){
        Map<String,String> map = new HashMap<>();
        Enumeration em = request.getParameterNames();
        while (em.hasMoreElements()) {
            String name = (String) em.nextElement();
            String value = request.getParameter(name);
            map.put(name, value);
        }
        return map;
    }

    protected String[] getRequestParam(HttpServletRequest request){
        List<String> lst = new ArrayList<>();
        Enumeration em = request.getParameterNames();
        while (em.hasMoreElements()) {
            String name = (String) em.nextElement();
            if (name.contains("%") || name.contains("#") || name.contains("$")){
                continue;
            }
            lst.add(name);
        }
        return lst.toArray(new String[0]);
    }

    protected String[] getJSONParam(String update){
        List<String> lst = new ArrayList<>();
        List<JSONObject> array = JSON.parseArray(update, JSONObject.class);
        if (CollectionUtil.isEmpty(array)){
            return null;
        }
        Set<String> set = array.get(0).keySet();
        for (String name : set) {
            if (name.contains("%") || name.contains("#") || name.contains("$")){
                continue;
            }
            lst.add(name);
        }
        return lst.toArray(new String[0]);
    }

    protected String[] getRequestEnum(HttpServletRequest request){
        List<String> lst = new ArrayList<>();
        Enumeration em = request.getParameterNames();
        while (em.hasMoreElements()) {
            lst.add(em.nextElement().toString());
        }
        return (String[])lst.toArray();
    }
    protected Map<String,String> getScriptMap (jdk.nashorn.api.scripting.ScriptObjectMirror so){
        Map<String,String> map = new HashMap<>();
        for(Map.Entry<String, Object> en: so.entrySet()){
            //System.out.println(en.getKey()+" : "+en.getValue());
            String name = en.getKey();
            String value = String.valueOf(en.getValue());
            map.put(name, value);
        }
        return map;
    }
    protected void printMap(Map<String,String> map){
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
        }
    }

    protected String getSysUserName(HttpServletRequest request){
        Object userName = request.getSession().getAttribute("sys.username");
        return userName == null ? "" : userName.toString();
    }

    protected String getSysUserName(org.json.JSONObject json){
        if (json.has("sys.username")) {
            return json.getString("sys.username");
        }
        return null;
    }

    protected String getTenancyId(HttpServletRequest request){
        Object tenancyId = request.getSession().getAttribute("sys.tenancyId");
        return tenancyId == null ? null : tenancyId.toString();
    }

    protected String getTenancyId(org.json.JSONObject json){
        if (json.has("sys.username")) {
            return json.getString("sys.tenancyId");
        }
        return null;
    }

    protected Date getNow(){
        return new Date();
    }

    protected void setSaveUser(HttpServletRequest request, ISerializable ent){
        ent.setCreatedBy(getSysUserName(request));
        ent.setUpdatedBy(getSysUserName(request));
        ent.setCreatedOn(getNow());
        ent.setUpdatedOn(getNow());
        ent.setTenancyId(getTenancyId(request));
        ent.setId(UUIDUtil.getUUID());
    }

    protected void setSaveUser(org.json.JSONObject json, ISerializable ent){
        ent.setCreatedBy(getSysUserName(json));
        ent.setUpdatedBy(getSysUserName(json));
        ent.setCreatedOn(getNow());
        ent.setUpdatedOn(getNow());
        ent.setTenancyId(getTenancyId(json));
        ent.setId(UUIDUtil.getUUID());
    }

    protected void setUpdateUser(HttpServletRequest request, ISerializable ent){
        ent.setUpdatedBy(getSysUserName(request));
        ent.setUpdatedOn(getNow());
    }

    protected void setUpdateUser(org.json.JSONObject json, ISerializable ent){
        ent.setUpdatedBy(getSysUserName(json));
        ent.setUpdatedOn(getNow());
    }

    protected String getUserIpAddress(org.json.JSONObject json){
        return json.has("sys.userIpAddress")?json.getString("sys.userIpAddress"):null;
    }

    public static void main(String[] args) throws IOException {

    }
}
