Controller 层代码优化。

This commit is contained in:
wangiegie@gmail.com 2017-12-16 17:11:24 +08:00
parent a54099499f
commit eae03d88f6
15 changed files with 214 additions and 67 deletions

View File

@ -64,6 +64,11 @@
<artifactId>qiniu-java-sdk</artifactId> <artifactId>qiniu-java-sdk</artifactId>
<version>[7.2.0, 7.2.99]</version> <version>[7.2.0, 7.2.99]</version>
</dependency> </dependency>
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-pay</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies> </dependencies>

View File

@ -5,14 +5,12 @@ import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page; import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.admin.service.SysLogService; import com.github.pig.admin.service.SysLogService;
import com.github.pig.common.constant.CommonConstant; import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.entity.SysLog;
import com.github.pig.common.util.Query; import com.github.pig.common.util.Query;
import com.github.pig.common.util.UserUtils; import com.github.pig.common.util.R;
import com.github.pig.common.web.BaseController; import com.github.pig.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.Map; import java.util.Map;
/** /**
@ -29,12 +27,6 @@ public class LogController extends BaseController {
@Autowired @Autowired
private SysLogService sysLogService; private SysLogService sysLogService;
@PostMapping
public Boolean log(@RequestBody SysLog sysLog) {
sysLog.setCreateBy(UserUtils.getUserName());
return sysLogService.insert(sysLog);
}
/** /**
* 分页查询日志信息 * 分页查询日志信息
* *
@ -44,7 +36,7 @@ public class LogController extends BaseController {
@RequestMapping("/logPage") @RequestMapping("/logPage")
public Page logPage(@RequestParam Map<String, Object> params) { public Page logPage(@RequestParam Map<String, Object> params) {
params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL); params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
return sysLogService.selectPage(new Query<>(params),new EntityWrapper<>()); return sysLogService.selectPage(new Query<>(params), new EntityWrapper<>());
} }
/** /**
@ -54,11 +46,7 @@ public class LogController extends BaseController {
* @return success/false * @return success/false
*/ */
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public Boolean delete(@PathVariable Integer id) { public R<Boolean> delete(@PathVariable Integer id) {
SysLog sysLog = new SysLog(); return new R<>(sysLogService.updateByLogId(id));
sysLog.setId(id);
sysLog.setDelFlag(CommonConstant.STATUS_DEL);
sysLog.setUpdateTime(new Date());
return sysLogService.updateById(sysLog);
} }
} }

View File

@ -6,6 +6,7 @@ import com.github.pig.admin.entity.SysMenu;
import com.github.pig.admin.service.SysMenuService; import com.github.pig.admin.service.SysMenuService;
import com.github.pig.admin.common.util.TreeUtil; import com.github.pig.admin.common.util.TreeUtil;
import com.github.pig.common.constant.CommonConstant; import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.R;
import com.github.pig.common.vo.MenuVo; import com.github.pig.common.vo.MenuVo;
import com.github.pig.common.web.BaseController; import com.github.pig.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -98,8 +99,8 @@ public class MenuController extends BaseController {
* @return success/false * @return success/false
*/ */
@PostMapping @PostMapping
public Boolean menu(@RequestBody SysMenu sysMenu) { public R<Boolean> menu(@RequestBody SysMenu sysMenu) {
return sysMenuService.insert(sysMenu); return new R<>(sysMenuService.insert(sysMenu));
} }
/** /**
@ -110,27 +111,13 @@ public class MenuController extends BaseController {
* TODO 级联删除下级节点 * TODO 级联删除下级节点
*/ */
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
@CacheEvict(value = "menu_details", allEntries = true) public R<Boolean> menuDel(@PathVariable Integer id) {
public Boolean menuDel(@PathVariable Integer id) { return new R<>(sysMenuService.deleteMenu(id,getRole().get(0)));
// 删除当前节点
SysMenu condition1 = new SysMenu();
condition1.setMenuId(id);
condition1.setDelFlag(CommonConstant.STATUS_DEL);
sysMenuService.updateById(condition1);
// 删除父节点为当前节点的节点
SysMenu conditon2 = new SysMenu();
conditon2.setParentId(id);
SysMenu sysMenu = new SysMenu();
sysMenu.setDelFlag(CommonConstant.STATUS_DEL);
sysMenuService.update(sysMenu, new EntityWrapper<>(conditon2));
return Boolean.TRUE;
} }
@PutMapping @PutMapping
@CacheEvict(value = "menu_details", allEntries = true) public R<Boolean> menuUpdate(@RequestBody SysMenu sysMenu) {
public Boolean menuUpdate(@RequestBody SysMenu sysMenu) { return new R<>(sysMenuService.updateMenuById(sysMenu,getRole().get(0)));
return sysMenuService.updateById(sysMenu);
} }
private List<MenuTree> getMenuTree(List<SysMenu> menus, int root) { private List<MenuTree> getMenuTree(List<SysMenu> menus, int root) {

View File

@ -48,8 +48,8 @@ public class RoleController extends BaseController {
* @return successfalse * @return successfalse
*/ */
@PostMapping @PostMapping
public R role(@RequestBody SysRole sysRole) { public R<Boolean> role(@RequestBody SysRole sysRole) {
return new R(sysRoleService.insert(sysRole)); return new R<>(sysRoleService.insert(sysRole));
} }
/** /**
@ -59,16 +59,15 @@ public class RoleController extends BaseController {
* @return success/false * @return success/false
*/ */
@PutMapping @PutMapping
public Boolean roleUpdate(@RequestBody SysRole sysRole) { public R<Boolean> roleUpdate(@RequestBody SysRole sysRole) {
sysRoleService.updateById(sysRole); return new R<>(sysRoleService.updateById(sysRole));
return Boolean.TRUE;
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public Boolean roleDel(@PathVariable Integer id) { public R<Boolean> roleDel(@PathVariable Integer id) {
SysRole sysRole = sysRoleService.selectById(id); SysRole sysRole = sysRoleService.selectById(id);
sysRole.setDelFlag(CommonConstant.STATUS_DEL); sysRole.setDelFlag(CommonConstant.STATUS_DEL);
return sysRoleService.updateById(sysRole); return new R<>(sysRoleService.updateById(sysRole));
} }
/** /**
@ -93,7 +92,7 @@ public class RoleController extends BaseController {
@RequestMapping("/rolePage") @RequestMapping("/rolePage")
public Page rolePage(@RequestParam Map<String, Object> params) { public Page rolePage(@RequestParam Map<String, Object> params) {
params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL); params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
return sysRoleService.selectPage(new Query<>(params),new EntityWrapper<>()); return sysRoleService.selectPage(new Query<>(params), new EntityWrapper<>());
} }
/** /**
@ -104,19 +103,7 @@ public class RoleController extends BaseController {
* @return successfalse * @return successfalse
*/ */
@PutMapping("/roleMenuUpd") @PutMapping("/roleMenuUpd")
@CacheEvict(value = "menu_details", allEntries = true) public R<Boolean> roleMenuUpd(Integer roleId, @RequestParam("menuIds[]") Integer[] menuIds) {
public Boolean roleMenuUpd(Integer roleId, @RequestParam("menuIds[]") Integer[] menuIds) { return new R<>(sysRoleMenuService.insertRoleMenus(getRole().get(0),roleId,menuIds));
SysRoleMenu condition = new SysRoleMenu();
condition.setRoleId(roleId);
sysRoleMenuService.delete(new EntityWrapper<>(condition));
List<SysRoleMenu> roleMenuList = new ArrayList<>();
for (Integer menuId : menuIds) {
SysRoleMenu roleMenu = new SysRoleMenu();
roleMenu.setRoleId(roleId);
roleMenu.setMenuId(menuId);
roleMenuList.add(roleMenu);
}
return sysRoleMenuService.insertBatch(roleMenuList);
} }
} }

View File

@ -2,8 +2,8 @@ package com.github.pig.admin.controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page; import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.common.bean.QiniuPropertiesConfig;
import com.github.pig.admin.dto.UserDto; import com.github.pig.admin.dto.UserDto;
import com.github.pig.common.bean.QiniuPropertiesConfig;
import com.github.pig.admin.dto.UserInfo; import com.github.pig.admin.dto.UserInfo;
import com.github.pig.admin.entity.SysUser; import com.github.pig.admin.entity.SysUser;
import com.github.pig.admin.entity.SysUserRole; import com.github.pig.admin.entity.SysUserRole;

View File

@ -2,6 +2,7 @@ package com.github.pig.admin.service;
import com.baomidou.mybatisplus.service.IService; import com.baomidou.mybatisplus.service.IService;
import com.github.pig.common.entity.SysLog; import com.github.pig.common.entity.SysLog;
import com.github.pig.common.util.R;
/** /**
* <p> * <p>
@ -13,4 +14,11 @@ import com.github.pig.common.entity.SysLog;
*/ */
public interface SysLogService extends IService<SysLog> { public interface SysLogService extends IService<SysLog> {
/**
* 通过ID删除日志逻辑删除
*
* @param id 日志ID
* @return true/false
*/
Boolean updateByLogId(Integer id);
} }

View File

@ -31,4 +31,22 @@ public interface SysMenuService extends IService<SysMenu> {
* @return 权限列表 * @return 权限列表
*/ */
String[] findPermission(String[] roles); String[] findPermission(String[] roles);
/**
* 级联删除菜单
*
* @param id 菜单ID
* @param role 角色
* @return 成功失败
*/
Boolean deleteMenu(Integer id, String role);
/**
* 更新菜单信息
*
* @param sysMenu 菜单信息
* @param role 角色
* @return 成功失败
*/
Boolean updateMenuById(SysMenu sysMenu, String role);
} }

View File

@ -13,5 +13,15 @@ import com.github.pig.admin.entity.SysRoleMenu;
* @since 2017-10-29 * @since 2017-10-29
*/ */
public interface SysRoleMenuService extends IService<SysRoleMenu> { public interface SysRoleMenuService extends IService<SysRoleMenu> {
/**
* 更新角色菜单
*
*
* @param role
* @param roleId 角色
* @param menuIds 菜单列表
* @return
*/
Boolean insertRoleMenus(String role, Integer roleId, Integer[] menuIds);
} }

View File

@ -3,9 +3,14 @@ package com.github.pig.admin.service.impl;
import com.baomidou.mybatisplus.service.impl.ServiceImpl; import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pig.admin.mapper.SysLogMapper; import com.github.pig.admin.mapper.SysLogMapper;
import com.github.pig.admin.service.SysLogService; import com.github.pig.admin.service.SysLogService;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.entity.SysLog; import com.github.pig.common.entity.SysLog;
import com.github.pig.common.util.Assert;
import com.github.pig.common.util.R;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Date;
/** /**
* <p> * <p>
* 日志表 服务实现类 * 日志表 服务实现类
@ -17,4 +22,14 @@ import org.springframework.stereotype.Service;
@Service @Service
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements SysLogService { public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements SysLogService {
@Override
public Boolean updateByLogId(Integer id) {
Assert.isNull(id, "日志ID为空");
SysLog sysLog = new SysLog();
sysLog.setId(id);
sysLog.setDelFlag(CommonConstant.STATUS_DEL);
sysLog.setUpdateTime(new Date());
return updateById(sysLog);
}
} }

View File

@ -1,12 +1,16 @@
package com.github.pig.admin.service.impl; package com.github.pig.admin.service.impl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl; import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pig.admin.entity.SysMenu; import com.github.pig.admin.entity.SysMenu;
import com.github.pig.admin.mapper.SysMenuMapper; import com.github.pig.admin.mapper.SysMenuMapper;
import com.github.pig.admin.service.SysMenuService; import com.github.pig.admin.service.SysMenuService;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.Assert;
import com.github.pig.common.vo.MenuVo; import com.github.pig.common.vo.MenuVo;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -50,4 +54,28 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
return permissions.toArray(new String[permissions.size()]); return permissions.toArray(new String[permissions.size()]);
} }
@Override
@CacheEvict(value = "menu_details", key = "#role + '_menu'")
public Boolean deleteMenu(Integer id, String role) {
Assert.isNull(id, "菜单ID不能为空");
// 删除当前节点
SysMenu condition1 = new SysMenu();
condition1.setMenuId(id);
condition1.setDelFlag(CommonConstant.STATUS_DEL);
this.updateById(condition1);
// 删除父节点为当前节点的节点
SysMenu conditon2 = new SysMenu();
conditon2.setParentId(id);
SysMenu sysMenu = new SysMenu();
sysMenu.setDelFlag(CommonConstant.STATUS_DEL);
return this.update(sysMenu, new EntityWrapper<>(conditon2));
}
@Override
@CacheEvict(value = "menu_details", key = "#role + '_menu'")
public Boolean updateMenuById(SysMenu sysMenu, String role) {
return this.updateById(sysMenu);
}
} }

View File

@ -1,11 +1,16 @@
package com.github.pig.admin.service.impl; package com.github.pig.admin.service.impl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl; import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pig.admin.entity.SysRoleMenu; import com.github.pig.admin.entity.SysRoleMenu;
import com.github.pig.admin.mapper.SysRoleMenuMapper; import com.github.pig.admin.mapper.SysRoleMenuMapper;
import com.github.pig.admin.service.SysRoleMenuService; import com.github.pig.admin.service.SysRoleMenuService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/** /**
* <p> * <p>
* 角色菜单表 服务实现类 * 角色菜单表 服务实现类
@ -16,5 +21,20 @@ import org.springframework.stereotype.Service;
*/ */
@Service @Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService { public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {
@Override
@CacheEvict(value = "menu_details", key = "#role + '_menu'")
public Boolean insertRoleMenus(String role, Integer roleId, Integer[] menuIds) {
SysRoleMenu condition = new SysRoleMenu();
condition.setRoleId(roleId);
this.delete(new EntityWrapper<>(condition));
List<SysRoleMenu> roleMenuList = new ArrayList<>();
for (Integer menuId : menuIds) {
SysRoleMenu roleMenu = new SysRoleMenu();
roleMenu.setRoleId(roleId);
roleMenu.setMenuId(menuId);
roleMenuList.add(roleMenu);
}
return this.insertBatch(roleMenuList);
}
} }

View File

@ -67,6 +67,12 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId> <artifactId>spring-boot-starter-aop</artifactId>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.1.Final</version>
</dependency>
</dependencies> </dependencies>

View File

@ -30,17 +30,37 @@ public class ControllerAop {
private static final Logger logger = LoggerFactory.getLogger(ControllerAop.class); private static final Logger logger = LoggerFactory.getLogger(ControllerAop.class);
@Pointcut("execution(public com.github.pig.common.util.R *(..))") @Pointcut("execution(public com.github.pig.common.util.R *(..))")
public void pointCut() { public void pointCutR() {
} }
/** /**
* 拦截器具体实现 * 拦截器具体实现
* *
* @param pjp 切点 * @param pjp 切点 所有返回对象R
* @return R 结果包装 * @return R 结果包装
*/ */
@Around("pointCut()") @Around("pointCutR()")
public Object methodHandler(ProceedingJoinPoint pjp) { public Object methodRHandler(ProceedingJoinPoint pjp) {
return methodHandler(pjp);
}
@Pointcut("execution(public com.baomidou.mybatisplus.plugins.Page *(..))")
public void pointCutPage() {
}
/**
* 拦截器具体实现
*
* @param pjp 切点 所有返回对象Page
* @return R 结果包装
*/
@Around("pointCutPage()")
public Object methodPageHandler(ProceedingJoinPoint pjp) {
return methodHandler(pjp);
}
private Object methodHandler(ProceedingJoinPoint pjp) {
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
@ -56,10 +76,10 @@ public class ControllerAop {
logger.info("CLASS_METHOD : " + pjp.getSignature().getDeclaringTypeName() + "." + pjp.getSignature().getName()); logger.info("CLASS_METHOD : " + pjp.getSignature().getDeclaringTypeName() + "." + pjp.getSignature().getName());
logger.info("ARGS : " + Arrays.toString(pjp.getArgs())); logger.info("ARGS : " + Arrays.toString(pjp.getArgs()));
R<?> result; Object result;
try { try {
result = (R<?>) pjp.proceed(); result = pjp.proceed();
logger.info(pjp.getSignature() + "use time:" + (System.currentTimeMillis() - startTime)); logger.info(pjp.getSignature() + "use time:" + (System.currentTimeMillis() - startTime));
} catch (Throwable e) { } catch (Throwable e) {
result = handlerException(pjp, e); result = handlerException(pjp, e);

View File

@ -0,0 +1,53 @@
package com.github.pig.common.util;
import com.github.pig.common.util.exception.CheckException;
import org.apache.commons.lang.StringUtils;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;
/**
* 数据校验
* @author chenshun
* @email sunlightcs@gmail.com
* @date 2017-03-23 15:50
*/
public abstract class Assert {
private static Validator validator;
static {
validator = Validation.buildDefaultValidatorFactory().getValidator();
}
/**
* 校验对象
* @param object 待校验对象
* @param groups 待校验的组
* @throws CheckException 校验不通过则报RRException异常
*/
public static void validateEntity(Object object, Class<?>... groups)
throws CheckException {
Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups);
if (!constraintViolations.isEmpty()) {
StringBuilder msg = new StringBuilder();
for(ConstraintViolation<Object> constraint: constraintViolations){
msg.append(constraint.getMessage()).append("<br>");
}
throw new CheckException(msg.toString());
}
}
public static void isBlank(String str, String message) {
if (StringUtils.isBlank(str)) {
throw new CheckException(message);
}
}
public static void isNull(Object object, String message) {
if (object == null) {
throw new CheckException(message);
}
}
}

View File

@ -32,4 +32,6 @@ public class BaseController {
List<String> roleNames = (List<String>) claims.get("authorities"); List<String> roleNames = (List<String>) claims.get("authorities");
return roleNames; return roleNames;
} }
} }