优化业务处理

This commit is contained in:
meng 2019-04-30 21:28:00 +08:00
parent 3b7055315e
commit 3f64677ec5
4 changed files with 136 additions and 98 deletions

View File

@ -5,17 +5,11 @@ import com.len.core.annotation.Log;
import com.len.core.annotation.Log.LOG_TYPE; import com.len.core.annotation.Log.LOG_TYPE;
import com.len.core.quartz.JobTask; import com.len.core.quartz.JobTask;
import com.len.entity.SysJob; import com.len.entity.SysJob;
import com.len.entity.SysUser;
import com.len.exception.MyException; import com.len.exception.MyException;
import com.len.service.JobService; import com.len.service.JobService;
import com.len.util.BeanUtil;
import com.len.util.Checkbox;
import com.len.util.JsonUtil; import com.len.util.JsonUtil;
import com.len.util.ReType; import com.len.util.ReType;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions; import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -106,15 +100,11 @@ public class JobController extends BaseController<SysJob> {
j.setMsg("已经启动任务无法更新,请停止后更新"); j.setMsg("已经启动任务无法更新,请停止后更新");
return j; return j;
} }
try { if (jobService.updateJob(job)) {
SysJob oldJob = jobService.selectByPrimaryKey(job.getId());
BeanUtil.copyNotNullBean(job, oldJob);
jobService.updateByPrimaryKey(oldJob);
j.setFlag(true); j.setFlag(true);
j.setMsg("修改成功"); j.setData("更新成功");
} catch (MyException e) { } else {
j.setMsg("更新失败"); j.setData("更新失败");
e.printStackTrace();
} }
return j; return j;
} }
@ -125,31 +115,7 @@ public class JobController extends BaseController<SysJob> {
@ResponseBody @ResponseBody
@RequiresPermissions("job:del") @RequiresPermissions("job:del")
public JsonUtil del(String id) { public JsonUtil del(String id) {
JsonUtil j = new JsonUtil(); return jobService.del(id);
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;
} }
@ -165,14 +131,10 @@ public class JobController extends BaseController<SysJob> {
j.setFlag(false); j.setFlag(false);
return j; return j;
} }
try { if (jobService.startJob(id)) {
SysJob job = jobService.selectByPrimaryKey(id);
jobTask.startJob(job);
job.setStatus(true);
jobService.updateByPrimaryKey(job);
msg = "启动成功"; msg = "启动成功";
} catch (MyException e) { } else {
e.printStackTrace(); msg = "启动失败";
} }
j.setMsg(msg); j.setMsg(msg);
return j; return j;
@ -184,20 +146,16 @@ public class JobController extends BaseController<SysJob> {
@RequiresPermissions("job:end") @RequiresPermissions("job:end")
public JsonUtil endJob(String id) { public JsonUtil endJob(String id) {
JsonUtil j = new JsonUtil(); JsonUtil j = new JsonUtil();
String msg = null; String msg;
if (StringUtils.isEmpty(id)) { if (StringUtils.isEmpty(id)) {
j.setMsg("获取数据失败"); j.setMsg("获取数据失败");
j.setFlag(false); j.setFlag(false);
return j; return j;
} }
try { if (jobService.stopJob(id)) {
SysJob job = jobService.selectByPrimaryKey(id);
jobTask.remove(job);
job.setStatus(false);
jobService.updateByPrimaryKey(job);
msg = "停止成功"; msg = "停止成功";
} catch (MyException e) { } else {
e.printStackTrace(); msg = "停止失败";
} }
j.setMsg(msg); j.setMsg(msg);
return j; return j;

View File

@ -1,11 +1,8 @@
package com.len.controller; package com.len.controller;
import com.alibaba.fastjson.JSONArray;
import com.len.core.annotation.Log; import com.len.core.annotation.Log;
import com.len.core.shiro.Principal; import com.len.core.shiro.Principal;
import com.len.entity.SysMenu;
import com.len.entity.SysUser; import com.len.entity.SysUser;
import com.len.service.MenuService;
import com.len.service.SysUserService; import com.len.service.SysUserService;
import com.len.util.CustomUsernamePasswordToken; import com.len.util.CustomUsernamePasswordToken;
import com.len.util.VerifyCodeUtils; 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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import java.util.List;
/** /**
* @author zhuxiaomeng * @author zhuxiaomeng
@ -38,11 +33,9 @@ import java.util.List;
@Slf4j @Slf4j
public class LoginController { public class LoginController {
@Autowired
private MenuService menuService;
@Autowired @Autowired
SysUserService userService; SysUserService userService;
private static final String CODE_ERROR = "code.error";
@GetMapping(value = "") @GetMapping(value = "")
public String loginInit() { public String loginInit() {
@ -50,7 +43,7 @@ public class LoginController {
} }
@GetMapping(value = "goLogin") @GetMapping(value = "goLogin")
public String goLogin(Model model, ServletRequest request) { public String goLogin(Model model) {
Subject sub = SecurityUtils.getSubject(); Subject sub = SecurityUtils.getSubject();
if (sub.isAuthenticated()) { if (sub.isAuthenticated()) {
return "/main/main"; return "/main/main";
@ -83,7 +76,7 @@ public class LoginController {
@PostMapping(value = "/login") @PostMapping(value = "/login")
public String login(SysUser user, Model model, String rememberMe, HttpServletRequest request) { public String login(SysUser user, Model model, String rememberMe, HttpServletRequest request) {
String codeMsg = (String) request.getAttribute("shiroLoginFailure"); String codeMsg = (String) request.getAttribute("shiroLoginFailure");
if ("code.error".equals(codeMsg)) { if (CODE_ERROR.equals(codeMsg)) {
model.addAttribute("message", "验证码错误"); model.addAttribute("message", "验证码错误");
return "/login"; return "/login";
} }
@ -94,7 +87,6 @@ public class LoginController {
try { try {
subject.login(token); subject.login(token);
if (subject.isAuthenticated()) { if (subject.isAuthenticated()) {
//userService.setMenuAndRoles(token.getUsername());
token.getUsername(); token.getUsername();
return "redirect:/main"; return "redirect:/main";
} }
@ -122,32 +114,6 @@ public class LoginController {
return "/login"; return "/login";
} }
/**
* 组装菜单json格式
* update by 17/12/13
*
* @return
*/
public JSONArray getMenuJson() {
List<SysMenu> 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<SysMenu> mList = menuService.getMenuChildren(id);
for (SysMenu menu : mList) {
SysMenu m = getChild(menu.getId());
sysMenu.addChild(m);
}
return sysMenu;
}
@GetMapping(value = "/getCode") @GetMapping(value = "/getCode")
public void getYzm(HttpServletResponse response, HttpServletRequest request) { public void getYzm(HttpServletResponse response, HttpServletRequest request) {

View File

@ -3,6 +3,7 @@ package com.len.service;
import com.len.base.BaseService; import com.len.base.BaseService;
import com.len.entity.SysJob; import com.len.entity.SysJob;
import com.len.entity.SysMenu; import com.len.entity.SysMenu;
import com.len.util.JsonUtil;
/** /**
* @author zhuxiaomeng * @author zhuxiaomeng
@ -11,4 +12,32 @@ import com.len.entity.SysMenu;
*/ */
public interface JobService extends BaseService<SysJob,String> { public interface JobService extends BaseService<SysJob,String> {
/**
* 更新
* @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);
} }

View File

@ -2,9 +2,15 @@ package com.len.service.impl;
import com.len.base.BaseMapper; import com.len.base.BaseMapper;
import com.len.base.impl.BaseServiceImpl; import com.len.base.impl.BaseServiceImpl;
import com.len.core.quartz.JobTask;
import com.len.entity.SysJob; import com.len.entity.SysJob;
import com.len.exception.MyException;
import com.len.mapper.SysJobMapper; import com.len.mapper.SysJobMapper;
import com.len.service.JobService; 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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -14,12 +20,91 @@ import org.springframework.stereotype.Service;
* @email 154040976@qq.com * @email 154040976@qq.com
*/ */
@Service @Service
public class JobServiceImpl extends BaseServiceImpl<SysJob,String> implements JobService { @Slf4j
public class JobServiceImpl extends BaseServiceImpl<SysJob, String> implements JobService {
@Autowired @Autowired
SysJobMapper jobMapper; SysJobMapper jobMapper;
@Autowired
JobTask jobTask;
@Autowired
JobService jobService;
@Override @Override
public BaseMapper<SysJob, String> getMappser() { public BaseMapper<SysJob, String> getMappser() {
return jobMapper; 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;
}
} }