com.fhs.pagex.service.JoinService Maven / Gradle / Ivy
The newest version!
package com.fhs.pagex.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.fhs.common.utils.CheckUtils;
import com.mybatis.jpa.common.ColumnNameUtil;
import com.mybatis.jpa.common.PersistentUtil;
import com.mybatis.jpa.common.scanner.AnnotationTypeFilterBuilder;
import com.mybatis.jpa.common.scanner.SpringClassScanner;
import com.fhs.common.utils.ConverterUtils;
import com.fhs.common.utils.ReflectUtils;
import com.fhs.common.utils.StringUtil;
import com.fhs.pagex.annotation.JoinShowColumn;
import com.fhs.pagex.dao.DefaultPageXDAO;
import com.fhs.pagex.dto.PagexListSettDTO;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.stereotype.Service;
import javax.persistence.Entity;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
/**
* 表关联查询服务
*/
@Service
public class JoinService implements InitializingBean {
private String basePackage = "com.fhs.**.bean";
/**
* pageXDBService
*/
@Autowired
private DefaultPageXDAO pageXDAO;
/**
* key model name value model对应的class
*/
private Map modelClassMap = new HashMap<>();
/**
* 每个model对应哪些字段是可能需要被关联的 val->map key db字段名,val Java字段名
*/
private Map> modelJoinFiledMap = new HashMap<>();
/**
* key model val 查询的sql
*/
private Map modelSqlMap = new HashMap<>();
/**
* key namespace value 此namespace需要关联查询的字段集合
* valMap-> key 字段名字 val namespace
*/
private Map> namespaceJoinColumnMap = new HashMap<>();
/**
* 填充Join数据
* @param rows 待被填充的数据集合
* @param namespace namespace
* @return 初始好的数据
*/
public JSONArray initJoinData(JSONArray rows, String namespace)
{
if(namespaceJoinColumnMap.containsKey(namespace)){
Map namespaceJoinColumnInfo = namespaceJoinColumnMap.get(namespace);
Set columnSet = namespaceJoinColumnInfo.keySet();
for(int i = 0;i columnVal = new HashSet<>();
camaelColumn = ColumnNameUtil.underlineToCamel(column);
for(int i = 0;i> namespaceDataList = pageXDAO.selectListForJoin(sql);
//关联表的一列数据
Map> joinTBLData = new HashMap<>();
Map joinField = this.modelJoinFiledMap.get(tempNameSpace);
// 转换字段名字
for(Map row:namespaceDataList)
{
for(String dbColumnName : joinField.keySet())
{
row.put(joinField.get(dbColumnName),row.get(dbColumnName));
row.remove(dbColumnName);
}
String rowPkey= ConverterUtils.toString(row.get("pkey"));
row.remove("pkey");
joinTBLData.put(rowPkey,row);
}
for(int i = 0;i> entitySet = scanClass();
entitySet.forEach(clazz -> {
modelClassMap.put(clazz.getSimpleName(), clazz);
modelJoinFiledMap.put(clazz.getSimpleName(), parseNeedJoinField(clazz));
});
PagexDataService.SIGNEL.registerJsRefreshListener(this::refreshJs);
}
/**
* 解析此类哪些字段需要join show
*
* @param clazz class
* @return 需要join show的字段 map
*/
private Map parseNeedJoinField(Class> clazz) {
Map needJoinFieldMap = new HashMap<>();
List fieldList = ReflectUtils.getAllField(clazz);
StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.append("SELECT ");
String tblColumnName = null;
for (Field field : fieldList) {
if (!field.isAnnotationPresent(JoinShowColumn.class)) {
continue;
}
tblColumnName = PersistentUtil.getColumnName(field);
needJoinFieldMap.put(tblColumnName,clazz.getSimpleName() + "_" + field.getName());
sqlBuilder.append(tblColumnName + ",");
}
String pkey = PersistentUtil.getTableName(clazz);
sqlBuilder.append(pkey + " as pkey FROM " + PersistentUtil.getTableName(clazz));
sqlBuilder.append(" WHERE " + PersistentUtil.getPrimaryKey(clazz) + " IN (@{pkeys})");
modelSqlMap.put(clazz.getSimpleName(), sqlBuilder.toString());
return needJoinFieldMap;
}
/**
* 扫描class
*
* @return 符合条件的class集合
*/
private Set> scanClass() {
/** scan entity **/
TypeFilter entityFilter = AnnotationTypeFilterBuilder.build(Entity.class);
SpringClassScanner entityScanner = new SpringClassScanner.Builder().typeFilter(entityFilter).build();
String[] pkgs = basePackage.split(";");
for (String pkg : pkgs) {
entityScanner.getScanPackages().add(pkg);
}
Set> entitySet = null;
try {
entitySet = entityScanner.scan();
} catch (ClassNotFoundException | IOException e) {
// log or throw runTimeExp
throw new RuntimeException(e);
}
return entitySet;
}
/**
* 当有js刷新的事件 刷新 对应的sql 和 列表字段
* @param namespace namespace
* @param js js
*/
public void refreshJs(String namespace,String js)
{
PagexListSettDTO pagexListSett = PagexDataService.SIGNEL.getPagexListSettDTOFromCache(namespace);
String joinColumns = ConverterUtils.toString(pagexListSett.getModelConfig().get("joinColumns"));
if(CheckUtils.isNotEmpty(joinColumns))
{
StringBuilder joinDBColumns = new StringBuilder();
JSONObject columns = JSON.parseObject(joinColumns);
for(Object key : columns.keySet())
{
joinDBColumns.append(key + ",");
}
String sql = "SELECT " + joinDBColumns
+ pagexListSett.getModelConfig().get("pkey") + " AS pkey FROM "
+ pagexListSett.getModelConfig().get("table") + " WHERE "
+ pagexListSett.getModelConfig().get("pkey") + " IN (@{pkeys})";
modelSqlMap.put(namespace,sql);
modelJoinFiledMap.put(namespace,JSON.parseObject(joinColumns, new TypeReference>() {}));
}
Map namespaceJoinColumnInfo = new HashMap<>();
pagexListSett.getListSett().forEach(column->{
//如果是join
if(ConverterUtils.toBoolean(column.get("isJoin")))
{
if(!namespaceJoinColumnInfo.containsKey(ConverterUtils.toString(column.get("name"))))
{
namespaceJoinColumnInfo.put(ConverterUtils.toString(column.get("name")),
ConverterUtils.toString(column.get("namespace")));
}
}
});
if(namespaceJoinColumnInfo.isEmpty())
{
return;
}
namespaceJoinColumnMap.put(namespace,namespaceJoinColumnInfo);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy