Commit 5938db43 authored by ranjun's avatar ranjun

权限的crud

parent 4d25a0fc
package com.pangding.web.tright.controller;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.pangding.web.tright.currency.Result;
import com.pangding.web.tright.dao.TrightDao;
import com.pangding.web.tright.service.TrightService;
import com.pangding.web.tright.vo.TrightVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/tright")
public class TrightController {
Result result = new Result();
@Autowired
TrightService trightServiceImpl;
@Autowired
TrightDao trightDao;
/**
* 新增权限
* @param trightVo
......@@ -18,7 +30,8 @@ public class TrightController {
*/
@RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Object saveTright(@RequestBody TrightVo trightVo){
trightServiceImpl.save(trightVo);
Result result = new Result(0,"SUCCESS",null);
return result;
}
......@@ -29,7 +42,8 @@ public class TrightController {
*/
@RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Object updateTright(@RequestBody TrightVo trightVo){
trightServiceImpl.update(trightVo);
Result result = new Result(0,"SUCCESS",null);
return result;
}
......@@ -39,17 +53,39 @@ public class TrightController {
*/
@RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Object listTrights(){
List<TrightVo> trightVos = trightDao.listAll();
List<TrightVo> list = Lists.newArrayList();
setPermissionsList(0L,trightVos, list);
Result result = new Result(0,"SUCCESS",list);
return result;
}
/**
* 菜单列表
*
* @param pId
* @param trightVos
* @param list
*/
private void setPermissionsList(Long pId, List<TrightVo> trightVos, List<TrightVo> list) {
for (TrightVo per : trightVos) {
if (per.getModuleId().equals(pId)) {
list.add(per);
if (trightVos.stream().filter(p -> p.getModuleId().equals(per.getTrId())).findAny() != null) {
setPermissionsList(per.getTrId(), trightVos, list);
}
}
}
}
/**
* 查询一级权限
* @return
*/
@RequestMapping(value = "/parent", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Object parentTrights(){
List<TrightVo> parentTrights = trightDao.listParents();
Result result = new Result(0,"SUCCESS",parentTrights);
return result;
}
......@@ -59,10 +95,35 @@ public class TrightController {
*/
@RequestMapping(value = "/all", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Object allTrights(){
List<TrightVo> trightVos = trightDao.listAll();
JSONArray array = new JSONArray();
setPermissionsTree(0L,trightVos, array);
Result result = new Result(0,"SUCCESS",array);
return result;
}
/**
* 菜单树
*
* @param pId
* @param trightVos
* @param array
*/
private void setPermissionsTree(Long pId, List<TrightVo> trightVos, JSONArray array) {
for (TrightVo per : trightVos) {
if (per.getModuleId().equals(pId)) {
String string = JSONObject.toJSONString(per);
JSONObject parent = (JSONObject) JSONObject.parse(string);
array.add(parent);
if (trightVos.stream().filter(p -> p.getModuleId().equals(per.getTrId())).findAny() != null) {
JSONArray child = new JSONArray();
parent.put("child", child);
setPermissionsTree(per.getTrId(), trightVos, child);
}
}
}
}
/**
* 根据ID查询权限
......@@ -71,7 +132,8 @@ public class TrightController {
*/
@RequestMapping(value = "/role", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Object tright(@PathVariable Long id){
trightDao.getById(id);
Result result = new Result(0,"SUCCESS",null);
return null;
}
......@@ -82,7 +144,8 @@ public class TrightController {
*/
@RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Object delete(@PathVariable Long id){
trightServiceImpl.delete(id);
Result result = new Result(0,"SUCCESS",null);
return result;
}
}
......@@ -10,8 +10,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.Date;
@RestController
@RequestMapping("/users")
@RequestMapping("/user")
public class UserController {
@Autowired
......@@ -20,15 +23,21 @@ public class UserController {
private UserDao userDao;
/**
* 新增用户
* @param userDto
* @param //userDto
* @return
*/
@RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Object saveUser(@RequestBody UserDto userDto){
UserVo u = userServiceImpl.getUser(userDto.getUserName());
if (u != null) {
throw new IllegalArgumentException(userDto.getUserName() + "已存在");
}
@RequestMapping(value = "/save", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Object saveUser(){//@RequestBody UserDto userDto
// UserVo u = userServiceImpl.getUser(userDto.getUserName());
// if (u != null) {
// throw new IllegalArgumentException(userDto.getUserName() + "已存在");
// }
UserDto userDto = new UserDto();
userDto.setUserName("test");
userDto.setPassword("123456");
userDto.setPhoneNumber("123456789110");
userDto.setCreateTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
userDto.setCount(0);
UserVo userVo = userServiceImpl.saveUser(userDto);
Result result = new Result(0,"SUCCESS",userVo);
return result;
......@@ -61,13 +70,15 @@ public class UserController {
/**
* 根据ID查询用户
* @param id
* @param //id
* @return
*/
@RequestMapping(value = "/user", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Object user(@PathVariable Long id){
UserVo userVo = userDao.getById(id);
Result result = new Result(0,"SUCCESS",userVo);
@RequestMapping(value = "/user", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Object user(){//@PathVariable Long id
UserVo vo = userDao.load(null,UserVo.class);
//UserVo userVo = userDao.getById(id);
Result result = new Result(0,"SUCCESS",vo);
return result;
}
......
......@@ -20,6 +20,6 @@ public interface RoleDao {
int deleteRoleTright(Long roleId);
int saveRolePermission(Long roleId, List<Long> trights);
int saveRoleTright(Long roleId, List<Long> trights);
}
package com.pangding.web.tright.dao;
import com.pangding.web.tright.vo.TrightVo;
import java.util.List;
public interface TrightDao {
int save(TrightVo trightVo);
int update(TrightVo trightVo);
int deleteRoleTright(Long id);
int delete(Long id);
int deleteByParentId(Long id);
TrightVo getById(Long id);
List<TrightVo> listAll();
List<TrightVo> listParents();
}
package com.pangding.web.tright.dao;
import com.pangding.web.tright.vo.UserVo;
import com.yanzuoguang.dao.BaseDao;
import java.util.List;
public interface UserDao {
int save(UserVo userVo);
UserVo getUser(String username);
int update(UserVo userVo);
int deleteUserRole(Long userId);
int saveUserRoles(Long userId, List<Long> roleIds);
UserVo getById(Long id);
public interface UserDao extends BaseDao {
// int saveUser(UserVo userVo);
//
// UserVo getUser(String username);
//
// int update(UserVo userVo);
//
// int deleteUserRole(Long userId);
//
// int saveUserRoles(Long userId, List<Long> roleIds);
//
// UserVo getById(Long id);
}
package com.pangding.web.tright.dao.impl;
import com.pangding.web.tright.dao.RoleDao;
import com.pangding.web.tright.vo.RoleVo;
import com.yanzuoguang.dao.Impl.BaseDaoImpl;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class RoleDaoImpl extends BaseDaoImpl implements RoleDao {
@Override
public int save(RoleVo roleVo) {
return 0;
}
@Override
public RoleVo getRole(String name) {
return null;
}
@Override
public int update(RoleVo roleVo) {
return 0;
}
@Override
public int deleteRole(Long id) {
return 0;
}
@Override
public int deleteRoleUser(Long roleId) {
return 0;
}
@Override
public RoleVo getById(Long id) {
return null;
}
@Override
public int deleteRoleTright(Long roleId) {
return 0;
}
@Override
public int saveRoleTright(Long roleId, List<Long> trights) {
return 0;
}
@Override
protected void init() {
}
}
package com.pangding.web.tright.dao.impl;
import com.pangding.web.tright.dao.TrightDao;
import com.pangding.web.tright.vo.TrightVo;
import com.yanzuoguang.dao.Impl.BaseDaoImpl;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class TrightDaoImpl extends BaseDaoImpl implements TrightDao {
@Override
public int save(TrightVo trightVo) {
return 0;
}
@Override
public int update(TrightVo trightVo) {
return 0;
}
@Override
public int deleteRoleTright(Long id) {
return 0;
}
@Override
public int delete(Long id) {
return 0;
}
@Override
public int deleteByParentId(Long id) {
return 0;
}
@Override
public TrightVo getById(Long id) {
return null;
}
@Override
public List<TrightVo> listAll() {
return null;
}
@Override
public List<TrightVo> listParents() {
return null;
}
@Override
protected void init() {
}
}
package com.pangding.web.tright.dao.impl;
import com.pangding.web.tright.dao.UserDao;
import com.pangding.web.tright.vo.UserVo;
import com.yanzuoguang.dao.Impl.BaseDaoImpl;
import com.yanzuoguang.util.vo.MapRow;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class UserDaoImpl extends BaseDaoImpl implements UserDao {
// @Override
// public int saveUser(UserVo userVo) {
//
// String s = this.saveWith(userVo.getClass(),"insert into ",null);
// System.out.println(s);
// return 0;
// }
//
// @Override
// public UserVo getUser(String username) {
// List<MapRow> list = this.query("select * from pd_user where id = @id",UserVo.class);
// return null;
// }
//
// @Override
// public int update(UserVo userVo) {
// return 0;
// }
//
// @Override
// public int deleteUserRole(Long userId) {
// return 0;
// }
//
// @Override
// public int saveUserRoles(Long userId, List<Long> roleIds) {
// return 0;
// }
//
// @Override
// public UserVo getById(Long id) {
// return null;
// }
//
@Override
protected void init() {
register(UserVo.class);
}
}
package com.pangding.web.tright.service;
import com.pangding.web.tright.vo.TrightVo;
public interface TrightService {
void save(TrightVo trightVo);
void update(TrightVo trightVo);
void delete(Long id);
}
......@@ -18,9 +18,9 @@ public class RoleServiceImpl implements RoleService {
@Override
public void saveRole(RoleDto roleDto) {
RoleVo role = roleDto;
if (role.getTrID() != null) {// 修改
if (role.getTrId() != null) {// 修改
RoleVo r = roleDao.getRole(role.getRoleName());
if (r != null && r.getTrID() != role.getTrID()) {
if (r != null && r.getTrId() != role.getTrId()) {
throw new IllegalArgumentException(role.getRoleName() + "已存在");
}
......@@ -32,14 +32,14 @@ public class RoleServiceImpl implements RoleService {
}
roleDao.save(role);
}
saveRoleTright(role.getTrID(), roleDto.getTrightIds());
saveRoleTright(role.getTrId(), roleDto.getTrightIds());
}
private void saveRoleTright(Long roleId, List<Long> trights) {
roleDao.deleteRoleTright(roleId);
trights.remove(0L);
if (!CollectionUtils.isEmpty(trights)) {
roleDao.saveRolePermission(roleId, trights);
roleDao.saveRoleTright(roleId, trights);
}
}
......
package com.pangding.web.tright.service.impl;
import com.pangding.web.tright.dao.TrightDao;
import com.pangding.web.tright.service.TrightService;
import com.pangding.web.tright.vo.TrightVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class TrightServiceImpl {
public class TrightServiceImpl implements TrightService {
@Autowired
TrightDao trightDao;
@Override
public void save(TrightVo trightVo) {
trightDao.save(trightVo);
}
@Override
public void update(TrightVo trightVo) {
trightDao.update(trightVo);
}
@Override
public void delete(Long id) {
trightDao.deleteRoleTright(id);
trightDao.delete(id);
trightDao.deleteByParentId(id);
}
}
......@@ -20,17 +20,18 @@ public class UserServiceImpl implements UserService {
public UserVo saveUser(UserDto userDto) {
UserVo userVo = userDto;
//
userDao.save(userVo);
saveUserRoles(userVo.getTuID(), userDto.getRoleIds());
userDao.create(userVo);
//userDao.saveUser(userVo);
saveUserRoles(userVo.getTuId(), userDto.getRoleIds());
return userVo;
}
private void saveUserRoles(Long userId, List<Long> roleIds) {
if (roleIds != null) {
userDao.deleteUserRole(userId);
//userDao.deleteUserRole(userId);
if (!CollectionUtils.isEmpty(roleIds)) {
userDao.saveUserRoles(userId, roleIds);
//userDao.saveUserRoles(userId, roleIds);
}
}
}
......@@ -38,13 +39,14 @@ public class UserServiceImpl implements UserService {
@Override
public UserVo updateUser(UserDto userDto) {
userDao.update(userDto);
saveUserRoles(userDto.getTuID(), userDto.getRoleIds());
saveUserRoles(userDto.getTuId(), userDto.getRoleIds());
return userDto;
}
@Override
public UserVo getUser(String username) {
return userDao.getUser(username);
return null;
//userDao.getUser(username);
}
@Override
......
package com.pangding.web.tright.vo;
import com.yanzuoguang.dao.TableAnnotation;
import java.io.Serializable;
import java.util.Date;
@TableAnnotation("pd_role")
public class RoleVo implements Serializable {
private Long trID;//主键ID
private Long parentTrID;//角色父ID
private Long trId;//主键ID
private Long parentTrId;//角色父ID
private String roleName;//角色名
private Date createTime;//创建时间
private String describe;//描述
public Long getTrID() {
return trID;
public Long getTrId() {
return trId;
}
public void setTrID(Long trID) {
this.trID = trID;
public void setTrId(Long trId) {
this.trId = trId;
}
public Long getParentTrID() {
return parentTrID;
public Long getParentTrId() {
return parentTrId;
}
public void setParentTrID(Long parentTrID) {
this.parentTrID = parentTrID;
public void setParentTrId(Long parentTrId) {
this.parentTrId = parentTrId;
}
public String getRoleName() {
......
package com.pangding.web.tright.vo;
import com.yanzuoguang.dao.TableAnnotation;
import java.io.Serializable;
/**
* 权限表
*/
@TableAnnotation("pd_tright")
public class TrightVo implements Serializable {
private Long trID;//权限ID
private Long sysID;//系统ID
private Long moduleID;//上级模块ID
private Long trId;//权限ID
private Long sysId;//系统ID
private Long moduleId;//上级模块ID
private String moduleName;//模块名
private Integer menuOrFunc;//菜单/功能(1:菜单,2:功能)
private String alias;//权限别名
......@@ -20,28 +23,28 @@ public class TrightVo implements Serializable {
private Integer dialogHeight;//对话框高度
private String operatOrField;//操作字段(表字段...)
public Long getTrID() {
return trID;
public Long getTrId() {
return trId;
}
public void setTrID(Long trID) {
this.trID = trID;
public void setTrId(Long trId) {
this.trId = trId;
}
public Long getSysID() {
return sysID;
public Long getSysId() {
return sysId;
}
public void setSysID(Long sysID) {
this.sysID = sysID;
public void setSysId(Long sysId) {
this.sysId = sysId;
}
public Long getModuleID() {
return moduleID;
public Long getModuleId() {
return moduleId;
}
public void setModuleID(Long moduleID) {
this.moduleID = moduleID;
public void setModuleId(Long moduleId) {
this.moduleId = moduleId;
}
public String getModuleName() {
......
package com.pangding.web.tright.vo;
import com.yanzuoguang.dao.TableAnnotation;
import com.yanzuoguang.util.helper.DateHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.vo.InitDao;
import java.io.Serializable;
import java.util.Date;
public class UserVo implements Serializable {
private Long tuID;//主键ID
@TableAnnotation("pd_user")
public class UserVo implements Serializable, InitDao {
@TableAnnotation("tu_id")
private Long tuId;//主键ID
@TableAnnotation("user_name")
private String userName;//用户名
private String password;//密码
@TableAnnotation("phone_number")
private String phoneNumber;//手机号
private Date createTime;//创建时间
private Date loginTime;//登录时间
private Date lastLoginTime;//上次登录时间
@TableAnnotation("create_time")
private String createTime;//创建时间
@TableAnnotation("login_time")
private String loginTime;//登录时间
@TableAnnotation("last_login_time")
private String lastLoginTime;//上次登录时间
private Integer count;//登录次数
public Long getTuID() {
return tuID;
public Long getTuId() {
return tuId;
}
public void setTuID(Long tuID) {
this.tuID = tuID;
public void setTuId(Long tuId) {
this.tuId = tuId;
}
public String getUserName() {
......@@ -45,27 +58,27 @@ public class UserVo implements Serializable {
this.phoneNumber = phoneNumber;
}
public Date getCreateTime() {
public String getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public Date getLoginTime() {
public String getLoginTime() {
return loginTime;
}
public void setLoginTime(Date loginTime) {
public void setLoginTime(String loginTime) {
this.loginTime = loginTime;
}
public Date getLastLoginTime() {
public String getLastLoginTime() {
return lastLoginTime;
}
public void setLastLoginTime(Date lastLoginTime) {
public void setLastLoginTime(String lastLoginTime) {
this.lastLoginTime = lastLoginTime;
}
......@@ -76,4 +89,11 @@ public class UserVo implements Serializable {
public void setCount(Integer count) {
this.count = count;
}
@Override
public void init() {
this.createTime = StringHelper.getFirst(this.createTime, DateHelper.getNow());
this.loginTime = StringHelper.getFirst(this.loginTime, DateHelper.getNow());
this.lastLoginTime = StringHelper.getFirst(this.lastLoginTime, DateHelper.getNow());
}
}
......@@ -19,3 +19,5 @@ spring:
max-interval: 1000
max-attempts: 1000
multiplier: 1.1
yzg:
PrintSql: true
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment