From 3f64677ec5d4dc6476f73fad54b824d719fc0cce Mon Sep 17 00:00:00 2001 From: meng <154040976@qq.com> Date: Tue, 30 Apr 2019 21:28:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=B8=9A=E5=8A=A1=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/len/controller/JobController.java | 66 +++---------- .../com/len/controller/LoginController.java | 40 +------- .../main/java/com/len/service/JobService.java | 29 ++++++ .../com/len/service/impl/JobServiceImpl.java | 99 +++++++++++++++++-- 4 files changed, 136 insertions(+), 98 deletions(-) diff --git a/len-sys/src/main/java/com/len/controller/JobController.java b/len-sys/src/main/java/com/len/controller/JobController.java index 6dd343a..6691999 100644 --- a/len-sys/src/main/java/com/len/controller/JobController.java +++ b/len-sys/src/main/java/com/len/controller/JobController.java @@ -5,17 +5,11 @@ import com.len.core.annotation.Log; import com.len.core.annotation.Log.LOG_TYPE; import com.len.core.quartz.JobTask; import com.len.entity.SysJob; -import com.len.entity.SysUser; import com.len.exception.MyException; import com.len.service.JobService; -import com.len.util.BeanUtil; -import com.len.util.Checkbox; import com.len.util.JsonUtil; import com.len.util.ReType; import io.swagger.annotations.ApiOperation; - -import java.util.List; - import org.apache.commons.lang3.StringUtils; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.beans.factory.annotation.Autowired; @@ -106,15 +100,11 @@ public class JobController extends BaseController { j.setMsg("已经启动任务无法更新,请停止后更新"); return j; } - try { - SysJob oldJob = jobService.selectByPrimaryKey(job.getId()); - BeanUtil.copyNotNullBean(job, oldJob); - jobService.updateByPrimaryKey(oldJob); + if (jobService.updateJob(job)) { j.setFlag(true); - j.setMsg("修改成功"); - } catch (MyException e) { - j.setMsg("更新失败"); - e.printStackTrace(); + j.setData("更新成功"); + } else { + j.setData("更新失败"); } return j; } @@ -125,31 +115,7 @@ public class JobController extends BaseController { @ResponseBody @RequiresPermissions("job:del") public JsonUtil del(String id) { - JsonUtil j = new JsonUtil(); - j.setFlag(false); - if (StringUtils.isEmpty(id)) { - j.setMsg("获取数据失败"); - return j; - } - try { - SysJob job = jobService.selectByPrimaryKey(id); - boolean flag = jobTask.checkJob(job); - if ((flag && !job.getStatus()) || !flag && job.getStatus()) { - j.setMsg("您任务表状态和web任务状态不一致,无法删除"); - return j; - } - if (flag) { - j.setMsg("该任务处于启动中,无法删除"); - return j; - } - jobService.deleteByPrimaryKey(id); - j.setFlag(true); - j.setMsg("任务删除成功"); - } catch (MyException e) { - j.setMsg("任务删除异常"); - e.printStackTrace(); - } - return j; + return jobService.del(id); } @@ -165,14 +131,10 @@ public class JobController extends BaseController { j.setFlag(false); return j; } - try { - SysJob job = jobService.selectByPrimaryKey(id); - jobTask.startJob(job); - job.setStatus(true); - jobService.updateByPrimaryKey(job); + if (jobService.startJob(id)) { msg = "启动成功"; - } catch (MyException e) { - e.printStackTrace(); + } else { + msg = "启动失败"; } j.setMsg(msg); return j; @@ -184,20 +146,16 @@ public class JobController extends BaseController { @RequiresPermissions("job:end") public JsonUtil endJob(String id) { JsonUtil j = new JsonUtil(); - String msg = null; + String msg; if (StringUtils.isEmpty(id)) { j.setMsg("获取数据失败"); j.setFlag(false); return j; } - try { - SysJob job = jobService.selectByPrimaryKey(id); - jobTask.remove(job); - job.setStatus(false); - jobService.updateByPrimaryKey(job); + if (jobService.stopJob(id)) { msg = "停止成功"; - } catch (MyException e) { - e.printStackTrace(); + } else { + msg = "停止失败"; } j.setMsg(msg); return j; diff --git a/len-sys/src/main/java/com/len/controller/LoginController.java b/len-sys/src/main/java/com/len/controller/LoginController.java index 3087891..0221688 100644 --- a/len-sys/src/main/java/com/len/controller/LoginController.java +++ b/len-sys/src/main/java/com/len/controller/LoginController.java @@ -1,11 +1,8 @@ package com.len.controller; -import com.alibaba.fastjson.JSONArray; import com.len.core.annotation.Log; import com.len.core.shiro.Principal; -import com.len.entity.SysMenu; import com.len.entity.SysUser; -import com.len.service.MenuService; import com.len.service.SysUserService; import com.len.util.CustomUsernamePasswordToken; import com.len.util.VerifyCodeUtils; @@ -22,11 +19,9 @@ import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; -import javax.servlet.ServletRequest; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; -import java.util.List; /** * @author zhuxiaomeng @@ -38,11 +33,9 @@ import java.util.List; @Slf4j public class LoginController { - @Autowired - private MenuService menuService; - @Autowired SysUserService userService; + private static final String CODE_ERROR = "code.error"; @GetMapping(value = "") public String loginInit() { @@ -50,7 +43,7 @@ public class LoginController { } @GetMapping(value = "goLogin") - public String goLogin(Model model, ServletRequest request) { + public String goLogin(Model model) { Subject sub = SecurityUtils.getSubject(); if (sub.isAuthenticated()) { return "/main/main"; @@ -83,7 +76,7 @@ public class LoginController { @PostMapping(value = "/login") public String login(SysUser user, Model model, String rememberMe, HttpServletRequest request) { String codeMsg = (String) request.getAttribute("shiroLoginFailure"); - if ("code.error".equals(codeMsg)) { + if (CODE_ERROR.equals(codeMsg)) { model.addAttribute("message", "验证码错误"); return "/login"; } @@ -94,7 +87,6 @@ public class LoginController { try { subject.login(token); if (subject.isAuthenticated()) { - //userService.setMenuAndRoles(token.getUsername()); token.getUsername(); return "redirect:/main"; } @@ -122,32 +114,6 @@ public class LoginController { return "/login"; } - /** - * 组装菜单json格式 - * update by 17/12/13 - * - * @return - */ - public JSONArray getMenuJson() { - List mList = menuService.getMenuNotSuper(); - JSONArray jsonArr = new JSONArray(); - for (SysMenu sysMenu : mList) { - SysMenu menu = getChild(sysMenu.getId()); - jsonArr.add(menu); - } - return jsonArr; - } - - public SysMenu getChild(String id) { - SysMenu sysMenu = menuService.selectByPrimaryKey(id); - List mList = menuService.getMenuChildren(id); - for (SysMenu menu : mList) { - SysMenu m = getChild(menu.getId()); - sysMenu.addChild(m); - } - return sysMenu; - } - @GetMapping(value = "/getCode") public void getYzm(HttpServletResponse response, HttpServletRequest request) { diff --git a/len-sys/src/main/java/com/len/service/JobService.java b/len-sys/src/main/java/com/len/service/JobService.java index 8dc6964..68d0ca9 100644 --- a/len-sys/src/main/java/com/len/service/JobService.java +++ b/len-sys/src/main/java/com/len/service/JobService.java @@ -3,6 +3,7 @@ package com.len.service; import com.len.base.BaseService; import com.len.entity.SysJob; import com.len.entity.SysMenu; +import com.len.util.JsonUtil; /** * @author zhuxiaomeng @@ -11,4 +12,32 @@ import com.len.entity.SysMenu; */ public interface JobService extends BaseService { + /** + * 更新 + * @param job + * @return + */ + public boolean updateJob(SysJob job); + + /** + * 删除 + * @param id + * @return + */ + public JsonUtil del(String id); + + /** + * 启动任务 + * @param id + * @return + */ + public boolean startJob(String id); + + /** + * 停止任务 + * @param id + * @return + */ + public boolean stopJob(String id); + } diff --git a/len-sys/src/main/java/com/len/service/impl/JobServiceImpl.java b/len-sys/src/main/java/com/len/service/impl/JobServiceImpl.java index 7976026..ec2138b 100644 --- a/len-sys/src/main/java/com/len/service/impl/JobServiceImpl.java +++ b/len-sys/src/main/java/com/len/service/impl/JobServiceImpl.java @@ -2,9 +2,15 @@ package com.len.service.impl; import com.len.base.BaseMapper; import com.len.base.impl.BaseServiceImpl; +import com.len.core.quartz.JobTask; import com.len.entity.SysJob; +import com.len.exception.MyException; import com.len.mapper.SysJobMapper; import com.len.service.JobService; +import com.len.util.BeanUtil; +import com.len.util.JsonUtil; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -14,12 +20,91 @@ import org.springframework.stereotype.Service; * @email 154040976@qq.com */ @Service -public class JobServiceImpl extends BaseServiceImpl implements JobService { +@Slf4j +public class JobServiceImpl extends BaseServiceImpl implements JobService { - @Autowired - SysJobMapper jobMapper; - @Override - public BaseMapper getMappser() { - return jobMapper; - } + @Autowired + SysJobMapper jobMapper; + + @Autowired + JobTask jobTask; + + @Autowired + JobService jobService; + + @Override + public BaseMapper getMappser() { + return jobMapper; + } + + @Override + public boolean updateJob(SysJob job) { + try { + SysJob oldJob = selectByPrimaryKey(job.getId()); + BeanUtil.copyNotNullBean(job, oldJob); + updateByPrimaryKey(oldJob); + return true; + } catch (MyException e) { + log.error(e.getMessage()); + return false; + } + } + + @Override + public JsonUtil del(String id) { + JsonUtil j = new JsonUtil(); + j.setFlag(false); + if (StringUtils.isEmpty(id)) { + j.setMsg("获取数据失败"); + return j; + } + try { + SysJob job = selectByPrimaryKey(id); + boolean flag = jobTask.checkJob(job); + if ((flag && !job.getStatus()) || !flag && job.getStatus()) { + j.setMsg("您任务表状态和web任务状态不一致,无法删除"); + return j; + } + if (flag) { + j.setMsg("该任务处于启动中,无法删除"); + return j; + } + jobService.deleteByPrimaryKey(id); + j.setFlag(true); + j.setMsg("任务删除成功"); + } catch (MyException e) { + j.setMsg("任务删除异常"); + e.printStackTrace(); + } + return j; + } + + @Override + public boolean startJob(String id) { + try { + SysJob job = jobService.selectByPrimaryKey(id); + jobTask.startJob(job); + job.setStatus(true); + jobService.updateByPrimaryKey(job); + return true; + } catch (MyException e) { + log.error(e.getMessage()); + e.printStackTrace(); + } + return false; + } + + @Override + public boolean stopJob(String id) { + try { + SysJob job = jobService.selectByPrimaryKey(id); + jobTask.remove(job); + job.setStatus(false); + jobService.updateByPrimaryKey(job); + return true; + } catch (MyException e) { + e.printStackTrace(); + } + return false; + } }