修改删除tag表逻辑(等级三级且time大于0)

master
bynt 3 years ago
parent f240da36f1
commit 95e1be5f4c

@ -112,11 +112,12 @@ public interface BuildRecordRepository extends JpaRepository<BuildRecord, Intege
/**
*
* @param result
* @param id
*/
@Modifying
@Query(value = "update BuildRecord set isDeleted = 1 where id = ?1")
void updateIsDeleteByBuildId(Integer id);
@Query(value = "update BuildRecord set isDeleted = ?1 where id = ?2")
void updateIsDeleteByBuildId(boolean result, Integer id);
/**
@ -126,6 +127,15 @@ public interface BuildRecordRepository extends JpaRepository<BuildRecord, Intege
* @return
*
*/
@Query(value = "select b from BuildRecord b where b.gmtCreate < ?1 and b.level = ?2 and b.isDeleted = false order by b.id ")
@Query(value = "select b from BuildRecord b where b.gmtCreate < ?1 and b.level = ?2 and b.isDeleted = false order by b.id")
List<BuildRecord> getBuildRecordByTime(DateTime oneMonthAhead, int threeNumber);
/**
* time
* @param threeNumber
* @param flag
* @return
*/
@Query(value = "select b from BuildRecord b where b.level = ?1 and b.isDeleted = ?2 and b.timePeriod > 0 order by b.id")
List<BuildRecord> getBuildRecordByIsDelete(int threeNumber, Boolean flag);
}

@ -136,7 +136,16 @@ public interface BuildRecordService {
/**
*
* @param result
* @param id
*/
void updateIsDeleteByBuildId(Integer id);
void updateIsDeleteByBuildId(boolean result, Integer id);
/**
*
* @param threeNumber
* @param flag
* @return
*/
List<BuildRecord> getBuildRecordByIsDelete(int threeNumber, Boolean flag);
}

@ -39,5 +39,8 @@ public class BuildRecordQueryCriteria{
@Query(type = Query.Type.EQUAL)
private Boolean flag;
@Query(type = Query.Type.EQUAL)
private Boolean isDeleted;
}

@ -69,6 +69,7 @@ public class BuildRecordServiceImpl implements BuildRecordService {
if (criteria.getFlag() != null && !criteria.getFlag()) {
criteria.setParentId(null);
criteria.setLevel(DefaultConstant.THREE_NUMBER);
criteria.setIsDeleted(Boolean.FALSE);
}
// 无实体对应 赋初始值
criteria.setFlag(null);
@ -101,9 +102,14 @@ public class BuildRecordServiceImpl implements BuildRecordService {
@Override
@Transactional(rollbackFor = Exception.class)
public BuildRecordDto create(BuildRecord resources) {
// 修改时间周期
resources.setTimePeriod(resources.getTimePeriod() != null ?
resources.getTimePeriod() : DefaultConstant.ONE_THOUSAND);
if (resources.getLevel() != null &&
resources.getLevel().equals(DefaultConstant.ONE_NUMBER)) {
resources.setParentId(DefaultConstant.ZERO_NUMBER);
resources.setIsDeleted(Boolean.FALSE);
}
return buildRecordMapper.toDto(buildRecordRepository.save(resources));
}
@ -197,8 +203,13 @@ public class BuildRecordServiceImpl implements BuildRecordService {
@Override
@Transactional(rollbackFor = Exception.class)
public void updateIsDeleteByBuildId(Integer id) {
buildRecordRepository.updateIsDeleteByBuildId(id);
public void updateIsDeleteByBuildId(boolean result, Integer id) {
buildRecordRepository.updateIsDeleteByBuildId(result,id);
}
@Override
public List<BuildRecord> getBuildRecordByIsDelete(int threeNumber, Boolean flag) {
return buildRecordRepository.getBuildRecordByIsDelete(threeNumber,flag);
}
/**

@ -48,5 +48,11 @@ public class DefaultConstant {
*/
public static final int TEN_NUMBER = 10;
/**
* 10
*/
public static final int ONE_THOUSAND = 1000;
}

@ -95,7 +95,6 @@ public class BuildRecordScheduled {
@Scheduled(cron = "0 0 4 * * ?")
// @Scheduled(cron = "0 0/1 * * * ?")
public void resendBuildRecordTask() {
log.info("scheduled resendBuildRecordTask start time [{}]", LocalTime.now());
// 偏移天数
@ -176,24 +175,32 @@ public class BuildRecordScheduled {
/**
*
*/
@Scheduled(cron = "0 0 1 * * ?")
// @Scheduled(cron = "0 0 1 * * ?")
@Scheduled(cron = "0 0/1 * * * ?")
public void deleteBuildRecordTask() {
// 偏移一个月
DateTime oneMonthAhead = DateUtil.offsetMonth(DateUtil.date(), -1);
List<BuildRecord> buildRecordList = buildRecordService.getBuildRecordByTime(oneMonthAhead,DefaultConstant.THREE_NUMBER);
if (!CollectionUtils.isEmpty(buildRecordList)) {
for (BuildRecord buildRecord : buildRecordList) {
if (buildRecord.getId() != null) {
List<BuildRecord> buildRecordByIsDelete =
buildRecordService.getBuildRecordByIsDelete
(DefaultConstant.THREE_NUMBER, Boolean.FALSE);
if (!CollectionUtils.isEmpty(buildRecordByIsDelete)) {
for (BuildRecord buildRecord : buildRecordByIsDelete) {
// 查询已做非空判断
buildRecord.setTimePeriod(buildRecord.getTimePeriod() != null ?
buildRecord.getTimePeriod() - DefaultConstant.ONE_NUMBER : 0);
if (buildRecord.getId() != null &&
buildRecord.getTimePeriod().equals(DefaultConstant.ZERO_NUMBER)) {
boolean result = tagService.deleteByTaskId(buildRecord.getId());
if (result) {
// 修改状态
buildRecordService.updateIsDeleteByBuildId(buildRecord.getId());
if (!result) {
// 删除失败周期重试
buildRecord.setTimePeriod
(buildRecord.getTimePeriod() + DefaultConstant.ONE_NUMBER);
}
buildRecord.setIsDeleted(result);
log.info("deleteBuildRecordTask buildRecord as [{}] result as [{}]", buildRecord.getTaskName(), result);
}
buildRecordService.update(buildRecord);
}
}
}
}

@ -21,6 +21,8 @@ import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import me.zhengjie.modules.buildrecord.util.WordFilter;
import org.apache.commons.lang3.StringUtils;
import net.lingala.zip4j.core.ZipFile;
@ -224,7 +226,10 @@ public class ZipUtils {
}
public static void main(String[] args) {
zip("C:\\Users\\Administrator\\Desktop\\123\\", "C:\\Users\\Administrator\\Desktop\\123\\cc.zip", "123");
Integer integer = 127;
Integer integer1 = 127;
System.out.println(integer.equals(integer1));
// try {
// File[] files = unzip("d:\\test\\汉字.zip", "aa");

@ -1,18 +1,18 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.tag.service.impl;
import cn.hutool.core.collection.CollectionUtil;
@ -49,15 +49,15 @@ import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.CompletableFuture;
/**
* @website https://el-admin.vip
* @description
* @author x
* @date 2020-09-22
**/
* @author x
* @website https://el-admin.vip
* @description
* @date 2020-09-22
**/
@Slf4j
@Service
@RequiredArgsConstructor
@ -74,27 +74,27 @@ public class TagServiceImpl implements TagService {
private EntityManager entityManager;
@Override
public Map<String,Object> queryAll(TagQueryCriteria criteria, Pageable pageable){
Page<Tag> page = tagRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
public Map<String, Object> queryAll(TagQueryCriteria criteria, Pageable pageable) {
Page<Tag> page = tagRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
return PageUtil.toPage(page.map(tagMapper::toDto));
}
@Override
public List<Tag> queryAllBySlice(TagQueryCriteria criteria, Pageable pageable) {
Slice<Tag> slice = tagRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
Slice<Tag> slice = tagRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
return slice.getContent();
}
@Override
public List<TagDto> queryAll(TagQueryCriteria criteria){
return tagMapper.toDto(tagRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
public List<TagDto> queryAll(TagQueryCriteria criteria) {
return tagMapper.toDto(tagRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
}
@Override
@Transactional
public TagDto findById(Long id) {
Tag tag = tagRepository.findById(id).orElseGet(Tag::new);
ValidationUtil.isNull(tag.getId(),"Tag","id",id);
ValidationUtil.isNull(tag.getId(), "Tag", "id", id);
return tagMapper.toDto(tag);
}
@ -108,7 +108,7 @@ public class TagServiceImpl implements TagService {
@Transactional(rollbackFor = Exception.class)
public void update(Tag resources) {
Tag tag = tagRepository.findById(resources.getId()).orElseGet(Tag::new);
ValidationUtil.isNull( tag.getId(),"Tag","id",resources.getId());
ValidationUtil.isNull(tag.getId(), "Tag", "id", resources.getId());
tag.copy(resources);
tagRepository.save(tag);
}
@ -124,7 +124,7 @@ public class TagServiceImpl implements TagService {
public void download(List<TagDto> all, HttpServletResponse response) throws IOException {
List<Map<String, Object>> list = new ArrayList<>();
for (TagDto tag : all) {
Map<String,Object> map = new LinkedHashMap<>();
Map<String, Object> map = new LinkedHashMap<>();
map.put(" uid", tag.getUid());
map.put(" taskId", tag.getTaskId());
map.put(" pushStatus", tag.getPushStatus());
@ -136,11 +136,11 @@ public class TagServiceImpl implements TagService {
@Override
@Transactional(rollbackFor = Exception.class)
public Integer saveAll(List<Tag> collect) {
if (CollectionUtils.isEmpty(collect)){
if (CollectionUtils.isEmpty(collect)) {
return 0;
}
List<Tag> tagList= tagRepository.saveAll(collect);
if (CollectionUtils.isEmpty(tagList)){
List<Tag> tagList = tagRepository.saveAll(collect);
if (CollectionUtils.isEmpty(tagList)) {
return 0;
}
return tagList.size();
@ -176,7 +176,7 @@ public class TagServiceImpl implements TagService {
}
// 坐标
if (Objects.nonNull(rectangle)){
if (Objects.nonNull(rectangle)) {
whereSql.append(" AND s.lng >= :minX AND s.lng < :maxX AND s.lat >= :minY AND s.lat < :maxY ");
}
// 拼接成完整sql
@ -186,13 +186,13 @@ public class TagServiceImpl implements TagService {
//创建本地sql查询实例
Query dataQuery = entityManager.createNativeQuery(insertSql.toString());
// 设置参数
if (CollectionUtil.isNotEmpty(stuGrade)){
if (CollectionUtil.isNotEmpty(stuGrade)) {
dataQuery.setParameter("stuGrade", stuGrade);
}
if (CollectionUtil.isNotEmpty(cityCode)){
if (CollectionUtil.isNotEmpty(cityCode)) {
dataQuery.setParameter("cityCode", cityCode);
}
if (Objects.nonNull(rectangle)){
if (Objects.nonNull(rectangle)) {
// 经度范围
dataQuery.setParameter("minX", rectangle.getMinX());
dataQuery.setParameter("maxX", rectangle.getMaxX());
@ -290,6 +290,7 @@ public class TagServiceImpl implements TagService {
return Boolean.FALSE;
}
@Override
@Transactional(rollbackFor = Exception.class)
public boolean deleteByTaskId(Integer id) {
@ -297,19 +298,42 @@ public class TagServiceImpl implements TagService {
int resultId = id % tableSum;
String deleteStr = "DELETE FROM dc_tag"
+ resultId
+ " WHERE task_id = :id";
+ " WHERE task_id = :id ";
Query deleteQuery = entityManager.createNativeQuery(deleteStr);
deleteQuery.setParameter("id", id);
// 执行删除任务
boolean result = deleteQuery.executeUpdate() > 0;
if (result){
if (result) {
//数据中需要删除的数据量
long expiredCount;
//已经删除数据量
long totalDeleted = 0L;
// 删除之前记录 分表情况下,单次可能会有遗漏 多次并不影响
String replaceStr = StringUtils.replace(deleteStr, " = ", " < ");
Query deleteTagByQuery = entityManager.createNativeQuery(replaceStr);
do {
Query deleteTagByQuery = entityManager.createNativeQuery(replaceStr + " limit 50000");
deleteTagByQuery.setParameter("id", id);
deleteTagByQuery.executeUpdate();
int executeUpdate = deleteTagByQuery.executeUpdate();
totalDeleted += executeUpdate;
expiredCount = queryCount(resultId, id);
} while (expiredCount > 0);
log.info("======== delete tag{} table size as [{}] ========", resultId, totalDeleted);
}
return result;
}
/**
*
* @param resultId
* @param id
* @return
*/
private Long queryCount(int resultId, int id) {
String sql = "select count(1) FROM dc_tag" + resultId + " where task_id < :id ";
Query selectQueryCount = entityManager.createNativeQuery(sql);
selectQueryCount.setParameter("id", id);
BigInteger bigInteger = (BigInteger) selectQueryCount.getSingleResult();
return bigInteger.longValue();
}
}

Loading…
Cancel
Save