优化业务处理

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.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<SysJob> {
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<SysJob> {
@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<SysJob> {
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<SysJob> {
@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;

View File

@ -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<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")
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.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<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.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
@Slf4j
public class JobServiceImpl extends BaseServiceImpl<SysJob, String> implements JobService {
@Autowired
SysJobMapper jobMapper;
@Autowired
JobTask jobTask;
@Autowired
JobService jobService;
@Override
public BaseMapper<SysJob, String> 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;
}
}