merge
This commit is contained in:
@@ -25,14 +25,14 @@ public class BaseEntity implements Serializable {
|
||||
@Column(length = 50)
|
||||
@ApiModelProperty(value = "主键")
|
||||
private String id;
|
||||
@ApiModelProperty(value = "创建者")
|
||||
@ApiModelProperty(value = "创建者",hidden = true)
|
||||
private String creator;
|
||||
@ApiModelProperty(value = "修改者")
|
||||
@ApiModelProperty(value = "修改者",hidden = true)
|
||||
private String modifier;
|
||||
@ApiModelProperty(value = "创建时间")
|
||||
@ApiModelProperty(value = "创建时间",hidden = true)
|
||||
@Column(name = "create_time")
|
||||
private Date createTime;
|
||||
@ApiModelProperty(value = "修改时间")
|
||||
@ApiModelProperty(value = "修改时间",hidden = true)
|
||||
@Column(name = "modified_time")
|
||||
private Date modifiedTime;
|
||||
}
|
||||
|
||||
@@ -3,11 +3,16 @@ package com.xwbing.repository;
|
||||
import com.xwbing.entity.SysRole;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 说明:
|
||||
* 项目名称: boot-module-demo
|
||||
* 创建时间: 2017/5/5 16:44
|
||||
* 作者: xiangwb
|
||||
*/
|
||||
public interface SysRoleRepository extends JpaRepository<SysRole,String> {
|
||||
public interface SysRoleRepository extends JpaRepository<SysRole, String> {
|
||||
List<SysRole> getByEnable(String enable);
|
||||
|
||||
SysRole getByCode(String code);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
package com.xwbing.service;
|
||||
|
||||
import com.xwbing.entity.SysRole;
|
||||
import com.xwbing.exception.BusinessException;
|
||||
import com.xwbing.repository.SysRoleRepository;
|
||||
import com.xwbing.util.PassWordUtil;
|
||||
import com.xwbing.util.RestMessage;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 项目名称: boot-module-demo
|
||||
* 创建时间: 2017/11/14 9:24
|
||||
* 作者: xiangwb
|
||||
* 说明:
|
||||
*/
|
||||
@Service
|
||||
public class SysRoleService {
|
||||
@Resource
|
||||
private SysRoleRepository sysRoleRepository;
|
||||
private final Logger logger = LoggerFactory.getLogger(SysRoleService.class);
|
||||
|
||||
/**
|
||||
* 保存角色
|
||||
*
|
||||
* @param sysRole
|
||||
* @return
|
||||
*/
|
||||
public RestMessage save(SysRole sysRole) {
|
||||
RestMessage result = new RestMessage();
|
||||
boolean b = uniqueCode(sysRole.getCode(), "");
|
||||
if (!b)
|
||||
throw new BusinessException("该编码已存在");
|
||||
String id = PassWordUtil.createId();
|
||||
sysRole.setId(id);
|
||||
sysRole.setCreateTime(new Date());
|
||||
SysRole save = sysRoleRepository.save(sysRole);
|
||||
if (save != null) {
|
||||
result.setSuccess(true);
|
||||
result.setId(id);
|
||||
result.setMessage("保存角色成功");
|
||||
} else {
|
||||
result.setMessage("保存角色失败");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除角色
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
public RestMessage removeById(String id) {
|
||||
RestMessage result = new RestMessage();
|
||||
SysRole one = getById(id);
|
||||
if (one == null)
|
||||
throw new BusinessException("该角色不存在");
|
||||
sysRoleRepository.delete(id);
|
||||
result.setMessage("删除成功");
|
||||
result.setSuccess(true);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改角色
|
||||
*
|
||||
* @param sysRole
|
||||
* @return
|
||||
*/
|
||||
public RestMessage update(SysRole sysRole) {
|
||||
RestMessage result = new RestMessage();
|
||||
String id = sysRole.getId();
|
||||
SysRole old = getById(id);
|
||||
if (old == null) {
|
||||
throw new BusinessException("该角色不存在");
|
||||
}
|
||||
boolean b = uniqueCode(sysRole.getCode(), id);
|
||||
if (!b)
|
||||
throw new BusinessException("该编码已存在");
|
||||
old.setModifiedTime(new Date());
|
||||
old.setName(sysRole.getName());
|
||||
old.setCode(sysRole.getCode());
|
||||
old.setEnable(sysRole.getEnable());
|
||||
old.setRemark(sysRole.getRemark());
|
||||
SysRole save = sysRoleRepository.save(old);
|
||||
if (save != null) {
|
||||
result.setSuccess(true);
|
||||
result.setId(id);
|
||||
result.setMessage("修改角色成功");
|
||||
} else {
|
||||
result.setMessage("修改角色失败");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据主键查找
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
public SysRole getById(String id) {
|
||||
return sysRoleRepository.findOne(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据是否启用列表查询
|
||||
*
|
||||
* @param enable
|
||||
* @return
|
||||
*/
|
||||
public List<SysRole> listAllByEnable(String enable) {
|
||||
return sysRoleRepository.getByEnable(enable);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查code是否唯一 true唯一
|
||||
*
|
||||
* @param code
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
private boolean uniqueCode(String code, String id) {
|
||||
if (StringUtils.isEmpty(code))
|
||||
throw new BusinessException("code不能为空");
|
||||
SysRole one = sysRoleRepository.getByCode(code);
|
||||
if (one != null) {
|
||||
return StringUtils.isNotEmpty(id) && id.equals(one.getId());
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.xwbing.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.xwbing.annotation.LogInfo;
|
||||
import com.xwbing.entity.SysRole;
|
||||
import com.xwbing.service.SysRoleService;
|
||||
import com.xwbing.util.JSONObjResult;
|
||||
import com.xwbing.util.RestMessage;
|
||||
import io.swagger.annotations.Api;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 项目名称: boot-module-demo
|
||||
* 创建时间: 2017/11/14 10:41
|
||||
* 作者: xiangwb
|
||||
* 说明:
|
||||
*/
|
||||
@Api(tags = "sysRoleApi", description = "角色相关接口")
|
||||
@RestController
|
||||
@RequestMapping("/role/")
|
||||
public class SysRoleControl {
|
||||
@Resource
|
||||
private SysRoleService sysRoleService;
|
||||
|
||||
@LogInfo("添加角色")
|
||||
@PostMapping("save")
|
||||
public JSONObject save(@RequestBody SysRole sysRole) {
|
||||
RestMessage result = sysRoleService.save(sysRole);
|
||||
return JSONObjResult.toJSONObj(result);
|
||||
}
|
||||
|
||||
@LogInfo("删除角色")
|
||||
@GetMapping("removeById")
|
||||
public JSONObject removeById(@RequestParam String id) {
|
||||
if (StringUtils.isEmpty(id)) {
|
||||
return JSONObjResult.toJSONObj("主键不能为空");
|
||||
}
|
||||
RestMessage result = sysRoleService.removeById(id);
|
||||
return JSONObjResult.toJSONObj(result);
|
||||
}
|
||||
|
||||
@LogInfo("修改角色")
|
||||
@PostMapping("update")
|
||||
public JSONObject update(@RequestBody SysRole sysRole) {
|
||||
if (StringUtils.isEmpty(sysRole.getId())) {
|
||||
return JSONObjResult.toJSONObj("主键不能为空");
|
||||
}
|
||||
RestMessage result = sysRoleService.update(sysRole);
|
||||
return JSONObjResult.toJSONObj(result);
|
||||
}
|
||||
|
||||
@LogInfo("获取角色详情")
|
||||
@GetMapping("getById")
|
||||
public JSONObject getById(@RequestParam String id) {
|
||||
if (StringUtils.isEmpty(id)) {
|
||||
return JSONObjResult.toJSONObj("主键不能为空");
|
||||
}
|
||||
SysRole sysRole = sysRoleService.getById(id);
|
||||
if (sysRole == null) {
|
||||
return JSONObjResult.toJSONObj("该角色不存在");
|
||||
}
|
||||
return JSONObjResult.toJSONObj(sysRole, true, "");
|
||||
}
|
||||
|
||||
@LogInfo("根据是否启用查询所有角色")
|
||||
@GetMapping("listAllByEnable")
|
||||
public JSONObject listAllByEnable(@RequestParam String enable) {
|
||||
if (StringUtils.isEmpty(enable))
|
||||
return JSONObjResult.toJSONObj("是否启用不能为空");
|
||||
List<SysRole> sysRoles = sysRoleService.listAllByEnable(enable);
|
||||
return JSONObjResult.toJSONObj(sysRoles, true, "");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user