Explorar el Código

增加:统一处理字段显示值(日期、金额、位置)

master
郑根木 hace 2 años
padre
commit
1aa2475608
Se han modificado 10 ficheros con 101 adiciones y 43 borrados
  1. +1
    -1
      smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/web/engine/dynPage/DynPageListHandler.java
  2. +1
    -5
      smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/web/engine/dynPage/DynPageProvider.java
  3. +1
    -0
      smtweb-framework/core/src/main/java/cc/smtweb/framework/core/common/SwEnum.java
  4. +69
    -4
      smtweb-framework/core/src/main/java/cc/smtweb/framework/core/db/EntityHelper.java
  5. +9
    -0
      smtweb-framework/core/src/main/java/cc/smtweb/framework/core/db/IFormatter.java
  6. +1
    -1
      smtweb-framework/core/src/main/java/cc/smtweb/framework/core/mvc/service/DefaultListHandler.java
  7. +1
    -2
      smtweb-framework/core/src/main/java/cc/smtweb/framework/core/mvc/service/DefaultProvider.java
  8. +4
    -0
      smtweb-framework/core/src/main/java/cc/smtweb/framework/core/util/CommUtil.java
  9. +14
    -2
      smtweb-framework/core/src/main/java/cc/smtweb/framework/core/util/DateUtil.java
  10. +0
    -28
      smtweb-framework/core/src/main/java/cc/smtweb/framework/core/util/NumberUtil.java

+ 1
- 1
smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/web/engine/dynPage/DynPageListHandler.java Ver fichero

@@ -59,7 +59,7 @@ public class DynPageListHandler extends AbstractListHandler {
}
if(pageDataSet.masterTable != 0 && pageDataSet.masterTable != -1){
ModelTable masterTable = ModelTableCache.getInstance().get(pageDataSet.masterTable);
EntityHelper.loadBeanLink(masterTable.getName(), list, sp.mapFieldAlias);
EntityHelper.loadBeanText(masterTable.getName(), list, sp.mapFieldAlias);
}

for (SwMap map: list) {


+ 1
- 5
smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/web/engine/dynPage/DynPageProvider.java Ver fichero

@@ -1,7 +1,6 @@
package cc.smtweb.system.bpm.web.engine.dynPage;

import cc.smtweb.framework.core.exception.BizException;
import cc.smtweb.framework.core.exception.SwException;
import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.db.EntityHelper;
@@ -9,10 +8,7 @@ import cc.smtweb.framework.core.db.cache.ModelTableCache;
import cc.smtweb.framework.core.db.vo.ModelTable;
import cc.smtweb.framework.core.mvc.service.AbstractCompProvider;
import cc.smtweb.framework.core.mvc.service.SqlNamedPara;
import cc.smtweb.framework.core.util.CommUtil;
import cc.smtweb.framework.core.util.NumberUtil;
import cc.smtweb.system.bpm.web.design.form.define.PageDataset;
import cc.smtweb.system.bpm.web.design.form.define.PageDatasetField;
import cc.smtweb.system.bpm.web.design.form.define.PageDatasets;

/**
@@ -31,7 +27,7 @@ public class DynPageProvider extends AbstractCompProvider {
throw new BizException("没有找到指定数据(ds=" + pageDataSet.name + ")");
}
ModelTable masterTable = ModelTableCache.getInstance().get(pageDataSet.masterTable);
EntityHelper.loadBeanLink(masterTable.getName(), map, sqlPara.mapFieldAlias);
EntityHelper.loadBeanText(masterTable.getName(), map, sqlPara.mapFieldAlias);
DynPageHelper.setCalcFields(map, pageDataSet);
return map;
});


+ 1
- 0
smtweb-framework/core/src/main/java/cc/smtweb/framework/core/common/SwEnum.java Ver fichero

@@ -133,6 +133,7 @@ public interface SwEnum {
public static DataTypeBean DATE = instance.addEnum("date", "日期型", "bigint", 0, "long","Long", Types.BIGINT, "0", EditorType.DATE.value);
public static DataTypeBean TIME = instance.addEnum("time", "时间型", "bigint", 0, "long", "Long", Types.BIGINT, "0", EditorType.TIME.value);
public static DataTypeBean DATETIME = instance.addEnum("datetime", "日期时间型", "bigint", 0, "long", "Long", Types.BIGINT, "0", EditorType.DATETIME.value);
public static DataTypeBean POS = instance.addEnum("pos", "经纬度", "bigint", 0, "long", "Long", Types.BIGINT, "0", EditorType.NUMBER.value);

@Override
protected DataTypeBean buildBean(String value, String name) {


+ 69
- 4
smtweb-framework/core/src/main/java/cc/smtweb/framework/core/db/EntityHelper.java Ver fichero

@@ -2,6 +2,7 @@ package cc.smtweb.framework.core.db;

import cc.smtweb.framework.core.cache.AbstractCache;
import cc.smtweb.framework.core.cache.CacheManager;
import cc.smtweb.framework.core.common.SwEnum;
import cc.smtweb.framework.core.exception.BizException;
import cc.smtweb.framework.core.exception.SwException;
import cc.smtweb.framework.core.common.SwMap;
@@ -9,6 +10,9 @@ import cc.smtweb.framework.core.db.cache.ModelTableCache;
import cc.smtweb.framework.core.db.vo.ModelField;
import cc.smtweb.framework.core.db.vo.ModelLinkName;
import cc.smtweb.framework.core.db.vo.ModelTable;
import cc.smtweb.framework.core.util.CommUtil;
import cc.smtweb.framework.core.util.DateUtil;
import cc.smtweb.framework.core.util.NumberUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
@@ -18,6 +22,27 @@ import java.util.*;
* 实体辅助类
*/
public class EntityHelper {
private static Map<String, IFormatter> mapFormatter;
static {
mapFormatter = new HashMap<>();
mapFormatter.put(SwEnum.DataType.BOOL.value, value -> {
if (CommUtil.strToBool(value)) return "是";
return "";
});
mapFormatter.put(SwEnum.DataType.CURRENCY.value, value -> {
long v = NumberUtil.getLongIgnoreErr(value);
return NumberUtil.toStdAmountString(v / 100.0);

});
mapFormatter.put(SwEnum.DataType.DATE.value, DateUtil::formatDate);
mapFormatter.put(SwEnum.DataType.DATETIME.value, DateUtil::formatDateTime);
mapFormatter.put(SwEnum.DataType.TIME.value, DateUtil::formatTime);
mapFormatter.put(SwEnum.DataType.POS.value, value -> {
long v = NumberUtil.getLongIgnoreErr(value);
return NumberUtil.toStdNumberString(v / 1000000.0, 6);
});
}

//获取字段别名
private static String getFieldAlias(Map<String, String> mapFieldAlias, String field) {
return (mapFieldAlias != null && mapFieldAlias.containsKey(field)) ? mapFieldAlias.get(field) : field;
@@ -25,13 +50,18 @@ public class EntityHelper {

/**
* 添加关联字段值
*
* @param tableName
* @param bean
*/
public static void loadBeanLink(String tableName, SwMap bean, Map<String, String> mapFieldAlias) {
public static void loadBeanText(String tableName, SwMap bean, Map<String, String> mapFieldAlias) {
//添加关联字段值
ModelTable table = ModelTableCache.getInstance().getByName(tableName);
if (table == null) return;
loadBeanLink(table, bean, mapFieldAlias);
}

private static void loadBeanLink(ModelTable table, SwMap bean, Map<String, String> mapFieldAlias) {
List<ModelLinkName> listLink = table.findLinkeNames();
if (listLink.isEmpty()) return;

@@ -87,10 +117,40 @@ public class EntityHelper {
}
}

public static void loadBeanLink(String tableName, List<SwMap> listData, Map<String, String> mapFieldAlias) {
public static void loadBeanText(String tableName, List<SwMap> listData, Map<String, String> mapFieldAlias) {
//添加关联字段值
ModelTable table = ModelTableCache.getInstance().getByName(tableName);
if (table == null) return;
loadBeanLink(table, listData, mapFieldAlias);
}

/**
* 设置非关联字段中金额、日期等类型字段的显示值
*
* @param table
* @param bean
* @param mapFieldAlias
*/
private static void setFieldDisplayText(ModelTable table, SwMap bean, Map<String, String> mapFieldAlias) {
for (ModelField field : table.getFields()) {
IFormatter formatter = mapFormatter.get(field.getDataType());
if (formatter == null) continue;
final String fieldName = getFieldAlias(mapFieldAlias, field.getName());
String value = bean.readString(fieldName);
if (StringUtils.isNotEmpty(value)) {
bean.put(fieldName + "_text", formatter.format(value));
}
}
}

/**
* 加载关联字段的Text
*
* @param table 表定义
* @param listData 数据
* @param mapFieldAlias 字段别名关系
*/
private static void loadBeanLink(ModelTable table, List<SwMap> listData, Map<String, String> mapFieldAlias) {
List<ModelLinkName> listLink = table.findLinkeNames();
if (listLink.isEmpty()) return;

@@ -159,6 +219,7 @@ public class EntityHelper {
if (fkTable == null) throw new BizException("待检查表(" + tableId + ")为空!");
checkExists(fkTable, id);
}

public static void checkExists(String tableName, long id) {
ModelTable fkTable = ModelTableCache.getInstance().getByName(tableName);
if (fkTable == null) throw new BizException("待检查表(" + tableName + ")为空!");
@@ -167,16 +228,17 @@ public class EntityHelper {

/**
* 检查记录是否被使用
*
* @param fkTable
* @param id
*/
public static void checkExists(ModelTable fkTable, long id) {
Set<ModelTable> list = ModelTableCache.getInstance().getByLink(fkTable.getId());
if (list == null || list.isEmpty()) return;
for (ModelTable table: list) {
for (ModelTable table : list) {
StringBuilder sql = new StringBuilder(256);
List<Object> args = new ArrayList<>();
for (ModelField field: table.getFields()) {
for (ModelField field : table.getFields()) {
if (field.getLink() == fkTable.getId()) {
sql.append(" or ").append(field.getName()).append("=?");
args.add(id);
@@ -191,6 +253,7 @@ public class EntityHelper {

/**
* 获取带数据库的表名,用于sql
*
* @param tableName
* @return
*/
@@ -199,4 +262,6 @@ public class EntityHelper {
if (table == null) throw new SwException("没有找到表定义:" + tableName);
return table.getSchemaTableName();
}


}

+ 9
- 0
smtweb-framework/core/src/main/java/cc/smtweb/framework/core/db/IFormatter.java Ver fichero

@@ -0,0 +1,9 @@
package cc.smtweb.framework.core.db;

/**
* Created by Akmm at 2022/7/21 19:09
* 值显示格式化
*/
public interface IFormatter {
String format(String value);
}

+ 1
- 1
smtweb-framework/core/src/main/java/cc/smtweb/framework/core/mvc/service/DefaultListHandler.java Ver fichero

@@ -68,6 +68,6 @@ public class DefaultListHandler<T extends DefaultEntity> extends AbstractListHan
@Override
protected void afterQuery(List<SwMap> listData) {
super.afterQuery(listData);
EntityHelper.loadBeanLink(tableName, listData, null);
EntityHelper.loadBeanText(tableName, listData, null);
}
}

+ 1
- 2
smtweb-framework/core/src/main/java/cc/smtweb/framework/core/mvc/service/DefaultProvider.java Ver fichero

@@ -1,7 +1,6 @@
package cc.smtweb.framework.core.mvc.service;

import cc.smtweb.framework.core.exception.BizException;
import cc.smtweb.framework.core.exception.SwException;
import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.db.EntityDao;
import cc.smtweb.framework.core.db.EntityHelper;
@@ -25,7 +24,7 @@ public class DefaultProvider<T extends DefaultEntity> extends AbstractCompProvid
EntityDao<T> bdao = (EntityDao<T>) DbEngine.getInstance().findDao(tableName);
T bean = bdao.queryEntity(id);
if (bean == null) throw new BizException("没有找到指定数据(id=" + id + ")");
EntityHelper.loadBeanLink(bean.getTableName(), bean.getData(), null);
EntityHelper.loadBeanText(bean.getTableName(), bean.getData(), null);
return bean;
}
}

+ 4
- 0
smtweb-framework/core/src/main/java/cc/smtweb/framework/core/util/CommUtil.java Ver fichero

@@ -137,4 +137,8 @@ public class CommUtil {
if (StringUtils.isEmpty(s)) return 0;
return s.getBytes(StandardCharsets.UTF_8).length;
}

public static boolean strToBool(String s) {
return "true".equalsIgnoreCase(s) || "1".equals(s) || "y".equalsIgnoreCase(s);
}
}

+ 14
- 2
smtweb-framework/core/src/main/java/cc/smtweb/framework/core/util/DateUtil.java Ver fichero

@@ -57,18 +57,30 @@ public class DateUtil {
* @return
*/
public static String formatDate(long date) {
String d = String.valueOf(date);
return formatDate(String.valueOf(date));
}
public static String formatDate(String d) {
if(d.length() < 8) return d; //就是2011-03-04 13:11:01这种形式
return d.substring(0, 4) + "-" + d.substring(4, 6) + "-" + d.substring(6, 8);
}

public static String formatDateTime(long date) {
String d = String.valueOf(date);
return formatDateTime(String.valueOf(date));
}
public static String formatDateTime(String d) {
if(d.length() < 8) return d; //就是2011-03-04 13:11:01这种形式
if(d.length() < 14) return d.substring(0, 4) + "-" + d.substring(4, 6) + "-" + d.substring(6, 8);
return d.substring(0, 4) + "-" + d.substring(4, 6) + "-" + d.substring(6, 8) + " " + d.substring(8, 10) + ":" + d.substring(10, 12) + ":" + d.substring(12);
}

public static String formatTime(long date) {
return formatTime(String.valueOf(date));
}
public static String formatTime(String d) {
if(d.length() < 6) return d; //就是13:11:01这种形式
return d.substring(0, 2) + ":" + d.substring(2, 4) + ":" + d.substring(4);
}

/**
* 按yyyyMMdd格式化
* @param date


+ 0
- 28
smtweb-framework/core/src/main/java/cc/smtweb/framework/core/util/NumberUtil.java Ver fichero

@@ -533,34 +533,6 @@ public class NumberUtil {
return false;
}

/*
public static Object calcExprObjectJ(String expr, Object... vars) throws Exception{
if (StringUtils.isEmpty(expr)) return "";
ExpressionEvaluator ee = new ExpressionEvaluator();
ee.setExpressionType(int.class);
ee.setParameters(new String[]{"a", "b", "c"}, new Class[]{int.class, int.class, String.class});
ee.cook(expr);
return ee.evaluate(vars);
}
*/

public static void main(String[] args) throws Exception{
/*long t = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
System.out.print(calcExprIntEx("\"add\".equals(c) ? a + b : a - b", "a", 5, "b", 2, "c", "t"));
}
System.out.println();
System.out.println(System.currentTimeMillis() - t);
// System.out.println(calcExprBoolEx("1==2"));

t = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
System.out.print(calcExprObjectJ("\"add\".equals(c) ? a + b : a - b", 5, 2, "t"));
}
System.out.println();
System.out.println(System.currentTimeMillis() - t);*/
}

/**
* double 去尾法
*


Cargando…
Cancelar
Guardar