@ -1,5 +1,19 @@
package cn.estsh.i3plus.pojo.report.sqlpack ;
package cn.estsh.i3plus.pojo.report.sqlpack ;
import cn.estsh.i3plus.pojo.base.bean.BaseResultBean ;
import cn.estsh.i3plus.pojo.base.enumutil.BlockReportEnumUtil ;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil ;
import cn.estsh.i3plus.pojo.model.report.TemplateModel ;
import cn.estsh.i3plus.pojo.report.bean.BrPojoAttr ;
import cn.estsh.i3plus.pojo.report.bean.BrRefServerPojo ;
import cn.estsh.i3plus.pojo.report.bean.BrTemplateCustomHql ;
import com.alibaba.fastjson.JSON ;
import org.apache.commons.lang3.StringUtils ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import java.util.* ;
/ * *
/ * *
* @Description : 报 表 对 象 封 装
* @Description : 报 表 对 象 封 装
* @Reference :
* @Reference :
@ -9,5 +23,400 @@ package cn.estsh.i3plus.pojo.report.sqlpack;
* * /
* * /
public class TemplateHqlPack {
public class TemplateHqlPack {
private static final Logger LOGGER = LoggerFactory . getLogger ( TemplateHqlPack . class ) ;
/ * *
* 拼 接 查 询 字 段
*
* @param attrs
* @return
* /
public static String getSelectHqlColumn ( List < BrPojoAttr > attrs ) {
StringBuffer result = new StringBuffer ( ) ;
BlockReportEnumUtil . HQL_AGGREGATION aggr = null ;
if ( attrs ! = null & & attrs . size ( ) > 0 ) {
for ( BrPojoAttr attr : attrs ) {
aggr = BlockReportEnumUtil . HQL_AGGREGATION . valueOf ( attr . getAggregationType ( ) ) ;
// 聚合函数控制
if ( aggr = = null ) {
result . append ( "," + attr . getPojoNameAlias ( ) + "." + attr . getAttrName ( ) ) ;
} else {
result . append ( ", " + aggr . getName ( ) + "(" + attr . getPojoNameAlias ( ) + "." + attr . getAttrName ( ) + ")" ) ;
}
// 添加别名
if ( StringUtils . isNotBlank ( attr . getAttrNameAlias ( ) ) ) {
result . append ( " AS " + attr . getAttrNameAlias ( ) ) ;
} else {
result . append ( " AS " + attr . getPojoNameAlias ( ) + "_" + attr . getAttrName ( ) ) ;
}
}
return result . substring ( result . indexOf ( "," ) + 1 , result . length ( ) ) ;
}
return result . toString ( ) ;
}
/ * *
* 拼 接 分 组 字 段
*
* @param attrs
* @return
* /
public static String getSelectHqlGroupColumn ( List < BrPojoAttr > attrs ) {
StringBuffer result = new StringBuffer ( ) ;
BlockReportEnumUtil . HQL_AGGREGATION aggr = null ;
if ( attrs ! = null & & attrs . size ( ) > 0 ) {
for ( BrPojoAttr attr : attrs ) {
result . append ( "," + attr . getPojoNameAlias ( ) + "." + attr . getAttrName ( ) ) ;
}
}
return result . toString ( ) ;
}
public static StringBuffer getSelectHqlJoin ( List < BrRefServerPojo > list ) {
StringBuffer hqlJoin = new StringBuffer ( ) ;
if ( list ! = null & & list . size ( ) > 0 ) {
BrRefServerPojo masterPojo = list . get ( 0 ) ;
BrRefServerPojo pojo ;
BlockReportEnumUtil . HQL_REF refType ;
BlockReportEnumUtil . HQL_WHERE whereType ; // 对象连接封装
hqlJoin . append ( " " + masterPojo . getMasterPojoName ( ) + " AS " + masterPojo . getMasterPojoNameAlias ( ) ) ;
for ( int i = 1 ; i < list . size ( ) ; i + + ) {
pojo = list . get ( i ) ;
refType = BlockReportEnumUtil . HQL_REF . valueOf ( pojo . getPojoRefType ( ) ) ;
whereType = BlockReportEnumUtil . HQL_WHERE . valueOf ( pojo . getPojoWhereType ( ) ) ;
if ( refType ! = null & & whereType ! = null ) {
hqlJoin . append ( " " + refType . getName ( ) ) ;
hqlJoin . append ( " " + pojo . getMasterPojoName ( ) + " AS " + pojo . getMasterPojoNameAlias ( ) ) ;
hqlJoin . append ( " ON " + pojo . getMasterPojoNameAlias ( ) + "." + pojo . getMasterPojoAttrName ( ) ) ;
hqlJoin . append ( " " + whereType . getName ( ) + " " + pojo . getSecondaryPojoNameAlias ( ) + "." + pojo . getSecondaryPojoAttrName ( ) ) ;
} else {
LOGGER . info ( "" ) ;
}
}
}
return hqlJoin ;
}
public static String getSelectHqlWhere ( List < BrPojoAttr > attrs , Map < String , Object > map ) {
StringBuffer hqlWhere = new StringBuffer ( ) ;
if ( attrs ! = null & & attrs . size ( ) > 0 ) {
BlockReportEnumUtil . HQL_WHERE whereType = null ;
for ( BrPojoAttr attr : attrs ) {
whereType = BlockReportEnumUtil . HQL_WHERE . valueOf ( attr . getAttrRefType ( ) ) ;
if ( whereType ! = null ) {
hqlWhere . append ( " AND " + attr . getPojoNameAlias ( ) + "." + attr . getAttrName ( ) ) ;
if ( BlockReportEnumUtil . HQL_WHERE . LIKE . equals ( whereType ) ) { // like %%
hqlWhere . append ( " " + whereType . getName ( ) + " :" + attr . getPojoNameAlias ( ) + "_" + attr . getAttrName ( ) ) ;
map . put ( attr . getPojoNameAlias ( ) + "_" + attr . getAttrName ( ) , "%" + attr . getAttrDefaultValue ( ) + "%" ) ;
} else if ( BlockReportEnumUtil . HQL_WHERE . LIKE_LEFT . equals ( whereType ) ) {
hqlWhere . append ( " " + whereType . getName ( ) + " :" + attr . getPojoNameAlias ( ) + "_" + attr . getAttrName ( ) ) ;
map . put ( attr . getPojoNameAlias ( ) + "_" + attr . getAttrName ( ) , "%" + attr . getAttrDefaultValue ( ) ) ;
} else if ( BlockReportEnumUtil . HQL_WHERE . LIKE_RIGHT . equals ( whereType ) ) {
hqlWhere . append ( " " + whereType . getName ( ) + " :" + attr . getPojoNameAlias ( ) + "_" + attr . getAttrName ( ) ) ;
map . put ( attr . getPojoNameAlias ( ) + "_" + attr . getAttrName ( ) , attr . getAttrDefaultValue ( ) + "%" ) ;
}
} else {
LOGGER . info ( "" ) ;
}
}
}
return hqlWhere . toString ( ) ;
}
public static String getSelectHqlWhereCustomHql ( List < BrTemplateCustomHql > customList ) {
StringBuffer hqlWhere = new StringBuffer ( ) ;
if ( customList ! = null & & customList . size ( ) > 0 ) {
for ( BrTemplateCustomHql hql : customList ) {
if ( hql . getDataType ( ) . intValue ( ) = = BlockReportEnumUtil . HQL_ATTR_DATA_TYPE . WHERE . getValue ( ) ) {
hqlWhere . append ( " " + hql . getCustomContent ( ) ) ;
}
}
}
return hqlWhere . toString ( ) ;
}
public static String getSelectHqlWhereGroup ( List < BrPojoAttr > customList ) {
StringBuffer hql = new StringBuffer ( ) ;
if ( customList ! = null & & customList . size ( ) > 0 ) {
hql . append ( " GROUP BY " ) ;
for ( BrPojoAttr attr : customList ) {
hql . append ( " " + attr . getPojoNameAlias ( ) + "." + attr . getAttrName ( ) ) ;
}
}
return hql . toString ( ) ;
}
/ * *
* 拼 接 查 询 语 句
*
* @param list
* @return
* /
public static TemplateModel getTemplateModel ( List < BrRefServerPojo > list , BrTemplateCustomHql hqlList ) {
if ( list ! = null & & list . size ( ) > 0 ) {
TemplateModel model = new TemplateModel ( ) ;
StringBuffer result = new StringBuffer ( ) ; // 查询语句主体
List < BrPojoAttr > attrShowList = new ArrayList < > ( ) ; // 查询列的属性
List < BrPojoAttr > attrWhereList = new ArrayList < > ( ) ; // 查询列的属性
List < BrPojoAttr > attrGroupList = new ArrayList < > ( ) ; // 查询列的属性
Map < String , Object > paramMap = new HashMap < > ( ) ;
List < BrPojoAttr > tmpList = null ;
// list 先排序下
list . sort ( Comparator . comparing ( BrRefServerPojo : : getPojoSort ) ) ;
// 对象属性封装
for ( BrRefServerPojo refServerPojo : list ) {
tmpList = refServerPojo . getPojoAttrList ( ) ;
if ( tmpList ! = null & & tmpList . size ( ) > 0 ) {
for ( BrPojoAttr attr : tmpList ) {
if ( BlockReportEnumUtil . HQL_ATTR_DATA_TYPE . SHOW . getValue ( ) = = attr . getDataType ( ) . intValue ( )
| | BlockReportEnumUtil . HQL_ATTR_DATA_TYPE . AGGREGATION . getValue ( ) = = attr . getDataType ( ) . intValue ( ) ) {
attrShowList . add ( attr ) ;
} else if ( BlockReportEnumUtil . HQL_ATTR_DATA_TYPE . WHERE . getValue ( ) = = attr . getDataType ( ) . intValue ( ) ) {
attrWhereList . add ( attr ) ;
} else if ( BlockReportEnumUtil . HQL_ATTR_DATA_TYPE . GROUP . getValue ( ) = = attr . getDataType ( ) . intValue ( ) ) {
attrGroupList . add ( attr ) ;
}
}
}
}
attrShowList . sort ( Comparator . comparing ( BrPojoAttr : : getAttrSort ) ) ;
attrGroupList . sort ( Comparator . comparing ( BrPojoAttr : : getAttrSort ) ) ;
attrWhereList . sort ( Comparator . comparing ( BrPojoAttr : : getAttrSort ) ) ;
// HQL 拼接
result . append ( " SELECT " ) ;
result . append ( getSelectHqlColumn ( attrShowList ) ) ;
result . append ( " FROM " ) ;
result . append ( getSelectHqlJoin ( list ) ) ;
result . append ( " WHERE 1=1 " ) ;
result . append ( getSelectHqlWhere ( attrWhereList , paramMap ) ) ;
result . append ( getSelectHqlWhereGroup ( attrGroupList ) ) ;
if ( hqlList ! = null ) {
result . append ( getSelectHqlWhereCustomHql ( Arrays . asList ( hqlList ) ) ) ;
}
model . setHql ( result . toString ( ) ) ;
model . setParamName ( paramMap . keySet ( ) . stream ( ) . toArray ( String [ ] : : new ) ) ;
model . setParamValue ( paramMap . values ( ) . stream ( ) . toArray ( Object [ ] : : new ) ) ;
return model ;
}
return null ;
}
public static List < Map < String , Object > > getResultListTable ( BaseResultBean bean , TemplateModel model ) {
List < Map < String , Object > > result = new ArrayList < > ( ) ;
Map < String , Object > line = null ;
if ( bean ! = null & & bean . isSuccess ( ) & & model ! = null ) {
List < List > list = bean . getResultList ( ) ;
if ( list ! = null & & list . size ( ) > 0 ) {
List < String > columnList = getSelectHqlColumnAs ( model . getHql ( ) ) ;
if ( columnList ! = null & & columnList . size ( ) > 0 ) {
if ( columnList . size ( ) = = 1 ) {
for ( Object value : list ) {
line = new HashMap < > ( ) ;
line . put ( columnList . get ( 0 ) , value ) ;
result . add ( line ) ;
}
} else {
for ( List rows : list ) {
line = new HashMap < > ( ) ;
for ( int i = 0 ; i < rows . size ( ) ; i + + ) {
line . put ( columnList . get ( i ) , rows . get ( i ) ) ;
}
result . add ( line ) ;
}
}
} else {
LOGGER . info ( "无法解析HQL 列属性" ) ;
}
}
}
return result ;
}
public static List < String > getSelectHqlColumnAs ( String hql ) {
List < String > result = new ArrayList < > ( ) ;
// hql 中必须包含 select 和 from
if ( StringUtils . isNotBlank ( hql ) & &
hql . indexOf ( "SELECT" ) ! = - 1 & & hql . indexOf ( "FROM" ) ! = - 1 ) {
hql = hql . substring ( hql . indexOf ( "SELECT" ) + 1 , hql . indexOf ( "FROM" ) ) ;
if ( StringUtils . isNotBlank ( hql ) ) {
String [ ] columnArray = hql . split ( "," ) ;
if ( columnArray ! = null & & columnArray . length > 0 ) {
for ( String column : columnArray ) {
result . add ( column . substring ( column . indexOf ( "AS" ) + 2 , column . length ( ) ) . trim ( ) ) ;
}
}
}
}
return result ;
}
public static Map < String , Object > getResultMap ( BaseResultBean < Object > bean , TemplateModel model ) {
Map < String , Object > result = new HashMap < > ( ) ;
if ( bean ! = null & & model ! = null ) {
if ( ResourceEnumUtil . MESSAGE . SUCCESS . getCode ( ) . equals ( bean . getCode ( ) ) ) {
if ( StringUtils . isNotBlank ( model . getHql ( ) ) ) { // 有HQL
String hql = model . getHql ( ) . substring ( 0 , model . getHql ( ) . indexOf ( "FROM" ) ) ;
System . out . println ( hql ) ;
}
} else {
LOGGER . info ( "请求处理失败" ) ;
}
}
return result ;
}
public static void main ( String [ ] args ) {
List < BrPojoAttr > suList = new ArrayList < > ( ) ;
List < BrPojoAttr > suiList = new ArrayList < > ( ) ;
List < BrPojoAttr > surList = new ArrayList < > ( ) ;
List < BrRefServerPojo > pojoList = new ArrayList < > ( ) ;
// ------------------------------------ SysUser Attr ------------------------------------
BrPojoAttr suAttr = new BrPojoAttr ( ) ;
suAttr . setAttrSort ( 0 ) ;
suAttr . setPojoNameAlias ( "su" ) ;
suAttr . setAttrName ( "userInfoId" ) ;
suAttr . setAttrNameAlias ( "德玛西亚" ) ;
suAttr . setDataType ( BlockReportEnumUtil . HQL_ATTR_DATA_TYPE . SHOW . getValue ( ) ) ;
BrPojoAttr suAttr1 = new BrPojoAttr ( ) ;
suAttr1 . setAttrSort ( 1 ) ;
suAttr1 . setPojoNameAlias ( "su" ) ;
suAttr1 . setAttrName ( "userName" ) ;
suAttr1 . setDataType ( BlockReportEnumUtil . HQL_ATTR_DATA_TYPE . SHOW . getValue ( ) ) ;
BrPojoAttr suAttr2 = new BrPojoAttr ( ) ;
suAttr2 . setAttrSort ( 2 ) ;
suAttr2 . setPojoNameAlias ( "su" ) ;
suAttr2 . setAttrName ( "userLoginName" ) ;
suAttr2 . setDataType ( BlockReportEnumUtil . HQL_ATTR_DATA_TYPE . SHOW . getValue ( ) ) ;
BrPojoAttr suAttr3 = new BrPojoAttr ( ) ;
suAttr3 . setAttrSort ( 3 ) ;
suAttr3 . setPojoNameAlias ( "su" ) ;
suAttr3 . setAttrName ( "userEmpNo" ) ;
suAttr3 . setDataType ( BlockReportEnumUtil . HQL_ATTR_DATA_TYPE . SHOW . getValue ( ) ) ;
suList . add ( suAttr ) ;
suList . add ( suAttr1 ) ;
suList . add ( suAttr2 ) ;
suList . add ( suAttr3 ) ;
// ------------------------------------ SysUserInfo Attr ------------------------------------
BrPojoAttr suiAttr = new BrPojoAttr ( ) ;
suiAttr . setAttrSort ( 0 ) ;
suiAttr . setPojoNameAlias ( "sui" ) ;
suiAttr . setAttrName ( "name" ) ;
suiAttr . setDataType ( BlockReportEnumUtil . HQL_ATTR_DATA_TYPE . SHOW . getValue ( ) ) ;
BrPojoAttr suiAttr1 = new BrPojoAttr ( ) ;
suiAttr1 . setAttrSort ( 1 ) ;
suiAttr1 . setPojoNameAlias ( "sui" ) ;
suiAttr1 . setAttrName ( "userEmployeeType" ) ;
suiAttr1 . setDataType ( BlockReportEnumUtil . HQL_ATTR_DATA_TYPE . SHOW . getValue ( ) ) ;
BrPojoAttr suiAttr2 = new BrPojoAttr ( ) ;
suiAttr2 . setAttrSort ( 2 ) ;
suiAttr2 . setPojoNameAlias ( "sui" ) ;
suiAttr2 . setAttrName ( "userBornDate" ) ;
suiAttr2 . setDataType ( BlockReportEnumUtil . HQL_ATTR_DATA_TYPE . SHOW . getValue ( ) ) ;
BrPojoAttr suiAttr3 = new BrPojoAttr ( ) ;
suiAttr3 . setAttrSort ( 3 ) ;
suiAttr3 . setPojoNameAlias ( "sui" ) ;
suiAttr3 . setAttrName ( "userSchool" ) ;
suiAttr3 . setDataType ( BlockReportEnumUtil . HQL_ATTR_DATA_TYPE . SHOW . getValue ( ) ) ;
BrPojoAttr suiAttr4 = new BrPojoAttr ( ) ;
suiAttr4 . setAttrSort ( 4 ) ;
suiAttr4 . setPojoNameAlias ( "sui" ) ;
suiAttr4 . setAttrName ( "userEmpNo" ) ;
suiAttr4 . setAttrNameAlias ( "userEmpNo" ) ;
suiAttr4 . setAttrDefaultValue ( "英雄联盟" ) ;
suiAttr4 . setAttrRefType ( BlockReportEnumUtil . HQL_WHERE . LIKE . getValue ( ) ) ;
suiAttr4 . setDataType ( BlockReportEnumUtil . HQL_ATTR_DATA_TYPE . WHERE . getValue ( ) ) ;
suiList . add ( suiAttr ) ;
suiList . add ( suiAttr1 ) ;
suiList . add ( suiAttr2 ) ;
suiList . add ( suiAttr3 ) ;
suiList . add ( suiAttr4 ) ;
// ------------------------------------ SysRefUserRole Attr ------------------------------------
BrPojoAttr surAttr = new BrPojoAttr ( ) ;
surAttr . setAttrSort ( 1 ) ;
surAttr . setPojoNameAlias ( "sur" ) ;
surAttr . setAttrName ( "id" ) ;
surAttr . setAggregationType ( BlockReportEnumUtil . HQL_AGGREGATION . COUNT . getValue ( ) ) ;
surAttr . setDataType ( BlockReportEnumUtil . HQL_ATTR_DATA_TYPE . AGGREGATION . getValue ( ) ) ;
surAttr . setDataType ( BlockReportEnumUtil . HQL_ATTR_DATA_TYPE . SHOW . getValue ( ) ) ;
surList . add ( surAttr ) ;
// ------------------------------------ Pojo SysUserInfo ------------------------------------
BrRefServerPojo suiPojo = new BrRefServerPojo ( ) ;
suiPojo . setMasterPojoName ( "SysUserInfo" ) ;
suiPojo . setMasterPojoNameAlias ( "sui" ) ;
suiPojo . setPojoSort ( 1 ) ;
suiPojo . setPojoAttrList ( suiList ) ;
// ------------------------------------ Pojo SysUser ------------------------------------
BrRefServerPojo suPojo = new BrRefServerPojo ( ) ;
suPojo . setMasterPojoName ( "SysUser" ) ;
suPojo . setMasterPojoNameAlias ( "su" ) ;
suPojo . setMasterPojoAttrName ( "userInfoId" ) ;
suPojo . setSecondaryPojoName ( "SysUserInfo" ) ;
suPojo . setSecondaryPojoNameAlias ( "sui" ) ;
suPojo . setSecondaryPojoAttrName ( "id" ) ;
suPojo . setPojoRefType ( BlockReportEnumUtil . HQL_REF . LEFT_JOIN . getValue ( ) ) ;
suPojo . setPojoWhereType ( BlockReportEnumUtil . HQL_WHERE . EQUAL . getValue ( ) ) ;
suPojo . setPojoSort ( 2 ) ;
suPojo . setPojoAttrList ( suList ) ;
// ------------------------------------ Pojo SysRefUserRole ------------------------------------
BrRefServerPojo surPojo = new BrRefServerPojo ( ) ;
surPojo . setMasterPojoName ( "SysRefUserRole" ) ;
surPojo . setMasterPojoNameAlias ( "sur" ) ;
surPojo . setMasterPojoAttrName ( "userId" ) ;
surPojo . setSecondaryPojoName ( "SysUser" ) ;
surPojo . setSecondaryPojoNameAlias ( "su" ) ;
surPojo . setSecondaryPojoAttrName ( "id" ) ;
surPojo . setPojoRefType ( BlockReportEnumUtil . HQL_REF . LEFT_JOIN . getValue ( ) ) ;
surPojo . setPojoWhereType ( BlockReportEnumUtil . HQL_WHERE . EQUAL . getValue ( ) ) ;
surPojo . setPojoSort ( 3 ) ;
surPojo . setPojoAttrList ( surList ) ;
pojoList . add ( suiPojo ) ;
pojoList . add ( suPojo ) ;
pojoList . add ( surPojo ) ;
// System.out.println(getSelectHqlColumn(suList));
// System.out.println(JSON.toJSONString(getTemplateModel(pojoList)));
// String json = "{\"code\":\"20001\",\"msg\":\"select sui.name as sui_name,su.userInfoId as 德玛西亚,sui.userEmployeeType as sui_userEmployeeType,su.userName as su_userName, count(sur.id) as sur_id,sui.userBornDate as sui_userBornDate,su.userLoginName as su_userLoginName,sui.userSchool as sui_userSchool,su.userEmpNo as su_userEmpNo from SysUserInfo as sui left join SysUser as su on su.userInfoId = sui.id left join SysRefUserRole as sur on sur.userId = su.id\",\"resultList\":[[\"管理员\",1077896159978196992,1,\"管理员\",2,\"1993-06-30\",\"admin\",\"复旦大学\",\"10001\"]],\"success\":true,\"totalCount\":0}";
// BaseResultBean resultBean = JSON.parseObject(json,BaseResultBean.class);
//
// System.out.println(getSelectHqlColumnAs(resultBean.getMsg()));;
//
// System.out.println(resultBean.getMsg());
// List<Map<String, Object>> table = getResultListTable(resultBean);
// System.out.println(JSON.toJSONString(table));
}
}
}