@@ -1,93 +1,111 @@ | |||
package cc.smtweb.framework.core; | |||
import cc.smtweb.framework.core.util.MapUtil; | |||
import com.sun.corba.se.spi.ior.ObjectKey; | |||
import java.util.HashMap; | |||
import java.util.Map; | |||
import java.util.Set; | |||
/** | |||
* 通用map对象,用于无具体类型的传值 | |||
* | |||
* @author kevin | |||
*/ | |||
public class SwMap extends HashMap<String, Object> { | |||
public SwMap() {} | |||
public SwMap() { | |||
} | |||
public SwMap(int initialCapacity) { | |||
super(initialCapacity); | |||
} | |||
public SwMap(int initialCapacity) { | |||
super(initialCapacity); | |||
} | |||
public String readString(String name) { | |||
return MapUtil.readString(this, name); | |||
} | |||
public String readString(String name) { | |||
return MapUtil.readString(this, name); | |||
} | |||
public String readString(String name, String defaultValue) { | |||
return MapUtil.readString(this, name, defaultValue); | |||
} | |||
public SwMap readMap(String name) { | |||
Object v = get(name); | |||
if (v == null) return null; | |||
if (v instanceof SwMap) return (SwMap)v; | |||
if (v instanceof Map) { | |||
Map m = (Map)v; | |||
SwMap map = new SwMap(m.size()); | |||
map.putAll(m); | |||
put(name, map); | |||
return map; | |||
} | |||
throw new SwException("不是合法的Map对象!" + v.getClass().getName()); | |||
} | |||
public long readLong(String name) { | |||
return MapUtil.readLong(this, name); | |||
} | |||
public String readString(String name, String defaultValue) { | |||
return MapUtil.readString(this, name, defaultValue); | |||
} | |||
public Long readLong(String name, Long defaultValue) { | |||
return MapUtil.readLong(this, name, defaultValue); | |||
} | |||
public long readLong(String name) { | |||
return MapUtil.readLong(this, name); | |||
} | |||
public Long[] readLongArray(String name) { | |||
return MapUtil.readLongArray(this, name); | |||
} | |||
public Long readLong(String name, Long defaultValue) { | |||
return MapUtil.readLong(this, name, defaultValue); | |||
} | |||
public Long[] readLongArray(String name, Long[] defaultValue) { | |||
return MapUtil.readLongArray(this, name, defaultValue); | |||
} | |||
public Long[] readLongArray(String name) { | |||
return MapUtil.readLongArray(this, name); | |||
} | |||
public Set<Long> readLongSet(String name) { | |||
return MapUtil.readLongSet(this, name); | |||
} | |||
public Long[] readLongArray(String name, Long[] defaultValue) { | |||
return MapUtil.readLongArray(this, name, defaultValue); | |||
} | |||
public int readInt(String name) { | |||
return MapUtil.readInt(this, name); | |||
} | |||
public Set<Long> readLongSet(String name) { | |||
return MapUtil.readLongSet(this, name); | |||
} | |||
public int readInt(String name, Integer defaultValue) { | |||
return MapUtil.readInt(this, name, defaultValue); | |||
} | |||
public int readInt(String name) { | |||
return MapUtil.readInt(this, name); | |||
} | |||
public float readFloat(String name) { | |||
return MapUtil.readFloat(this, name); | |||
} | |||
public int readInt(String name, Integer defaultValue) { | |||
return MapUtil.readInt(this, name, defaultValue); | |||
} | |||
public float readFloat(String name, Float defaultValue) { | |||
return MapUtil.readFloat(this, name, defaultValue); | |||
} | |||
public float readFloat(String name) { | |||
return MapUtil.readFloat(this, name); | |||
} | |||
public double readDouble(String name) { | |||
return MapUtil.readDouble(this, name); | |||
} | |||
public float readFloat(String name, Float defaultValue) { | |||
return MapUtil.readFloat(this, name, defaultValue); | |||
} | |||
public double readDouble(String name, Double defaultValue) { | |||
return MapUtil.readDouble(this, name, defaultValue); | |||
} | |||
public double readDouble(String name) { | |||
return MapUtil.readDouble(this, name); | |||
} | |||
public boolean readBool(String name) { | |||
return MapUtil.readBool(this, name); | |||
} | |||
public double readDouble(String name, Double defaultValue) { | |||
return MapUtil.readDouble(this, name, defaultValue); | |||
} | |||
public boolean readBool(String name, Boolean defaultValue) { | |||
return MapUtil.readBool(this, name, defaultValue); | |||
} | |||
public boolean readBool(String name) { | |||
return MapUtil.readBool(this, name); | |||
} | |||
@Override | |||
public SwMap put(String name, Object value) { | |||
if (value != null) { | |||
super.put(name, value); | |||
} else { | |||
super.remove(name); | |||
public boolean readBool(String name, Boolean defaultValue) { | |||
return MapUtil.readBool(this, name, defaultValue); | |||
} | |||
return this; | |||
} | |||
@Override | |||
public SwMap put(String name, Object value) { | |||
if (value != null) { | |||
super.put(name, value); | |||
} else { | |||
super.remove(name); | |||
} | |||
public static SwMap of(String name, Object value) { | |||
return new SwMap().put(name, value); | |||
} | |||
return this; | |||
} | |||
public static SwMap of(String name, Object value) { | |||
return new SwMap().put(name, value); | |||
} | |||
} |
@@ -174,9 +174,9 @@ public interface SwEnum { | |||
class DatasetType extends StrEnum { | |||
public static DatasetType instance = new DatasetType(); | |||
public static StrEnumBean LISTR = instance.addEnum("listr", "查询列表"); | |||
public static StrEnumBean LIST = instance.addEnum("list", "查询列表"); | |||
public static StrEnumBean FORM = instance.addEnum("form", "表单"); | |||
public static StrEnumBean LISTW = instance.addEnum("listw", "编辑列表"); | |||
public static StrEnumBean ITEM = instance.addEnum("item", "子表编辑"); | |||
public static StrEnumBean TREE = instance.addEnum("tree", "树"); | |||
public static StrEnumBean ENUM = instance.addEnum("enum", "枚举"); | |||
} | |||
@@ -225,6 +225,10 @@ public class EntityDao<T> extends AbstractEntityDao<T> { | |||
return jdbcEngine.update(sb.toString(), params); | |||
} | |||
public int deleteEntity(List<Long> ids) { | |||
return deleteEntity(" where " + modelTable.getIdField() + " in (" + CommUtil.getSqlInIds(ids) + ")"); | |||
} | |||
private void setTableName(T bean) { | |||
if (bean instanceof DefaultEntity) { | |||
((DefaultEntity) bean).setTableName(this.tableName); | |||
@@ -271,7 +275,7 @@ public class EntityDao<T> extends AbstractEntityDao<T> { | |||
/** | |||
* 查询对象所有数据,返回列表 | |||
*/ | |||
public Map<String, String> queryNames(List<String> ids) { | |||
public Map<String, String> queryNames(List<Long> ids) { | |||
if (ids == null || ids.isEmpty()) return new HashMap<>(); | |||
ModelField field = modelTable.findFieldByType(SwEnum.FieldType.NAME.value); | |||
if (field == null) return new HashMap<>(); | |||
@@ -316,6 +320,15 @@ public class EntityDao<T> extends AbstractEntityDao<T> { | |||
return list; | |||
} | |||
public List<Long> queryIdListWhere(String sqlWhere, Object... params) { | |||
StringBuilder sb = new StringBuilder(); | |||
handleSelect(sb, modelTable.getIdField()); | |||
if (StringUtils.isNotEmpty(sqlWhere)) { | |||
sb.append(" where ").append(sqlWhere); | |||
} | |||
return jdbcEngine.queryLongList(sb.toString(), type, params); | |||
} | |||
public void checkUnique(T bean, String... fields) { | |||
if (fields.length == 0) return; | |||
@@ -0,0 +1,153 @@ | |||
package cc.smtweb.framework.core.db; | |||
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.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/5/6 15:43 | |||
* 实体辅助类 | |||
*/ | |||
public class EntityHelper { | |||
//获取字段别名 | |||
private static String getFieldAlias(Map<String, String> mapFieldAlias, String field) { | |||
return (mapFieldAlias != null && mapFieldAlias.containsKey(field)) ? mapFieldAlias.get(field) : field; | |||
} | |||
/** | |||
* 添加关联字段值 | |||
* @param tableName | |||
* @param bean | |||
*/ | |||
public static void loadBeanLink(String tableName, SwMap bean, Map<String, String> mapFieldAlias) { | |||
//添加关联字段值 | |||
ModelTable table = ModelTableCache.getInstance().getByName(tableName); | |||
if (table == null) return; | |||
List<ModelLinkName> listLink = table.findLinkeNames(); | |||
if (listLink.isEmpty()) return; | |||
//有缓存的,从缓存拿,无缓存的放Map,最后从数据库去拿 | |||
Map<String, List<String>> mapIds = new HashMap<>(); | |||
for (ModelLinkName l : listLink) { | |||
final String fieldName = getFieldAlias(mapFieldAlias, l.getFieldName()); | |||
String value = bean.readString(fieldName); | |||
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(fieldName + "_text", names.length() > 1 ? names.substring(1) : ""); | |||
} else { | |||
List<String> list = mapIds.computeIfAbsent(l.getLinkTable().getName(), k -> new ArrayList<>()); | |||
Collections.addAll(list, ids); | |||
} | |||
} | |||
if (mapIds.isEmpty()) return; | |||
//数据库查询 | |||
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()); | |||
final String fieldName = getFieldAlias(mapFieldAlias, l.getFieldName()); | |||
String value = bean.readString(fieldName); | |||
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(fieldName + "_text", names.substring(1)); | |||
} | |||
} | |||
public static void loadBeanLink(String tableName, List<SwMap> listData, Map<String, String> mapFieldAlias) { | |||
//添加关联字段值 | |||
ModelTable table = ModelTableCache.getInstance().getByName(tableName); | |||
if (table == null) return; | |||
List<ModelLinkName> listLink = table.findLinkeNames(); | |||
if (listLink.isEmpty()) return; | |||
//有缓存的,从缓存拿,无缓存的放Map,最后从数据库去拿 | |||
Map<String, List<Long>> mapIds = new HashMap<>(); | |||
for (SwMap row : listData) { | |||
for (ModelLinkName l : listLink) { | |||
final String fieldName = getFieldAlias(mapFieldAlias, l.getFieldName()); | |||
String value = row.readString(fieldName); | |||
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; | |||
} | |||
if (StringUtils.isNotEmpty(names)) { | |||
row.put(fieldName + "_text", names.substring(1)); | |||
} else { | |||
row.put(fieldName + "_text", ""); | |||
} | |||
} else { | |||
List<Long> list = mapIds.computeIfAbsent(l.getLinkTable().getName(), k -> new ArrayList<>()); | |||
for (String id : ids) { | |||
list.add(Long.parseLong(id)); | |||
} | |||
} | |||
} | |||
} | |||
if (mapIds.isEmpty()) return; | |||
//数据库查询 | |||
Map<String, Map<String, String>> mapValue = new HashMap<>(); | |||
for (Map.Entry<String, List<Long>> entry : mapIds.entrySet()) { | |||
EntityDao dao = DbEngine.getInstance().findDao(entry.getKey()); | |||
mapValue.put(entry.getKey(), dao.queryNames(entry.getValue())); | |||
} | |||
//加值 | |||
for (SwMap row : listData) { | |||
for (ModelLinkName l : listLink) { | |||
if (!mapValue.containsKey(l.getLinkTable().getName())) continue; | |||
Map<String, String> mapV = mapValue.get(l.getLinkTable().getName()); | |||
final String fieldName = getFieldAlias(mapFieldAlias, l.getFieldName()); | |||
String value = row.readString(fieldName); | |||
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; | |||
} | |||
row.put(fieldName + "_text", names.substring(1)); | |||
} | |||
} | |||
} | |||
} |
@@ -1,5 +1,6 @@ | |||
package cc.smtweb.framework.core.db.dao; | |||
import cc.smtweb.framework.core.SwException; | |||
import cc.smtweb.framework.core.annotation.SwTable; | |||
import cc.smtweb.framework.core.cache.CacheManager; | |||
import cc.smtweb.framework.core.common.AbstractEnum; | |||
@@ -388,14 +389,18 @@ public abstract class AbstractEntityDao<T> { | |||
return (Long) idColumn.readValue(entity); | |||
} | |||
public T createBean() throws Exception { | |||
T bean = this.type.newInstance(); | |||
if (bean instanceof DefaultEntity) { | |||
DefaultEntity b = (DefaultEntity)bean; | |||
b.init(); | |||
b.setTableName(this.tableName); | |||
public T createBean() throws SwException { | |||
try { | |||
T bean = this.type.newInstance(); | |||
if (bean instanceof DefaultEntity) { | |||
DefaultEntity b = (DefaultEntity)bean; | |||
b.init(); | |||
b.setTableName(this.tableName); | |||
} | |||
return bean; | |||
} catch (Exception e) { | |||
throw new SwException(e); | |||
} | |||
return bean; | |||
} | |||
} | |||
@@ -18,20 +18,20 @@ import java.util.Map; | |||
*/ | |||
@JsonSerialize(using = BaseBeanSerializer.class) | |||
public class BaseBean implements Serializable { | |||
protected Map<String, Object> data = new HashMap<>(); | |||
protected SwMap data = new SwMap(); | |||
public Map<String, Object> getData() { | |||
public SwMap getData() { | |||
return data; | |||
} | |||
public void setData(Map<String, Object> data) { | |||
public void setData(SwMap data) { | |||
this.data = data; | |||
} | |||
@Override | |||
public BaseBean clone() throws CloneNotSupportedException { | |||
BaseBean bean = (BaseBean) super.clone(); | |||
bean.data = new HashMap<>(); | |||
bean.data = new SwMap(); | |||
bean.getData().putAll(this.data); | |||
return bean; | |||
} | |||
@@ -16,6 +16,7 @@ import java.io.Serializable; | |||
public class DefaultEntity extends BaseBean implements Serializable, Cloneable { | |||
//表名 | |||
private final static String tableNameKey = "_def_table_name"; | |||
private final static String statusKey = "_status"; | |||
public DefaultEntity() {} | |||
@@ -34,6 +35,18 @@ public class DefaultEntity extends BaseBean implements Serializable, Cloneable { | |||
put(tableNameKey, tableName); | |||
} | |||
public void setIsNew(boolean isNew) { | |||
put(statusKey, isNew); | |||
} | |||
public boolean isNew() { | |||
return getBool(statusKey) || getEntityId() <= 0L; | |||
} | |||
public void removeStatus() { | |||
data.remove(statusKey); | |||
} | |||
//根据实体定义,设默认值 | |||
public void init() { | |||
ModelTable entity = getModelTable(); | |||
@@ -64,10 +77,6 @@ public class DefaultEntity extends BaseBean implements Serializable, Cloneable { | |||
data.put(getPkFieldName(), id); | |||
} | |||
public boolean isNew() { | |||
return getEntityId() <= 0L; | |||
} | |||
@Override | |||
public DefaultEntity clone() throws CloneNotSupportedException { | |||
DefaultEntity bean = (DefaultEntity) super.clone(); | |||
@@ -6,8 +6,8 @@ package cc.smtweb.framework.core.db.jdbc; | |||
*/ | |||
public abstract class AbsDbWorker implements IDbWorker { | |||
@Override | |||
public void doAfterDbCommit() throws Exception {} | |||
public void doAfterDbCommit() {} | |||
@Override | |||
public void doAfterDbRollback() throws Exception {} | |||
public void doAfterDbRollback(){} | |||
} |
@@ -1,15 +1,17 @@ | |||
package cc.smtweb.framework.core.db.jdbc; | |||
import cc.smtweb.framework.core.SwException; | |||
/** | |||
* Created by Akmm at 14-2-3 下午4:52 | |||
* 数据库事务操作方法类 | |||
*/ | |||
public interface IDbWorker { | |||
public void work() throws Exception; | |||
public void work(); | |||
//数据库提交完后的业务或缓存处理 | |||
public void doAfterDbCommit() throws Exception; | |||
public void doAfterDbCommit(); | |||
//数据库回滚后的业务或缓存处理 | |||
public void doAfterDbRollback() throws Exception; | |||
public void doAfterDbRollback(); | |||
} |
@@ -1,5 +1,6 @@ | |||
package cc.smtweb.framework.core.db.jdbc; | |||
import cc.smtweb.framework.core.SwException; | |||
import cc.smtweb.framework.core.SwMap; | |||
import cc.smtweb.framework.core.db.impl.BaseBean; | |||
import cc.smtweb.framework.core.db.impl.DefaultEntity; | |||
@@ -298,7 +299,7 @@ public class JdbcEngine { | |||
return false; | |||
} | |||
public void beginTrans() throws Exception { | |||
public void beginTrans() { | |||
Thread t = Thread.currentThread(); | |||
JdbcTrans jdbcTrans = mapThreadTrans.get(t); | |||
if (jdbcTrans != null) { | |||
@@ -308,10 +309,10 @@ public class JdbcEngine { | |||
mapThreadTrans.put(t, openTrans()); | |||
} | |||
public void commit() throws Exception { | |||
public void commit() throws SwException { | |||
Thread t = Thread.currentThread(); | |||
JdbcTrans tm = this.mapThreadTrans.get(t); | |||
if (tm == null) throw new Exception("当前没有开启事务。"); | |||
if (tm == null) throw new SwException("当前没有开启事务。"); | |||
if (tm.canCommit()) { | |||
tm.doEnd("commit"); | |||
tm.commit(); | |||
@@ -325,19 +326,19 @@ public class JdbcEngine { | |||
} | |||
} | |||
public void rollback(Exception e) throws Exception { | |||
public void rollback(Exception e) throws SwException { | |||
Thread t = Thread.currentThread(); | |||
JdbcTrans tm = mapThreadTrans.get(t); | |||
if (tm == null) throw new Exception("当前没有开启事务。"); | |||
if (tm == null) throw new SwException("当前没有开启事务。"); | |||
if (tm.canCommit()) { | |||
tm.doEnd("rollback"); | |||
mapThreadTrans.remove(t); | |||
tm.rollback(); | |||
} | |||
if (e != null) throw e; | |||
if (e != null) throw new SwException(e); | |||
} | |||
public void doTrans(IDbWorker dbWorker) throws Exception { | |||
public void doTrans(IDbWorker dbWorker) throws SwException { | |||
beginTrans(); | |||
try { | |||
dbWorker.work(); | |||
@@ -345,7 +346,7 @@ public class JdbcEngine { | |||
} catch (Exception e) { | |||
rollback(e); | |||
dbWorker.doAfterDbRollback(); | |||
throw e; | |||
throw new SwException(e); | |||
} | |||
dbWorker.doAfterDbCommit(); | |||
} | |||
@@ -429,6 +430,15 @@ public class JdbcEngine { | |||
/*================以下为具名参数方法================================================*/ | |||
public int queryIntN(String sql, Map<String, ?> params) { | |||
List<Integer> list = namedJdbcTemplate.query(sql, params, (resultSet, i) -> resultSet.getInt(1)); | |||
if (list != null && !list.isEmpty()) { | |||
return list.get(0); | |||
} | |||
return 0; | |||
} | |||
public <T> T queryEntityN(String sql, Map<String, ?> params, Class<T> type) { | |||
List<T> list = queryN(sql, params, type); | |||
@@ -1,9 +0,0 @@ | |||
package cc.smtweb.framework.core.db.vo; | |||
import lombok.Data; | |||
@Data | |||
public class KeyValueVO { | |||
private String key; | |||
private String value; | |||
} |
@@ -36,4 +36,5 @@ public class ModelField { | |||
public boolean isNotNull() { | |||
return notNull == 1; | |||
} | |||
} |
@@ -20,7 +20,7 @@ public abstract class AbstractCompService { | |||
protected abstract IHandler createHandler(String type); | |||
private IHandler getHandler(SwMap params, UserSession us, String type) throws Exception { | |||
protected IHandler getHandler(SwMap params, UserSession us, String type) throws Exception { | |||
IHandler handler = createHandler(type); | |||
if (handler == null) throw new SwException("暂不支持此类服务:" + type); | |||
if (params == null) params = new SwMap(); | |||
@@ -1,8 +1,10 @@ | |||
package cc.smtweb.framework.core.mvc.service; | |||
import cc.smtweb.framework.core.R; | |||
import cc.smtweb.framework.core.SwException; | |||
import cc.smtweb.framework.core.SwMap; | |||
import cc.smtweb.framework.core.db.DbEngine; | |||
import cc.smtweb.framework.core.db.jdbc.AbsDbWorker; | |||
import cc.smtweb.framework.core.session.UserSession; | |||
import lombok.extern.slf4j.Slf4j; | |||
@@ -19,15 +21,22 @@ public abstract class AbstractDelHandler extends AbstractHandler{ | |||
id = readId(); | |||
checkValid(); | |||
DbEngine.getInstance().doTrans(() -> { | |||
try { | |||
DbEngine.getInstance().doTrans(new AbsDbWorker() { | |||
@Override | |||
public void work() { | |||
del(); | |||
} | |||
@Override | |||
public void doAfterDbCommit() { | |||
super.doAfterDbCommit(); | |||
saveSuccess(); | |||
return true; | |||
} catch (Exception e) { | |||
} | |||
@Override | |||
public void doAfterDbRollback() { | |||
super.doAfterDbRollback(); | |||
saveFailed(); | |||
log.error("保存失败!", e); | |||
return false; | |||
} | |||
}); | |||
return R.success(); | |||
@@ -41,8 +50,8 @@ public abstract class AbstractDelHandler extends AbstractHandler{ | |||
return params.readLong("id", 0L); | |||
} | |||
protected abstract void checkValid() throws Exception; | |||
protected abstract void del() throws Exception; | |||
protected abstract void checkValid(); | |||
protected abstract void del(); | |||
protected void saveSuccess() {} | |||
protected void saveFailed() {} | |||
} |
@@ -32,17 +32,17 @@ public abstract class AbstractSaveHandler<T> extends AbstractHandler { | |||
checkValid(); | |||
DbEngine.getInstance().doTrans(new AbsDbWorker() { | |||
@Override | |||
public void work() throws Exception { | |||
public void work(){ | |||
save(); | |||
} | |||
@Override | |||
public void doAfterDbCommit() throws Exception { | |||
public void doAfterDbCommit(){ | |||
saveSuccess(); | |||
} | |||
@Override | |||
public void doAfterDbRollback() throws Exception { | |||
public void doAfterDbRollback(){ | |||
saveFailed(); | |||
} | |||
}); | |||
@@ -63,10 +63,10 @@ public abstract class AbstractSaveHandler<T> extends AbstractHandler { | |||
protected abstract void readFromPage(); | |||
//保存前的校验 | |||
protected abstract void checkValid() throws Exception; | |||
protected abstract void checkValid(); | |||
//保存到数据库 | |||
protected abstract void save() throws Exception; | |||
protected abstract void save(); | |||
//保存成功之后 | |||
protected void saveSuccess() { | |||
@@ -24,7 +24,7 @@ public class DefaultDelHandler<T extends DefaultEntity> extends AbstractDelHandl | |||
} | |||
@Override | |||
protected void checkValid() throws Exception { | |||
protected void checkValid() { | |||
ModelTable table = ModelTableCache.getInstance().getByName(tableName); | |||
//todo 检查外键引用的使用情况 | |||
@@ -6,6 +6,7 @@ import cc.smtweb.framework.core.cache.CacheManager; | |||
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.EntityHelper; | |||
import cc.smtweb.framework.core.db.cache.ModelTableCache; | |||
import cc.smtweb.framework.core.db.impl.DefaultEntity; | |||
import cc.smtweb.framework.core.db.vo.ModelField; | |||
@@ -69,64 +70,6 @@ public class DefaultListHandler<T extends DefaultEntity> extends AbstractListHan | |||
@Override | |||
protected void afterQuery(List<SwMap> listData) { | |||
super.afterQuery(listData); | |||
//添加关联字段值 | |||
ModelTable table = ModelTableCache.getInstance().getByName(tableName); | |||
if (table == null) return; | |||
List<ModelLinkName> listLink = table.findLinkeNames(); | |||
if (listLink.isEmpty()) return; | |||
//有缓存的,从缓存拿,无缓存的放Map,最后从数据库去拿 | |||
Map<String, List<String>> mapIds = new HashMap<>(); | |||
for (SwMap row : listData) { | |||
for (ModelLinkName l : listLink) { | |||
String value = row.readString(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; | |||
} | |||
if (StringUtils.isNotEmpty(names)) { | |||
row.put(l.getFieldName() + "_text", names.substring(1)); | |||
} else { | |||
row.put(l.getFieldName() + "_text", ""); | |||
} | |||
} else { | |||
List<String> list = mapIds.computeIfAbsent(l.getLinkTable().getName(), k -> new ArrayList<>()); | |||
Collections.addAll(list, ids); | |||
} | |||
} | |||
} | |||
if (mapIds.isEmpty()) return; | |||
//数据库查询 | |||
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 (SwMap row : listData) { | |||
for (ModelLinkName l : listLink) { | |||
if (!mapValue.containsKey(l.getLinkTable().getName())) continue; | |||
Map<String, String> mapV = mapValue.get(l.getLinkTable().getName()); | |||
String value = row.readString(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; | |||
} | |||
row.put(l.getFieldName() + "_text", names.substring(1)); | |||
} | |||
} | |||
EntityHelper.loadBeanLink(tableName, listData, null); | |||
} | |||
} |
@@ -1,10 +1,12 @@ | |||
package cc.smtweb.framework.core.mvc.service; | |||
import cc.smtweb.framework.core.SwException; | |||
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.EntityHelper; | |||
import cc.smtweb.framework.core.db.cache.ModelTableCache; | |||
import cc.smtweb.framework.core.db.impl.DefaultEntity; | |||
import cc.smtweb.framework.core.db.vo.ModelLinkName; | |||
@@ -30,60 +32,8 @@ public class DefaultProvider<T extends DefaultEntity> extends AbstractCompProvid | |||
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.length()>1 ? 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)); | |||
} | |||
if (bean == null) throw new SwException("没有找到指定数据(id=" + id + ")"); | |||
EntityHelper.loadBeanLink(bean.getTableName(), bean.getData(), null); | |||
return bean; | |||
} | |||
} |
@@ -64,7 +64,7 @@ public class DefaultSaveHandler<T extends DefaultEntity> extends AbstractSaveHan | |||
} | |||
@Override | |||
protected void checkValid() throws Exception { | |||
protected void checkValid() { | |||
ModelTable table = ModelTableCache.getInstance().getByName(tableName); | |||
for (ModelField field : table.getFields()) { | |||
if (field.isNotNull() && StringUtils.isEmpty(bean.getStr(field.getName()))) { | |||
@@ -81,7 +81,7 @@ public class DefaultSaveHandler<T extends DefaultEntity> extends AbstractSaveHan | |||
} | |||
@Override | |||
protected void save() throws Exception { | |||
protected void save() { | |||
EntityDao<T> dao = DbEngine.getInstance().findDao(tableName); | |||
if (isNew) { | |||
dao.insertEntity(bean); | |||
@@ -145,19 +145,19 @@ public class DefaultSaveHandler<T extends DefaultEntity> extends AbstractSaveHan | |||
bean.put(fieldParentCode.getName(), parentId); | |||
DbEngine.getInstance().doTrans(new AbsDbWorker() { | |||
@Override | |||
public void work() throws Exception { | |||
public void work() { | |||
EntityDao dao = DbEngine.getInstance().findDao(tableName); | |||
listTreeBean = TreeHelper.getTreeHelper(tableName).resetTreeLevel(bean); | |||
dao.updateEntity(bean, fieldParentCode.getName()); | |||
} | |||
@Override | |||
public void doAfterDbCommit() throws Exception { | |||
public void doAfterDbCommit(){ | |||
saveSuccess(); | |||
} | |||
@Override | |||
public void doAfterDbRollback() throws Exception { | |||
public void doAfterDbRollback() { | |||
saveFailed(); | |||
} | |||
}); | |||
@@ -14,6 +14,8 @@ public class SqlNamedPara { | |||
public int page = 0; | |||
//每页记录数 | |||
public int rows = 20; | |||
//字段别名 | |||
public Map<String, String> mapFieldAlias = new HashMap<>(); | |||
public SqlNamedPara(String sql) { | |||
this.sql = sql; | |||
@@ -27,4 +29,8 @@ public class SqlNamedPara { | |||
public void addParas(String name, Object value) { | |||
paras.put(name, value); | |||
} | |||
public void addFieldAlias(String fieldName, String alias) { | |||
mapFieldAlias.put(fieldName, alias); | |||
} | |||
} |
@@ -88,7 +88,7 @@ public class TreeHelper<T extends DefaultEntity> { | |||
return list; | |||
} | |||
public List<T> resetTreeLevel(T bean) throws Exception { | |||
public List<T> resetTreeLevel(T bean) { | |||
List<T> list = new ArrayList<>(); | |||
if (table.getType() != SwEnum.TableType.TYPE_TREE.value) return list; | |||
if (!table.isNeedCache()) throw new SwException("请定义为需要缓存!"); | |||
@@ -100,7 +100,7 @@ public class TreeHelper<T extends DefaultEntity> { | |||
return list; | |||
} | |||
private void resetParentChildren(T bean, List<T> list) throws Exception { | |||
private void resetParentChildren(T bean, List<T> list) { | |||
T parent = cache.get(getParentId(bean)); | |||
if (parent != null) { | |||
bean.put(fieldLevelCode, getLevelCode(parent) + SwConsts.SPLIT_CHAR + parent.getEntityId()); | |||
@@ -1,5 +1,6 @@ | |||
package cc.smtweb.framework.core.util; | |||
import cc.smtweb.framework.core.SwException; | |||
import cc.smtweb.framework.core.db.impl.BaseBean; | |||
import cc.smtweb.framework.core.db.impl.DefaultEntity; | |||
import cc.smtweb.framework.core.util.kryo.KryoTool; | |||
@@ -7,8 +8,10 @@ import lombok.extern.slf4j.Slf4j; | |||
import org.apache.commons.lang3.StringUtils; | |||
import java.io.Serializable; | |||
import java.io.UnsupportedEncodingException; | |||
import java.lang.reflect.ParameterizedType; | |||
import java.lang.reflect.Type; | |||
import java.nio.charset.StandardCharsets; | |||
import java.text.Collator; | |||
import java.util.Collection; | |||
import java.util.HashSet; | |||
@@ -88,7 +91,7 @@ public class CommUtil { | |||
} | |||
} | |||
public static String getSqlInIds(Collection<String> ids) { | |||
/*public static String getSqlInIds(Collection<String> ids) { | |||
if (null == ids || ids.isEmpty()) return ""; | |||
StringBuilder s = new StringBuilder(128); | |||
Set<String> set = new HashSet<>(); | |||
@@ -100,10 +103,30 @@ public class CommUtil { | |||
} | |||
if (s.length() == 0) return ""; | |||
return s.substring(0, s.length() - 1); | |||
}*/ | |||
public static String getSqlInIds(Collection<Long> ids) { | |||
if (null == ids || ids.isEmpty()) return ""; | |||
StringBuilder s = new StringBuilder(128); | |||
Set<Long> set = new HashSet<>(); | |||
for (Long id : ids) { | |||
if (set.contains(id)) continue; | |||
set.add(id); | |||
if (id <= 0) continue; | |||
s.append(id).append(","); | |||
} | |||
if (s.length() == 0) return ""; | |||
return s.substring(0, s.length() - 1); | |||
} | |||
//中文字符串比较 | |||
public static int chineseCompare(String s1, String s2) { | |||
return chineseCollator.compare(s1, s2); | |||
} | |||
//获取字段字符串长度 | |||
public static int getStrLenB(String s) { | |||
if (StringUtils.isEmpty(s)) return 0; | |||
return s.getBytes(StandardCharsets.UTF_8).length; | |||
} | |||
} |
@@ -26,11 +26,11 @@ public class UiControlPropsVO extends SwMap { | |||
return StringUtils.isNotBlank(v.toString()); | |||
} | |||
public Map<String, Object> readMap(String name) { | |||
public SwMap readMap(String name) { | |||
Object item = get(name); | |||
if (item instanceof Map) { | |||
return (Map<String, Object>) item; | |||
if (item instanceof SwMap) { | |||
return (SwMap) item; | |||
} | |||
return null; | |||
@@ -4,17 +4,13 @@ import cc.smtweb.framework.core.SwException; | |||
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.util.CommUtil; | |||
import cc.smtweb.framework.core.util.JsonUtil; | |||
import cc.smtweb.framework.core.util.MapUtil; | |||
import cc.smtweb.system.bpm.web.design.form.define.PageDataSet; | |||
import cc.smtweb.system.bpm.web.design.form.define.PageDatasetDynCond; | |||
import cc.smtweb.system.bpm.web.design.form.define.PageDatasetField; | |||
import cc.smtweb.system.bpm.web.design.form.define.PageInfo; | |||
import com.fasterxml.jackson.core.JsonGenerator; | |||
import com.fasterxml.jackson.core.JsonProcessingException; | |||
import com.fasterxml.jackson.databind.ObjectMapper; | |||
import com.fasterxml.jackson.databind.SerializerProvider; | |||
import com.fasterxml.jackson.databind.ser.FilterProvider; | |||
import com.fasterxml.jackson.databind.ser.PropertyWriter; | |||
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter; | |||
@@ -1,10 +1,10 @@ | |||
package cc.smtweb.system.bpm.web.design.form.define; | |||
import cc.smtweb.framework.core.SwException; | |||
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 lombok.Data; | |||
import org.apache.commons.lang3.StringUtils; | |||
import java.util.HashMap; | |||
@@ -28,6 +28,10 @@ public class PageDataSet { | |||
public String sql; | |||
//固定条件,如f1='a' and f2=:p1 | |||
public String fixedCond; | |||
//是否懒加载 | |||
public boolean lazy; | |||
//是否可编辑 | |||
public boolean canEdit; | |||
//select的字段 | |||
public List<PageDatasetField> fields; | |||
public List<PageDatasetFilter> filters; | |||
@@ -84,4 +88,13 @@ public class PageDataSet { | |||
field.remark = mf.getRemark(); | |||
} | |||
} | |||
public PageDatasetFilter findFilterByDs(String dsName) { | |||
for (PageDatasetFilter filter: filters) { | |||
if (SwEnum.FilterType.LINK.value.equals(filter.type) && dsName.equals(filter.linkDb)) { | |||
return filter; | |||
} | |||
} | |||
return null; | |||
} | |||
} |
@@ -12,7 +12,9 @@ import cc.smtweb.framework.core.mvc.service.TreeHelper; | |||
import cc.smtweb.framework.core.session.UserSession; | |||
import cc.smtweb.framework.core.util.CommUtil; | |||
import java.util.*; | |||
import java.util.ArrayList; | |||
import java.util.Collection; | |||
import java.util.List; | |||
/** | |||
* Created by Akmm at 2022/3/21 18:22 | |||
@@ -1,7 +1,6 @@ | |||
package cc.smtweb.system.bpm.web.design.table; | |||
import cc.smtweb.framework.core.db.vo.ModelDatabase; | |||
import cc.smtweb.framework.core.db.vo.ModelTable; | |||
import cc.smtweb.framework.core.mvc.service.DefaultComboHandler; | |||
import java.util.List; | |||
@@ -1,9 +1,9 @@ | |||
package cc.smtweb.system.bpm.web.design.table; | |||
import cc.smtweb.framework.core.annotation.SwService; | |||
import cc.smtweb.framework.core.db.vo.ModelCatalog; | |||
import cc.smtweb.framework.core.db.vo.ModelDatabase; | |||
import cc.smtweb.framework.core.mvc.service.*; | |||
import cc.smtweb.framework.core.db.vo.ModelCatalog; | |||
/** | |||
* Created by Akmm at 2022/3/22 9:12 | |||
@@ -1,9 +1,9 @@ | |||
package cc.smtweb.system.bpm.web.design.table; | |||
import cc.smtweb.framework.core.annotation.SwService; | |||
import cc.smtweb.framework.core.db.vo.ModelCatalog; | |||
import cc.smtweb.framework.core.db.vo.ModelTable; | |||
import cc.smtweb.framework.core.mvc.service.*; | |||
import cc.smtweb.framework.core.db.vo.ModelCatalog; | |||
/** | |||
* Created by Akmm at 2022/3/22 9:12 | |||
@@ -1,19 +1,12 @@ | |||
package cc.smtweb.system.bpm.web.engine; | |||
import cc.smtweb.framework.core.R; | |||
import cc.smtweb.framework.core.SwException; | |||
import cc.smtweb.framework.core.SwMap; | |||
import cc.smtweb.framework.core.common.SwEnum; | |||
import cc.smtweb.framework.core.mvc.service.AbstractHandler; | |||
import cc.smtweb.framework.core.mvc.service.SwListData; | |||
import cc.smtweb.framework.core.session.UserSession; | |||
import cc.smtweb.system.bpm.web.design.form.ModelFormHelper; | |||
import cc.smtweb.system.bpm.web.design.form.define.PageDataSet; | |||
import cc.smtweb.system.bpm.web.design.form.define.PageInfo; | |||
import java.util.HashMap; | |||
import java.util.Map; | |||
/** | |||
* Created by Akmm at 2022/4/21 17:53 | |||
*/ | |||
@@ -1,16 +1,12 @@ | |||
package cc.smtweb.system.bpm.web.engine; | |||
import cc.smtweb.framework.core.R; | |||
import cc.smtweb.framework.core.SwException; | |||
import cc.smtweb.framework.core.SwMap; | |||
import cc.smtweb.framework.core.common.SwEnum; | |||
import cc.smtweb.framework.core.db.DbEngine; | |||
import cc.smtweb.framework.core.mvc.service.SqlNamedPara; | |||
import cc.smtweb.framework.core.mvc.service.SwListData; | |||
import cc.smtweb.system.bpm.web.design.form.define.PageDataSet; | |||
import java.util.HashMap; | |||
import java.util.List; | |||
import java.util.Map; | |||
/** | |||
@@ -18,55 +14,28 @@ import java.util.Map; | |||
* 新增操作,初始化定义的数据集 | |||
*/ | |||
public class DynPageAddHandler extends AbstractDynPageHandler { | |||
//数据集 | |||
private String dbName; | |||
//过滤条件 | |||
private Map<String, Object> filter = new HashMap<>(); | |||
//对应的数据集定义 | |||
private PageDataSet pageDataSet; | |||
@Override | |||
public R doWork() throws Exception { | |||
dbName = params.readString("dataset"); | |||
filter = (Map<String, Object>)params.get("filter"); | |||
pageDataSet = pageInfo.findDataSet(dbName); | |||
if (pageDataSet == null) throw new SwException("没有找到指定的的数据集定义:" + dbName + "!"); | |||
DynRetBean bean = null; | |||
if (SwEnum.DatasetType.LISTR.value.equals(pageDataSet.type)) {//列表类 | |||
bean = DynRetBean.createList(loadList()); | |||
} else if (SwEnum.DatasetType.TREE.value.equals(pageDataSet.type)) {//列表类 | |||
// return new DynRetBean(loadTree()); | |||
} else { | |||
bean = DynRetBean.createBean(loadOne()); | |||
} | |||
return R.success(bean); | |||
} | |||
/** | |||
* 返回单个对象 | |||
* @return | |||
*/ | |||
private SwMap loadOne() { | |||
SqlNamedPara sqlPara = DynPageHelper.buildSelectSql(pageDataSet, filter); | |||
return DbEngine.getInstance().queryEntityN(sqlPara.sql, sqlPara.paras, SwMap.class); | |||
} | |||
/** | |||
* 返回list | |||
* @return | |||
*/ | |||
private SwListData loadList() { | |||
SqlNamedPara sqlPara = DynPageHelper.buildSelectSql(pageDataSet, filter); | |||
List<SwMap> list; | |||
if (sqlPara.page > 0 && sqlPara.rows > 0) { | |||
list = DbEngine.getInstance().pagedQueryN(sqlPara.sql, SwMap.class, (sqlPara.page - 1) * sqlPara.rows, sqlPara.rows, sqlPara.paras); | |||
} else { | |||
list = DbEngine.getInstance().queryN(sqlPara.sql, sqlPara.paras, SwMap.class); | |||
//返回的数据,以dataset.name为key,查出的结果(bean或list)为value | |||
Map<String, Object> mapRet = new HashMap<>(); | |||
for (PageDataSet dataSet : pageInfo.db) { | |||
if (!dataSet.canEdit) { | |||
continue; | |||
} | |||
if (SwEnum.DatasetType.FORM.value.equals(dataSet.type)) {//单表 | |||
//懒加载,给个空对象 | |||
if (dataSet.lazy) { | |||
mapRet.put(dataSet.name, new SwMap()); | |||
continue; | |||
} | |||
mapRet.put(dataSet.name, DynPageHelper.createBean(dataSet)); | |||
} else if (SwEnum.DatasetType.TREE.value.equals(dataSet.type)) {//树 | |||
mapRet.put(dataSet.name, new SwMap()); | |||
} else if (!SwEnum.DatasetType.ENUM.value.equals(dataSet.type)) {//非枚举 | |||
mapRet.put(dataSet.name, SwListData.create(null, 0)); | |||
} | |||
} | |||
return SwListData.create(list, sqlPara.rows); | |||
return R.success(mapRet); | |||
} | |||
} |
@@ -0,0 +1,168 @@ | |||
package cc.smtweb.system.bpm.web.engine; | |||
import cc.smtweb.framework.core.R; | |||
import cc.smtweb.framework.core.SwException; | |||
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.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.jdbc.AbsDbWorker; | |||
import cc.smtweb.framework.core.db.jdbc.IDbWorker; | |||
import cc.smtweb.framework.core.db.vo.ModelTable; | |||
import cc.smtweb.framework.core.mvc.service.SwListData; | |||
import cc.smtweb.system.bpm.web.design.form.define.PageDataSet; | |||
import cc.smtweb.system.bpm.web.design.form.define.PageDatasetFilter; | |||
import org.apache.commons.lang3.StringUtils; | |||
import java.util.ArrayList; | |||
import java.util.HashMap; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.function.Function; | |||
/** | |||
* Created by Akmm at 2022/4/21 17:53 | |||
* 删除操作 | |||
*/ | |||
public class DynPageDelHandler extends AbstractDynPageHandler { | |||
@Override | |||
public R doWork() throws Exception { | |||
return delAll(); | |||
} | |||
/** | |||
* 删除指定数据集,入参{pageId, dataset, id} | |||
* | |||
* @return | |||
*/ | |||
public R delOne() { | |||
//数据集 | |||
String dbName = params.readString("dataset"); | |||
//对应的数据集定义 | |||
PageDataSet pageDataSet = pageInfo.findDataSet(dbName); | |||
if (pageDataSet == null) throw new SwException("没有找到指定的的数据集定义:" + dbName + "!"); | |||
long id = params.readLong("id"); | |||
if (id == 0) throw new SwException("没有收到待删除记录Id!"); | |||
checkBean(pageDataSet, id); | |||
DbEngine.getInstance().doTrans(new AbsDbWorker() { | |||
@Override | |||
public void work() { | |||
ModelTable table = ModelTableCache.getInstance().get(pageDataSet.masterTable); | |||
if (table == null) throw new SwException("没有找到指定的的表定义:" + pageDataSet.name + "!"); | |||
EntityDao dao = DbEngine.getInstance().findDao(table.getName()); | |||
dao.deleteEntity(id); | |||
} | |||
@Override | |||
public void doAfterDbCommit() { | |||
ModelTable table = ModelTableCache.getInstance().get(pageDataSet.masterTable); | |||
if (table.isNeedCache()) { | |||
AbstractCache cache = CacheManager.getIntance().getCache(table.getName()); | |||
cache.remove(id); | |||
} | |||
} | |||
}); | |||
return R.success(); | |||
} | |||
public R delAll() { | |||
long id = params.readLong("id"); | |||
if (id == 0) throw new SwException("没有收到待删除记录Id!"); | |||
//校验主表即可 | |||
PageDataSet masterDs = pageInfo.db.get(0); | |||
if (!masterDs.canEdit || !SwEnum.DatasetType.FORM.value.equals(masterDs.type)) | |||
throw new SwException("主表不允许删除!"); | |||
checkBean(masterDs, id); | |||
Map<String, RemovableInfo> mapRemovableInfo = new HashMap<>(); | |||
for (PageDataSet pageDataSet : pageInfo.db) { | |||
//非表单编辑,不管 | |||
if (!pageDataSet.canEdit || !SwEnum.DatasetType.FORM.value.equals(pageDataSet.type)) continue; | |||
ModelTable table = ModelTableCache.getInstance().get(pageDataSet.masterTable); | |||
RemovableInfo info = mapRemovableInfo.computeIfAbsent(table.getName(), k -> { | |||
RemovableInfo ret = new RemovableInfo(k); | |||
if (pageDataSet != masterDs) {//非主表,记录一下关联字段 | |||
PageDatasetFilter f = pageDataSet.findFilterByDs(masterDs.name); | |||
if (f == null) { | |||
throw new SwException("无法删除表【" + pageDataSet.name + "】,此表未关联主表!"); | |||
} | |||
ret.field = f.field; | |||
} | |||
return ret; | |||
}); | |||
if (pageDataSet == masterDs || !table.isNeedCache()) { | |||
//主表或没有缓存,直接按FK删除 | |||
info.addId(id); | |||
} else { | |||
info.needCache = true; | |||
//有缓存,需要先把id查出来 | |||
EntityDao<DefaultEntity> dao = DbEngine.getInstance().findDao(table.getName()); | |||
List<Long> list = dao.queryIdListWhere(info.field + " = ?", id); | |||
info.ids.addAll(list); | |||
} | |||
} | |||
DbEngine.getInstance().doTrans(new AbsDbWorker() { | |||
@Override | |||
public void work() { | |||
for (RemovableInfo info : mapRemovableInfo.values()) { | |||
EntityDao<DefaultEntity> dao = DbEngine.getInstance().findDao(info.tableName); | |||
if (!info.needCache && StringUtils.isNotEmpty(info.field)) { | |||
//按外键来删除 | |||
dao.deleteEntity(" where " + info.field + "=?", id); | |||
} else { | |||
dao.deleteEntity(info.ids); | |||
} | |||
} | |||
} | |||
@Override | |||
public void doAfterDbCommit() { | |||
for (RemovableInfo info : mapRemovableInfo.values()) { | |||
if (!info.needCache) continue; | |||
AbstractCache cache = CacheManager.getIntance().getCache(info.tableName); | |||
cache.remove(id); | |||
} | |||
} | |||
}); | |||
return R.success(); | |||
} | |||
/** | |||
* 删除校验 | |||
* | |||
* @param pageDataSet | |||
* @param id | |||
*/ | |||
protected void checkBean(PageDataSet pageDataSet, long id) { | |||
//校验外键引用关系 | |||
} | |||
/** | |||
* 待删除信息 | |||
*/ | |||
class RemovableInfo { | |||
String tableName; | |||
String field = null; | |||
boolean needCache = false; | |||
List<Long> ids = new ArrayList<>(); | |||
public RemovableInfo(String tableName) { | |||
this.tableName = tableName; | |||
} | |||
void addId(long id) { | |||
ids.add(id); | |||
} | |||
} | |||
} |
@@ -4,21 +4,42 @@ import cc.smtweb.framework.core.SwException; | |||
import cc.smtweb.framework.core.SwMap; | |||
import cc.smtweb.framework.core.common.SwConsts; | |||
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.ModelTable; | |||
import cc.smtweb.framework.core.mvc.service.SqlNamedPara; | |||
import cc.smtweb.framework.core.mvc.service.SqlPara; | |||
import cc.smtweb.framework.core.util.MapUtil; | |||
import cc.smtweb.system.bpm.web.design.form.define.*; | |||
import javafx.scene.control.Tab; | |||
import org.apache.commons.lang3.StringUtils; | |||
import java.util.*; | |||
import java.util.HashMap; | |||
import java.util.HashSet; | |||
import java.util.Map; | |||
import java.util.Set; | |||
/** | |||
* Created by Akmm at 2022/4/23 10:01 | |||
* 动态页面辅助类 | |||
*/ | |||
public class DynPageHelper { | |||
/** | |||
* 新建bean | |||
* @param dataSet | |||
* @return | |||
*/ | |||
public static SwMap createBean(PageDataSet dataSet) { | |||
//主表 | |||
ModelTable masterTable = ModelTableCache.getInstance().get(dataSet.masterTable); | |||
EntityDao<DefaultEntity> dao = DbEngine.getInstance().findDao(masterTable.getTableName()); | |||
try { | |||
DefaultEntity bean = dao.createBean(); | |||
return bean.getData(); | |||
} catch (Exception e) { | |||
throw new SwException(e); | |||
} | |||
} | |||
/** | |||
* 构建select fields from table | |||
@@ -28,9 +49,10 @@ public class DynPageHelper { | |||
*/ | |||
public static SqlNamedPara buildSelectSql(PageDataSet dataSet, Map<String, Object> params) { | |||
StringBuilder sql = new StringBuilder(512); | |||
sql.append(buildSelFieldsSql(dataSet)); | |||
SqlNamedPara sqlNamedPara = buildWhereSql(dataSet, params); | |||
sql.append(buildSelFieldsSql(dataSet, sqlNamedPara)); | |||
if (StringUtils.isNotEmpty(sqlNamedPara.sql)) { | |||
sql.append(" where ").append(sqlNamedPara.sql); | |||
} | |||
@@ -49,18 +71,19 @@ public class DynPageHelper { | |||
return sqlNamedPara; | |||
} | |||
private static String buildSelFieldsSql(PageDataSet dataSet) { | |||
private static String buildSelFieldsSql(PageDataSet dataSet, SqlNamedPara sqlNamedPara) { | |||
StringBuilder sql = new StringBuilder(512); | |||
//主表 | |||
ModelTable masterTable = ModelTableCache.getInstance().get(dataSet.masterTable); | |||
//非查询列表,或sql为空,则自己组装select sql | |||
if (!SwEnum.DatasetType.LISTR.equals(dataSet.type) || StringUtils.isEmpty(dataSet.sql)) { | |||
if (!SwEnum.DatasetType.LIST.equals(dataSet.type) || StringUtils.isEmpty(dataSet.sql)) { | |||
sql.append("select "); | |||
for (PageDatasetField field : dataSet.fields) { | |||
sql.append(field.field); | |||
//加别名 | |||
if (!field.field.equalsIgnoreCase(field.name)) { | |||
sql.append(" ").append(field.name); | |||
sqlNamedPara.addFieldAlias(field.field, field.name); | |||
} | |||
sql.append(","); | |||
} | |||
@@ -104,7 +127,16 @@ public class DynPageHelper { | |||
return new SqlNamedPara(sql.toString(), args); | |||
} | |||
public static String buildDynCondSql(PageDataSet dataSet, PageDatasetDynCond dynCond, Map<String, Object> params, SwMap args, Set<String> setFixedFilter) { | |||
/** | |||
* 构建动态条件 | |||
* @param dataSet | |||
* @param dynCond | |||
* @param params | |||
* @param args | |||
* @param setFixedFilter | |||
* @return | |||
*/ | |||
private static String buildDynCondSql(PageDataSet dataSet, PageDatasetDynCond dynCond, Map<String, Object> params, SwMap args, Set<String> setFixedFilter) { | |||
if (dynCond.isOpt()) {//是and/or | |||
StringBuilder sql = new StringBuilder(256); | |||
boolean b = false; | |||
@@ -144,7 +176,6 @@ public class DynPageHelper { | |||
return builder.build(dynCond.opt, filter.sqlName, ns, value, args); | |||
} | |||
private static Map<String, IBuilderExpr> mapBuilder; | |||
private static IBuilderExpr baseBuilder; | |||
@@ -5,11 +5,13 @@ import cc.smtweb.framework.core.SwException; | |||
import cc.smtweb.framework.core.SwMap; | |||
import cc.smtweb.framework.core.common.SwEnum; | |||
import cc.smtweb.framework.core.db.DbEngine; | |||
import cc.smtweb.framework.core.mvc.service.*; | |||
import cc.smtweb.system.bpm.web.design.form.ModelForm; | |||
import cc.smtweb.system.bpm.web.design.form.ModelFormHelper; | |||
import cc.smtweb.framework.core.db.EntityHelper; | |||
import cc.smtweb.framework.core.db.cache.ModelTableCache; | |||
import cc.smtweb.framework.core.db.vo.ModelTable; | |||
import cc.smtweb.framework.core.mvc.service.SqlNamedPara; | |||
import cc.smtweb.framework.core.mvc.service.SqlPara; | |||
import cc.smtweb.framework.core.mvc.service.SwListData; | |||
import cc.smtweb.system.bpm.web.design.form.define.PageDataSet; | |||
import cc.smtweb.system.bpm.web.design.form.define.PageInfo; | |||
import java.util.HashMap; | |||
import java.util.List; | |||
@@ -18,11 +20,11 @@ import java.util.Map; | |||
/** | |||
* Created by Akmm at 2022/4/21 17:53 | |||
*/ | |||
public class DynPageLoadHandler extends AbstractDynPageHandler { | |||
public class DynPageLoadOneHandler extends AbstractDynPageHandler { | |||
//数据集 | |||
private String dbName; | |||
//过滤条件 | |||
private Map<String, Object> filter = new HashMap<>(); | |||
private SwMap filter = new SwMap(); | |||
//对应的数据集定义 | |||
private PageDataSet pageDataSet; | |||
@@ -30,17 +32,17 @@ public class DynPageLoadHandler extends AbstractDynPageHandler { | |||
@Override | |||
public R doWork() throws Exception { | |||
dbName = params.readString("dataset"); | |||
filter = (Map<String, Object>)params.get("filter"); | |||
filter = (SwMap)params.get("filter"); | |||
pageDataSet = pageInfo.findDataSet(dbName); | |||
if (pageDataSet == null) throw new SwException("没有找到指定的的数据集定义:" + dbName + "!"); | |||
DynRetBean bean = null; | |||
if (SwEnum.DatasetType.LISTR.value.equals(pageDataSet.type)) {//列表类 | |||
if (SwEnum.DatasetType.LIST.value.equals(pageDataSet.type)) {//列表类 | |||
bean = DynRetBean.createList(loadList()); | |||
} else if (SwEnum.DatasetType.TREE.value.equals(pageDataSet.type)) {//列表类 | |||
// return new DynRetBean(loadTree()); | |||
} else { | |||
} else {//列表类 | |||
bean = DynRetBean.createBean(loadOne()); | |||
} | |||
@@ -53,7 +55,13 @@ public class DynPageLoadHandler extends AbstractDynPageHandler { | |||
*/ | |||
private SwMap loadOne() { | |||
SqlNamedPara sqlPara = DynPageHelper.buildSelectSql(pageDataSet, filter); | |||
return DbEngine.getInstance().queryEntityN(sqlPara.sql, sqlPara.paras, SwMap.class); | |||
SwMap map = DbEngine.getInstance().queryEntityN(sqlPara.sql, sqlPara.paras, SwMap.class); | |||
if (map == null) { | |||
throw new SwException("没有找到指定数据(ds=" + pageDataSet.name + ")"); | |||
} | |||
ModelTable masterTable = ModelTableCache.getInstance().get(pageDataSet.masterTable); | |||
EntityHelper.loadBeanLink(masterTable.getName(), map, sqlPara.mapFieldAlias); | |||
return map; | |||
} | |||
/** | |||
@@ -68,6 +76,27 @@ public class DynPageLoadHandler extends AbstractDynPageHandler { | |||
} else { | |||
list = DbEngine.getInstance().queryN(sqlPara.sql, sqlPara.paras, SwMap.class); | |||
} | |||
ModelTable masterTable = ModelTableCache.getInstance().get(pageDataSet.masterTable); | |||
EntityHelper.loadBeanLink(masterTable.getName(), list, sqlPara.mapFieldAlias); | |||
return SwListData.create(list, sqlPara.rows); | |||
} | |||
/** | |||
* 计算分页数据 | |||
* @return | |||
*/ | |||
public R getTotal() { | |||
try { | |||
SqlNamedPara sqlPara = DynPageHelper.buildSelectSql(pageDataSet, filter); | |||
int total = DbEngine.getInstance().queryIntN("select count(1) from (" + sqlPara.sql + ") xxxa", sqlPara.paras); | |||
R r = R.success(); | |||
r.put("total", total); | |||
// r.put("footer", mapFooter); | |||
return r; | |||
} catch (Exception e) { | |||
return R.error("计算合计失败!", e); | |||
} | |||
} | |||
} |
@@ -0,0 +1,264 @@ | |||
package cc.smtweb.system.bpm.web.engine; | |||
import cc.smtweb.framework.core.R; | |||
import cc.smtweb.framework.core.SwException; | |||
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.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.jdbc.IDbWorker; | |||
import cc.smtweb.framework.core.db.vo.ModelField; | |||
import cc.smtweb.framework.core.db.vo.ModelIndex; | |||
import cc.smtweb.framework.core.db.vo.ModelTable; | |||
import cc.smtweb.framework.core.mvc.service.SwListData; | |||
import cc.smtweb.framework.core.mvc.service.TreeHelper; | |||
import cc.smtweb.framework.core.util.CommUtil; | |||
import cc.smtweb.system.bpm.web.design.form.define.PageDataSet; | |||
import cc.smtweb.system.bpm.web.design.form.define.PageDatasetFilter; | |||
import org.apache.commons.lang3.StringUtils; | |||
import java.util.*; | |||
/** | |||
* Created by Akmm at 2022/4/21 17:53 | |||
* 保存指定数据集操作 | |||
* 入参:{pageId, data:} | |||
*/ | |||
public class DynPageSaveHandler extends AbstractDynPageHandler { | |||
private Map<String, List<DefaultEntity>> mapTreeBean = null; | |||
@Override | |||
public R doWork() throws Exception { | |||
return saveAll(); | |||
} | |||
/** | |||
* 保存指定数据集操作,入参:{pageId,dataset:"", data:{form:{},list: {total:0,rows:[]}}, filter:{}} | |||
*/ | |||
public R saveOne() { | |||
//数据集 | |||
String dbName = params.readString("dataset"); | |||
SwMap filter = (SwMap) params.get("filter"); | |||
//待保存数据 | |||
SwMap data = params.readMap("data"); | |||
if (data == null) throw new SwException("没有收到待保存的的数据:" + dbName + "!"); | |||
//对应的数据集定义 | |||
PageDataSet pageDataSet = pageInfo.findDataSet(dbName); | |||
if (pageDataSet == null) throw new SwException("没有找到指定的的数据集定义:" + dbName + "!"); | |||
//读取待保存的bean | |||
DefaultEntity bean = readBeanFromPage(pageDataSet, data); | |||
if (filter != null && bean.isNew()) {//有过滤条件,将关联的值设上 | |||
setLinkValue(pageDataSet, bean, f -> filter.get(f.name)); | |||
} | |||
checkBean(bean); | |||
DbEngine.getInstance().doTrans(new IDbWorker() { | |||
@Override | |||
public void work() { | |||
saveBean(bean); | |||
} | |||
@Override | |||
public void doAfterDbCommit() { | |||
afterCommit(bean); | |||
} | |||
@Override | |||
public void doAfterDbRollback() { | |||
afterRollback(bean); | |||
} | |||
}); | |||
return R.success(bean); | |||
} | |||
/** | |||
* 保存对象,所有数据集数据对象,{ ds1: {form:{},list:{total:0,rows:[]}}} | |||
* | |||
* @return | |||
*/ | |||
public R saveAll() { | |||
SwMap data = params.readMap("data"); | |||
if (data == null) throw new SwException("没有收到待保存的的数据!"); | |||
SwMap filter = (SwMap) params.get("filter"); | |||
Map<String, DefaultEntity> map = new LinkedHashMap<>(); | |||
for (PageDataSet pageDataSet : pageInfo.db) { | |||
//非表单编辑,不管 | |||
if (!pageDataSet.canEdit || !SwEnum.DatasetType.FORM.value.equals(pageDataSet.type)) continue; | |||
SwMap dsData = (SwMap) data.get(pageDataSet.name); | |||
if (dsData == null) continue; | |||
//读取待保存的bean | |||
DefaultEntity bean = readBeanFromPage(pageDataSet, dsData); | |||
SwMap dsFilter = (SwMap)filter.get(pageDataSet.name); | |||
if (dsFilter != null && bean.isNew()) {//有过滤条件,将关联的值设上 | |||
setLinkValue(pageDataSet, bean, f -> { | |||
//link类型,去取另一个dataset的值 | |||
if (SwEnum.FilterType.LINK.value.equals(f.type)) { | |||
DefaultEntity lv = map.get(f.linkDb); | |||
if (lv != null) return lv.get(f.linkField); | |||
} | |||
return dsFilter.get(f.name); | |||
}); | |||
} | |||
checkBean(bean); | |||
} | |||
DbEngine.getInstance().doTrans(new IDbWorker() { | |||
@Override | |||
public void work() { | |||
for (DefaultEntity bean: map.values()) { | |||
saveBean(bean); | |||
} | |||
} | |||
@Override | |||
public void doAfterDbCommit() { | |||
for (DefaultEntity bean: map.values()) { | |||
afterCommit(bean); | |||
} | |||
} | |||
@Override | |||
public void doAfterDbRollback() { | |||
for (DefaultEntity bean: map.values()) { | |||
afterRollback(bean); | |||
} | |||
} | |||
}); | |||
return R.success(map); | |||
} | |||
/** | |||
* 从页面获取待保存的bean | |||
* | |||
* @param pageDataSet | |||
* @param data | |||
* @return | |||
*/ | |||
protected DefaultEntity readBeanFromPage(PageDataSet pageDataSet, SwMap data) { | |||
ModelTable table = ModelTableCache.getInstance().get(pageDataSet.masterTable); | |||
if (table == null) throw new SwException("没有找到待保存的表定义:" + pageDataSet.name); | |||
long id = data.readLong(table.getIdField()); | |||
EntityDao<DefaultEntity> dao = DbEngine.getInstance().findDao(table.getName()); | |||
DefaultEntity bean; | |||
if (id <= 0) { | |||
bean = dao.createBean(); | |||
bean.setIsNew(true); | |||
bean.setEntityId(DbEngine.getInstance().nextId()); | |||
} else { | |||
bean = dao.queryEntity(id); | |||
if (bean == null) { | |||
throw new SwException("没有找到待保存的记录:" + table.getName() + "." + id); | |||
} | |||
} | |||
//暂时不考虑list保存的情况 | |||
data = (SwMap) data.get("form"); | |||
bean.getData().putAll(data); | |||
return bean; | |||
} | |||
/** | |||
* 保存校验 | |||
* | |||
* @param bean | |||
*/ | |||
protected void checkBean(DefaultEntity bean) { | |||
ModelTable table = ModelTableCache.getInstance().getByName(bean.getTableName()); | |||
for (ModelField field : table.getFields()) { | |||
String value = bean.getStr(field.getName()); | |||
//非空校验 | |||
if (field.isNotNull() && StringUtils.isEmpty(value)) { | |||
throw new SwException("字段不允许为空:" + field.getTitle()); | |||
} | |||
//长度校验 | |||
if (StringUtils.isNotEmpty(value)) { | |||
int len = SwEnum.DataType.instance.getByValue(field.getDataType()).dataLength; | |||
if (len > 0 && CommUtil.getStrLenB(value) > len) { | |||
throw new SwException("字段值超长:" + field.getTitle()); | |||
} | |||
} | |||
} | |||
//唯一键校验 | |||
EntityDao<DefaultEntity> dao = DbEngine.getInstance().findDao(bean.getTableName()); | |||
for (ModelIndex mi : table.getIndexes()) { | |||
if (mi.isUnique()) { | |||
dao.checkUnique(bean, mi.getFields().split(",")); | |||
} | |||
} | |||
} | |||
protected void saveBean(DefaultEntity bean) { | |||
final String tableName = bean.getTableName(); | |||
EntityDao<DefaultEntity> dao = DbEngine.getInstance().findDao(tableName); | |||
if (bean.isNew()) { | |||
dao.insertEntity(bean); | |||
} else { | |||
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) { | |||
List<DefaultEntity> listTreeBean = TreeHelper.getTreeHelper(tableName).resetTreeLevel(bean); | |||
mapTreeBean.put(tableName, listTreeBean); | |||
} | |||
} | |||
} | |||
protected void afterCommit(DefaultEntity bean) { | |||
final String tableName = bean.getTableName(); | |||
ModelTable table = ModelTableCache.getInstance().getByName(tableName); | |||
if (table.isNeedCache()) { | |||
AbstractCache<DefaultEntity> cache = CacheManager.getIntance().getCache(tableName); | |||
List<DefaultEntity> listTreeBean = mapTreeBean.get(tableName); | |||
//树型表,父亲改变了,要多处理下缓存;还有个东东:级次码 | |||
if (listTreeBean != null && !listTreeBean.isEmpty()) { | |||
for (DefaultEntity b : listTreeBean) { | |||
cache.put(b); | |||
} | |||
} else { | |||
cache.put(bean); | |||
} | |||
} | |||
} | |||
protected void afterRollback(DefaultEntity bean) { | |||
final String tableName = bean.getTableName(); | |||
ModelTable table = ModelTableCache.getInstance().getByName(tableName); | |||
if (table.isNeedCache()) { | |||
AbstractCache<DefaultEntity> cache = CacheManager.getIntance().getCache(tableName); | |||
cache.reset(bean); | |||
} | |||
} | |||
//将关联的值设上 | |||
protected void setLinkValue(PageDataSet pageDataSet, DefaultEntity bean, IGetValue iGetValue) { | |||
ModelTable table = ModelTableCache.getInstance().getByName(bean.getTableName()); | |||
for (PageDatasetFilter f : pageDataSet.filters) { | |||
String v = bean.getStr(f.field); | |||
//有值,就不管 | |||
if (v != null && !v.equals(table.findField(f.field).getDefaultValue())) continue; | |||
if (SwEnum.FilterType.CONST.value.equals(f.type)) {//常量 | |||
bean.put(f.field, f.value); | |||
} else if (SwEnum.FilterType.PARAM.value.equals(f.type)) {//参数 | |||
bean.put(f.field, f.value); | |||
} else if (SwEnum.FilterType.LINK.value.equals(f.type)) {//参数 | |||
Object value = iGetValue.getValue(f); | |||
if (value != null) { | |||
bean.put(f.field, value); | |||
} | |||
} | |||
} | |||
} | |||
interface IGetValue { | |||
Object getValue(PageDatasetFilter f); | |||
} | |||
} |
@@ -16,17 +16,22 @@ import cc.smtweb.system.bpm.web.design.table.ModelCatalogTreeHandler; | |||
@SwService | |||
public class DynPageService extends AbstractCompService { | |||
public final static String TYPE_ADD = "add"; | |||
public final static String TYPE_LOAD_ONE = "loadOne"; | |||
public final static String TYPE_DEL_ONE = "delOne"; | |||
@Override | |||
protected IHandler createHandler(String type) { | |||
switch (type) { | |||
case TYPE_ADD: | |||
return new DefaultLoadHandler<ModelCatalog>(ModelCatalog.ENTITY_NAME); | |||
case TYPE_LOAD: | |||
return new DynPageLoadHandler(); | |||
return new DynPageAddHandler(); | |||
case TYPE_LOAD_ONE: | |||
return new DynPageLoadOneHandler(); | |||
case TYPE_SAVE: | |||
return new DefaultSaveHandler<>(ModelCatalog.ENTITY_NAME); | |||
return new DynPageSaveHandler(); | |||
case TYPE_DEL: | |||
return new DefaultDelHandler<>(ModelCatalog.ENTITY_NAME); | |||
case TYPE_DEL_ONE: | |||
return new DefaultDelHandler<>(ModelCatalog.ENTITY_NAME); | |||
case TYPE_LIST: | |||
return new DefaultListHandler<>(ModelCatalog.ENTITY_NAME); | |||
case TYPE_TREE: | |||
@@ -35,8 +40,28 @@ public class DynPageService extends AbstractCompService { | |||
return null; | |||
} | |||
//树,换爹 | |||
//新增 | |||
public R add(@SwBody SwMap params, UserSession us) { | |||
return pageHandler(params, us, TYPE_SAVE); | |||
return pageHandler(params, us, TYPE_ADD); | |||
} | |||
//保存指定数据集 | |||
public R saveOne(@SwBody SwMap params, UserSession us) { | |||
try { | |||
DynPageSaveHandler handler = (DynPageSaveHandler) getHandler(params, us, TYPE_SAVE); | |||
return handler.saveOne(); | |||
} catch (Exception e) { | |||
return R.error("操作失败!", e); | |||
} | |||
} | |||
//列表总记录数及合计栏 | |||
public R total(@SwBody SwMap params, UserSession us) { | |||
try { | |||
DynPageLoadOneHandler handler = (DynPageLoadOneHandler) getHandler(params, us, TYPE_LOAD_ONE); | |||
return handler.getTotal(); | |||
} catch (Exception e) { | |||
return R.error("操作失败!", e); | |||
} | |||
} | |||
} |
@@ -3,8 +3,6 @@ package cc.smtweb.system.bpm.web.engine; | |||
import cc.smtweb.framework.core.SwMap; | |||
import cc.smtweb.framework.core.mvc.service.SwListData; | |||
import java.util.Map; | |||
/** | |||
* Created by Akmm at 2022/4/21 19:26 | |||
* 动态页面加载 | |||