增加多选删除的服务端接口

This commit is contained in:
keran 2019-07-09 11:15:09 +08:00
parent e508d71c70
commit 6f8bd9b1b1
2 changed files with 109 additions and 0 deletions

View File

@ -37,6 +37,7 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@ -228,6 +229,32 @@ public class ConfigController {
return true;
}
/**
* @author klw
* @Description: delete configuration based on multiple config ids
* @Date 2019/7/5 10:26
* @Param [request, response, dataId, group, tenant, tag]
* @return java.lang.Boolean
*/
@RequestMapping(method = RequestMethod.DELETE)
@ResponseBody
public RestResult<Boolean> deleteConfigs(HttpServletRequest request, HttpServletResponse response,
@RequestParam(value = "ids")List<Long> ids) {
String clientIp = RequestUtil.getRemoteIp(request);
final Timestamp time = TimeUtils.getCurrentTime();
List<ConfigInfo> configInfoList = persistService.removeConfigInfoByIds(ids, clientIp, null);
if(!CollectionUtils.isEmpty(configInfoList)){
for(ConfigInfo configInfo : configInfoList) {
ConfigTraceService.logPersistenceEvent(configInfo.getDataId(), configInfo.getGroup(),
configInfo.getTenant(), null, time.getTime(), clientIp,
ConfigTraceService.PERSISTENCE_EVENT_REMOVE, null);
EventDispatcher.fireEvent(new ConfigDataChangeEvent(false, configInfo.getDataId(),
configInfo.getGroup(), configInfo.getTenant(), time.getTime()));
}
}
return ResultBuilder.buildSuccessResult(true);
}
@RequestMapping(value = "/catalog", method = RequestMethod.GET)
@ResponseBody
public RestResult<ConfigAdvanceInfo> getConfigAdvanceInfo(HttpServletRequest request, HttpServletResponse response,

View File

@ -23,6 +23,7 @@ import com.alibaba.nacos.config.server.utils.MD5;
import com.alibaba.nacos.config.server.utils.PaginationHelper;
import com.alibaba.nacos.config.server.utils.ParamUtils;
import com.alibaba.nacos.config.server.utils.event.EventDispatcher;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
@ -75,6 +76,10 @@ public class PersistService {
private static final String SQL_TENANT_INFO_COUNT_BY_TENANT_ID = "select count(1) from tenant_info where tenant_id = ?";
private static final String SQL_FIND_CONFIG_INFO_BY_IDS = "SELECT ID,data_id,group_id,tenant_id,app_name,content,md5 FROM config_info WHERE id in (?)";
private static final String SQL_DELETE_CONFIG_INFO_BY_IDS = "DELETE FROM config_info WHERE id in (?)";
/**
* @author klw
* @Description: constant variables
@ -746,6 +751,42 @@ public class PersistService {
});
}
/**
* @author klw
* @Description: delete config info by ids
* @Date 2019/7/5 16:45
* @Param [ids, srcIp, srcUser]
* @return List<ConfigInfo> deleted configInfos
*/
public List<ConfigInfo> removeConfigInfoByIds(final List<Long> ids, final String srcIp, final String srcUser) {
if(CollectionUtils.isEmpty(ids)){
return null;
}
ids.removeAll(Collections.singleton(null));
return tjt.execute(new TransactionCallback<List<ConfigInfo>>() {
final Timestamp time = new Timestamp(System.currentTimeMillis());
@Override
public List<ConfigInfo> doInTransaction(TransactionStatus status) {
try {
String idsStr = Joiner.on(",").join(ids);
List<ConfigInfo> configInfoList = findConfigInfosByIds(idsStr);
if (!CollectionUtils.isEmpty(configInfoList)) {
removeConfigInfoByIdsAtomic(idsStr);
for(ConfigInfo configInfo : configInfoList){
removeTagByIdAtomic(configInfo.getId());
insertConfigHistoryAtomic(configInfo.getId(), configInfo, srcIp, srcUser, time, "D");
}
}
return configInfoList;
} catch (CannotGetJdbcConnectionException e) {
fatalLog.error("[db-error] " + e.toString(), e);
throw e;
}
}
});
}
/**
* 删除beta配置信息, 物理删除
*/
@ -2787,6 +2828,25 @@ public class PersistService {
}
}
/**
* @author klw
* @Description: Delete configuration; database atomic operation, minimum SQL action, no business encapsulation
* @Date 2019/7/5 16:39
* @Param [id]
* @return void
*/
private void removeConfigInfoByIdsAtomic(final String ids) {
if(StringUtils.isBlank(ids)){
return;
}
try {
jt.update(SQL_DELETE_CONFIG_INFO_BY_IDS, ids);
} catch (CannotGetJdbcConnectionException e) {
fatalLog.error("[db-error] " + e.toString(), e);
throw e;
}
}
/**
* 删除配置数据库原子操作最小sql动作无业务封装
*
@ -2865,6 +2925,28 @@ public class PersistService {
}
}
/**
* @author klw
* @Description: find ConfigInfo by ids
* @Date 2019/7/5 16:37
* @Param [ids]
* @return java.util.List<com.alibaba.nacos.config.server.model.ConfigInfo>
*/
public List<ConfigInfo> findConfigInfosByIds(final String ids) {
if(StringUtils.isBlank(ids)){
return null;
}
try {
return this.jt.query(SQL_FIND_CONFIG_INFO_BY_IDS,
new Object[] {ids}, CONFIG_INFO_ROW_MAPPER);
} catch (EmptyResultDataAccessException e) { // 表明数据不存在, 返回null
return null;
} catch (CannotGetJdbcConnectionException e) {
fatalLog.error("[db-error] " + e.toString(), e);
throw e;
}
}
/**
* 查询配置信息数据库原子操作最小sql动作无业务封装
*