@@ -46,7 +46,7 @@ public class DynPageDelHandler extends AbstractDynPageHandler { | |||
if (table == null) throw new BizException("没有找到指定的的表定义:" + pageDataSet.name + "!"); | |||
EntityDao dao = DbEngine.getInstance().findDao(table.getName()); | |||
dao.deleteEntity(id); | |||
localDel(id); | |||
localDel(id, table); | |||
} | |||
@Override | |||
@@ -56,17 +56,17 @@ public class DynPageDelHandler extends AbstractDynPageHandler { | |||
AbstractCache cache = CacheManager.getIntance().getCache(table.getName()); | |||
cache.remove(id); | |||
} | |||
localDelSuccess(id); | |||
localDelSuccess(id, table); | |||
} | |||
}); | |||
return R.success(); | |||
} | |||
protected void localDel(long id) { | |||
protected void localDel(long id, ModelTable table) { | |||
} | |||
protected void localDelSuccess(long id) { | |||
protected void localDelSuccess(long id, ModelTable table) { | |||
} | |||
@@ -3,12 +3,16 @@ package cc.smtweb.system.bpm.web.sys.user.user; | |||
import cc.smtweb.framework.core.common.SwMap; | |||
import cc.smtweb.framework.core.db.DbEngine; | |||
import cc.smtweb.framework.core.db.impl.DefaultEntity; | |||
import cc.smtweb.framework.core.db.vo.ModelTable; | |||
import cc.smtweb.framework.core.util.PubUtil; | |||
import cc.smtweb.system.bpm.web.design.form.define.PageDataset; | |||
import cc.smtweb.system.bpm.web.engine.dynPage.DynPageDelHandler; | |||
import cc.smtweb.system.bpm.web.engine.dynPage.DynPageListHandler; | |||
import cc.smtweb.system.bpm.web.engine.dynPage.DynPageLoadHandler; | |||
import cc.smtweb.system.bpm.web.engine.dynPage.DynPageSaveHandler; | |||
import java.util.ArrayList; | |||
import java.util.List; | |||
import java.util.Set; | |||
/** | |||
@@ -19,12 +23,25 @@ public class UserHandler { | |||
static class UserSaveHandler extends DynPageSaveHandler { | |||
private UserStatu userStatu; | |||
private List<UserRole> roleList; | |||
@Override | |||
protected DefaultEntity readBeanFromPage(PageDataset pageDataSet, SwMap data) { | |||
if (pageDataSet.name.equals("userPartyCard")) { | |||
String userRoles = data.readString("sup_roles"); | |||
return readBeanFromPage(pageDataSet, data); | |||
roleList = new ArrayList<>(); | |||
Set<Long> userRoles = data.readLongSet("sup_roles"); | |||
DefaultEntity bean = super.readBeanFromPage(pageDataSet, data); | |||
if (PubUtil.isEmpty(userRoles)) { | |||
return bean; | |||
} | |||
userRoles.forEach(k -> { | |||
UserRole userRole = new UserRole(); | |||
userRole.init(); | |||
userRole.setEntityId(DbEngine.getInstance().nextId()); | |||
userRole.setSupId(bean.getEntityId()); | |||
userRole.setRoleId(k); | |||
roleList.add(userRole); | |||
}); | |||
} | |||
return super.readBeanFromPage(pageDataSet, data); | |||
} | |||
@@ -32,13 +49,22 @@ public class UserHandler { | |||
@Override | |||
protected void saveBean(DefaultEntity bean) { | |||
super.saveBean(bean); | |||
userStatu = UserStatuCache.getInstance().get(bean.getEntityId()); | |||
if (userStatu == null || userStatu.isEmpty()) { | |||
userStatu = new UserStatu(); | |||
userStatu.init(); | |||
userStatu.setEntityId(bean.getEntityId()); | |||
DbEngine.getInstance().findDao(UserStatu.ENTITY_NAME).insertEntity(userStatu); | |||
UserStatuCache.getInstance().put(userStatu); | |||
if (bean instanceof User) { | |||
userStatu = UserStatuCache.getInstance().get(bean.getEntityId()); | |||
if (userStatu == null || userStatu.isEmpty()) { | |||
userStatu = new UserStatu(); | |||
userStatu.init(); | |||
userStatu.setEntityId(bean.getEntityId()); | |||
DbEngine.getInstance().findDao(UserStatu.ENTITY_NAME).insertEntity(userStatu); | |||
UserStatuCache.getInstance().put(userStatu); | |||
} | |||
} | |||
if (bean instanceof UserParty) { | |||
Set<Long> ur = UserRoleCache.getInstance().getIdByUser(((UserParty) bean).getUserId()); | |||
DbEngine.getInstance().findDao(UserRole.ENTITY_NAME).deleteEntity(ur); | |||
DbEngine.getInstance().findDao(UserRole.ENTITY_NAME).batchInsertEntity(roleList); | |||
UserRoleCache.getInstance().removeList(ur); | |||
UserRoleCache.getInstance().putList(roleList); | |||
} | |||
} | |||
} | |||
@@ -46,14 +72,30 @@ public class UserHandler { | |||
static class UserDelHandler extends DynPageDelHandler { | |||
@Override | |||
protected void localDel(long id) { | |||
super.localDel(id); | |||
DbEngine.getInstance().findDao(UserStatu.ENTITY_NAME).deleteEntity(id); | |||
protected void localDel(long id, ModelTable table) { | |||
super.localDel(id, table); | |||
if (table.getName().equals(User.ENTITY_NAME)) { | |||
DbEngine.getInstance().findDao(UserStatu.ENTITY_NAME).deleteEntity(id); | |||
DbEngine.getInstance().findDao(UserParty.ENTITY_NAME).deleteEntity(UserPartyCache.getInstance().getIdByUser(id)); | |||
DbEngine.getInstance().findDao(UserRole.ENTITY_NAME).deleteEntity(UserRoleCache.getInstance().getIdByUser(id)); | |||
} | |||
if (table.getName().equals(UserParty.ENTITY_NAME)) { | |||
DbEngine.getInstance().findDao(UserParty.ENTITY_NAME).deleteEntity(id); | |||
DbEngine.getInstance().findDao(UserRole.ENTITY_NAME).deleteEntity(UserRoleCache.getInstance().getByS(id)); | |||
} | |||
} | |||
@Override | |||
protected void localDelSuccess(long id) { | |||
UserStatuCache.getInstance().remove(id); | |||
protected void localDelSuccess(long id, ModelTable table) { | |||
if (table.getName().equals(User.ENTITY_NAME)) { | |||
UserStatuCache.getInstance().remove(id); | |||
UserPartyCache.getInstance().removeList(UserPartyCache.getInstance().getIdByUser(id)); | |||
UserRoleCache.getInstance().removeList(UserRoleCache.getInstance().getIdByUser(id)); | |||
} | |||
if (table.getName().equals(UserParty.ENTITY_NAME)) { | |||
UserPartyCache.getInstance().remove(id); | |||
UserRoleCache.getInstance().removeList(UserRoleCache.getInstance().getByS(id)); | |||
} | |||
} | |||
} | |||
@@ -3,24 +3,41 @@ package cc.smtweb.system.bpm.web.sys.user.user; | |||
import cc.smtweb.framework.core.annotation.SwCache; | |||
import cc.smtweb.framework.core.cache.AbstractEntityCache; | |||
import cc.smtweb.framework.core.cache.CacheManager; | |||
import cc.smtweb.framework.core.util.PubUtil; | |||
import java.util.ArrayList; | |||
import java.util.Comparator; | |||
import java.util.List; | |||
import java.util.HashSet; | |||
import java.util.Set; | |||
/** | |||
* Created by 1 at 2022-07-28 20:05:08 | |||
* Created by 1 at 2022-07-29 09:25:43 | |||
* 实体【[用户机构关系](SYS_USER_PARTY)】的缓存类 | |||
*/ | |||
@SwCache(ident = "SYS_USER_PARTY", title = "用户机构关系") | |||
public class UserPartyCache extends AbstractEntityCache<UserParty> { | |||
//缓存key:按用户缓存 | |||
public final static String mk_u = "u"; | |||
public static UserPartyCache getInstance() { | |||
return CacheManager.getIntance().getCache(UserPartyCache.class); | |||
} | |||
public UserPartyCache() { | |||
//缓存key:按用户缓存 | |||
regList(mk_u, "sup_user_id"); | |||
} | |||
//缓存key:按用户缓存 | |||
public final Set<UserParty> getByU(String key) { | |||
return getListByKey(mk_u, key); | |||
} | |||
public final Set<Long> getIdByUser(Long key) { | |||
Set<Long> ids = new HashSet<>(); | |||
Set<UserParty> set = getByU(String.valueOf(key)); | |||
if (PubUtil.isEmpty(set)) return ids; | |||
set.forEach(bean -> { | |||
ids.add(bean.getEntityId()); | |||
}); | |||
return ids; | |||
} | |||
} |
@@ -0,0 +1,46 @@ | |||
package cc.smtweb.system.bpm.web.sys.user.user; | |||
import cc.smtweb.framework.core.annotation.SwTable; | |||
import cc.smtweb.framework.core.common.SwMap; | |||
import cc.smtweb.framework.core.db.impl.DefaultEntity; | |||
/** | |||
* Created by 1 at 2022-07-29 09:25:43 | |||
* 实体【[用户角色关系](SYS_USER_ROLE)】的Entity类 | |||
*/ | |||
@SwTable("SYS_USER_ROLE") | |||
public class UserRole extends DefaultEntity { | |||
public static final String ENTITY_NAME = "SYS_USER_ROLE"; | |||
public UserRole() { | |||
super(ENTITY_NAME); | |||
} | |||
/** 主键 */ | |||
public long getId() { | |||
return getLong("sur_id"); | |||
} | |||
/** 主键 */ | |||
public void setId(long sur_id) { | |||
put("sur_id", sur_id); | |||
} | |||
/** 机构关系 */ | |||
public long getSupId() { | |||
return getLong("sur_sup_id"); | |||
} | |||
/** 机构关系 */ | |||
public void setSupId(long sur_sup_id) { | |||
put("sur_sup_id", sur_sup_id); | |||
} | |||
/** 角色 */ | |||
public long getRoleId() { | |||
return getLong("sur_role_id"); | |||
} | |||
/** 角色 */ | |||
public void setRoleId(long sur_role_id) { | |||
put("sur_role_id", sur_role_id); | |||
} | |||
} |
@@ -0,0 +1,67 @@ | |||
package cc.smtweb.system.bpm.web.sys.user.user; | |||
import cc.smtweb.framework.core.annotation.SwCache; | |||
import cc.smtweb.framework.core.cache.AbstractEntityCache; | |||
import cc.smtweb.framework.core.cache.CacheManager; | |||
import cc.smtweb.framework.core.util.PubUtil; | |||
import java.util.HashSet; | |||
import java.util.Set; | |||
/** | |||
* Created by 1 at 2022-07-29 09:25:43 | |||
* 实体【[用户角色关系](SYS_USER_ROLE)】的缓存类 | |||
*/ | |||
@SwCache(ident = "SYS_USER_ROLE", title = "用户角色关系") | |||
public class UserRoleCache extends AbstractEntityCache<UserRole> { | |||
//缓存key:按机构关系缓存 | |||
public final static String mk_s = "s"; | |||
public static UserRoleCache getInstance() { | |||
return CacheManager.getIntance().getCache(UserRoleCache.class); | |||
} | |||
public UserRoleCache() { | |||
//缓存key:按机构关系缓存 | |||
regList(mk_s, "sur_sup_id"); | |||
} | |||
//缓存key:按机构关系缓存 | |||
public final Set<UserRole> getByS(String key) { | |||
return getListByKey(mk_s, key); | |||
} | |||
public final Set<Long> getByS(Long key) { | |||
Set<Long> ids = new HashSet<>(); | |||
Set<UserRole> set = getByS(String.valueOf(key)); | |||
if (PubUtil.isEmpty(set)) return ids; | |||
set.forEach(bean -> { | |||
ids.add(bean.getEntityId()); | |||
}); | |||
return ids; | |||
} | |||
public final Set<Long> getIdByUser(Long key) { | |||
return getIdByUser(String.valueOf(key)); | |||
} | |||
public final Set<Long> getIdByUser(String key) { | |||
Set<Long> roleSet = new HashSet<>(); | |||
Set<Long> upSet = UserPartyCache.getInstance().getListIdByKey(UserPartyCache.mk_u, key); | |||
if (PubUtil.isEmpty(upSet)) return roleSet; | |||
upSet.forEach(k -> { | |||
Set<Long> urSet = getListIdByKey(mk_s, String.valueOf(k)); | |||
if (PubUtil.isEmpty(urSet)) return; | |||
roleSet.addAll(upSet); | |||
}); | |||
return roleSet; | |||
} | |||
public final Set<UserRole> getSetByUser(String key) { | |||
Set<UserRole> set = new HashSet<>(); | |||
getIdByUser(key).forEach(k -> { | |||
set.add(get(k)); | |||
}); | |||
return set; | |||
} | |||
} |
@@ -5,6 +5,7 @@ import cc.smtweb.framework.core.cache.redis.RedisBroadcastEvent; | |||
import cc.smtweb.framework.core.cache.redis.RedisManager; | |||
import cc.smtweb.framework.core.common.SwConsts; | |||
import cc.smtweb.framework.core.util.CommUtil; | |||
import cc.smtweb.framework.core.util.PubUtil; | |||
import com.github.benmanes.caffeine.cache.Caffeine; | |||
import com.github.benmanes.caffeine.cache.LoadingCache; | |||
import com.github.benmanes.caffeine.cache.Scheduler; | |||
@@ -260,7 +261,7 @@ public abstract class AbstractCache<T extends Serializable> implements ISwCache< | |||
for (Map.Entry<String, IGetBeanKeys<T>> entry : mapListRegEx.entrySet()) { | |||
String[] keys = entry.getValue().getKey(oldbean); | |||
for (String k: keys) { | |||
for (String k : keys) { | |||
doRemoveList(entry.getKey(), k, bean); | |||
} | |||
} | |||
@@ -287,7 +288,7 @@ public abstract class AbstractCache<T extends Serializable> implements ISwCache< | |||
for (Map.Entry<String, IGetBeanKeys<T>> entry : mapListRegEx.entrySet()) { | |||
String[] keys = entry.getValue().getKey(value); | |||
for (String k: keys) { | |||
for (String k : keys) { | |||
doUpdateList(entry.getKey(), k, value); | |||
} | |||
} | |||
@@ -345,6 +346,12 @@ public abstract class AbstractCache<T extends Serializable> implements ISwCache< | |||
} | |||
} | |||
public final void putList(Collection<T> list) { | |||
list.forEach(bean -> { | |||
put(bean); | |||
}); | |||
} | |||
/** | |||
* 将对象重置为修改前的值,用于取出修改后,事务提交失败 | |||
* | |||
@@ -445,6 +452,13 @@ public abstract class AbstractCache<T extends Serializable> implements ISwCache< | |||
publishRemove(key); | |||
} | |||
public void removeList(Collection<Long> key) { | |||
if (PubUtil.isEmpty(key)) return; | |||
key.forEach(k -> { | |||
remove(k); | |||
}); | |||
} | |||
public void remove(long key) { | |||
remove(String.valueOf(key)); | |||
} | |||
@@ -4,8 +4,11 @@ import cc.smtweb.framework.core.common.SwConsts; | |||
import cc.smtweb.framework.core.db.DbEngine; | |||
import cc.smtweb.framework.core.db.EntityDao; | |||
import cc.smtweb.framework.core.db.impl.DefaultEntity; | |||
import cc.smtweb.framework.core.util.PubUtil; | |||
import java.util.HashSet; | |||
import java.util.List; | |||
import java.util.Set; | |||
/** | |||
* Created by Akmm at 2022/6/16 15:53 | |||
@@ -18,11 +21,11 @@ public class AbstractEntityCache<T extends DefaultEntity> extends AbstractCache< | |||
private String getCacheKey(T bean, String fields) { | |||
String[] fs = fields.split(","); | |||
String ret = ""; | |||
for (String f: fs) { | |||
ret += SwConsts.SPLIT_CHAR + bean.getStr(f); | |||
} | |||
return ret.substring(SwConsts.SPLIT_CHAR.length()); | |||
String ret = ""; | |||
for (String f : fs) { | |||
ret += SwConsts.SPLIT_CHAR + bean.getStr(f); | |||
} | |||
return ret.substring(SwConsts.SPLIT_CHAR.length()); | |||
} | |||
/** | |||
@@ -51,4 +54,15 @@ public class AbstractEntityCache<T extends DefaultEntity> extends AbstractCache< | |||
EntityDao<T> dao = DbEngine.getInstance().findDao(pTypeClass); | |||
return dao.query(); | |||
} | |||
public final Set<Long> getListIdByKey(String rk, String key) { | |||
Set<Long> idSet = new HashSet<>(); | |||
Set<T> beanSet = getListByKey(rk, key); | |||
if (PubUtil.isEmpty(beanSet)) return idSet; | |||
beanSet.forEach(bean -> { | |||
idSet.add(bean.getEntityId()); | |||
}); | |||
return idSet; | |||
} | |||
} |
@@ -1,14 +1,14 @@ | |||
package cc.smtweb.framework.core.db; | |||
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.db.dao.AbstractEntityDao; | |||
import cc.smtweb.framework.core.db.dao.EntityColumn; | |||
import cc.smtweb.framework.core.db.impl.DefaultEntity; | |||
import cc.smtweb.framework.core.db.jdbc.JdbcEngine; | |||
import cc.smtweb.framework.core.db.vo.ModelField; | |||
import cc.smtweb.framework.core.exception.BizException; | |||
import cc.smtweb.framework.core.util.CommUtil; | |||
import cc.smtweb.framework.core.util.PubUtil; | |||
import lombok.Getter; | |||
import org.apache.commons.lang3.StringUtils; | |||
@@ -68,11 +68,11 @@ public class EntityDao<T> extends AbstractEntityDao<T> { | |||
return updateEntity(entity, null, null); | |||
} | |||
Set<String> excludeFields = new HashSet<>(exfields.length); | |||
for (String f: exfields) { | |||
for (String f : exfields) { | |||
excludeFields.add(f.toLowerCase()); | |||
} | |||
StringBuilder fields = new StringBuilder(512); | |||
for (ModelField field: modelTable.getFields()) { | |||
for (ModelField field : modelTable.getFields()) { | |||
if (!excludeFields.contains(field.getName())) { | |||
fields.append(",").append(field.getName()); | |||
} | |||
@@ -240,7 +240,8 @@ public class EntityDao<T> extends AbstractEntityDao<T> { | |||
return jdbcEngine.update(sb.toString(), params); | |||
} | |||
public int deleteEntity(List<Long> ids) { | |||
public int deleteEntity(Collection<Long> ids) { | |||
if (PubUtil.isEmpty(ids)) return 0; | |||
return deleteEntity(" where " + modelTable.getIdField() + " in (" + CommUtil.getSqlInIds(ids) + ")"); | |||
} | |||
@@ -252,7 +253,7 @@ public class EntityDao<T> extends AbstractEntityDao<T> { | |||
private void setTableName(List<T> list) { | |||
if (!list.isEmpty() && list.get(0) instanceof DefaultEntity) { | |||
for (T bean: list) { | |||
for (T bean : list) { | |||
((DefaultEntity) bean).setTableName(this.tableName); | |||
} | |||
} | |||
@@ -347,6 +348,7 @@ public class EntityDao<T> extends AbstractEntityDao<T> { | |||
/** | |||
* 检查表字段是否违反唯一索引 | |||
* | |||
* @param bean | |||
* @param fields | |||
*/ | |||