一个公用的数据状态修改组件

ops/2024/10/22 13:35:04/

灵感来自于一项重复的工作,下图中,这类禁用启用、审核通过不通过、设计成是什么状态否什么状态的场景很多。每一个都需要单独提供接口。重复工作还蛮大的。于是,基于该组件类捕获组件跳转写了这款通用接口。省时省力。
在这里插入图片描述
代码如下:

/** 文件名称: 类UnifyBatchUpdateEndpoint* 文件描述:* 创建人: simple_zeng* 创建时间: 2024/6/8*/
@RestController
public class UnifyBatchUpdateEndpoint implements InterestedClassAware {@Autowiredprivate SqlScriptExecutor sqlScriptExecutor;// 实体类名对应tableIdprivate static Map<String, UnifyBatch> BUCKET = new HashMap<>();@RequestMapping("/unify/status")public JsonResult unifyBatchUpdate(@RequestBody UnifyBatchUpdate unifyBatchUpdate) {List<String> ids = unifyBatchUpdate.getIds();if (ZYListUtils.isEmptyList(ids)) {throw new LocalException("请至少选择一条数据");}Integer status = unifyBatchUpdate.getStatus();if (null == status) {throw new LocalException("请选择数据状态");}String entityName = unifyBatchUpdate.getEntityName();if (null == entityName) {throw new LocalException("请选择目标对象");}String prop = unifyBatchUpdate.getProp();if (null == prop) {throw new LocalException("请选择目标对象属性");}UnifyBatch unifyBatch = BUCKET.get(entityName.toLowerCase());if (null == unifyBatch) {throw new LocalException("不能识别的状态类型");}String tableName = unifyBatch.getTableName(); // 表名String keyColumnName = unifyBatch.getKeyColumnName(); // 主键字段名String column = unifyBatch.getColumn(prop); // 状态字段名String idInWhere = ZYWrapperHelper.toIn(ids); // id条件String sqlTemplate = "update %s set %s=%s where %s in %s";  // update sys_user set is_using=1 where id in ('1')String sql = String.format(sqlTemplate, tableName, column, status, keyColumnName, idInWhere);sqlScriptExecutor.executeUpdateScript(sql);return JsonResult.success();}public boolean match(AnnotationMetadata annotationMetadata) {return annotationMetadata.hasAnnotation(TableName.class.getName());}@Overridepublic void setClasses(Set<Class<?>> classes) {for (Class<?> aClass : classes) {TableName tableName = aClass.getAnnotation(TableName.class);if (null == tableName) {continue;}String simpleName = aClass.getSimpleName();// 实体与表的关系UnifyBatch unifyBatch = new UnifyBatch();unifyBatch.setTableName(tableName.value());Field[] fields = ZYReflectUtils.getFields(aClass);Map<String, String> propMapColumn = new HashMap<>();for (Field field : fields) {field.setAccessible(true);Class<?> type = field.getType();// 主键TableId tableId = field.getAnnotation(TableId.class);if (null != tableId) {unifyBatch.setKeyColumnName(tableId.value());}// 只处理int类型的状态值修改if (!Integer.class.isAssignableFrom(type)) {continue;}// 字段TableField tableField = field.getAnnotation(TableField.class);if (null != tableField) {propMapColumn.put(field.getName().toLowerCase(), tableField.value());}}unifyBatch.setPropMapColumn(propMapColumn);if (!unifyBatch.empty()) {BUCKET.put(simpleName.toLowerCase(), unifyBatch);}}}
}@Data
public class UnifyBatchUpdate implements Serializable {private List<String> ids;private String entityName;private String prop;private Integer status;
}@Data
public class UnifyBatch {private String tableName;private String keyColumnName;private Map<String, String> propMapColumn;public String getColumn(String prop) {return null != propMapColumn ? propMapColumn.get(prop.toLowerCase()) : null;}public boolean empty() {if (ZYStrUtils.isNull(tableName)) {return true;}if (ZYStrUtils.isNull(keyColumnName)) {return true;}if (null == propMapColumn || propMapColumn.isEmpty()) {return true;}return false;}
}

调用示例,后端不用写任何代码,只需要告诉前端调哪个实体类和某个属性即可。岂不美哉。

POST http://localhost:{{port}}/unify/status
Content-Type: application/json
Authorization: {{auth_token}}
u-login-areaId: {{areaId}}{"entityName": "User","prop": "isUsing","status": 1,"ids": ["1","1790218983664807938","1790219261998821377"]
}

http://www.ppmy.cn/ops/47975.html

相关文章

psql导入数据报错排查

问题&#xff1a;采用pg_dump导出表数据后&#xff0c;用psql导入表数据&#xff0c;导入时报错 无效的命令 \N定位该问题的方法 --进入psql \set ON_ERROR_STOP on --退出psqlpsql -U postgres -d test -v ON_ERROR_STOPon < /home/postgres/test.dmp参考文章&#xff1a…

IDEA使用阿里通义灵码插件

在这个AI火热的时代&#xff0c;纯手工写代码已经有点out了&#xff0c;使用AI插件可以帮我们快速写代码&#xff0c;起码能省去写那些简单、重复性的代码&#xff0c;大大提高编码效率&#xff0c;在这里我推荐使用阿里的通义灵码 注册安装 安装注册好后&#xff0c;打开我们…

javaWeb项目-在线考试系统详细功能介绍

项目关键技术 开发工具&#xff1a;IDEA 、Eclipse 编程语言: Java 数据库: MySQL5.7 框架&#xff1a;ssm、Springboot 前端&#xff1a;Vue、ElementUI 关键技术&#xff1a;springboot、SSM、vue、MYSQL、MAVEN 数据库工具&#xff1a;Navicat、SQLyog 1、Java简介 Java语…

『 Linux 』内存管理与文件系统

文章目录 交换分区页与页框(页帧)交换分区与内存之间的交换操作系统如何管理内存物理地址转换页号与页内偏移量 内存管理,文件系统与文件管理之间的联系 交换分区 在Linux的安装过程中,用户将会被提示创建一个交换分区; 这是一个特殊的分区,其大小可以由用户根据系统内存需求和…

什么是SpringMVC

StringMvc简介 Spring web mvc和Struts2都属于表现层的框架,它是Spring框架的一部分,我们可以从Spring的整体结构中看得出来&#xff1a;

vue面试题十二

一、请解释Vue 3中的Composition API与Options API的区别&#xff0c;并说明个人倾向使用哪种方式&#xff0c;为什么&#xff1f; Vue 3中的Composition API与Options API的区别主要体现在以下几个方面&#xff1a; 代码组织和复用性&#xff1a; Composition API&#xff1a…

JavaWeb基础(JQuery,XML及解析)

这个阶段有点拖沓了&#xff0c;因为事情比较多&#xff0c;耽搁了一段时间&#xff0c;学习的主要内容为JQuery和XML&#xff0c;因为vue的出现&#xff0c;JQuery技术现在已经不流行了&#xff0c;但是不流行不代表我不会&#xff0c;JQuery最最最最核心的就是他的$()核心函数…

基于vue3实现倒计时60s的

短信倒计时60s 使用vue3的computed计算属性 <n-button type"primary" :disabled"btnDisabled" click"handleClick">{{Countdown}}</n-button><n-input v-model:value"model.inputSign" placeholder"请输入验证码…