package cn.estsh.i3plus.pojo.form.sqlpack;

import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.base.tool.HqlPack;
import cn.estsh.i3plus.pojo.form.bean.*;
import org.apache.commons.lang3.StringUtils;

/**
 * @Description :
 * @Reference :
 * @Author : Adair Peng
 * @CreateDate : 2019-03-21 15:16
 * @Modify:
 **/
public final class FormHqlPack {

    private FormHqlPack() {
    }

    /**
     * In 参数封装
     * @param columnName 列名
     * @param params 参数
     * @return hql
     */
    public static String packHqlIds(String columnName, String[] params) {
        StringBuffer result =  new StringBuffer();

        // 参数数组 [1,2,3] -> "1,2,3"
        HqlPack.getInPack(String.join(",", params), columnName, result);
        return result.toString();
    }

    /**
     * In 参数封装
     * @param columnName 列名
     * @param params 参数
     * @return hql
     */
    public static DdlPackBean packHqlIds(String columnName, Long[] params) {
        DdlPackBean result =  DdlPackBean.getDdlPackBean();

        // 参数数组 [1,2,3] -> "1,2,3"
        DdlPreparedPack.getInPack(StringUtils.join(params, ","), columnName, result);
        return result;
    }

    /**
     * In 参数封装
     * @param columnName 列名
     * @param params 参数
     * @param isDeleted 数据状态
     * @return hql
     */
    public static String packHqlIdsAndIsDeleted(String columnName, String[] params, Integer isDeleted) {
        StringBuffer result =  new StringBuffer(FormHqlPack.packHqlIds(columnName, params));

        HqlPack.getNumEqualPack(isDeleted, "isDeleted", result);
        return result.toString();
    }


    /**
     * In 参数封装
     * @param columnName 列名
     * @param params 参数
     * @param isDeleted 数据状态
     * @return hql
     */
    public static DdlPackBean packHqlIdsAndIsDeleted(String columnName, Long[] params, Integer isDeleted) {
        DdlPackBean result = FormHqlPack.packHqlIds(columnName, params);

        DdlPreparedPack.getNumEqualPack(isDeleted, "isDeleted", result);
        return result;
    }

    /**
     * 表单布局复杂查询
     * @param bfLayout 表单布局
     * @return hql
     */
    public static DdlPackBean packHqlBfLayout(BfLayout bfLayout) {
        DdlPackBean result =  DdlPackBean.getDdlPackBean(bfLayout);

        DdlPreparedPack.getStringLikerPack(bfLayout.getLayoutName(), "layoutName", result);
        DdlPreparedPack.getNumEqualPack(bfLayout.getIsDeleted(), "isDeleted", result);

        return result;
    }

    /**
     * 根据表单布局查询表单布局行
     * @param bfLayoutRow 表单布局
     * @return hql
     */
    public static DdlPackBean packHqlBfLayoutRow(BfLayoutRow bfLayoutRow) {
        DdlPackBean result =DdlPackBean.getDdlPackBean(bfLayoutRow);

        DdlPreparedPack.getNumEqualPack(bfLayoutRow.getLayoutId(), "layoutId", result);
        DdlPreparedPack.getNumEqualPack(bfLayoutRow.getIsDeleted(), "isDeleted", result);

        return result;
    }

    /**
     * 根据表单布局行查询表单布局列
     * @param bfLayoutColumn 表单布局行
     * @return hql
     */
    public static DdlPackBean packHqlBfLayoutColumn(BfLayoutColumn bfLayoutColumn) {
        DdlPackBean result = DdlPackBean.getDdlPackBean(bfLayoutColumn);

        DdlPreparedPack.getNumEqualPack(bfLayoutColumn.getLayoutRowId(), "layoutRowId", result);
        DdlPreparedPack.getNumEqualPack(bfLayoutColumn.getIsDeleted(), "isDeleted", result);

        return result;
    }

    /**
     * 菜单目录复杂查询
     * @param bfMenu 菜单目录
     * @return hql
     */
    public static DdlPackBean packHqlBfMenu(BfMenu bfMenu) {
        DdlPackBean result =  DdlPackBean.getDdlPackBean(bfMenu);

        DdlPreparedPack.getStringLikerPack(bfMenu.getMenuName(), "menuName", result);
        DdlPreparedPack.getNumEqualPack(bfMenu.getParentId(), "parentId", result);
        DdlPreparedPack.getNumEqualPack(bfMenu.getMenuStatus(), "menuStatus", result);
        DdlPreparedPack.getNumEqualPack(bfMenu.getIsDeleted(), "isDeleted", result);
        DdlPreparedPack.getNumEqualPack(bfMenu.getSoftType(), "softType", result);

        return result;
    }

    /**
     * 表单功能复杂查询
     * @param bfMethod 表单功能查询条件
     * @return hql
     */
    public static DdlPackBean packHqlBfMethod(BfMethod bfMethod) {
        DdlPackBean result =  DdlPackBean.getDdlPackBean(bfMethod);

        DdlPreparedPack.getStringLikerPack(bfMethod.getMethodName(), "methodName", result);
        DdlPreparedPack.getNumEqualPack(bfMethod.getLayoutId(), "layoutId", result);
        DdlPreparedPack.getNumEqualPack(bfMethod.getIsDeleted(), "isDeleted", result);

        return result;
    }

    /**
     * 表单功能明细复杂查询
     * @param bfMethodDetail 表单功能明细查询条件
     * @return hql
     */
    public static DdlPackBean packHqlBfMethodDetail(BfMethodDetail bfMethodDetail) {
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(bfMethodDetail);

        DdlPreparedPack.getStringLikerPack(bfMethodDetail.getMethodDetailName(), "methodDetailName", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(bfMethodDetail.getMethodId(), "layoutId", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(bfMethodDetail.getIsDeleted(), "isDeleted", ddlPackBean);

        return ddlPackBean;
    }

    /**
     * 表单拦截器复杂查询
     * @param bfIntercept 查询条件
     * @return hql
     */
    public static DdlPackBean packHqlBfIntercept(BfIntercept bfIntercept) {
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(bfIntercept);

        DdlPreparedPack.getStringLikerPack(bfIntercept.getInterceptName(), "interceptName", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(bfIntercept.getExecuteMode(), "executeMode", ddlPackBean);
        DdlPreparedPack.getStringLikerPack(bfIntercept.getExecuteContent(),"executeContent",ddlPackBean);
        DdlPreparedPack.getNumEqualPack(bfIntercept.getInterceptStatus(), "interceptStatus", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(bfIntercept.getIsDeleted(), "isDeleted", ddlPackBean);

        return ddlPackBean;
    }

    /**
     * 表单按钮复杂查询
     * @param bfButton 查询条件
     * @return 查询条件
     */
    public static DdlPackBean packHqlBfButton(BfButton bfButton){
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(bfButton);

        DdlPreparedPack.getStringLikerPack(bfButton.getButtonName(), "buttonName", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(bfButton.getTriggerMode(), "triggerMode", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(bfButton.getIsDeleted(), "isDeleted", ddlPackBean);
        ddlPackBean.setOrderByStr(bfButton.orderBy());

        return ddlPackBean;
    }

    /**
     * 根据角色id查询关联功能
     * @param roleIds 角色id
     * @return 查询条件
     */
    public static DdlPackBean packHqlBfRefMethodRoleByRoleIds(Long[] roleIds){
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean();

        DdlPreparedPack.getInPackArray(roleIds, "roleId", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), "isDeleted", ddlPackBean);

        return ddlPackBean;
    }

    /**
     * 按钮功能关系数据查询条件
     * @param bfRefButtonMethod 查询条件
     * @return DdlPackBean
     */
    public static DdlPackBean packHqlBfRefButtonMethod(BfRefButtonMethod bfRefButtonMethod) {
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(bfRefButtonMethod);

        DdlPreparedPack.getNumEqualPack(bfRefButtonMethod.getButtonId(), "buttonId", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(bfRefButtonMethod.getButtonPositionType(), "buttonPositionType", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(bfRefButtonMethod.getButtonPositionRefId(), "buttonPositionRefId", ddlPackBean);
        DdlPreparedPack.getStringLikerPack(bfRefButtonMethod.getButtonNameRdd(), "buttonNameRdd", ddlPackBean);
        DdlPreparedPack.getStringLikerPack(bfRefButtonMethod.getButtonPositionRefNameRdd(), "buttonPositionRefNameRdd", ddlPackBean);

        return ddlPackBean;
    }

    /**
     * 功能权限数据查询条件
     * @param bfRefMethodRole 查询条件
     * @return DdlPackBean
     */
    public static DdlPackBean packHqlBfRefMethodRole(BfRefMethodRole bfRefMethodRole) {
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(bfRefMethodRole);

        DdlPreparedPack.getNumEqualPack(bfRefMethodRole.getMethodId(), "methodId", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(bfRefMethodRole.getRoleId(), "roleId", ddlPackBean);

        return ddlPackBean;
    }

    /**
     * 拦截器明细查询条件
     * @param bfInterceptDetail 查询条件
     * @return DdlPackBean
     */
    public static DdlPackBean packHqlBfInterceptDetail(BfInterceptDetail bfInterceptDetail) {
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean();

        DdlPreparedPack.getNumEqualPack(bfInterceptDetail.getInterceptId(), "interceptId", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(bfInterceptDetail.getInterceptTargetId(), "interceptTargetId", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(bfInterceptDetail.getInterceptType(), "interceptType", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(bfInterceptDetail.getInterceptProcessResult(), "interceptProcessResult", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(bfInterceptDetail.getInterceptPosition(), "interceptPosition", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(bfInterceptDetail.getIsDeleted(), "isDeleted", ddlPackBean);

        ddlPackBean.setOrderByStr(bfInterceptDetail.orderBy());

        return ddlPackBean;
    }

    /**
     * 根据id数组查询按钮功能关系
     * @return
     */
    public static DdlPackBean packHqlBfRefButtonMethodByRefIds(Long[] ids) {
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean();

        DdlPreparedPack.getInPackArray(ids, "buttonPositionRefId", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), "isDeleted", ddlPackBean);

        return ddlPackBean;
    }

    /**
     * 根据功能id数组查询菜单信息
     * @param methodIds 功能id
     * @param status 菜单状态
     * @return
     */
    public static DdlPackBean findBfMenuByMethodIdsAndStatus(Long[] methodIds, int status) {
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean();

        DdlPreparedPack.getInPackArray(methodIds, "methodId", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(status, "menuStatus", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), "isDeleted", ddlPackBean);

        return ddlPackBean;
    }

    /**
     * 元素信息复杂查询
     * @param bfElement 查询条件
     * @return DdlPackBean
     */
    public static DdlPackBean packHqlBfElement(BfElement bfElement){
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean();

        DdlPreparedPack.getStringLikerPack(bfElement.getElementName(), "elementName", ddlPackBean);
        DdlPreparedPack.getStringLikerPack(bfElement.getElementCode(), "elementCode", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(bfElement.getDataObjectId(), "dataObjectId", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(bfElement.getElementType(), "elementType", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(bfElement.getIsDeleted(), "isDeleted", ddlPackBean);

        return ddlPackBean;
    }

    /**
     * 元素信息复杂查询
     * @param source 查询条件
     * @return DdlPackBean
     */
    public static DdlPackBean packHqlBfDataSource(BfDataSource source){
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(source);

        DdlPreparedPack.getStringLikerPack(source.getSourceHost(), "sourceHost", ddlPackBean);
        DdlPreparedPack.getStringLikerPack(source.getSourceName(), "sourceName", ddlPackBean);
        DdlPreparedPack.getStringLikerPack(source.getSourceCode(), "sourceCode", ddlPackBean);
        DdlPreparedPack.getStringLikerPack(source.getSourceDataBaseName(), "sourceDataBaseName", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(source.getSourceType(), "sourceType", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(source.getSourceStatus(), "sourceStatus", ddlPackBean);

        return ddlPackBean;
    }


    /**
     * 菜单树顺序
     * @param bfMenu 菜单目录
     * @return hql
     */
    public static DdlPackBean packHqlBfMenuTree(BfMenu bfMenu) {
        DdlPackBean result =  DdlPackBean.getDdlPackBean(bfMenu);

        DdlPreparedPack.getNegativeNumEqualPack(bfMenu.getId(), "parentId", result);
        DdlPreparedPack.getNumEqualPack(
                CommonEnumUtil.IS_VAILD.VAILD.getValue(), "isValid", result);
        DdlPreparedPack.getNumEqualPack(
                CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), "isDeleted", result);

        // 按顺序字段进行排序
        bfMenu.setOrderByParam("menuSort");
        bfMenu.setAscOrDesc(CommonEnumUtil.ASC_OR_DESC.DESC.getValue());
        result.setOrderByStr(bfMenu.orderBy());

        return result;
    }

    /**
     * 元素约束复杂查询
     * @param bfElementConstraint 查询条件
     * @return DdlPackBean
     */
    public static DdlPackBean packHqlBfElementConstraint(BfElementConstraint bfElementConstraint){
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(bfElementConstraint);

        DdlPreparedPack.getStringLikerPack(bfElementConstraint.getConstraintName(), "constraintName", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(bfElementConstraint.getElementId(), "elementId", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(bfElementConstraint.getConstraintType(), "constraintType", ddlPackBean);

        return ddlPackBean;
    }

    /**
     * 元素约束检查重复
     * @param bfElementConstraint 查询条件
     * @return DdlPackBean
     */
    public static DdlPackBean packHqlBfElementConstraintOnly(BfElementConstraint bfElementConstraint){
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(bfElementConstraint);

        DdlPreparedPack.getNumNOEqualPack(bfElementConstraint.getId(), "id", ddlPackBean);
        DdlPreparedPack.getStringLikerPack(bfElementConstraint.getConstraintName(), "constraintName", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(bfElementConstraint.getElementId(), "elementId", ddlPackBean);

        return ddlPackBean;
    }

    /**
     * 数据对象查询条件拼接
     * @param bfDataObject
     * @return
     */
    public static DdlPackBean packHqlBfDataObject(BfDataObject bfDataObject){
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(bfDataObject);

        DdlPreparedPack.getStringLikerPack(bfDataObject.getObjectName(), "objectName", ddlPackBean);
        DdlPreparedPack.getStringLikerPack(bfDataObject.getObjectClassName(), "objectClassName", ddlPackBean);

        return ddlPackBean;
    }

    /**
     * 数据对象查询条件拼接
     * @param bfCascade
     * @return
     */
    public static DdlPackBean packHqlBfCascade(BfCascade bfCascade){
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(bfCascade);

        DdlPreparedPack.getStringLikerPack(bfCascade.getCascadeName(), "cascadeName", ddlPackBean);
        DdlPreparedPack.getStringLikerPack(bfCascade.getCascadeDescription(), "cascadeDescription", ddlPackBean);

        return ddlPackBean;
    }

    /**
     * 数据对象查询条件拼接
     * @param bfMethodDetailPropertyRef
     * @return
     */
    public static DdlPackBean packHqlBfMethodDetailPropertyRef(BfMethodDetailPropertyRef bfMethodDetailPropertyRef){
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(bfMethodDetailPropertyRef);

        DdlPreparedPack.getNumEqualPack(bfMethodDetailPropertyRef.getDetailPropertyId(), "detailPropertyId", ddlPackBean);

        return ddlPackBean;
    }

}
