package cn.estsh.i3plus.pojo.base.bean;

import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;

import java.util.HashMap;

/**
 * @Description : hql脚本封装类
 * @Reference :
 * @Author : alwaysfrin
 * @CreateDate : 2019-03-29 15:57
 * @Modify:
 **/
public class DdlPackBean {

    /**
     * String 为属性及条件
     * example： and name = :name
     * and num > :num  or .....
     * Object 为属性值
     * example: 值
     *
     * dao层将map进行解封，hql部分直接添加key值，然后再进行value赋值
     */
    private HashMap<String,Object> hqlPreparedMap;

    //是否是hql
    private boolean isHql;

    //手动hql，尽量避免直接传参，使用？代替
    private String hqlAppend;

    //where后跟着的hql/sql
    private StringBuilder whereAppend;

    //排序内容
    private String orderByStr;

    //需要拼接的hql
    private StringBuilder sb = null;

    public DdlPackBean(){
        this.isHql = true;
        whereAppend = new StringBuilder();
        hqlPreparedMap = new HashMap<>();
    }

    public DdlPackBean(boolean isHql){
        this.isHql = isHql;
        whereAppend = new StringBuilder();
        hqlPreparedMap = new HashMap<>();
    }

    /**
     * 查询制定组织有效的数据
     * @param organizeCode
     * @return
     */
    public static DdlPackBean getDdlPackBean(String organizeCode){
        DdlPackBean result = new DdlPackBean();

        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(),"isDeleted",result);
        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(),"isValid",result);
        DdlPreparedPack.getStringEqualPack(organizeCode,"organizeCode",result);

        return result;
    }

    /**
     * 查询指定组织、指定是否有效的数据
     * @param organizeCode
     * @param isValid
     * @return
     */
    public static DdlPackBean getDdlPackBean(String organizeCode, int isValid){
        DdlPackBean result = new DdlPackBean();

        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), "isDeleted", result);
        DdlPreparedPack.getNumEqualPack(isValid, "isValid", result);
        DdlPreparedPack.getStringEqualPack(organizeCode, "organizeCode", result);

        return result;
    }

    public static DdlPackBean getDdlPackBean(){
        DdlPackBean result = new DdlPackBean();

        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(),"isDeleted",result);
        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(),"isValid",result);

        return result;
    }

    public static DdlPackBean getDdlPackBean(BaseBean bean){
        DdlPackBean result = new DdlPackBean();

        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(),"isDeleted",result);
        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(),"isValid",result);
        if(bean != null){
            result.setOrderByStr(bean.orderBy());
        }

        return result;
    }

    public boolean isHql() {
        return isHql;
    }

    public void setHql(boolean hql) {
        isHql = hql;
    }

    public String getOrderByStr() {
        return orderByStr;
    }

    public void setOrderByStr(String orderByStr) {
        this.orderByStr = orderByStr;
    }

    public String getHqlAppend() {
        return hqlAppend;
    }

    public void setHqlAppend(String hqlAppend) {
        this.hqlAppend = hqlAppend;
    }

    public String getWhereAppend() {
        return whereAppend.toString();
    }

    public void setWhereAppend(String whereAppend) {
        this.whereAppend = new StringBuilder(whereAppend);
    }

    public HashMap<String, Object> getHqlPreparedMap() {
        return hqlPreparedMap;
    }

    /**
     * 添加查询条件
     * @param hql
     * @param val
     */
    public void addColumnQuery(String column,String hql,Object val){
        if(hqlPreparedMap == null){
            hqlPreparedMap = new HashMap<>();
        }
        if(column != null && val != null) {
            hqlPreparedMap.put(column, val);
        }

        whereAppend.append(hql);
    }

    /**
     * 添加查询条件-处理多个属性
     * @param hql
     * @param hqlMap
     */
    public void addColumnQuery(String hql,HashMap<String,Object> hqlMap){
        if(hqlPreparedMap == null){
            hqlPreparedMap = new HashMap<>();
        }
        if(hqlMap != null && !hqlMap.isEmpty()) {
            hqlPreparedMap.putAll(hqlMap);
        }

        whereAppend.append(hql);
    }



    /**
     * 需要查询属性的数量
     * @return
     */
    public int getColumnSize(){
        return hqlPreparedMap.size();
    }

    /**
     * 获取包含hql的最终的ddl
     * @return
     */
    public String getPackedHqlAll(){
        sb = new StringBuilder();
        if(hqlAppend != null){
            sb.append(hqlAppend);
        }
        if(whereAppend != null){
            sb.append(whereAppend);
        }
        if(orderByStr != null){
            sb.append(orderByStr);
        }
        return sb.toString();
    }

    /**
     * 获取最终的ddl
     * @return
     */
    public String getPackedHql(){
        sb = new StringBuilder();
        if(whereAppend != null){
            sb.append(whereAppend);
        }
        if(orderByStr != null){
            sb.append(orderByStr);
        }
        return sb.toString();
    }
}
