package com.liangjian.dataplatform.member.entity.pojo;

import java.util.Date;

/**
 * @Author: 黄子奇
 * @Date: 2019/10/23
 */
public class BirdGirdFilter {
    private String field;
    private String operate;
    private String value;

    public BirdGirdFilter() {
    }

    public BirdGirdFilter(String field, String operate, String value) {
        this.field = field;
        this.operate = operate;
        this.value = value;
    }

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }

    public String getOperate() {
        return operate;
    }

    public void setOperate(String operate) {
        this.operate = operate;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getPreValue() {
        switch (this.operate) {
            case "contains": return "%" + value + "%";
            case "startswith": return value + "%";
            case "endswith": return "%" + value;
            default: return this.value;
        }
    }

    public String toSql() {
        //改成预编译的形式
//        switch (this.operate) {
//            case "equal": return String.format(" and %s = %s",field, value);
//            case "notequal": return String.format(" and %s != %s",field, value);
//            case "less": return String.format(" and %s < %s",field,value);
//            case "lessorequal": return String.format(" and %s <= %s",field, value);
//            case "greater": return String.format(" and %s > %s",field,value);
//            case "greateroreeuqal": return String.format(" and %s >= %s",field, value);
//            case "contains": return " and " + field + " like '%" + value + "%'";
//            case "startswith": return " and " + field + " like '" + value + "%'";
//            case "endswith": return " and " + field + " like '%" + value + "'";
//            default: return "";
//        }

        switch (this.operate) {
            case "equal": return String.format(" and %s = ?",field);
            case "notequal": return String.format(" and %s != ?",field);
            case "less": return String.format(" and %s < ?",field);
            case "lessorequal": return String.format(" and %s <= ?",field);
            case "greater": return String.format(" and %s > ?",field);
            case "greateroreeuqal": return String.format(" and %s >= ?",field);
            case "contains": return " and " + field + " like ?";
            case "startswith": return " and " + field + " like ?";
            case "endswith": return " and " + field + " like ?";
            default: return "";
        }
    }

    public <A> A transportType(Class<A> clazz) {
        if(clazz.equals(Integer.class)) {
            return (A) Integer.valueOf(value);
        } else if(clazz.equals(Long.class)) {
            return (A) Long.valueOf(value);
        } else if(clazz.equals(Double.class)) {
            return (A) Double.valueOf(value);
        } else if(clazz.equals(Float.class)) {
            return (A) Float.valueOf(value);
        } else if(clazz.equals(Date.class)) {
            return (A) new Date(value);
        } else if(clazz.equals(Boolean.class)) {
            return (A) Boolean.valueOf(value);
        } else if(clazz.equals(String.class)) {
            return (A) value;
        } else {
            throw new RuntimeException("unknown filter type");
        }
    }
}
