定时归档消息

master
wujingtao 3 years ago
parent f3b73ddde8
commit 6085af7201

@ -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 com.baiye.modules.system.repository;
import com.baiye.modules.system.domain.UserMessage;
@ -23,11 +23,13 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
/**
* @website https://el-admin.vip
* @author Enzo
* @date 2021-12-30
**/
* @author Enzo
* @website https://el-admin.vip
* @date 2021-12-30
**/
public interface UserMessageRepository extends JpaRepository<UserMessage, Long>, JpaSpecificationExecutor<UserMessage> {
/**
@ -43,6 +45,7 @@ public interface UserMessageRepository extends JpaRepository<UserMessage, Long>,
/**
*
*
* @param aTrue
* @param aLong
* @return
@ -53,6 +56,7 @@ public interface UserMessageRepository extends JpaRepository<UserMessage, Long>,
/**
*
*
* @param isTop
* @param id
* @return
@ -64,6 +68,7 @@ public interface UserMessageRepository extends JpaRepository<UserMessage, Long>,
/**
*
*
* @param isTop
* @param id
* @return
@ -72,5 +77,22 @@ public interface UserMessageRepository extends JpaRepository<UserMessage, Long>,
@Query(value = "update UserMessage set isTop = ?1 where id = ?2")
int changeMessageIsTop(Boolean isTop, Long id);
/**
*
*
* @param time
* @param status
* @return
*/
@Query(value = "select um from UserMessage um where um.updateTime < ?1 and um.status =?2 and um.isTop = ?3")
List<UserMessage> selectByTimeAndStatus(String time, Integer status, Boolean isTop);
/**
*
*
* @param status
* @return
*/
@Query(value = "select um from UserMessage um where um.status =?1")
List<UserMessage> selectByStatus(Integer status);
}

@ -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 com.baiye.modules.system.service;
import com.baiye.http.CommonResponse;
@ -28,62 +28,69 @@ import java.util.List;
import java.util.Map;
/**
* @website https://el-admin.vip
* @description
* @author Enzo
* @date 2021-12-30
**/
* @author Enzo
* @website https://el-admin.vip
* @description
* @date 2021-12-30
**/
public interface UserMessageService {
/**
*
* @param criteria
* @param pageable
* @return Map<String,Object>
*/
Map<String,Object> queryAll(UserMessageQueryCriteria criteria, Pageable pageable);
*
*
* @param criteria
* @param pageable
* @return Map<String, Object>
*/
Map<String, Object> queryAll(UserMessageQueryCriteria criteria, Pageable pageable);
/**
*
* @param criteria
* @param page
*
*
* @param criteria
* @param page
* @param size
* @return List<UserMessageDto>
*/
*/
Map<String, Object> queryAll(UserMessageQueryCriteria criteria, Integer page, Integer size);
/**
* ID
*
* @param id ID
* @return UserMessageDto
*/
UserMessageDto findById(Long id);
/**
*
* @param resources /
* @return UserMessageDto
*/
*
*
* @param resources /
* @return UserMessageDto
*/
UserMessageDto create(UserMessage resources);
/**
*
* @param resources /
*/
*
*
* @param resources /
*/
void update(UserMessage resources);
/**
*
* @param ids /
*/
void deleteAll(Long[] ids);
*
*
* @param ids /
*/
void deleteAll(List<Long> ids);
/**
*
* @param all
* @param response /
* @throws IOException /
*/
*
*
* @param all
* @param response /
* @throws IOException /
*/
void download(List<UserMessageDto> all, HttpServletResponse response) throws IOException;
/**
@ -98,16 +105,17 @@ public interface UserMessageService {
/**
*
*
* @param num
* @param messageId
* @return
*/
Boolean deleteAllByMessageId(Integer num,Long messageId);
Boolean deleteAllByMessageId(Integer num, Long messageId);
/**
*
*
* @param body
* @return
*/
@ -115,6 +123,7 @@ public interface UserMessageService {
/**
*
*
* @param isTop
* @param id
* @return
@ -123,6 +132,7 @@ public interface UserMessageService {
/**
*
*
* @param body
* @return
*/
@ -130,11 +140,34 @@ public interface UserMessageService {
/**
*
*
* @param message
* @param num
* @return
*/
UserMessageDto changeUserMessage(String message, int num);
/**
*
*
* @param time
* @param status
* @return
*/
List<UserMessage> selectByTimeAndStatus(String time, Integer status, Boolean isTop);
/**
*
*
* @param status
* @return
*/
List<UserMessage> selectByStatus(Integer status);
/**
*
*
* @param userMessages
*/
void saveAll(List<UserMessage> userMessages);
}

@ -64,7 +64,6 @@ public class UserMessageServiceImpl implements UserMessageService {
private final WebSocketServer webSocketServer;
@Override
public Map<String, Object> queryAll(UserMessageQueryCriteria criteria, Pageable pageable) {
@ -78,17 +77,17 @@ public class UserMessageServiceImpl implements UserMessageService {
Sort sort =
Sort.by(Sort.Direction.DESC, "updateTime");
if (criteria.getStatus() != null &&
criteria.getStatus() != DefaultNumberConstants.MINUS_ONE_NUMBER){
sort = Sort.by(Sort.Direction.ASC, "messageType").
and(Sort.by(Sort.Direction.ASC, "level")).
and(Sort.by(Sort.Direction.DESC, "isTop")).
and(Sort.by(Sort.Direction.ASC, "isRead")).
and(Sort.by(Sort.Direction.DESC, "updateTime")).
and(Sort.by(Sort.Direction.DESC, "id"));
criteria.getStatus() != DefaultNumberConstants.MINUS_ONE_NUMBER) {
sort = Sort.by(Sort.Direction.ASC, "messageType").
and(Sort.by(Sort.Direction.ASC, "level")).
and(Sort.by(Sort.Direction.DESC, "isTop")).
and(Sort.by(Sort.Direction.ASC, "isRead")).
and(Sort.by(Sort.Direction.DESC, "updateTime")).
and(Sort.by(Sort.Direction.DESC, "id"));
}
PageRequest pageRequest = PageRequest.of(page, size, sort);
Page<UserMessage> pageAll = userMessageRepository.findAll(
(root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder),pageRequest);
(root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageRequest);
return PageUtil.toPage(pageAll.map(userMessageMapper::toDto));
}
@ -117,7 +116,7 @@ public class UserMessageServiceImpl implements UserMessageService {
}
@Override
public void deleteAll(Long[] ids) {
public void deleteAll(List<Long> ids) {
for (Long id : ids) {
userMessageRepository.deleteById(id);
}
@ -145,11 +144,11 @@ public class UserMessageServiceImpl implements UserMessageService {
@Override
public Boolean createUserMessage(List<Long> longList, MessageNotification messageNotification,Integer type) {
public Boolean createUserMessage(List<Long> longList, MessageNotification messageNotification, Integer type) {
Map<Long, Object> hashMap = new HashMap<>(DefaultNumberConstants.FIFTEEN_NUMBER);
if (CollUtil.isNotEmpty(longList)) {
// 自提醒保存任务
if (messageNotification.getMessageType() != null &&
if (messageNotification.getMessageType() != null &&
messageNotification.getMessageType() == DefaultNumberConstants.TWO_NUMBER
&& type == DefaultNumberConstants.ONE_NUMBER) {
autoReminderService.addTimeTask(messageNotification, longList.get(DefaultNumberConstants.ZERO_NUMBER));
@ -179,7 +178,7 @@ public class UserMessageServiceImpl implements UserMessageService {
public Boolean deleteAllByMessageId(Integer num, Long messageId) {
return userMessageRepository.
deleteUserMessageByMessageId(num, messageId) >
DefaultNumberConstants.ZERO_NUMBER;
DefaultNumberConstants.ZERO_NUMBER;
}
@ -227,7 +226,7 @@ public class UserMessageServiceImpl implements UserMessageService {
case DefaultNumberConstants.TWO_NUMBER:
userMessage.setStatus(DefaultNumberConstants.MINUS_ONE_NUMBER);
userMessageRepository.deleteUserMessageByMessageId
(DefaultNumberConstants.MINUS_TWO_NUMBER,userMessage.getMessageId());
(DefaultNumberConstants.MINUS_TWO_NUMBER, userMessage.getMessageId());
break;
case DefaultNumberConstants.THREE_NUMBER:
userMessage.setIsRead(Boolean.TRUE);
@ -242,5 +241,20 @@ public class UserMessageServiceImpl implements UserMessageService {
return userMessageMapper.toDto(new UserMessage());
}
@Override
public List<UserMessage> selectByTimeAndStatus(String time, Integer status, Boolean isTop) {
return userMessageRepository.selectByTimeAndStatus(time, status, isTop);
}
@Override
public List<UserMessage> selectByStatus(Integer status) {
return userMessageRepository.selectByStatus(status);
}
@Override
public void saveAll(List<UserMessage> userMessages) {
userMessageRepository.saveAll(userMessages);
}
}

@ -3,7 +3,13 @@ package com.baiye.timed;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.json.JSONUtil;
import com.baiye.config.properties.FileProperties;
import com.baiye.constant.DefaultNumberConstants;
import com.baiye.exception.BadRequestException;
import com.baiye.modules.system.domain.AutoReminder;
import com.baiye.modules.system.domain.UserMessage;
import com.baiye.modules.system.service.AutoReminderService;
@ -15,12 +21,16 @@ import com.baiye.socket.WebSocketServer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author Enzo
@ -39,6 +49,7 @@ public class SendMessageSync {
private final MessageNotificationService messageNotificationService;
private final FileProperties properties;
@Scheduled(cron = "0 0 0/1 * * ?")
public void updateAccountMassNum() {
@ -76,4 +87,54 @@ public class SendMessageSync {
log.info("========================= sync send message end time as {} =========================", DateTime.now());
}
/**
*
* 7
*/
@Scheduled(cron = "0 0 0 1/1 * ?")
public void automaticFilingMessage() {
String beginTime = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), -7), "yyyy-MM-dd HH:mm:ss");
List<UserMessage> userMessages = userMessageService.selectByTimeAndStatus(beginTime, DefaultNumberConstants.ONE_NUMBER, Boolean.FALSE);
if (ObjectUtil.isNotNull(userMessages)) {
userMessages.forEach(x -> x.setStatus(DefaultNumberConstants.MINUS_ONE_NUMBER));
userMessageService.saveAll(userMessages);
}
}
/**
*
* 7
*/
@Scheduled(cron = "0 0 0 1/7 * ?")
public void saveMessagesZip() {
List<UserMessage> userMessages = userMessageService.selectByStatus(DefaultNumberConstants.MINUS_ONE_NUMBER);
if (ObjectUtil.isNotNull(userMessages)) {
try {
saveZip(userMessages);
List<Long> ids = userMessages.stream().map(UserMessage::getId).collect(Collectors.toList());
userMessageService.deleteAll(ids);
} catch (Exception e) {
throw new BadRequestException("删除归档消息失败");
}
}
}
private void saveZip(List<UserMessage> list) {
try {
File file = FileUtil.file(properties.getPath() + "message" + File.separator + DateUtil.now());
FileWriter fileWriter = new FileWriter(file);
for (UserMessage info : list) {
fileWriter.write(JSONUtil.toJsonStr(info));
fileWriter.write(System.getProperty("line.separator"));
}
fileWriter.close();
ZipUtil.zip(file);
FileUtil.del(file);
} catch (Exception e) {
log.error("定时归档失败");
throw new BadRequestException("定时归档失败");
}
}
}

Loading…
Cancel
Save