Browse Source

初始

master
郑根木 2 years ago
parent
commit
74fef2d6ec
11 changed files with 149 additions and 47 deletions
  1. +10
    -8
      smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/common/SwEnum.java
  2. +7
    -1
      smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/db/dao/AbstractEntityDao.java
  3. +11
    -2
      smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/db/impl/DefaultEntity.java
  4. +1
    -1
      smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/mvc/service/AbstractListHandler.java
  5. +2
    -2
      smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/mvc/service/DefaultComboHandler.java
  6. +11
    -1
      smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/mvc/service/DefaultLoadHandler.java
  7. +70
    -2
      smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/mvc/service/DefaultProvider.java
  8. +11
    -2
      smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/mvc/service/DefaultSaveHandler.java
  9. +26
    -26
      smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/mvc/service/SwListData.java
  10. +0
    -1
      smtweb-framework/sw-framework-core/src/main/resources/config/demo.json
  11. +0
    -1
      smtweb-framework/sw-framework-core/target/classes/config/demo.json

+ 10
- 8
smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/common/SwEnum.java View File

@@ -15,14 +15,16 @@ public interface SwEnum {
class FieldType extends IntEnum{
public static FieldType instance = new FieldType();

public static IntEnumBean ID = instance.addEnum(0, "主键");
public static IntEnumBean CODE = instance.addEnum(1, "编码字段");
public static IntEnumBean NAME = instance.addEnum(2, "名称字段");
public static IntEnumBean PARENT_ID = instance.addEnum(3, "父ID");
public static IntEnumBean LEVEL_CODE = instance.addEnum(4, "级次码");
public static IntEnumBean ORDER = instance.addEnum(5, "排序字段");
public static IntEnumBean CREATE_TIME = instance.addEnum(6, "创建时间");
public static IntEnumBean LAST_TIME = instance.addEnum(7, "更新时间");
public static IntEnumBean ID = instance.addEnum(1, "主键");
public static IntEnumBean CODE = instance.addEnum(2, "编码字段");
public static IntEnumBean NAME = instance.addEnum(3, "名称字段");
public static IntEnumBean PARENT_ID = instance.addEnum(4, "父ID");
public static IntEnumBean LEVEL_CODE = instance.addEnum(5, "级次码");
public static IntEnumBean ORDER = instance.addEnum(6, "排序字段");
public static IntEnumBean CREATE_USER = instance.addEnum(7, "创建人");
public static IntEnumBean CREATE_TIME = instance.addEnum(8, "创建时间");
public static IntEnumBean UPDATE_USER = instance.addEnum(9, "更新人");
public static IntEnumBean LAST_TIME = instance.addEnum(10, "更新时间");
}

/**


+ 7
- 1
smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/db/dao/AbstractEntityDao.java View File

@@ -388,7 +388,13 @@ public abstract class AbstractEntityDao<T> {
}

public T createBean() throws Exception {
return this.type.newInstance();
T bean = this.type.newInstance();
if (bean instanceof DefaultEntity) {
DefaultEntity b = (DefaultEntity)bean;
b.init();
b.setTableName(this.tableName);
}
return bean;
}
}


+ 11
- 2
smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/db/impl/DefaultEntity.java View File

@@ -1,8 +1,10 @@
package cc.smtweb.framework.core.db.impl;

import cc.smtweb.framework.core.common.SwEnum;
import cc.smtweb.framework.core.db.cache.ModelTableCache;
import cc.smtweb.framework.core.db.vo.ModelField;
import cc.smtweb.framework.core.db.vo.ModelTable;
import cc.smtweb.framework.core.util.DateUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
@@ -28,6 +30,9 @@ public class DefaultEntity extends BaseBean implements Serializable, Cloneable {
public String getTableName() {
return getStr(tableNameKey);
}
public void setTableName(String tableName) {
put(tableNameKey, tableName);
}

//根据实体定义,设默认值
public void init() {
@@ -37,8 +42,12 @@ public class DefaultEntity extends BaseBean implements Serializable, Cloneable {
for (ModelField field : entity.getFields()) {
if (data.containsKey(field.getName())) continue;//有值了,不能动
if (field.getName().equalsIgnoreCase(pkField)) continue;//是pk,不要初始化
String s = field.getDefaultValue();
if (StringUtils.isNotEmpty(s)) put(field.getName(), s);
if (field.getFieldType() == SwEnum.FieldType.CREATE_TIME.value || field.getFieldType() == SwEnum.FieldType.LAST_TIME.value) {
put(field.getName(), DateUtil.nowDateTimeLong());
} else {
String s = field.getDefaultValue();
if (StringUtils.isNotEmpty(s)) put(field.getName(), s);
}
}
}



+ 1
- 1
smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/mvc/service/AbstractListHandler.java View File

@@ -82,7 +82,7 @@ public abstract class AbstractListHandler extends AbstractHandler {
}

afterQuery(listData);
return SwListData.create(listData, false);
return SwListData.create(listData, rows);
}

protected SqlPara buildSumSqlPara() {


+ 2
- 2
smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/mvc/service/DefaultComboHandler.java View File

@@ -51,9 +51,9 @@ public class DefaultComboHandler<T extends DefaultEntity> extends DefaultListHan
}
}

List<SwMap> listData = DbEngine.getInstance().query(sql, SwMap.class, args.toArray());
List<SwMap> listData = DbEngine.getInstance().pagedQuery(sql, SwMap.class, 0, 10, args.toArray());
afterQuery(listData);
return SwListData.create(listData, false);
return SwListData.create(listData, 0);
}

//搜索条件


+ 11
- 1
smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/mvc/service/DefaultLoadHandler.java View File

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

import cc.smtweb.framework.core.common.SwEnum;
import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.db.EntityDao;
import cc.smtweb.framework.core.db.cache.ModelTableCache;
import cc.smtweb.framework.core.db.impl.DefaultEntity;
import cc.smtweb.framework.core.db.vo.ModelField;
import cc.smtweb.framework.core.db.vo.ModelTable;

/**
* Created by Akmm at 2022/3/2 19:52
@@ -16,7 +21,12 @@ public class DefaultLoadHandler<T extends DefaultEntity> extends AbstractLoadHan

@Override
protected T createComp() throws Exception {
return (T)DbEngine.getInstance().findDao(tableName).createBean();
final EntityDao<T> dao = DbEngine.getInstance().findDao(tableName);
T bean = dao.createBean();
ModelTable table = ModelTableCache.getInstance().getByName(tableName);
ModelField field = table.findFieldByType(SwEnum.FieldType.CREATE_USER.value);
if (field != null) bean.put(field.getName(), us.getUserId());
return bean;
}

@Override


+ 70
- 2
smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/mvc/service/DefaultProvider.java View File

@@ -1,8 +1,17 @@
package cc.smtweb.framework.core.mvc.service;

import cc.smtweb.framework.core.SwMap;
import cc.smtweb.framework.core.cache.AbstractCache;
import cc.smtweb.framework.core.cache.CacheManager;
import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.db.EntityDao;
import cc.smtweb.framework.core.db.cache.ModelTableCache;
import cc.smtweb.framework.core.db.impl.DefaultEntity;
import cc.smtweb.framework.core.db.vo.ModelLinkName;
import cc.smtweb.framework.core.db.vo.ModelTable;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
* Created by Akmm at 2022/3/3 20:04
@@ -15,7 +24,66 @@ public class DefaultProvider<T extends DefaultEntity> extends AbstractCompProvid
}

public T getBean(long id) {
EntityDao<T> dao = (EntityDao<T>)DbEngine.getInstance().findDao(tableName);
return doGetData(tableName + "." + id, () -> dao.queryEntity(id));
return doGetData(tableName + "." + id, () -> this.loadBean(id));
}

private T loadBean(long id) {
EntityDao<T> bdao = (EntityDao<T>) DbEngine.getInstance().findDao(tableName);
T bean = bdao.queryEntity(id);
//添加关联字段值
ModelTable table = ModelTableCache.getInstance().getByName(tableName);
if (table == null) return bean;
List<ModelLinkName> listLink = table.findLinkeNames();
if (listLink.isEmpty()) return bean;

//有缓存的,从缓存拿,无缓存的放Map,最后从数据库去拿
Map<String, List<String>> mapIds = new HashMap<>();

for (ModelLinkName l : listLink) {
String value = bean.getStr(l.getFieldName());
if (StringUtils.isEmpty(value)) continue;
String[] ids = StringUtils.split(value, ",");
if (l.getLinkTable().isNeedCache()) {
EntityDao dao = DbEngine.getInstance().findDao(l.getLinkTable().getName());
AbstractCache cache = CacheManager.getIntance().getCache(l.getLinkTable().getName());
String names = "";
for (String sId : ids) {
Object b = cache.get(sId);
String sn = (String) dao.readValue(b, l.getLinkNameField());
if (StringUtils.isNotEmpty(sn)) names += "," + sn;
}
bean.put(l.getFieldName() + "_text", names.substring(1));
} else {
List<String> list = mapIds.computeIfAbsent(l.getLinkTable().getName(), k -> new ArrayList<>());
Collections.addAll(list, ids);
}
}


if (mapIds.isEmpty()) return bean;
//数据库查询
Map<String, Map<String, String>> mapValue = new HashMap<>();
for (Map.Entry<String, List<String>> entry : mapIds.entrySet()) {
EntityDao dao = DbEngine.getInstance().findDao(entry.getKey());
mapValue.put(entry.getKey(), dao.queryNames(entry.getValue()));
}
//加值

for (ModelLinkName l : listLink) {
if (!mapValue.containsKey(l.getLinkTable().getName())) continue;
Map<String, String> mapV = mapValue.get(l.getLinkTable().getName());

String value = bean.getStr(l.getFieldName());
if (StringUtils.isEmpty(value)) continue;

String[] ids = StringUtils.split(value, ",");
String names = "";
for (String sId : ids) {
String sn = mapV.get(sId);
if (StringUtils.isNotEmpty(sn)) names += "," + sn;
}
bean.put(l.getFieldName() + "_text", names.substring(1));
}
return bean;
}
}

+ 11
- 2
smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/mvc/service/DefaultSaveHandler.java View File

@@ -33,7 +33,13 @@ public class DefaultSaveHandler<T extends DefaultEntity> extends AbstractSaveHan
@Override
protected T createComp() throws Exception {
final EntityDao<T> dao = DbEngine.getInstance().findDao(tableName);
return dao.createBean();
T bean = dao.createBean();
ModelTable table = ModelTableCache.getInstance().getByName(tableName);
ModelField field = table.findFieldByType(SwEnum.FieldType.CREATE_USER.value);
if (field != null) bean.put(field.getName(), us.getUserId());
field = table.findFieldByType(SwEnum.FieldType.UPDATE_USER.value);
if (field != null) bean.put(field.getName(), us.getUserId());
return bean;
}

@Override
@@ -80,8 +86,11 @@ public class DefaultSaveHandler<T extends DefaultEntity> extends AbstractSaveHan
if (isNew) {
dao.insertEntity(bean);
} else {
dao.updateEntity(bean);
ModelTable table = ModelTableCache.getInstance().getByName(tableName);
ModelField field = table.findFieldByType(SwEnum.FieldType.UPDATE_USER.value);
if (field != null) bean.put(field.getName(), us.getUserId());

dao.updateEntity(bean);
if (table.getType() == SwEnum.TableType.TYPE_TREE.value) {
listTreeBean = TreeHelper.getTreeHelper(tableName).resetTreeLevel(bean);
}


+ 26
- 26
smtweb-framework/sw-framework-core/src/main/java/cc/smtweb/framework/core/mvc/service/SwListData.java View File

@@ -8,30 +8,30 @@ import java.util.List;

@Getter
public class SwListData {
public static final SwListData EMPTY = new SwListData(new ArrayList<>(), 0);
private final List<SwMap> rows;
// 总数, -1 表示需要异步获取数量, >=0 表示总数
private int total;
private SwListData(List<SwMap> rows, int total) {
this.rows = rows;
this.total = total;
}
public static SwListData create(List<SwMap> list, boolean loadCount) {
if (list == null) {
return SwListData.EMPTY;
}
if (loadCount) {
return new SwListData(list, -1);
} else {
return new SwListData(list, list.size());
}
}
public boolean isEmpty() {
return rows == null || rows.isEmpty();
}
public static final SwListData EMPTY = new SwListData(new ArrayList<>(), 0);
private final List<SwMap> listRow;
// 总数, -1 表示需要异步获取数量, >=0 表示总数
private int total;
private SwListData(List<SwMap> rows, int total) {
this.listRow = rows;
this.total = total;
}
public static SwListData create(List<SwMap> list, int rows) {
if (list == null) {
return SwListData.EMPTY;
}
if (rows > 0 && list.size() >= rows) {
return new SwListData(list, -1);
} else {
return new SwListData(list, list.size());
}
}
public boolean isEmpty() {
return listRow == null || listRow.isEmpty();
}
}

+ 0
- 1
smtweb-framework/sw-framework-core/src/main/resources/config/demo.json View File

@@ -1 +0,0 @@
{"fields":[{"name":"tb_id","fieldType":"0","dataType":"ID","null":"1","default":"-1","title":"ID"},{"name":"tb_prj_id","fieldType":"","dataType":"ID","null":"1","default":"-1","title":"ID","link":"1"},{"name":"tb_mc_id","fieldType":"","dataType":"ID","null":"1","default":"-1","title":"ID","link":"2"},{"name":"tb_db_id","fieldType":"","dataType":"ID","null":"1","default":"-1","title":"ID","link":"3"},{"name":"tb_extends","fieldType":"","dataType":"REMARK","null":"0","default":"","title":"继承关系","link":"4","editor":""},{"name":"tb_name","fieldType":"1","dataType":"CODE","null":"0","default":"","title":"表名","link":"","editor":""},{"name":"tb_title","fieldType":"2","dataType":"NAME","null":"0","default":"","title":"中文名","link":"","editor":""},{"name":"tb_abbr","fieldType":"","dataType":"CODE","null":"1","default":"","title":"缩写,用于字段和索引组成","link":"","editor":""},{"name":"tb_type","fieldType":"","dataType":"SMALLINT","null":"1","default":"0","title":"类别:0-普通表,1树型表2编码表9-虚拟抽象表11视图","link":"","editor":""},{"name":"tb_need_cache","fieldType":"","dataType":"BOOL","null":"1","default":"0","title":"需要缓存","link":"","editor":""},{"name":"tb_content","fieldType":"","dataType":"TEXT","null":"1","default":"0","title":"表详细信息","link":"","editor":""},{"name":"tb_create_uid","fieldType":"","dataType":"ID","null":"1","default":"-1","title":"创建人","link":"","editor":""},{"name":"tb_update_uid","fieldType":"","dataType":"ID","null":"1","default":"-1","title":"最后更新人","link":"","editor":""},{"name":"tb_create_at","fieldType":"","dataType":"DATETIME","null":"1","default":"-1","title":"创建时间","link":"","editor":""},{"name":"tb_update_at","fieldType":"","dataType":"DATETIME","null":"1","default":"-1","title":"最后更新时间","link":"","editor":""}],"indexes":[{"name":"pk","fields":"tb_id","type":"P"}],"caches":[{"name":"n","title":"按表名","fields":"tb_name","type":"L"},{"name":"db","title":"按库名","fields":"tb_db_id","type":"L"},{"name":"prj","title":"按项目","fields":"tb_prj_id","type":"L"},{"name":"mc","title":"按项目","fields":"tb_mc_id","type":"L"}]}

+ 0
- 1
smtweb-framework/sw-framework-core/target/classes/config/demo.json View File

@@ -1 +0,0 @@
{"fields":[{"name":"tb_id","fieldType":"0","dataType":"ID","null":"1","default":"-1","title":"ID"},{"name":"tb_prj_id","fieldType":"","dataType":"ID","null":"1","default":"-1","title":"ID","link":"1"},{"name":"tb_mc_id","fieldType":"","dataType":"ID","null":"1","default":"-1","title":"ID","link":"2"},{"name":"tb_db_id","fieldType":"","dataType":"ID","null":"1","default":"-1","title":"ID","link":"3"},{"name":"tb_extends","fieldType":"","dataType":"REMARK","null":"0","default":"","title":"继承关系","link":"4","editor":""},{"name":"tb_name","fieldType":"1","dataType":"CODE","null":"0","default":"","title":"表名","link":"","editor":""},{"name":"tb_title","fieldType":"2","dataType":"NAME","null":"0","default":"","title":"中文名","link":"","editor":""},{"name":"tb_abbr","fieldType":"","dataType":"CODE","null":"1","default":"","title":"缩写,用于字段和索引组成","link":"","editor":""},{"name":"tb_type","fieldType":"","dataType":"SMALLINT","null":"1","default":"0","title":"类别:0-普通表,1树型表2编码表9-虚拟抽象表11视图","link":"","editor":""},{"name":"tb_need_cache","fieldType":"","dataType":"BOOL","null":"1","default":"0","title":"需要缓存","link":"","editor":""},{"name":"tb_content","fieldType":"","dataType":"TEXT","null":"1","default":"0","title":"表详细信息","link":"","editor":""},{"name":"tb_create_uid","fieldType":"","dataType":"ID","null":"1","default":"-1","title":"创建人","link":"","editor":""},{"name":"tb_update_uid","fieldType":"","dataType":"ID","null":"1","default":"-1","title":"最后更新人","link":"","editor":""},{"name":"tb_create_at","fieldType":"","dataType":"DATETIME","null":"1","default":"-1","title":"创建时间","link":"","editor":""},{"name":"tb_update_at","fieldType":"","dataType":"DATETIME","null":"1","default":"-1","title":"最后更新时间","link":"","editor":""}],"indexes":[{"name":"pk","fields":"tb_id","type":"P"}],"caches":[{"name":"n","title":"按表名","fields":"tb_name","type":"L"},{"name":"db","title":"按库名","fields":"tb_db_id","type":"L"},{"name":"prj","title":"按项目","fields":"tb_prj_id","type":"L"},{"name":"mc","title":"按项目","fields":"tb_mc_id","type":"L"}]}

Loading…
Cancel
Save