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


import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MdmEnumUtil;
import cn.estsh.i3plus.pojo.mdm.sqlpack.DdlMdmModelPreparedPack;

import java.util.ArrayList;
import java.util.List;

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

    /**
     * 条件实体
     */
    List<MdmEnumUtil.HQL_ENTITY_TYPE> entityTypeList;

    /**
     * 条件属性名称
     */
    List<String> keyList;

    /**
     * 条件值
     */
    List<Object> valueList;

    //是否是hql
    private boolean isHql;

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

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

    //排序内容
    private String orderByStr;

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

    public DdlPackMdmModelBean() {
        this.isHql = true;
        whereAppend = new StringBuilder();
        entityTypeList = new ArrayList<>();
        keyList = new ArrayList<>();
        valueList = new ArrayList<>();
    }

    public DdlPackMdmModelBean(boolean isHql) {
        this.isHql = isHql;
        whereAppend = new StringBuilder();
        entityTypeList = new ArrayList<>();
        keyList = new ArrayList<>();
        valueList = new ArrayList<>();
    }

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

        DdlMdmModelPreparedPack.getNumEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.MASTER, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(), "isMdmPublished", result);
        DdlMdmModelPreparedPack.getNumEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.MASTER, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), "isDeleted", result);
        DdlMdmModelPreparedPack.getNumEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.MASTER, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(), "isValid", result);
        DdlMdmModelPreparedPack.getStringEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.MASTER, organizeCode, "organizeCode", result);

        DdlMdmModelPreparedPack.getNumEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.BUSI, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(), "isMdmPublished", result);
        DdlMdmModelPreparedPack.getNumEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.BUSI, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), "isDeleted", result);
        DdlMdmModelPreparedPack.getNumEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.BUSI, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(), "isValid", result);
        DdlMdmModelPreparedPack.getStringEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.BUSI, organizeCode, "organizeCode", result);

        return result;
    }

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

        DdlMdmModelPreparedPack.getNumEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.MASTER, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(), "isMdmPublished", result);
        DdlMdmModelPreparedPack.getNumEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.MASTER, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), "isDeleted", result);
        DdlMdmModelPreparedPack.getNumEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.MASTER, isValid, "isValid", result);
        DdlMdmModelPreparedPack.getStringEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.MASTER, organizeCode, "organizeCode", result);

        DdlMdmModelPreparedPack.getNumEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.BUSI, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(), "isMdmPublished", result);
        DdlMdmModelPreparedPack.getNumEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.BUSI, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), "isDeleted", result);
        DdlMdmModelPreparedPack.getNumEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.BUSI, isValid, "isValid", result);
        DdlMdmModelPreparedPack.getStringEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.BUSI, organizeCode, "organizeCode", result);

        return result;
    }

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

        DdlMdmModelPreparedPack.getNumEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.MASTER, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(), "isMdmPublished", result);
        DdlMdmModelPreparedPack.getNumEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.MASTER, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), "isDeleted", result);
        DdlMdmModelPreparedPack.getNumEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.MASTER, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(), "isValid", result);

        DdlMdmModelPreparedPack.getNumEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.BUSI, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(), "isMdmPublished", result);
        DdlMdmModelPreparedPack.getNumEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.BUSI, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), "isDeleted", result);
        DdlMdmModelPreparedPack.getNumEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.BUSI, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(), "isValid", result);

        return result;
    }

    public static DdlPackMdmModelBean getDdlPackBean(BaseMdmBusiModelBean bean) {
        DdlPackMdmModelBean result = new DdlPackMdmModelBean();

        DdlMdmModelPreparedPack.getNumEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.MASTER, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(), "isMdmPublished", result);
        DdlMdmModelPreparedPack.getNumEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.MASTER, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), "isDeleted", result);
        DdlMdmModelPreparedPack.getNumEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.MASTER, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(), "isValid", result);

        DdlMdmModelPreparedPack.getNumEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.BUSI, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(), "isMdmPublished", result);
        DdlMdmModelPreparedPack.getNumEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.BUSI, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), "isDeleted", result);
        DdlMdmModelPreparedPack.getNumEqualPack(MdmEnumUtil.HQL_ENTITY_TYPE.BUSI, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(), "isValid", result);

        if (bean != null) {
            if (bean.getBusi() != null) {
                result.setOrderByStr(bean.getBusi().orderBy());
            } else if (bean.getMaster() != null) {
                result.setOrderByStr(bean.getMaster().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 List<MdmEnumUtil.HQL_ENTITY_TYPE> getEntityTypeList() {
        return entityTypeList;
    }

    public void setEntityTypeList(List<MdmEnumUtil.HQL_ENTITY_TYPE> entityTypeList) {
        this.entityTypeList = entityTypeList;
    }

    public List<String> getKeyList() {
        return keyList;
    }

    public void setKeyList(List<String> keyList) {
        this.keyList = keyList;
    }

    public List<Object> getValueList() {
        return valueList;
    }

    public void setValueList(List<Object> valueList) {
        this.valueList = valueList;
    }

    /**
     * 添加查询条件
     *
     * @param hql
     * @param val
     */
    public void addColumnQuery(MdmEnumUtil.HQL_ENTITY_TYPE entityType, String column, String hql, Object val) {
        if (entityTypeList == null) {
            entityTypeList = new ArrayList<>();
        }
        if (keyList == null) {
            keyList = new ArrayList<>();
        }
        if (valueList == null) {
            valueList = new ArrayList<>();
        }
        if (entityType != null && column != null && val != null) {
            entityTypeList.add(entityType);
            keyList.add(column);
            valueList.add(val);
        }

        whereAppend.append(hql);
    }

    /**
     * 需要查询属性的数量
     *
     * @return
     */
    public int getColumnSize() {
        return keyList.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();
    }
}
