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

import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.BlockReportEnumUtil;
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.report.bean.*;
import org.apache.commons.lang3.StringUtils;

/**
 * @Description : 报表对象封装
 * @Reference :
 * @Author : yunhao
 * @CreateDate : 2019-01-17 15:41
 * @Modify:
 **/
public class ReportHqlPack {

    /**
     * In 参数封装
     * @param columnName
     * @return
     */
    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
     * @return
     */
    public static String packHqlIds(String columnName,Long[] params){
        StringBuffer result =  new StringBuffer();

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

    /**
     * 目录查询封装
     * @param menu
     * @return
     */
    public static String packHqlBrMenu(BrMenu menu){
        StringBuffer result = new StringBuffer();

        // 查询参数封装
        HqlPack.getNumEqualPack(menu.getParentId(),"parentId",result);
        HqlPack.getNumEqualPack(menu.getMenuType(),"menuType",result);
        HqlPack.getNumEqualPack(menu.getMenuStatus(),"menuStatus",result);
        HqlPack.getStringLikerPack(menu.getName(),"name",result);
        HqlPack.getStringLikerPack(menu.getMenuCode(),"menuCode",result);

        if(menu.getFindIdList() != null && menu.getFindIdList().size() > 0){
            HqlPack.getInPack(StringUtils.join(menu.getFindIdList(),","),"id",result);
        }

        // 添加默认排序
        HqlPack.getOrderDefault(menu);

        return result.toString();
    }

//    /**
//     * 目录查询封装
//     * @param template
//     * @return
//     */
//    public static String packHqlBrTemplate(BrTemplate template){
//        StringBuffer result = new StringBuffer();
//
//        // 查询参数封装
//        HqlPack.getNumEqualPack(template.getTemplateType(),"templateType",result);
//        if(template.getTemplateStatus() != null){
//            if(template.getTemplateStatus().intValue() == CommonEnumUtil.USER_STATUS.DISABLE.getValue()){
//                HqlPack.getNumEqualPack(BlockReportEnumUtil.TEMPLATE_STATUS.SAVE_TEMPLATE_ATTR_CONFIRM.getValue(),"templateStatus",result);
//            }else {
//                HqlPack.getNumSmallerPack(BlockReportEnumUtil.TEMPLATE_STATUS.SAVE_TEMPLATE_ATTR_CONFIRM.getValue(),"templateStatus",result);
//            }
//        }
//        HqlPack.getStringLikerPack(template.getName(),"name",result);
//
//        // 添加默认排序
//        HqlPack.getOrderDefault(template);
//
//        return result.toString();
//    }
//
    /**
     * 报表元素查询
     * @param brElement
     * @return
     */
    public static DdlPackBean packHqlBrElement(BrElement brElement){
        DdlPackBean result = DdlPackBean.getDdlPackBean(brElement);

        // 查询参数封装
        DdlPreparedPack.getNumEqualPack(brElement.getElementType(),"elementType",result);
        DdlPreparedPack.getStringLikerPack(brElement.getElementName(),"elementName",result);

        return result;
    }

    /**
     * 报表布局查询
     * @param brLayout
     * @return
     */
        public static DdlPackBean packHqlBrLayout(BrLayout brLayout){
        DdlPackBean result = DdlPackBean.getDdlPackBean(brLayout);

        // 查询参数封装
        DdlPreparedPack.getStringLikerPack(brLayout.getLayoutName(),"layoutName",result);

        return result;
    }

    /**
     * 按顺序查询行中的单元格
     * @param rowId
     * @return
     */
    public static String packHqlBrLayoutColumnByRowIdSort(Long rowId){
        StringBuffer result = new StringBuffer();

        // 查询参数封装
        HqlPack.getNumEqualPack(rowId,"layoutRowId",result);
        HqlPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue(),CommonEnumUtil.ASC_OR_DESC.DESC.getValue()},new String[]{"columnSort","modifyDatetime"},result);

        return result.toString();
    }

    /**
     * 按顺序查询报表中的行
     * @param layoutId
     * @return
     */
    public static String packHqlBrLayoutRowByLayoutIdSort(Long layoutId){
        StringBuffer result = new StringBuffer();

        // 查询参数封装
        HqlPack.getNumEqualPack(layoutId,"layoutId",result);
        HqlPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue(),CommonEnumUtil.ASC_OR_DESC.DESC.getValue()},new String[]{"rowSort","modifyDatetime"},result);

        return result.toString();
    }

    /**
     * 报表查询
     * @param brReport
     * @return
     */
    public static DdlPackBean packHqlBrReport(BrReport brReport){
        DdlPackBean result = DdlPackBean.getDdlPackBean(brReport);

        // 查询参数封装
        DdlPreparedPack.getStringLikerPack(brReport.getReportName(),"reportName",result);
        DdlPreparedPack.getNumEqualPack(brReport.getLayoutId(),"layoutId",result);

        return result;
    }

    /**
     * 报表查询
     * @param refReport
     * @return
     */
    public static DdlPackBean packHqlBrRefReport(BrRefReport refReport){
        DdlPackBean result = DdlPackBean.getDdlPackBean(refReport);

        // 查询参数封装
        DdlPreparedPack.getNumEqualPack(refReport.getReportId(),"reportId",result);
        DdlPreparedPack.getNumEqualPack(refReport.getLayoutId(),"layoutId",result);
        DdlPreparedPack.getNumEqualPack(refReport.getLayoutElementId(),"layoutElementId",result);
        DdlPreparedPack.getNumEqualPack(refReport.getRefTypeId(),"refTypeId",result);
        DdlPreparedPack.getNumEqualPack(refReport.getRefId(),"refId",result);

        return result;
    }

    /**
     * 元素名称是否存在
     * @param brElement
     * @return
     */
    public static String packHqlBrElementName(BrElement brElement){
        StringBuffer result =  new StringBuffer();

        // and
        HqlPack.getStringEqualPack(brElement.getElementName(),"elementName",result);
        // not
        HqlPack.getNumNOEqualPack(brElement.getId(),"id",result);

        return result.toString();
    }

    public static DdlPackBean packHqlBrPojo(BrPojo pojo){
        DdlPackBean result = DdlPackBean.getDdlPackBean();

        DdlPreparedPack.getNumEqualPack(pojo.getMasterId(),"masterId",result);
        DdlPreparedPack.getNumEqualPack(pojo.getSoftType(),"softType",result);
        DdlPreparedPack.getNumEqualPack(pojo.getJoinType(),"joinType",result);
        DdlPreparedPack.getNumEqualPack(pojo.getPojoType(),"pojoType",result);
        DdlPreparedPack.getNumEqualPack(pojo.getWhereType(),"whereType",result);
        DdlPreparedPack.getStringEqualPack(pojo.getPojoTableNameAlias(),"pojoTableNameAlias",result);
        DdlPreparedPack.getStringLikerPack(pojo.getPojoName(),"pojoName",result);
        DdlPreparedPack.getStringLikerPack(pojo.getPojoTableName(),"pojoTableName",result);

        return result;
    }

    public static DdlPackBean packHqlBrPojoFilter(BrPojoFilter bean){
        DdlPackBean result = DdlPackBean.getDdlPackBean();

        DdlPreparedPack.getNumEqualPack(bean.getMasterId(),"masterId",result);

        return result;
    }

    public static DdlPackBean packHqlBrPojoFilterAttr(BrPojoFilterAttr bean){
        DdlPackBean result = DdlPackBean.getDdlPackBean();

        DdlPreparedPack.getNumEqualPack(bean.getFilterId(),"filterId",result);
        DdlPreparedPack.getNumEqualPack(bean.getMasterId(),"masterId",result);
        DdlPreparedPack.getNumEqualPack(bean.getPojoId(),"pojoId",result);

        return result;
    }

    public static DdlPackBean packHqlBrPojoView(BrPojoView bean){
        DdlPackBean result = DdlPackBean.getDdlPackBean(bean);

        DdlPreparedPack.getNumEqualPack(bean.getPojoId(),"pojoId",result);
        DdlPreparedPack.getNumEqualPack(bean.getViewType(),"viewType",result);

        return result;
    }

    public static DdlPackBean packHqlBrPojoViewAttr(BrPojoViewAttr bean){
        DdlPackBean result = DdlPackBean.getDdlPackBean(bean);

        DdlPreparedPack.getNumEqualPack(bean.getPojoId(),"pojoId",result);
        DdlPreparedPack.getNumEqualPack(bean.getViewId(),"viewId",result);

        return result;
    }

    public static DdlPackBean packHqlBrLayoutElement(BrLayoutElement bean){
        DdlPackBean result = DdlPackBean.getDdlPackBean(bean);

        DdlPreparedPack.getNumEqualPack(bean.getLayoutId(),"layoutId",result);

        return result;
    }
}
