Ver código fonte

完善视图规则聚合。

zhangjinjun 6 anos atrás
pai
commit
f5402e8fcd

+ 16 - 0
src/main/java/com/yihu/quota/contants/ViewConstant.java

@ -0,0 +1,16 @@
package com.yihu.quota.contants;
/**
 * 视图相关的常量
 *
 * @author 张进军
 * @date 2018/10/10 11:08
 */
public class ViewConstant {
    /**
     * 合计维度编码
     */
    public static final String TOTAL_DIMENSION_CODE = "total";
}

+ 53 - 31
src/main/java/com/yihu/quota/service/view/ViewService.java

@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.ehr.elasticsearch.ElasticSearchPool;
import com.yihu.ehr.exception.ApiException;
import com.yihu.ehr.query.BaseJpaService;
import com.yihu.quota.contants.ViewConstant;
import com.yihu.quota.dao.view.ViewDao;
import com.yihu.quota.dao.view.ViewDimensionDao;
import com.yihu.quota.dao.view.ViewQuotaDao;
@ -90,6 +91,7 @@ public class ViewService extends BaseJpaService<View, ViewDao> {
     *
     * @param viewCode 视图编码
     * @return 视图规则
     * @author 张进军
     */
    public View getViewRule(String viewCode) {
        View view = viewDao.findByCode(viewCode);
@ -121,6 +123,8 @@ public class ViewService extends BaseJpaService<View, ViewDao> {
     *
     * @param viewCode        视图编码
     * @param filterModelList 页面的过滤条件模型集合
     * @return 聚合对象
     * @author 张进军
     */
    public Aggregations statViewResult(String viewCode, List<ViewQuotaFilterModel> filterModelList) throws Exception {
        // 获取视图规则
@ -195,26 +199,10 @@ public class ViewService extends BaseJpaService<View, ViewDao> {
                        ViewDimension otherRowDimension = otherRowDimensionList.get(i);
                        AggregationBuilder rowAgg = this.joinDimensionAgg(otherRowDimension);
                        // 末端行维度场合
                        if (i == otherRowDimensionCount - 1) {
                            // 组装列维度的聚合
                            if (colDimensionList != null && colDimensionList.size() != 0) {
                                for (ViewDimension colDimension : colDimensionList) {
                                    AggregationBuilder calAgg = this.joinDimensionAgg(colDimension);
                                    // 在列维度聚合上,嵌套指标的聚合
                                    List<AbstractAggregationBuilder> quotaAggList = this.gatherQuotaMetricsAgg(viewQuotaList, colDimension);
                                    for (AbstractAggregationBuilder quotaAgg : quotaAggList) {
                                        calAgg.subAggregation(quotaAgg);
                                    }
                                    rowAgg.subAggregation(calAgg);
                                }
                            } else {
                                // 没有列维度的场合,在末端行维度聚合上,嵌套指标的聚合
                                List<AbstractAggregationBuilder> quotaAggList = this.gatherQuotaMetricsAgg(viewQuotaList, otherRowDimension);
                                for (AbstractAggregationBuilder quotaAgg : quotaAggList) {
                                    rowAgg.subAggregation(quotaAgg);
                                }
                            }
                            // 末端行维度场合
                            // 汇总列维度的聚合及嵌套其指标聚合,并列嵌套于末端行维度的聚合
                            this.gatherColDimensionsAndQuotasAgg(rowAgg, otherRowDimension, colDimensionList, viewQuotaList);
                            preRowAgg = rowAgg;
                        } else {
                            rowAgg.subAggregation(preRowAgg);
@ -224,11 +212,9 @@ public class ViewService extends BaseJpaService<View, ViewDao> {
                        topAgg.subAggregation(rowAgg);
                    }
                } else {
                    // 行维度组内只有一个行维度的场合,在顶层行维度聚合上,嵌套指标的聚合
                    List<AbstractAggregationBuilder> quotaAggList = this.gatherQuotaMetricsAgg(viewQuotaList, topRowDimension);
                    for (AbstractAggregationBuilder quotaAgg : quotaAggList) {
                        topAgg.subAggregation(quotaAgg);
                    }
                    // 行维度组内只有一个行维度的场合
                    // 汇总列维度的聚合及嵌套其指标聚合,并列嵌套于顶层行维度的聚合
                    this.gatherColDimensionsAndQuotasAgg(topAgg, topRowDimension, colDimensionList, viewQuotaList);
                }
                if (topFilterAgg != null) {
@ -328,16 +314,19 @@ public class ViewService extends BaseJpaService<View, ViewDao> {
     * @throws IOException
     */
    private List<AbstractAggregationBuilder> gatherQuotaMetricsAgg(List<ViewQuota> viewQuotaList, ViewDimension viewDimension) throws IOException {
        String aggNamePre = "";
        if (viewDimension != null) {
            String dimensionCode = viewDimension.getDimensionCode();
            // TODO 合计 二字需要替换成固定的编码
            aggNamePre = "合计".equals(dimensionCode) ? dimensionCode + "-" : "";
        }
        List<AbstractAggregationBuilder> quotaAggList = new ArrayList<>();
        for (ViewQuota quota : viewQuotaList) {
            if ("basic".equals(quota.getFormulaMode())) {
                String aggNamePre = "";
                if (viewDimension == null) {
                    // 视图展示位数值类型的场合
                    aggNamePre += quota.getCode() + "-";
                } else {
                    // 合计维度场合
                    String dimensionCode = viewDimension.getDimensionCode();
                    aggNamePre = ViewConstant.TOTAL_DIMENSION_CODE.equals(dimensionCode) ? dimensionCode + "-" : "";
                }
                if ("sum".equals(quota.getBasicFormulaType())) {
                    // 求和
                    String aggName = aggNamePre + "sum-result";
@ -475,4 +464,37 @@ public class ViewService extends BaseJpaService<View, ViewDao> {
        return dimensionAgg;
    }
    /**
     * 汇总列维度的聚合及嵌套其指标聚合,并列嵌套于上层聚合
     *
     * @param upperAgg         上层聚合对象
     * @param rowViewDimension 视图的行维度
     * @param colDimensionList 视图的列维度集合
     * @param viewQuotaList    视图的指标集合
     * @throws IOException
     */
    private void gatherColDimensionsAndQuotasAgg(AggregationBuilder upperAgg,
                                                 ViewDimension rowViewDimension,
                                                 List<ViewDimension> colDimensionList,
                                                 List<ViewQuota> viewQuotaList) throws IOException {
        if (colDimensionList != null && colDimensionList.size() != 0) {
            // 组装列维度的聚合
            for (ViewDimension colDimension : colDimensionList) {
                AggregationBuilder calAgg = this.joinDimensionAgg(colDimension);
                // 在列维度聚合上,嵌套指标的聚合
                List<AbstractAggregationBuilder> quotaAggList = this.gatherQuotaMetricsAgg(viewQuotaList, colDimension);
                for (AbstractAggregationBuilder quotaAgg : quotaAggList) {
                    calAgg.subAggregation(quotaAgg);
                }
                upperAgg.subAggregation(calAgg);
            }
        } else {
            // 没有列维度的场合,在上层行维度聚合上,嵌套指标的聚合
            List<AbstractAggregationBuilder> quotaAggList = this.gatherQuotaMetricsAgg(viewQuotaList, rowViewDimension);
            for (AbstractAggregationBuilder quotaAgg : quotaAggList) {
                upperAgg.subAggregation(quotaAgg);
            }
        }
    }
}