diff --git a/src/main/java/neatlogic/module/tenant/api/util/ToggleTableGzipContentApi.java b/src/main/java/neatlogic/module/tenant/api/util/ToggleTableGzipContentApi.java new file mode 100644 index 0000000000000000000000000000000000000000..23d23124dc6238c305b4dfe0e98cb59aab6c0dc8 --- /dev/null +++ b/src/main/java/neatlogic/module/tenant/api/util/ToggleTableGzipContentApi.java @@ -0,0 +1,127 @@ +/* + * Copyright (C) 2025 TechSure Co., Ltd. All Rights Reserved. + * This file is part of the NeatLogic software. + * Licensed under the NeatLogic Sustainable Use License (NSUL), Version 4.x – 2025. + * You may use this file only in compliance with the License. + * See the LICENSE file distributed with this work for the full license text. + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + */ + +package neatlogic.module.tenant.api.util; + +import com.alibaba.fastjson.JSONObject; +import neatlogic.framework.auth.core.AuthAction; +import neatlogic.framework.auth.label.ADMIN; +import neatlogic.framework.common.constvalue.ApiParamType; +import neatlogic.framework.dao.plugin.CompressHandler; +import neatlogic.framework.restful.annotation.*; +import neatlogic.framework.restful.constvalue.OperationTypeEnum; +import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; +import neatlogic.framework.util.GzipUtil; +import neatlogic.module.tenant.dao.mapper.TestMapper; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.collections4.MapUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +@Service +@AuthAction(action = ADMIN.class) +@OperationType(type = OperationTypeEnum.UPDATE) +public class ToggleTableGzipContentApi extends PrivateApiComponentBase { + + @Resource + private TestMapper testMapper; + + @Override + public String getName() { + return "转换表压缩内容"; + } + + @Input({ + @Param(name = "tableName", type = ApiParamType.STRING, isRequired = true, desc = "表名"), + @Param(name = "columnName", type = ApiParamType.STRING, isRequired = true, desc = "字段名"), + @Param(name = "action", type = ApiParamType.ENUM, rule = "compress,uncompress", isRequired = true, desc = "操作,压缩或解压") + }) + @Output({ + @Param(name = "updateCount", type = ApiParamType.INTEGER, isRequired = true, desc = "影响行数"), + }) + @Description(desc = "转换表压缩内容") + @Override + public Object myDoService(JSONObject paramObj) throws Exception { + String tableName = paramObj.getString("tableName"); + String columnName = paramObj.getString("columnName"); + String action = paramObj.getString("action"); + JSONObject resultObj = new JSONObject(); + // 先查询表结构,检查字段是否存在,压缩时字段是否是text、mediumtext、longtext类型 + Object fieldType = null; + List> tableStructureList = testMapper.getDatabaseTableStructure(tableName); + for (Map map : tableStructureList) { + Object field = map.get("Field"); + if (Objects.equals(field, columnName)) { + fieldType = map.get("Type"); + } + } + if (fieldType == null) { + resultObj.put("message", "数据库表`" + tableName + "`没有`" + columnName + "`字段"); + return resultObj; + } + CompressHandler compressHandler = null; + if (Objects.equals(action, "compress")) { + List list = Arrays.asList("text", "mediumtext", "longtext"); + if (!list.contains(fieldType.toString())) { + resultObj.put("message", "数据库表`" + tableName + "` 字段`" + columnName + "`是" +fieldType + "类型" + "不能压缩"); + return resultObj; + } + compressHandler = new CompressHandler(); + } + int rowNum = testMapper.getGzipContentCountByTableNameAndColumnName(tableName, columnName, action); + String prefix = "GZIP:"; + int updateCount = 0; + while (updateCount < rowNum) { + List> gzipContentList = testMapper.getGzipContentListByTableNameAndColumnName(tableName, columnName, action); + if (CollectionUtils.isNotEmpty(gzipContentList)) { + for (Map map : gzipContentList) { + if (MapUtils.isNotEmpty(map)) { + String content = map.get("content"); + if (StringUtils.isNotBlank(content)) { + if (Objects.equals(action, "uncompress")) { + if (content.startsWith(prefix)) { + String newContent = GzipUtil.uncompress(content.substring(prefix.length())); + testMapper.updateGzipContentByTableNameAndColumnName(tableName, columnName, content, newContent); + updateCount++; + } + } else if (Objects.equals(action, "compress")) { + if (!content.startsWith(prefix) && compressHandler != null) { + String newContent = compressHandler.handleParameter(content); + testMapper.updateGzipContentByTableNameAndColumnName(tableName, columnName, content, newContent); + updateCount++; + } + } + } + } + } + } else { + break; + } + } + resultObj.put("updateCount", updateCount); + return resultObj; + } + + @Override + public String getToken() { + return "/util/table/togglegzipcontent"; + } + + @Override + public int needAudit() { + return 1; + } +} diff --git a/src/main/java/neatlogic/module/tenant/dao/mapper/TestMapper.java b/src/main/java/neatlogic/module/tenant/dao/mapper/TestMapper.java index db9e03ee363bbd3cd59429e696394f69baf000ef..a807439ee045cd866b654100802a99ac7d29d0df 100644 --- a/src/main/java/neatlogic/module/tenant/dao/mapper/TestMapper.java +++ b/src/main/java/neatlogic/module/tenant/dao/mapper/TestMapper.java @@ -12,6 +12,8 @@ package neatlogic.module.tenant.dao.mapper; +import org.apache.ibatis.annotations.Param; + import java.util.HashMap; import java.util.List; import java.util.Map; @@ -24,4 +26,12 @@ public interface TestMapper { void insertContent(String content); Map getProcessTaskByIdForUpdate(Long id); + + List> getDatabaseTableStructure(@Param("tableName") String tableName); + + int getGzipContentCountByTableNameAndColumnName(@Param("tableName") String tableName, @Param("columnName") String columnName, @Param("action") String action); + + List> getGzipContentListByTableNameAndColumnName(@Param("tableName") String tableName, @Param("columnName") String columnName, @Param("action") String action); + + int updateGzipContentByTableNameAndColumnName(@Param("tableName") String tableName, @Param("columnName") String columnName, @Param("oldContent") String oldContent, @Param("newContent") String newContent); } diff --git a/src/main/java/neatlogic/module/tenant/dao/mapper/TestMapper.xml b/src/main/java/neatlogic/module/tenant/dao/mapper/TestMapper.xml index c94b89e355d2aab81a1297fb3f85050928077502..3886a3f44524a60a0586cac5dfb2bf75f44b9703 100644 --- a/src/main/java/neatlogic/module/tenant/dao/mapper/TestMapper.xml +++ b/src/main/java/neatlogic/module/tenant/dao/mapper/TestMapper.xml @@ -23,5 +23,34 @@ + + + + + + + + + + + + + UPDATE `${tableName}` SET `${columnName}` = #{newContent} WHERE `${columnName}` = #{oldContent} +