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

import cn.estsh.i3plus.pojo.base.enumutil.BlockReportEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
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 String packHqlBrElement(BrElement brElement){
        StringBuffer result = new StringBuffer();

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

        return result.toString();
    }

    /**
     * 报表布局查询
     * @param brLayout
     * @return
     */
    public static String packHqlBrLayout(BrLayout brLayout){
        StringBuffer result = new StringBuffer();

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

        return result.toString();
    }

    /**
     * 按顺序查询行中的单元格
     * @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 String packHqlBrReport(BrReport brReport){
        StringBuffer result = new StringBuffer();

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

        return result.toString();
    }

    /**
     * 元素名称是否存在
     * @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();
    }
}
