package cn.com.countrygarden.bi.main.service.dup.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.com.countrygarden.bi.main.service.dup.CurrencyCommonService;
import cn.com.countrygarden.bi.main.util.JsonUtils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;

/**
 * <pre>
 * 公共类
 * </pre>
 *
 * @author 陈幸浩
 * @version 1.00.00
 */
@Service
public class CurrencyCommonServiceImpl implements CurrencyCommonService {

    private final static String KEY_PREFIX = "[";

    private final static String KEY_SUFFIX = "]";

    /**
     * 校验必填项
     * @param t
     * @param mandatoryField
     * @param <T>
     * @return
     */
    @Override
    public <T> Map<String, Boolean> checkFormAuth(T t,String[] mandatoryField) {
        JSONArray arr = this.getObjectToJSONArray(t);

        Map<String, Boolean> result = new HashMap<String, Boolean>(16);
        // 封装校验结果
        for (String field : mandatoryField) {
            if (StringUtils.isNotBlank(field)) {
                boolean isNotBank = false;
                boolean flag = this.checkHasKey(field, t);
                if (flag) {
                    isNotBank = this.checkJsonArray(arr, field);
                }
                result.put(field, isNotBank);
            }
        }
        return result;
    }


    /**
     * 把对象转为JSON数组。
     *
     * @param t
     * @return
     */
    private <T> JSONArray getObjectToJSONArray(T t) {
        String str = JsonUtils.toJson(t);
        if (StringUtils.isNotBlank(str) && !str.startsWith(KEY_PREFIX) && !str.endsWith(KEY_SUFFIX)) {
            str = KEY_PREFIX + str + KEY_SUFFIX;
        }
        JSONArray arr = JSONArray.parseArray(str);
        return arr;
    }


    /**
     * 校验是否存在KEY值。
     *
     * @param key
     * @param t
     * @return
     */
    private <T> boolean checkHasKey(String key, T t) {
        String value = JsonUtils.toJson(t);
        StringBuffer str = new StringBuffer();
        str.append("\"");
        str.append(key);
        str.append("\":");
        int index = value.indexOf(str.toString());
        return (index >= 0);
    }

    /**
     * 检查JSONObject。
     *
     * @param obj
     * @param fieldName
     * @return
     */
    private boolean checkJsonObject(JSONObject obj, String fieldName) {
        boolean flag = true;
        if (obj.isEmpty()) {
            return false;
        }
        for (String key : obj.keySet()) {
            Object o = obj.get(key);
            if (fieldName.equals(key)) {
                if (o != null) {
                    if (o instanceof String) {
                        return StringUtils.isNotBlank((String) o);
                    } else if (o instanceof JSONArray) {
                        return ((JSONArray) o).size() > 0;
                    } else {
                        return true;
                    }
                } else {
                    return false;
                }
            } else if (o instanceof JSONArray) {
                flag = flag && checkJsonArray((JSONArray) o, fieldName);
            } else if (o instanceof JSONObject) {
                flag = flag && checkJsonObject((JSONObject) o, fieldName);
            }
        }
        return flag;
    }

    /**
     * 检查JSONArray
     *
     * @param arr
     * @param fieldName
     * @return
     */
    private boolean checkJsonArray(JSONArray arr, String fieldName) {
        boolean flag = true;
        for (int i = 0; i < arr.size(); i++) {
            Object o = arr.get(i);
            if (o instanceof JSONArray) {
                flag = flag && this.checkJsonArray((JSONArray) o, fieldName);
            } else if (o instanceof JSONObject) {
                flag = flag && this.checkJsonObject((JSONObject) o, fieldName);
            }
        }
        return flag;
    }



}
