杜洪波
4 天以前 3b6b2e8d625b3d9dfc1a819b52e23da1a493506e
src/main/java/com/product/server/report/service/GenerateEChartService.java
@@ -21,9 +21,12 @@
import com.product.server.report.service.idel.IGenerateEChartService;
import com.product.util.BaseUtil;
import com.product.util.SystemParamReplace;
import org.apache.bcel.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
/**
@@ -49,6 +52,7 @@
    public DataListReportService dataListReportService;
    @Autowired
    QueryFilterService queryFilterService;
    @Override
    public JSONArray generatePieEChartMaster(FieldSetEntity fse) throws BaseException {
        String uuid = fse.getString(CmnConst.UUID);
@@ -77,101 +81,105 @@
    @Override
    public JSONObject generatePieEChart(FieldSetEntity fse) throws BaseException {
        //数据源uuid
        // 获取数据源UUID 和 报表类型UUID
        String report_datasource_uuid = fse.getString(CmnConst.REPORT_DATASOURCE_UUID);
        //类型uuid
        String report_type_config_uuid = fse.getString(CmnConst.REPORT_TYPE_CONFIG_UUID);
        if(BaseUtil.strIsNull(report_datasource_uuid) || BaseUtil.strIsNull(report_type_config_uuid)){
            return BaseUtil.fieldSetEntityToJson(fse);
        }
        //查询类型表
        FieldSetEntity typeField = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_REPORT_TYPE_CONFIG, report_type_config_uuid,false);
        //获取配置报表数据源data数据表 .PRODUCT_SYS_report_datasource_config子表
        FieldSetEntity datasourceField = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_REPORT_DATASOURCE_CONFIG,report_datasource_uuid,true);
        // 获取报表类型
        FieldSetEntity fseReportType = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_REPORT_TYPE_CONFIG, report_type_config_uuid, false);
        // 获取报表数据源配置表和字段表
        FieldSetEntity datasourceField = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_REPORT_DATASOURCE_CONFIG, report_datasource_uuid, true);
        //获取查询的数据sql
        String sql_text = datasourceField.getString(CmnConst.SQL_TEXT);
        //解析SQL里面的系统参数
        sql_text = SystemParamReplace.systemParamsReplace(sql_text);
        String type_group = typeField.getString(CmnConst.TYPE_GROUP);
        String typeGroup = fseReportType.getString(CmnConst.TYPE_GROUP);
        //为关系图
        if("relationCharts".equals(type_group)){
        if("relationCharts".equals(typeGroup)){
            JSONObject obj = this.chooseRelationship(sql_text);
            String typeGroup = typeField.getString(CmnConst.TYPE_GROUP);
            JSONObject object = new JSONObject();
            object.put(CmnConst.TYPE_GROUP,typeGroup);
            object.put(CmnConst.TYPE_GROUP, typeGroup);
            object.put(CmnConst.OPTION,obj);
//            object.put("function",this.functionArray(uuid));
            return object;
        }
        // 获取报表数据源数据
        DataTableEntity valueData = baseDao.listTable(sql_text,new Object[]{});
        //获取报表配置属性表  加echarts配置属性
        //只能查询到关联属性值的属性表,如果父级属性下有多个相同子属性。通过grouping分组。
        DataTableEntity attributeData = baseDao.listTable("SELECT a.field_name,a.report_type_attr_value,a.uuid,a.grouping ,b.* FROM product_sys_report_config_attribute a LEFT JOIN product_sys_report_type_attribute b on a.report_type_attr = b.uuid WHERE a.report_config_uuid = ? ORDER BY a.grouping,b.parentAttributeUuid " ,new Object[]{fse.getString(CmnConst.UUID)});
        //只能查询到关联属性值的属性表,如果父级属性下有多个相同子属性。通过group_sign分组。
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT a.uuid,a.field_name,a.report_type_attr_value,a.group_sign ,b.* \n");
        sql.append("FROM product_sys_report_config_attribute a \n");
        sql.append("LEFT JOIN product_sys_report_type_attribute b on a.report_type_attr = b.uuid \n");
        sql.append("WHERE a.report_config_uuid = ? \n");
        sql.append("ORDER BY a.group_sign,b.parent_attribute_uuid");
        DataTableEntity attributeData = baseDao.listTable(sql.toString() ,new Object[]{fse.getString(CmnConst.UUID)});
        // 创建图表属性容器
        JSONObject allProperties = new JSONObject();
        if(attributeData.getRows() > 0){
        if(!BaseUtil.dataTableIsEmpty(attributeData)){
            JSONObject propertyObject = new JSONObject();
            String parenUuid1 = attributeData.getString(0,"parentAttributeUuid");
            String grouping1 = attributeData.getString(0,"grouping");
            for (int i = 0,length = attributeData.getRows(); i < length; i++) {
                FieldSetEntity fieldSet = attributeData.getFieldSetEntity(i);
                //上级uuid
                String parenUuid2 = fieldSet.getString("parentAttributeUuid");
                //上级name
                String parenName = fieldSet.getString("parentAttributeName");
                //图表属性值
                String attr_value = fieldSet.getString("report_type_attr_value");
                //为空传默认值
                if(BaseUtil.strIsNull(attr_value)){
                    attr_value = fieldSet.getString("default_value");
            String preParenAttrUuid = attributeData.getString(0, CmnConst.PARENT_ATTRIBUTE_UUID);
            String preGroupSign = attributeData.getString(0, CmnConst.GROUP_SIGN);
            for (int i = 0, length = attributeData.getRows(); i < length; i++) {
                FieldSetEntity fseAttr = attributeData.getFieldSetEntity(i);
                // 图表属性和配置值
                String attrName = fseAttr.getString(CmnConst.ATTRIBUTE_NAME);
                String attrValue = fseAttr.getString(CmnConst.REPORT_TYPE_ATTR_VALUE);
                if(BaseUtil.strIsNull(attrValue)){
                   // 配置值为空则用默认值
                   attrValue = fseAttr.getString("default_value");
                }
                //图表属
                String attribute_name = fieldSet.getString("attribute_name");
                //通过数据库插入数据
                String fieldName = fieldSet.getString(CmnConst.FIELD_NAME);
                //通过数据库插入数据
                String grouping2 = fieldSet.getString("grouping");
                // 当前属性的上级属性uuid和name
                String curParenAttrUuid = fseAttr.getString(CmnConst.PARENT_ATTRIBUTE_UUID);
                String curParenAttrName = fseAttr.getString(CmnConst.PARENT_ATTRIBUTE_NAME);
                // 获取当前属性配置字段 和 分组值
                String fieldName = fseAttr.getString(CmnConst.FIELD_NAME);
                String curGroupSign = fseAttr.getString(CmnConst.GROUP_SIGN);
                //普通属性都有上级属性, 特殊的颜色风格属性无上级属性,本身就是顶级属性。
                if(!BaseUtil.strIsNull(parenUuid2)) {
                if(!BaseUtil.strIsNull(curParenAttrUuid)) {
                    //之前上级uuid为空
                    if(BaseUtil.strIsNull(parenUuid1)) {
                        parenUuid1 = parenUuid2;
                    if(BaseUtil.strIsNull(preParenAttrUuid)) {
                       preParenAttrUuid = curParenAttrUuid;
                        //有不同的属性,先把之前同样的属性封装。
                    }else if (!BaseUtil.strIsNull(parenUuid1) && !parenUuid2.equals(parenUuid1)) {
                    }else if (!BaseUtil.strIsNull(preParenAttrUuid) && !curParenAttrUuid.equals(preParenAttrUuid)) {
                        //封装之前的json数据
                        this.getAllProperties(parenUuid1, propertyObject, allProperties);
                        parenUuid1 = parenUuid2;
                    } else if ((parenUuid2.equals(parenUuid1) && !BaseUtil.strIsNull(grouping2))) {
                        if (!grouping2.equals(grouping1)) {
                            this.getAllProperties(parenUuid1, propertyObject, allProperties);
                        this.getAllJSONAttrs(preParenAttrUuid, propertyObject, allProperties);
                        preParenAttrUuid = curParenAttrUuid;
                    } else if ((curParenAttrUuid.equals(preParenAttrUuid) && !BaseUtil.strIsNull(curGroupSign))) {
                        if (!curGroupSign.equals(preGroupSign)) {
                            this.getAllJSONAttrs(preParenAttrUuid, propertyObject, allProperties);
                        }
                    }
                }else{
                    //上级属性为option,说明他为最大属性没有上级  直接添加进allProperties
                    if("option".equals(parenName)){
                        allProperties.put(attribute_name,changeDataFormat(attr_value)[0]);
                    if("option".equals(curParenAttrName)){
                        allProperties.put(attrName, changeDataFormat(attrValue)[0]);
                        continue;
                    }
                }
                grouping1 = grouping2;
                preGroupSign = curGroupSign;
                //饼状图插入数据同属一个父元素  多个元素和对应的数据需全部找出来一起封装json
                if(!BaseUtil.strIsNull(fieldName)){
                    String parentAttributeName = fieldSet.getString("parentAttributeName");
                    String parentAttributeName = fseAttr.getString(CmnConst.PARENT_ATTRIBUTE_NAME);
                    //下级为对象还是数组
                    String type = fieldSet.getString("subordinateElementType");
                    String type = fseAttr.getString(CmnConst.SUBORDINATE_ELEMENT_TYPE);
                    List<String> keys = ListUtils.newArrayList();
                    List<String> keys2 = ListUtils.newArrayList();
                    keys.add(fieldName);
                    keys2.add(attribute_name);
                    keys2.add(attrName);
                    //为最后一位
                    while (i < length-1) {
                        FieldSetEntity fieldSet2 = attributeData.getFieldSetEntity(i+1);
                        String parenUuid3 = fieldSet2.getString("parentAttributeUuid");
                        String parenUuid3 = fieldSet2.getString(CmnConst.PARENT_ATTRIBUTE_UUID);
                        String fieldName3 = fieldSet2.getString(CmnConst.FIELD_NAME);
                        if(parenUuid2.equals(parenUuid3) && !BaseUtil.strIsNull(fieldName3)){
                        if(curParenAttrUuid.equals(parenUuid3) && !BaseUtil.strIsNull(fieldName3)){
                            keys.add(fieldName3);
                            keys2.add(fieldSet2.getString("attribute_name"));
                            keys2.add(fieldSet2.getString(CmnConst.ATTRIBUTE_NAME));
                        }else {
                            break;
                        }
@@ -183,21 +191,21 @@
                        propertyObject.put(parentAttributeName, this.attributesAndValues(valueData, keys, keys2, type));
                    }
                }else {
                    propertyObject.put(attribute_name,changeDataFormat(attr_value)[0]);
                    propertyObject.put(attrName, changeDataFormat(attrValue)[0]);
                }
                if(i == (length - 1)){
                    //封装最后一组数据
                    this.getAllProperties(parenUuid1,propertyObject,allProperties);
                    this.getAllJSONAttrs(preParenAttrUuid, propertyObject, allProperties);
                }
            }
        }
        JSONObject s=new JSONObject();
        //图表边距
//        JSONObject s=new JSONObject();
//        s.put("bottom",30);
//        s.put("left",30);
//        s.put("right",0);
//        allProperties.put("grid",s);
        String typeGroup = typeField.getString(CmnConst.TYPE_GROUP);
        JSONObject object = BaseUtil.fieldSetEntityToJson(fse);
        object.put(CmnConst.TYPE,typeGroup);
        object.put(CmnConst.OPTION,allProperties);
@@ -207,38 +215,35 @@
    /**
     * 获取上级数据并递归拼装上级属性
     * @param superiorUuid 上级uuid
     * @param obj
     * @param superiorAttrUUID      上级属性UUID
     * @param noSuperiorJsonAttr   未封装上级的JSON属性
     * @param allProperties         图表总JSON属性
     * @return
     * @throws BaseException
     */
    @Override
    public void getAllProperties(String superiorUuid,JSONObject obj,JSONObject allProperties) throws BaseException {
        FieldSetEntity fieldSet = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_REPORT_TYPE_ATTRIBUTE,superiorUuid,false);
        String attribute_name = fieldSet.getString("attribute_name");
    public void getAllJSONAttrs(String superiorAttrUUID, JSONObject noSuperiorJsonAttr, JSONObject allProperties) throws BaseException {
       // 获取更高级父级属性
        FieldSetEntity fseAttrInfo = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_REPORT_TYPE_ATTRIBUTE, superiorAttrUUID, false);
        String attribute_name = fseAttrInfo.getString(CmnConst.ATTRIBUTE_NAME);
        JSONObject superiorObj = new JSONObject();
        JSONArray objSubArray = obj.getJSONArray(attribute_name);
        //确定他的子属性时(1)array 还是(0)object
//        if(objSubArray == null){
            if ("1".equals(fieldSet.getString("subordinateElementType"))) {
                JSONArray array = new JSONArray();
                array.add(obj);
                superiorObj.put(attribute_name, array);
            } else {
                superiorObj.put(attribute_name, obj);
            }
//        }else if(objSubArray != null){
//            superiorObj = obj;
//        }
        //上级属性name
        superiorUuid = fieldSet.getString("parentAttributeUuid");
        //上级名称
        String parentAttributeName = fieldSet.getString("parentAttributeName");
        // 判定属性值是Object数组还是Object对象(即子属性是对象还是数组,0为obj对象,1为array数组)
        if ("1".equals(fseAttrInfo.getString(CmnConst.SUBORDINATE_ELEMENT_TYPE))) {
           JSONArray array = new JSONArray();
           array.add(noSuperiorJsonAttr);
           superiorObj.put(attribute_name, array);
//           superiorObj.put(attribute_name, Arrays.asList(noSuperiorJsonAttr));
        } else {
           superiorObj.put(attribute_name, noSuperiorJsonAttr);
        }
        // 获取当前属性父级属性UUID 和 NAME
        superiorAttrUUID = fseAttrInfo.getString(CmnConst.PARENT_ATTRIBUTE_UUID);
        String parentAttributeName = fseAttrInfo.getString(CmnConst.PARENT_ATTRIBUTE_NAME);
        //如果父元素uuid为空
        if(BaseUtil.strIsNull(superiorUuid)){
        if(BaseUtil.strIsNull(superiorAttrUUID)){
            JSONArray allSubProperties = allProperties.getJSONArray(attribute_name);
            if(allSubProperties != null){
                JSONObject subObj = JSONObject.parseObject(obj.toJSONString());
                JSONObject subObj = JSONObject.parseObject(noSuperiorJsonAttr.toJSONString());
                if(allSubProperties.size() > 0){
                    //是扩充当前子元素  还是同级新增一个子元素
                    Boolean is_extend = true;
@@ -258,23 +263,21 @@
                        for (String key :subObj.keySet()) {
                            subPropertiesJSONObject.put(key, subObj.get(key));
                        }
                        obj.clear();
                        noSuperiorJsonAttr.clear();
                        return;
                    }
                    }
                allSubProperties.add(subObj);
                obj.clear();
                noSuperiorJsonAttr.clear();
                return;
            }
            //克隆json
            JSONObject copy;
            Object o = superiorObj.get(attribute_name);
            if(o instanceof JSONArray){
                JSONArray oar = (JSONArray) o;
                JSONArray arr = JSONObject.parseArray(oar.toJSONString());
                allProperties.put(attribute_name,arr);
            }else {
                copy = JSONObject.parseObject(superiorObj.toJSONString());
               JSONObject copy = JSONObject.parseObject(superiorObj.toJSONString());
                allProperties.put(attribute_name,copy.getJSONObject(attribute_name));
            }
        }else {
@@ -295,7 +298,7 @@
                        }
                    }
                }
                 this.getAllProperties(superiorUuid,newObj,allProperties);
                 this.getAllJSONAttrs(superiorAttrUUID, newObj, allProperties);
            }else {
                JSONObject currentProperty = null;
                if(o instanceof JSONArray) {
@@ -321,7 +324,22 @@
                        if(theOriginalData == null){
                            currentProperty.put(attribute_name,arr);
                        }else {
                            theOriginalData.add(arr.getJSONObject(arr.size()-1));
                           JSONObject lastJSONObject = theOriginalData.getJSONObject(theOriginalData.size() - 1);
                           JSONObject insertJsonObject = arr.getJSONObject(arr.size()-1);
                           for (String key : insertJsonObject.keySet()) {
                                if (lastJSONObject.containsKey(key)) {
                                    // key已存在,新建对象
                                   theOriginalData.add(insertJsonObject);
                                    break;
                                } else {
                                    // key不存在,合并
                                   lastJSONObject.putAll(insertJsonObject);
                                    break;
                                }
                            }
//                           theOriginalData.getJSONObject(theOriginalData.size() - 1)
//                            .putAll(JSONObject.parseObject(arr.getJSONObject(arr.size()-1).toJSONString()));
//                            theOriginalData.add(arr.getJSONObject(arr.size()-1));
                        }
                    }else {
@@ -339,7 +357,7 @@
                }
            }
        }
        obj.clear();
        noSuperiorJsonAttr.clear();
    }
    /**
@@ -423,7 +441,7 @@
                oArray[0] = subObj;
            }
        }else {
            oArray[0] = attr_value;
            oArray[0] = SystemParamReplace.systemParamsReplace(attr_value);
        }
        return oArray;
    }