From c7f02347f9adf2c3526ae7d1c081232cab27c36e Mon Sep 17 00:00:00 2001 From: APLS Date: Sun, 20 Dec 2020 00:21:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E7=94=A8=E6=88=B7=E7=AC=AC=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/qyi/e5/outlook/bean/OutlookListVo.java | 2 +- .../outlook/controller/OutlookController.java | 18 ++- .../io/qyi/e5/outlook/entity/Outlook.java | 3 + .../e5/outlook/service/IOutlookService.java | 16 +++ .../service/impl/OutlookServiceImpl.java | 134 +++++++++++++++++- .../service/rabbitMQ/impl/ListenerImpl.java | 11 +- .../io/qyi/e5/service/task/impl/TaskImpl.java | 9 +- src/main/resources/application.properties | 2 +- 8 files changed, 185 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/qyi/e5/outlook/bean/OutlookListVo.java b/src/main/java/io/qyi/e5/outlook/bean/OutlookListVo.java index edd1fca..90aa2e9 100644 --- a/src/main/java/io/qyi/e5/outlook/bean/OutlookListVo.java +++ b/src/main/java/io/qyi/e5/outlook/bean/OutlookListVo.java @@ -20,7 +20,7 @@ public class OutlookListVo { /*描述*/ private String describes; /*下次调用时间*/ - private long nextTime; + private Integer nextTime; /*运行状态*/ private int status; } diff --git a/src/main/java/io/qyi/e5/outlook/controller/OutlookController.java b/src/main/java/io/qyi/e5/outlook/controller/OutlookController.java index 49306e6..1935936 100644 --- a/src/main/java/io/qyi/e5/outlook/controller/OutlookController.java +++ b/src/main/java/io/qyi/e5/outlook/controller/OutlookController.java @@ -134,7 +134,7 @@ public class OutlookController { return ResultUtil.success(vo); } /** - * 设置状态暂停状态 + * 设置暂停状态 * @title setPause * @description * @author 落叶随风 @@ -149,11 +149,27 @@ public class OutlookController { outlookService.setPause(authentication.getGithub_id(),id); return ResultUtil.success(); } + /** + * 设置运行状态 + * @title setPause + * @description + * @author 落叶随风 + * @param: id outlook id + * @updateTime 2020/12/13 19:24 + * @return: io.qyi.e5.bean.result.Result + * @throws + */ @GetMapping("/setStart") public Result setStart(@RequestParam int id) { UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); outlookService.setStart(authentication.getGithub_id(),id); return ResultUtil.success(); } + @GetMapping("/delete") + public Result delete(@RequestParam int id) { + UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); + outlookService.delete(authentication.getGithub_id(),id); + return ResultUtil.success(); + } } diff --git a/src/main/java/io/qyi/e5/outlook/entity/Outlook.java b/src/main/java/io/qyi/e5/outlook/entity/Outlook.java index 11e2a90..7e2efa5 100644 --- a/src/main/java/io/qyi/e5/outlook/entity/Outlook.java +++ b/src/main/java/io/qyi/e5/outlook/entity/Outlook.java @@ -73,4 +73,7 @@ public class Outlook implements Serializable { */ private Integer status; + /*下次调用时间*/ + private Integer nextTime; + } diff --git a/src/main/java/io/qyi/e5/outlook/service/IOutlookService.java b/src/main/java/io/qyi/e5/outlook/service/IOutlookService.java index 1a86146..463087f 100644 --- a/src/main/java/io/qyi/e5/outlook/service/IOutlookService.java +++ b/src/main/java/io/qyi/e5/outlook/service/IOutlookService.java @@ -34,4 +34,20 @@ public interface IOutlookService extends IService { void setPause(int github_id, int outlookId); void setStart(int github_id, int outlookId); + + /** + * 更新数据 + * + * @param github_id: github_id + * @param outlookId: outlookId + * @param outlook: 更新的数据 + * @Author: 落叶随风 + * @Date: 2020/12/19 21:29 + * @Return: * @return: void + */ + void update(int github_id, int outlookId, Outlook outlook); + + void delete(int github_id, int outlookId); + + boolean isStatusRun(int github_id, int outlookId); } diff --git a/src/main/java/io/qyi/e5/outlook/service/impl/OutlookServiceImpl.java b/src/main/java/io/qyi/e5/outlook/service/impl/OutlookServiceImpl.java index e21b9cc..e6ce7a7 100644 --- a/src/main/java/io/qyi/e5/outlook/service/impl/OutlookServiceImpl.java +++ b/src/main/java/io/qyi/e5/outlook/service/impl/OutlookServiceImpl.java @@ -14,13 +14,14 @@ import io.qyi.e5.outlook.entity.Outlook; import io.qyi.e5.outlook.mapper.OutlookMapper; import io.qyi.e5.outlook.service.IOutlookService; import io.qyi.e5.util.netRequest.*; +import io.qyi.e5.util.redis.RedisUtil; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; -import java.io.Serializable; import java.util.Arrays; import java.util.HashMap; import java.util.List; @@ -42,6 +43,8 @@ public class OutlookServiceImpl extends ServiceImpl impl @Value("${outlook.errorMsg}") private String[] errorMsg; + @Autowired + RedisUtil redisUtil; // 2020-03-2 10:38 这里需要进行查询判断数据库是否有内容再进行插入。 @Override @@ -69,6 +72,8 @@ public class OutlookServiceImpl extends ServiceImpl impl Outlook outlook = new Outlook(); outlook.setAccessToken(access_token) .setRefreshToken(refresh_token) + .setStatus(3) + .setIdToken(id_token); UpdateWrapper outlookUpdateWrapper = new UpdateWrapper<>(); outlookUpdateWrapper.eq("client_id", client_id); @@ -77,6 +82,15 @@ public class OutlookServiceImpl extends ServiceImpl impl } } + /* + * + * @param name: 插入一条新列表 + * @param describe: 描述 + * @param github_id: github_id + * @Author: 落叶随风 + * @Date: 2020/12/19 21:25 + * @Return: * @return: io.qyi.e5.outlook.entity.Outlook + */ @Override public Outlook insertOne(String name, String describe, int github_id) { if (StringUtils.isBlank(name)) { @@ -92,7 +106,16 @@ public class OutlookServiceImpl extends ServiceImpl impl } return outlook; } - + /* + * 保存key + * @param client_id: + * @param client_secret: + * @param outlook_id: + * @param github_id: + * @Author: 落叶随风 + * @Date: 2020/12/19 21:24 + * @Return: * @return: boolean + */ @Override public boolean save(String client_id, String client_secret, int outlook_id, int github_id) { if (github_id == 0) { @@ -124,7 +147,17 @@ public class OutlookServiceImpl extends ServiceImpl impl } return true; } - + /* + * 保存随机调用时间 + * @param github_id: github_id + * @param cron_time: cron_time + * @param outlook_id: outlook_id + * @param cron_time_random_start: 开始时间 + * @param cron_time_random_end: 结束时间 + * @Author: 落叶随风 + * @Date: 2020/12/19 21:24 + * @Return: * @return: boolean + */ @Override public boolean saveRandomTime(int github_id, int cron_time, int outlook_id, int cron_time_random_start, int cron_time_random_end) { if (github_id == 0 || outlook_id == 0) { @@ -147,7 +180,12 @@ public class OutlookServiceImpl extends ServiceImpl impl } return false; } - + /* + * 查询所有列表 + * @Author: 落叶随风 + * @Date: 2020/12/19 21:23 + * @Return: * @return: java.util.List + */ @Override public List findAll() { return baseMapper.selectList(null); @@ -169,6 +207,13 @@ public class OutlookServiceImpl extends ServiceImpl impl return baseMapper.delete(outlookQueryWrapper); } + /* + * 调用邮件列表 + * @param outlook: + * @Author: 落叶随风 + * @Date: 2020/12/19 21:22 + * @Return: * @return: int + */ @Override public int getMailList(Outlook outlook) throws Exception { String s = MailList(outlook.getAccessToken()); @@ -237,6 +282,15 @@ public class OutlookServiceImpl extends ServiceImpl impl return count; } + /** + * @title 获取邮件列表,默认5封 + * @description + * @author 落叶随风 + * @param: access_token + * @updateTime 2020/12/19 21:17 + * @return: java.lang.String + * @throws + */ public String MailList(String access_token) throws Exception { Map head = new HashMap<>(); head.put("Content-Type", "application/json"); @@ -297,12 +351,32 @@ public class OutlookServiceImpl extends ServiceImpl impl return false; } + /** + * @title 获取本账号下的outlook 应用列表 + * @description + * @author 落叶随风 + * @param: github_id + * @updateTime 2020/12/19 21:16 + * @return: java.util.List + * @throws + */ @Override public List getOutlooklist(int github_id) { QueryWrapper qw = new QueryWrapper().eq("github_id", github_id); List outlooks = baseMapper.selectList(qw); return outlooks; } + + /** + * 设置暂停状态 + * @title setPause + * @description + * @author 落叶随风 + * @param: github_id + * @param: outlookId + * @updateTime 2020/12/19 21:16 + * @throws + */ @Override public void setPause(int github_id, int outlookId) { UpdateWrapper up = new UpdateWrapper<>(); @@ -312,7 +386,7 @@ public class OutlookServiceImpl extends ServiceImpl impl throw new APIException("查无此记录!"); } /*只允许运行状态的应用设置暂停*/ - if (outlook.getStatus() != 2) { + if (outlook.getStatus() != 3) { throw new APIException("只允许 运行状态 的应用设置暂停!"); } if (baseMapper.update(new Outlook().setStatus(2), up) != 1) { @@ -320,6 +394,16 @@ public class OutlookServiceImpl extends ServiceImpl impl } } + /** + * 设置开始状态 + * @title setStart + * @description + * @author 落叶随风 + * @param: github_id + * @param: outlookId + * @updateTime 2020/12/19 21:16 + * @throws + */ @Override public void setStart(int github_id, int outlookId) { UpdateWrapper up = new UpdateWrapper<>(); @@ -328,5 +412,45 @@ public class OutlookServiceImpl extends ServiceImpl impl throw new APIException("更新失败!"); } } + /** + * 更新数据 + * @param github_id: github_id + * @param outlookId: outlookId + * @param outlook: 更新的数据 + * @Author: 落叶随风 + * @Date: 2020/12/19 21:29 + * @Return: * @return: void + */ + @Override + public void update(int github_id, int outlookId, Outlook outlook) { + UpdateWrapper uw = new UpdateWrapper<>(); + uw.eq("id", outlookId); + uw.eq("github_id", github_id); + baseMapper.update(outlook, uw); + } + @Override + public void delete(int github_id, int outlookId) { + QueryWrapper wp = new QueryWrapper<>(); + wp.eq("github_id", github_id); + wp.eq("id", outlookId); + if (baseMapper.delete(wp) != 1) { + log.error("删除数据失败! github_id:{github_id} - outlookId:{outlookId}"); + throw new APIException("删除失败!"); + } + } + @Override + public boolean isStatusRun(int github_id, int outlookId){ + QueryWrapper wp = new QueryWrapper<>(); + wp.eq("github_id", github_id); + wp.eq("id", outlookId); + Outlook outlook = baseMapper.selectOne(wp); + if (outlook != null) { + if (outlook.getStatus() == 3) { + return true; + } + } + + return false; + } } diff --git a/src/main/java/io/qyi/e5/service/rabbitMQ/impl/ListenerImpl.java b/src/main/java/io/qyi/e5/service/rabbitMQ/impl/ListenerImpl.java index 8a2871d..f70ebec 100644 --- a/src/main/java/io/qyi/e5/service/rabbitMQ/impl/ListenerImpl.java +++ b/src/main/java/io/qyi/e5/service/rabbitMQ/impl/ListenerImpl.java @@ -4,6 +4,7 @@ import com.google.gson.Gson; import com.rabbitmq.client.Channel; import io.qyi.e5.outlook.bean.OutlookMq; import io.qyi.e5.outlook.service.IOutlookService; +import io.qyi.e5.outlook_log.service.IOutlookLogService; import io.qyi.e5.service.task.ITask; import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; @@ -30,6 +31,8 @@ public class ListenerImpl { IOutlookService outlookService; @Autowired ITask Task; + @Autowired + IOutlookLogService outlookLogService; private static final Gson gson = new Gson(); @@ -42,7 +45,13 @@ public class ListenerImpl { channel.basicAck(message.getMessageProperties().getDeliveryTag(), true); /*再次进行添加任务*/ if (b) { - Task.sendTaskOutlookMQ(mq.getGithubId(),mq.getOutlookId()); + if (outlookService.isStatusRun(mq.getGithubId(), mq.getOutlookId())) { + Task.sendTaskOutlookMQ(mq.getGithubId(), mq.getOutlookId()); + } else { + outlookLogService.addLog(mq.getGithubId(), mq.getOutlookId(), "error", 0, "检测到手动设置了运行状态,停止调用!"); + } + } else { + outlookLogService.addLog(mq.getGithubId(), mq.getOutlookId(), "error", 0, "执行失败,结束调用!"); } } } diff --git a/src/main/java/io/qyi/e5/service/task/impl/TaskImpl.java b/src/main/java/io/qyi/e5/service/task/impl/TaskImpl.java index d5def40..2db6744 100644 --- a/src/main/java/io/qyi/e5/service/task/impl/TaskImpl.java +++ b/src/main/java/io/qyi/e5/service/task/impl/TaskImpl.java @@ -59,8 +59,11 @@ public class TaskImpl implements ITask { /*将此用户信息加入redis,如果存在则代表在队列中,同时提前10秒过期*/ String rsKey = "user.mq:" + github_id + ".outlookId:" + outlookId; if (!redisUtil.hasKey(rsKey)) { - redisUtil.set(rsKey, 0, Expiration - 10); + redisUtil.set(rsKey, (System.currentTimeMillis() / 1000) + Expiration, Expiration - 10); OutlookMq mq = new OutlookMq(github_id, outlookId); + Outlook ol = new Outlook(); + ol.setNextTime((int) ((System.currentTimeMillis() / 1000) + Expiration)); + outlookService.update(github_id,outlookId,ol); send(mq, Expiration * 1000); } else { logger.info("Key 存在,不执行{}",rsKey); @@ -111,6 +114,10 @@ public class TaskImpl implements ITask { if (error_count >= errorCountMax) { outlookLogService.addLog(github_id, outlookId,"error", 0, e.getMessage()); outlookLogService.addLog(github_id, outlookId,"error", 0, "检测到3次连续错误,下次将不再自动调用,请修正错误后再授权开启续订。"); + /*设置状态为停止*/ + Outlook outlook = new Outlook(); + outlook.setStatus(5); + outlookService.update(github_id,outlookId,outlook); isExecuteE5 = false; } else { redisUtil.incr(errorKey, 1); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e295525..257b306 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1 @@ -spring.profiles.active=online \ No newline at end of file +spring.profiles.active=dev \ No newline at end of file