package org.jeecg.module.custom.ocr.utils.requestFormat;


import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangyong on 2018/2/9.
 */
public class SearchQuery implements java.io.Serializable{

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    private static final long serialVersionUID = 7991431227775165087L;

    private  Map<String,Object> query = new HashMap();

    public void setSearchQuery(SearchQuery searchQuery){
        if(null != searchQuery){
            this.query.putAll(searchQuery.getQuery());
        }
    }

    public void addJsonQuery(String fvalue){
        SearchQueryUtil.queryStringFormat(fvalue,query);
    }

    public void addJsonQuery(JSONObject jsonObject){
        SearchQueryUtil.queryStringFormat(jsonObject,query);
    }

    /**
     * 添加对象，没有条件，在mybatis配置文件中自己写
     * @param field 字段名称
     * @param value 条件值
     */
    public void addObject(String field,Object value){
        query.put(field,value);
    }

    /**
     * 添加全等
     * @param field 字段名称
     * @param value 条件值
     */
    public void addEqual(String field,String value){
        addQuery(field,value, SearchQueryConstant.SEARCH_EQUAL);
    }

    /**
     * 不等条件
     * @param field 字段名称
     * @param value 条件值
     */
    public void addNotEqual(String field,String value){
        addQuery(field,value, SearchQueryConstant.SEARCH_EQUAL_NOT);
    }

    public void addLike(String field,String value){
        addQuery(field,value, SearchQueryConstant.SEARCH_LIKE);
    }

    public void addIn(String field,List<String> value){
        addQuery(field,value, SearchQueryConstant.SEARCH_IN);
    }
    public void addIn(String field,String value){
        addQuery(field,value, SearchQueryConstant.SEARCH_IN);
    }
    public void addInLong(String field,List<Long> value){
        addQuery(field,value, SearchQueryConstant.SEARCH_IN);
    }
    public void addInObject(String field,List value){
        addQuery(field,value, SearchQueryConstant.SEARCH_IN);
    }
    public void addIn(String field,String[] value){
        addQuery(field,value, SearchQueryConstant.SEARCH_IN);
    }

    public void addNotIn(String field,List<String> value){
        addQuery(field,value, SearchQueryConstant.SEARCH_NOTIN);
    }
    public void addNotInObject(String field,List value){
        addQuery(field,value, SearchQueryConstant.SEARCH_NOTIN);
    }
    public void addNotIn(String field,String[] value){
        addQuery(field,value, SearchQueryConstant.SEARCH_NOTIN);
    }

    public void addGreater(String field,Integer value){
        addQuery(field,value, SearchQueryConstant.SEARCH_GREATER);
    }
    public void addGreater(String field,String value){
        addQuery(field,value, SearchQueryConstant.SEARCH_GREATER);
    }

    public void addGreater(String field,Long value){
        addQuery(field,value, SearchQueryConstant.SEARCH_GREATER);
    }

    public void addGreater(String field,BigDecimal value){
        addQuery(field,value, SearchQueryConstant.SEARCH_GREATER);
    }

    public void addGreater(String field,Date value){
        addQuery(field,value, SearchQueryConstant.SEARCH_GREATER);
    }

    public void addGreaterEqual(String field,Integer value){
        addQuery(field,value, SearchQueryConstant.SEARCH_GREATER_EQUAL);
    }
    public void addGreaterEqual(String field,String value){
        addQuery(field,value, SearchQueryConstant.SEARCH_GREATER_EQUAL);
    }

    public void addGreaterEqual(String field,BigDecimal value){
        addQuery(field,value, SearchQueryConstant.SEARCH_GREATER_EQUAL);
    }
    public void addGreaterEqual(String field,Date value){
        addQuery(field,value, SearchQueryConstant.SEARCH_GREATER_EQUAL);
    }
    public void addGreaterEqual(String field,Long value){
        addQuery(field,value, SearchQueryConstant.SEARCH_GREATER_EQUAL);
    }
    public void addLess(String field,Integer value){
        addQuery(field,value, SearchQueryConstant.SEARCH_LESS);
    }
    public void addLess(String field,String value){
        addQuery(field,value, SearchQueryConstant.SEARCH_LESS);
    }

    public void addLess(String field,Long value){
        addQuery(field,value, SearchQueryConstant.SEARCH_LESS);
    }
    public void addLess(String field,BigDecimal value){
        addQuery(field,value, SearchQueryConstant.SEARCH_LESS);
    }
    public void addLess(String field,Date value){
        addQuery(field,value, SearchQueryConstant.SEARCH_LESS);
    }
    public void addLessEqual(String field,Integer value){
        addQuery(field,value, SearchQueryConstant.SEARCH_LESS_EQUAL);
    }
    public void addLessEqual(String field,String value){
        addQuery(field,value, SearchQueryConstant.SEARCH_LESS_EQUAL);
    }

    public void addLessEqual(String field,BigDecimal value){
        addQuery(field,value, SearchQueryConstant.SEARCH_LESS_EQUAL);
    }

    public void addLessEqual(String field,Date value){
        addQuery(field,value, SearchQueryConstant.SEARCH_LESS_EQUAL);
    }

    public void addLessEqual(String field,Long value){
        addQuery(field,value, SearchQueryConstant.SEARCH_LESS_EQUAL);
    }

    public void addRulesOp(String field,String rulesOp){
        SearchQueryForm searchQueryForm = new SearchQueryForm();
        if(query.get(field)!=null){
            searchQueryForm = (SearchQueryForm) query.get(field);
            searchQueryForm.setRulesOp(rulesOp);
            return;
        }
        searchQueryForm.setRulesOp(rulesOp);
        query.put(field,searchQueryForm);
    }
    /**
     *
     * @param field 字段名称
     * @param value 条件值
     * @param filter 条件类型
     */
    private void addQuery(String field,Object value,String filter){
        if(null == field || "".equals(field.trim())){

            log.error("【添加搜索条件】-addEqualQuery-错误，field不能为空");
            return;
        }
        if(value instanceof String){
            if( null == value || "".equals(value.toString().trim())){

                log.error("【添加搜索条件】字符串-value-错误，value不能为空");
                return;
            }
        } else if(null == value){

            log.error("【添加搜索条件】对象-value-错误，value不能为空");
            return;
        }
//        if(null != query.get(field)){
//            log.error("【添加搜索条件】-query-错误，query已经存在数据,field={}",field);
//            return;
//        }
        SearchQueryForm lf = new SearchQueryForm();
        if(query.get(field)!=null){
            lf = (SearchQueryForm) query.get(field);
        }

        lf.setField(field);
        lf.setDataOp(SearchQueryUtil.filterMap.get(filter));
        lf.setOp(filter);
        lf.setValue(value);
        query.put(field,lf);
    }

    public Map<String, Object> getQuery() {
        return query;
    }

//    public static void main(String args[]){
//     String fvalue =
//     "[{\"field\":\"CustomerID\",\"op\":\"notin\",\"value\":\"1\",\"type\":\"string\"},{\"field\":\"CompanyName\",\"op\":\"like\",\"value\":\"22\",\"type\":\"string\"},{\"field\":\"AddTime\",\"op\":\"greater\",\"value\":\"2012-07-10T05:53:00.000Z\",\"type\":\"date\"},{\"field\":\"Amount\",\"op\":\"greater\",\"value\":33,\"type\":\"number\"}]";
//
//     //String fvalue
//     //="{\"rules\":[{\"field\":\"departname\",\"op\":\"notequal\",\"value\":\"1111\",\"type\":\"string\"},{\"field\":\"enabled\",\"op\":\"equal\",\"value\":\"0\",\"type\":\"number\"}]}";
//
//
//        SearchQuery searchQuery = new SearchQuery();
//
//        searchQuery.addJsonQuery(fvalue);
//
//        searchQuery.getQuery();
//
//        searchQuery.addEqual("username","zhangyy");
//
//        JSONObject json = new JSONObject(searchQuery.getQuery());
//
//        json.get("username");
//     }

}
