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

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

代码如下:

java 复制代码
/*
 * 文件名称: 类UnifyBatchUpdateEndpoint
 * 文件描述:
 * 创建人: simple_zeng
 * 创建时间: 2024/6/8
 */
@RestController
public class UnifyBatchUpdateEndpoint implements InterestedClassAware {

    @Autowired
    private SqlScriptExecutor sqlScriptExecutor;

    // 实体类名对应tableId
    private 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());
    }

    @Override
    public 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;
    }
}

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

java 复制代码
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"
  ]
}
相关推荐
笨鸟先飞,勤能补拙1 小时前
AI 赋能网络安全:技术全景、成熟度评估与实战案例
人工智能·python·安全·web安全·网络安全·sqlite·github
微学AI2 小时前
一根针指向所有方向:挂谷猜想对 LLM Agent 技能-记忆架构的启示
开发语言·人工智能·架构·挂谷猜想
长和信泰光伏储能2 小时前
京津冀光伏发电:绿色能源的未来之路
python·能源
豆瓣鸡3 小时前
算法日记 - Day3
java·开发语言·算法
浦信仿真大讲堂3 小时前
从重复操作到自动化闭环:如何让 CST 与 Python 真正协同起来
python·自动化·cst·仿真软件·达索软件
韭菜炒鸡肝天3 小时前
VTK开发笔记(一):VTK介绍,Qt..+VSx+VTK.编译
开发语言·笔记·qt
Gu Gu Study3 小时前
ScoutLoop开放域深度研究引擎(agent的初步设计想法)
人工智能·python
Aaron - Wistron4 小时前
Web API C# (Furion版)带 单元测试
开发语言·后端·c#
卷无止境4 小时前
写代码这件事,到底该讲究点什么?
后端·python
卷无止境4 小时前
循环复杂度到底在算什么,Python 代码怎么才能写得让人一看就懂
后端·python